import {z} from 'zod' export type GetApiV1GatewayProvidersArgs = {limit?: number | undefined, cursor?: (string | null) | undefined}; export type GetApiV1GatewayProvidersOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fixedApiKeyProviderRead = z.object({vendor: z.enum(["openai", "anthropic", "doubleword", "google-gla", "groq", "huggingface", "mistral"])}); export const bedrockProviderReadConfig = z.object({api: z.enum(["runtime", "mantle"]), region: z.string().min(3).max(63)}); export const bedrockProviderRead = z.object({vendor: z.literal("bedrock"), config: bedrockProviderReadConfig}); export const azureProviderReadConfig = z.object({endpoint: z.string()}); export const azureProviderRead = z.object({vendor: z.literal("azure"), config: azureProviderReadConfig}); export const googleVertexProviderReadConfig = z.object({endpoint: z.string()}); export const googleVertexProviderRead = z.object({vendor: z.literal("google-vertex"), config: googleVertexProviderReadConfig}); export const gatewayProviderPricingOutput = z.object({required: z.boolean()}); export const gatewayProviderRead = z.object({id: z.string(), slug: z.string(), provider: z.discriminatedUnion("vendor", [fixedApiKeyProviderRead, bedrockProviderRead, azureProviderRead, googleVertexProviderRead]), pricing: gatewayProviderPricingOutput, created_at: z.string()}); export const gatewayProvidersPage = z.object({providers: z.array(gatewayProviderRead), next_cursor: z.string().nullable()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List Gateway Providers * * GET /v1/gateway/providers/ */ export const getApiV1GatewayProviders = async (args: GetApiV1GatewayProvidersArgs, options: GetApiV1GatewayProvidersOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gateway/providers/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(gatewayProvidersPage, await res.json()) };