Skip to content

Commit

Permalink
refactor(shared): request-url util
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Dec 18, 2024
1 parent 7f9974b commit d1dab26
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/embed/src/utils/embed-many.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CommonRequestOptions, requestBody, requestHeaders } from '@xsai/shared'
import { type CommonRequestOptions, requestBody, requestHeaders, requestURL } from '@xsai/shared'

import type { EmbedResponse, EmbedResponseUsage } from './embed'

Expand All @@ -14,7 +14,7 @@ export interface EmbedManyResult {
}

export const embedMany = async (options: EmbedManyOptions): Promise<EmbedManyResult> =>
await (options.fetch ?? globalThis.fetch)(new URL('embeddings', options.baseURL), {
await (options.fetch ?? globalThis.fetch)(requestURL('embeddings', options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
'Content-Type': 'application/json',
Expand Down
4 changes: 2 additions & 2 deletions packages/embed/src/utils/embed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CommonRequestOptions, requestBody, requestHeaders } from '@xsai/shared'
import { type CommonRequestOptions, requestBody, requestHeaders, requestURL } from '@xsai/shared'

export interface EmbedOptions extends CommonRequestOptions {
[key: string]: unknown
Expand Down Expand Up @@ -29,7 +29,7 @@ export interface EmbedResult {
}

export const embed = async (options: EmbedOptions): Promise<EmbedResult> =>
await (options.fetch ?? globalThis.fetch)(new URL('embeddings', options.baseURL), {
await (options.fetch ?? globalThis.fetch)(requestURL('embeddings', options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
'Content-Type': 'application/json',
Expand Down
4 changes: 2 additions & 2 deletions packages/generate-speech/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CommonRequestOptions, requestBody, requestHeaders } from '@xsai/shared'
import { type CommonRequestOptions, requestBody, requestHeaders, requestURL } from '@xsai/shared'

export interface GenerateSpeechOptions extends CommonRequestOptions {
[key: string]: unknown
Expand All @@ -16,7 +16,7 @@ export interface GenerateSpeechOptions extends CommonRequestOptions {
* @returns audio array buffer
*/
export const generateSpeech = async (options: GenerateSpeechOptions): Promise<ArrayBuffer> =>
await (options.fetch ?? globalThis.fetch)(new URL('audio/speech', options.baseURL), {
await (options.fetch ?? globalThis.fetch)(requestURL('audio/speech', options.baseURL), {
body: requestBody(options),
headers: requestHeaders({
'Content-Type': 'application/json',
Expand Down
4 changes: 2 additions & 2 deletions packages/generate-transcription/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CommonRequestOptions, requestHeaders } from '@xsai/shared'
import { type CommonRequestOptions, requestHeaders, requestURL } from '@xsai/shared'

export interface GenerateTranscriptionOptions extends CommonRequestOptions {
file: Blob
Expand All @@ -24,7 +24,7 @@ export const generateTranscription = async (options: GenerateTranscriptionOption
body.append('model', options.model)
body.append('file', options.file, options.fileName)

return await (options.fetch ?? globalThis.fetch)(new URL('audio/transcriptions', options.baseURL), {
return await (options.fetch ?? globalThis.fetch)(requestURL('audio/transcriptions', options.baseURL), {
body,
headers: requestHeaders(options.headers, options.apiKey),
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/utils/list-models.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type CommonRequestOptions, requestHeaders } from '@xsai/shared'
import { type CommonRequestOptions, requestHeaders, requestURL } from '@xsai/shared'

import type { Model } from '../types/model'

Expand All @@ -10,7 +10,7 @@ export interface ListModelsResponse {
}

export const listModels = async (options: ListModelsOptions): Promise<Model[]> =>
await (options.fetch ?? globalThis.fetch)(new URL('models', options.baseURL), {
await (options.fetch ?? globalThis.fetch)(requestURL('models', options.baseURL), {
headers: requestHeaders({
'Content-Type': 'application/json',
...options.headers,
Expand Down
4 changes: 2 additions & 2 deletions packages/model/src/utils/retrieve-model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type CommonRequestOptions, requestHeaders } from '@xsai/shared'
import { type CommonRequestOptions, requestHeaders, requestURL } from '@xsai/shared'

import type { Model } from '../types/model'

export interface RetrieveModelOptions extends CommonRequestOptions {}

export const retrieveModel = async (options: RetrieveModelOptions): Promise<Model> =>
await (options.fetch ?? globalThis.fetch)(new URL(`models/${options.model}`, options.baseURL), {
await (options.fetch ?? globalThis.fetch)(requestURL(`models/${options.model}`, options.baseURL), {
headers: requestHeaders({
'Content-Type': 'application/json',
...options.headers,
Expand Down
4 changes: 2 additions & 2 deletions packages/shared-chat/src/utils/chat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { requestBody, requestHeaders } from '@xsai/shared'
import { requestBody, requestHeaders, requestURL } from '@xsai/shared'

import type { ChatOptions, Tool } from '../types'

export const chat = async <T extends ChatOptions>(options: T) =>
await (options.fetch ?? globalThis.fetch)(new URL('chat/completions', options.baseURL), {
await (options.fetch ?? globalThis.fetch)(requestURL('chat/completions', options.baseURL), {
body: requestBody({
...options,
tools: (options.tools as Tool[] | undefined)?.map(tool => ({
Expand Down
5 changes: 1 addition & 4 deletions packages/shared/src/types/common-request-options.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
export interface CommonRequestOptions {
abortSignal?: AbortSignal
apiKey?: string
/**
* @example `https://openai.com/v1/`
* @remarks make sure the baseURL ends with a slash.
*/
/** @example `https://openai.com/v1/` */
baseURL: string | URL
fetch?: typeof globalThis.fetch
/** @default `undefined` */
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { objCamelToSnake, strCamelToSnake } from './case'
export { clean } from './clean'
export { requestBody } from './request-body'
export { requestHeaders } from './request-headers'
export { requestURL } from './request-url'
5 changes: 5 additions & 0 deletions packages/shared/src/utils/request-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const requestURL = (path: string, baseURL: string | URL) => {
const base = baseURL.toString()

return new URL(path, base.endsWith('/') ? base : `${base}/`)
}

0 comments on commit d1dab26

Please sign in to comment.