import {z} from 'zod' export type CreateProviderRequest = {type: 'awsS3' | 'openAI' | 'awsBedrock', name: string, configuration: {accessKeyId: string, secretAccessKey: string, sessionToken?: string | undefined, awsRegion: string, bucket: string, folderPath?: string | undefined} | {apiKey: string} | {accessKeyId?: string | undefined, secretAccessKey?: string | undefined, apiKey?: string | undefined, region?: ('us-east-1' | 'us-west-2' | 'ap-northeast-1' | 'ap-northeast-2' | 'ap-northeast-3' | 'ap-south-1' | 'ap-south-2' | 'ap-southeast-1' | 'ap-southeast-2' | 'ca-central-1' | 'eu-central-1' | 'eu-central-2' | 'eu-north-1' | 'eu-south-1' | 'eu-south-2' | 'eu-west-1' | 'eu-west-2' | 'eu-west-3' | 'sa-east-1') | undefined}}; export type CreateApiV4OrganizationsOrganizationIdAiServicesProvidersArgs = {body: CreateProviderRequest}; export type CreateApiV4OrganizationsOrganizationIdAiServicesProvidersOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const createProviderResponse = z.object({id: z.string()}); 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 }; /** * Create provider * * POST /v4/organizations/{organizationId}/aiServices/providers */ export const createApiV4OrganizationsOrganizationIdAiServicesProviders = async (args: CreateApiV4OrganizationsOrganizationIdAiServicesProvidersArgs, options?: CreateApiV4OrganizationsOrganizationIdAiServicesProvidersOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/aiServices/providers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createProviderResponse, await res.json()) };