import {z} from 'zod' export type WorkspaceId = string; export type OAuthScopeList = Array; export type CreateApiCredentialRequest = {name: string, scopes: OAuthScopeList, expires_at?: (string | null) | undefined}; export type CreateApiV1WorkspacesWorkspaceIdApiCredentialsArgs = {workspace_id: WorkspaceId, 'Idempotency-Key': string, body: CreateApiCredentialRequest}; export type CreateApiV1WorkspacesWorkspaceIdApiCredentialsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const requestId = z.string(); export const runtimeLocation = z.enum(["local", "cloud"]); export const execution = z.object({location: runtimeLocation, capability_id: z.string().nullable().optional(), capability_version: z.string().nullable().optional()}); export const usage = z.object({billable: z.boolean(), quantity: z.number().gte(0), unit: z.string(), credits: z.number().gte(0).optional()}); export const apiCredentialId = z.string(); export const workspaceId = z.string(); export const oAuthScopeList = z.array(z.string().min(1)); export const apiCredential = z.object({id: apiCredentialId, workspace_id: workspaceId, name: z.string().min(1), prefix: z.string().min(1), scopes: oAuthScopeList, created_at: z.string(), last_used_at: z.string().nullable(), expires_at: z.string().nullable(), revoked_at: z.string().nullable()}); export const createdApiCredential = z.object({credential: apiCredential, secret: z.string().min(1)}); export const createdApiCredentialResponse = z.object({request_id: requestId, execution: execution, usage: usage, warnings: z.array(z.string()), data: createdApiCredential}); 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 a scoped API credential and return its secret once * * POST /v1/workspaces/{workspace_id}/api-credentials * * Also accepts auth: apiKeyAuth */ export const createApiV1WorkspacesWorkspaceIdApiCredentials = async (args: CreateApiV1WorkspacesWorkspaceIdApiCredentialsArgs, options?: CreateApiV1WorkspacesWorkspaceIdApiCredentialsOptions) => { const {baseUrl = "https://api.cerul.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace_id))}/api-credentials${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(createdApiCredentialResponse, await res.json()) };