From 7d771a8e5744fbed9bafce8f42e163e4341f565f Mon Sep 17 00:00:00 2001 From: canisminor1990 Date: Sun, 19 Nov 2023 16:56:38 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Fix=20header=20props?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/EdgeSpeechTTS/index.ts | 20 ++++++++++++------- src/core/MicrosoftSpeechTTS/index.ts | 19 ++++++++++++------ src/core/OpenAISTT/index.ts | 16 ++++++++++----- src/core/OpenAITTS/index.ts | 7 +++++-- src/react/useEdgeSpeech/index.ts | 5 ++--- src/react/useMicrosoftSpeech/index.ts | 5 ++--- src/react/useOpenAISTT/useOpenAISTTCore.ts | 5 ++--- .../useOpenAISTT/useOpenAISTTRecorder.ts | 1 - src/react/useOpenAITTS/index.ts | 5 ++--- 9 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/core/EdgeSpeechTTS/index.ts b/src/core/EdgeSpeechTTS/index.ts index 5435f6e..5aade00 100644 --- a/src/core/EdgeSpeechTTS/index.ts +++ b/src/core/EdgeSpeechTTS/index.ts @@ -9,17 +9,19 @@ import { getEdgeVoiceOptions } from './options'; export type { EdgeSpeechPayload } from './createEdgeSpeech'; export interface EdgeSpeechAPI { + headers?: Headers; locale?: string; serviceUrl?: string; } export class EdgeSpeechTTS { private locale?: string; - private serviceUrl: string | undefined; - - constructor({ serviceUrl, locale }: EdgeSpeechAPI = {}) { + private serviceUrl?: string; + private headers?: Headers; + constructor({ serviceUrl, locale, headers }: EdgeSpeechAPI = {}) { this.locale = locale; this.serviceUrl = serviceUrl; + this.headers = headers; } get voiceOptions() { @@ -31,16 +33,20 @@ export class EdgeSpeechTTS { static voiceName = voiceName; static createRequest = createEdgeSpeech; - private fetch = async (payload: EdgeSpeechPayload, headers?: Headers) => { + private fetch = async (payload: EdgeSpeechPayload) => { const response = await (this.serviceUrl - ? fetch(this.serviceUrl, { body: JSON.stringify(payload), headers, method: 'POST' }) + ? fetch(this.serviceUrl, { + body: JSON.stringify(payload), + headers: this.headers, + method: 'POST', + }) : createEdgeSpeech({ payload })); return response; }; - create = async (payload: EdgeSpeechPayload, headers?: Headers): Promise => { - return this.fetch(payload, headers); + create = async (payload: EdgeSpeechPayload): Promise => { + return this.fetch(payload); }; /** diff --git a/src/core/MicrosoftSpeechTTS/index.ts b/src/core/MicrosoftSpeechTTS/index.ts index 8f6ce4a..99bf997 100644 --- a/src/core/MicrosoftSpeechTTS/index.ts +++ b/src/core/MicrosoftSpeechTTS/index.ts @@ -9,17 +9,20 @@ import azureVoiceList, { getAzureVoiceOptions } from './voiceList'; export type { MicrosoftSpeechPayload } from './createMicrosoftSpeech'; export interface MicrosoftSpeechAPI { + headers?: Headers; locale?: string; serviceUrl?: string; } export class MicrosoftSpeechTTS { private locale?: string; - private serviceUrl: string | undefined; + private serviceUrl?: string; + private headers?: Headers; - constructor({ serviceUrl, locale }: MicrosoftSpeechAPI = {}) { + constructor({ serviceUrl, locale, headers }: MicrosoftSpeechAPI = {}) { this.locale = locale; this.serviceUrl = serviceUrl; + this.headers = headers; } get voiceOptions() { return getAzureVoiceOptions(this.locale); @@ -32,16 +35,20 @@ export class MicrosoftSpeechTTS { static voiceName = voiceName; static styleList = styleList; - private fetch = async (payload: MicrosoftSpeechPayload, headers?: Headers) => { + private fetch = async (payload: MicrosoftSpeechPayload) => { const response = await (this.serviceUrl - ? fetch(this.serviceUrl, { body: JSON.stringify(payload), headers, method: 'POST' }) + ? fetch(this.serviceUrl, { + body: JSON.stringify(payload), + headers: this.headers, + method: 'POST', + }) : createMicrosoftSpeech({ payload })); return response; }; - create = async (payload: MicrosoftSpeechPayload, headers?: Headers): Promise => { - return await this.fetch(payload, headers); + create = async (payload: MicrosoftSpeechPayload): Promise => { + return await this.fetch(payload); }; createAudio = async (payload: MicrosoftSpeechPayload): Promise => { diff --git a/src/core/OpenAISTT/index.ts b/src/core/OpenAISTT/index.ts index f20e9d2..42191fb 100644 --- a/src/core/OpenAISTT/index.ts +++ b/src/core/OpenAISTT/index.ts @@ -23,6 +23,7 @@ export interface OpenAISTTPayload { export interface OpenAISTTAPI { OPENAI_API_KEY?: string; OPENAI_PROXY_URL?: string; + headers?: Headers; serviceUrl?: string; } @@ -44,19 +45,24 @@ export class OpenaiSTT { private OPENAI_BASE_URL: string; private OPENAI_API_KEY: string | undefined; private serviceUrl: string | undefined; - + private headers?: Headers; constructor(api: OpenAISTTAPI = {}) { this.OPENAI_BASE_URL = api.OPENAI_PROXY_URL || OPENAI_BASE_URL; this.OPENAI_API_KEY = api.OPENAI_API_KEY; this.serviceUrl = api.serviceUrl; + this.headers = api.headers; } static safeRecordMineType = getRecordMineType; - fetch = async (payload: OpenAISTTPayload, headers?: Headers) => { + fetch = async (payload: OpenAISTTPayload) => { const url = urlJoin(this.OPENAI_BASE_URL, 'audio/speech'); return this.serviceUrl - ? fetch(this.serviceUrl, { body: JSON.stringify(payload), headers, method: 'POST' }) + ? fetch(this.serviceUrl, { + body: JSON.stringify(payload), + headers: this.headers, + method: 'POST', + }) : fetch(url, { body: genSTTBody(payload), headers: new Headers({ @@ -65,8 +71,8 @@ export class OpenaiSTT { method: 'POST', }); }; - create = async (payload: OpenAISTTPayload, headers?: Headers): Promise => { - const response = await this.fetch(payload, headers); + create = async (payload: OpenAISTTPayload): Promise => { + const response = await this.fetch(payload); return response; }; diff --git a/src/core/OpenAITTS/index.ts b/src/core/OpenAITTS/index.ts index 80d209b..4ada73b 100644 --- a/src/core/OpenAITTS/index.ts +++ b/src/core/OpenAITTS/index.ts @@ -27,6 +27,7 @@ export interface OpenAITTSPayload { export interface OpenAITTSAPI { OPENAI_API_KEY?: string; OPENAI_PROXY_URL?: string; + headers?: Headers; serviceUrl?: string; } @@ -34,11 +35,13 @@ export class OpenAITTS { private OPENAI_BASE_URL: string; private OPENAI_API_KEY: string | undefined; private serviceUrl: string | undefined; + private headers?: Headers; constructor(api: OpenAITTSAPI = {}) { this.OPENAI_BASE_URL = api.OPENAI_PROXY_URL || OPENAI_BASE_URL; this.OPENAI_API_KEY = api.OPENAI_API_KEY; this.serviceUrl = api.serviceUrl; + this.headers = api.headers; } get voiceOptions() { @@ -65,8 +68,8 @@ export class OpenAITTS { }); }; - create = async (payload: OpenAITTSPayload, headers?: Headers): Promise => { - const response = await this.fetch(payload, headers); + create = async (payload: OpenAITTSPayload): Promise => { + const response = await this.fetch(payload); return response; }; diff --git a/src/react/useEdgeSpeech/index.ts b/src/react/useEdgeSpeech/index.ts index 7aa720b..4287f79 100644 --- a/src/react/useEdgeSpeech/index.ts +++ b/src/react/useEdgeSpeech/index.ts @@ -5,20 +5,19 @@ import { type TTSConfig, useTTS } from '@/react/useTTS'; export interface EdgeSpeechOptions extends Pick, TTSConfig { api?: EdgeSpeechAPI; - headers?: Headers; locale?: string; } export const useEdgeSpeech = (defaultText: string, init: EdgeSpeechOptions) => { const [text, setText] = useState(defaultText); - const { options, api, locale, headers, ...swrConfig } = init; + const { options, api, locale, ...swrConfig } = init; const [response, setResponse] = useState(); const rest = useTTS( options.voice, text, async (segmentText: string) => { const instance = new EdgeSpeechTTS({ ...api, locale }); - const res = await instance.create({ input: segmentText, options }, headers); + const res = await instance.create({ input: segmentText, options }); setResponse(res); return res.arrayBuffer(); }, diff --git a/src/react/useMicrosoftSpeech/index.ts b/src/react/useMicrosoftSpeech/index.ts index adfe742..4350ac1 100644 --- a/src/react/useMicrosoftSpeech/index.ts +++ b/src/react/useMicrosoftSpeech/index.ts @@ -9,20 +9,19 @@ import { type TTSConfig, useTTS } from '@/react/useTTS'; export interface MicrosoftSpeechOptions extends Pick, TTSConfig { api?: MicrosoftSpeechAPI; - headers?: Headers; locale?: string; } export const useMicrosoftSpeech = (defaultText: string, init: MicrosoftSpeechOptions) => { const [text, setText] = useState(defaultText); - const { options, locale, api, headers, ...swrConfig } = init; + const { options, locale, api, ...swrConfig } = init; const [response, setResponse] = useState(); const rest = useTTS( options.voice, text, async (segmentText: string) => { const instance = new MicrosoftSpeechTTS({ ...api, locale }); - const res = await instance.create({ input: segmentText, options }, headers); + const res = await instance.create({ input: segmentText, options }); setResponse(res); return res.arrayBuffer(); }, diff --git a/src/react/useOpenAISTT/useOpenAISTTCore.ts b/src/react/useOpenAISTT/useOpenAISTTCore.ts index 3456cf4..633be6e 100644 --- a/src/react/useOpenAISTT/useOpenAISTTCore.ts +++ b/src/react/useOpenAISTT/useOpenAISTTCore.ts @@ -4,18 +4,17 @@ import { type OpenAISTTAPI, type OpenAISTTPayload, OpenaiSTT } from '@/core/Open export interface OpenAISTTCoreOptions extends OpenAISTTPayload, SWRConfiguration { api?: OpenAISTTAPI; - headers?: Headers; shouldFetch?: boolean; } export const useOpenAISTTCore = (init: OpenAISTTCoreOptions) => { const key = new Date().getDate().toString(); - const { shouldFetch, api, options, speech, headers, ...swrConfig } = init; + const { shouldFetch, api, options, speech, ...swrConfig } = init; return useSWR( shouldFetch && speech ? key : null, async () => { const instance = new OpenaiSTT(api); - return instance.create({ options, speech }, headers); + return instance.create({ options, speech }); }, swrConfig, ); diff --git a/src/react/useOpenAISTT/useOpenAISTTRecorder.ts b/src/react/useOpenAISTT/useOpenAISTTRecorder.ts index d172e83..5887efb 100644 --- a/src/react/useOpenAISTT/useOpenAISTTRecorder.ts +++ b/src/react/useOpenAISTT/useOpenAISTTRecorder.ts @@ -11,7 +11,6 @@ export interface OpenAISTTRecorderOptions extends SpeechRecognitionRecorderOptions, SWRConfiguration, Partial { - headers?: Headers; onFinished?: SWRConfiguration['onSuccess']; } diff --git a/src/react/useOpenAITTS/index.ts b/src/react/useOpenAITTS/index.ts index 010a050..e4fabf7 100644 --- a/src/react/useOpenAITTS/index.ts +++ b/src/react/useOpenAITTS/index.ts @@ -5,19 +5,18 @@ import { type TTSConfig, useTTS } from '@/react/useTTS'; export interface OpenAITTSOptions extends Pick, TTSConfig { api?: OpenAITTSAPI; - headers?: Headers; } export const useOpenAITTS = (defaultText: string, init: OpenAITTSOptions) => { const [text, setText] = useState(defaultText); - const { options, api, headers, ...swrConfig } = init; + const { options, api, ...swrConfig } = init; const [response, setResponse] = useState(); const rest = useTTS( options.voice, text, async (segmentText: string) => { const instance = new OpenAITTS(api); - const res = await instance.create({ input: segmentText, options }, headers); + const res = await instance.create({ input: segmentText, options }); setResponse(res); return res.arrayBuffer(); },