Skip to content

Commit

Permalink
🐛 fix: Fix header props
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 19, 2023
1 parent 10eff1e commit 7d771a8
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 33 deletions.
20 changes: 13 additions & 7 deletions src/core/EdgeSpeechTTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<Response> => {
return this.fetch(payload, headers);
create = async (payload: EdgeSpeechPayload): Promise<Response> => {
return this.fetch(payload);
};

/**
Expand Down
19 changes: 13 additions & 6 deletions src/core/MicrosoftSpeechTTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Response> => {
return await this.fetch(payload, headers);
create = async (payload: MicrosoftSpeechPayload): Promise<Response> => {
return await this.fetch(payload);
};

createAudio = async (payload: MicrosoftSpeechPayload): Promise<AudioBuffer> => {
Expand Down
16 changes: 11 additions & 5 deletions src/core/OpenAISTT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface OpenAISTTPayload {
export interface OpenAISTTAPI {
OPENAI_API_KEY?: string;
OPENAI_PROXY_URL?: string;
headers?: Headers;
serviceUrl?: string;
}

Expand All @@ -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({
Expand All @@ -65,8 +71,8 @@ export class OpenaiSTT {
method: 'POST',
});
};
create = async (payload: OpenAISTTPayload, headers?: Headers): Promise<Response> => {
const response = await this.fetch(payload, headers);
create = async (payload: OpenAISTTPayload): Promise<Response> => {
const response = await this.fetch(payload);

return response;
};
Expand Down
7 changes: 5 additions & 2 deletions src/core/OpenAITTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ export interface OpenAITTSPayload {
export interface OpenAITTSAPI {
OPENAI_API_KEY?: string;
OPENAI_PROXY_URL?: string;
headers?: Headers;
serviceUrl?: string;
}

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() {
Expand All @@ -65,8 +68,8 @@ export class OpenAITTS {
});
};

create = async (payload: OpenAITTSPayload, headers?: Headers): Promise<Response> => {
const response = await this.fetch(payload, headers);
create = async (payload: OpenAITTSPayload): Promise<Response> => {
const response = await this.fetch(payload);

return response;
};
Expand Down
5 changes: 2 additions & 3 deletions src/react/useEdgeSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import { type TTSConfig, useTTS } from '@/react/useTTS';

export interface EdgeSpeechOptions extends Pick<EdgeSpeechPayload, 'options'>, TTSConfig {
api?: EdgeSpeechAPI;
headers?: Headers;
locale?: string;
}

export const useEdgeSpeech = (defaultText: string, init: EdgeSpeechOptions) => {
const [text, setText] = useState<string>(defaultText);
const { options, api, locale, headers, ...swrConfig } = init;
const { options, api, locale, ...swrConfig } = init;
const [response, setResponse] = useState<Response>();
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();
},
Expand Down
5 changes: 2 additions & 3 deletions src/react/useMicrosoftSpeech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@ import { type TTSConfig, useTTS } from '@/react/useTTS';

export interface MicrosoftSpeechOptions extends Pick<MicrosoftSpeechPayload, 'options'>, TTSConfig {
api?: MicrosoftSpeechAPI;
headers?: Headers;
locale?: string;
}

export const useMicrosoftSpeech = (defaultText: string, init: MicrosoftSpeechOptions) => {
const [text, setText] = useState<string>(defaultText);
const { options, locale, api, headers, ...swrConfig } = init;
const { options, locale, api, ...swrConfig } = init;
const [response, setResponse] = useState<Response>();
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();
},
Expand Down
5 changes: 2 additions & 3 deletions src/react/useOpenAISTT/useOpenAISTTCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
1 change: 0 additions & 1 deletion src/react/useOpenAISTT/useOpenAISTTRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export interface OpenAISTTRecorderOptions
extends SpeechRecognitionRecorderOptions,
SWRConfiguration,
Partial<OpenAISTTCoreOptions> {
headers?: Headers;
onFinished?: SWRConfiguration['onSuccess'];
}

Expand Down
5 changes: 2 additions & 3 deletions src/react/useOpenAITTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import { type TTSConfig, useTTS } from '@/react/useTTS';

export interface OpenAITTSOptions extends Pick<OpenAITTSPayload, 'options'>, TTSConfig {
api?: OpenAITTSAPI;
headers?: Headers;
}

export const useOpenAITTS = (defaultText: string, init: OpenAITTSOptions) => {
const [text, setText] = useState<string>(defaultText);
const { options, api, headers, ...swrConfig } = init;
const { options, api, ...swrConfig } = init;
const [response, setResponse] = useState<Response>();
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();
},
Expand Down

0 comments on commit 7d771a8

Please sign in to comment.