import {z} from 'zod' export type ApikeyV1CreateAPIKeyRequest = {billingMode?: string | undefined, expiresAt?: string | undefined, name?: string | undefined, scopes?: Array | undefined, subscriptionId?: string | undefined, teamId?: string | undefined}; export type CreateApiV1MeApikeysArgs = {body: ApikeyV1CreateAPIKeyRequest}; export type CreateApiV1MeApikeysOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const apikeyV1APIKeyInfo = z.object({billingMode: z.string().optional(), channelSourceCode: z.string().optional(), channelSourceId: z.string().optional(), createdAt: z.string().optional(), creatorId: z.string().optional(), creatorName: z.string().optional(), cumulativeUsedMicros: z.string().optional(), expiresAt: z.string().optional(), id: z.string().optional(), keyPrefix: z.string().optional(), lastUsedAt: z.string().optional(), lastUsedIp: z.string().optional(), name: z.string().optional(), scopes: z.array(z.string()).optional(), status: z.number().int().optional(), subscriptionId: z.string().optional(), teamId: z.string().optional(), teamName: z.string().optional(), updatedAt: z.string().optional(), userId: z.string().optional()}); export const apikeyV1CreateAPIKeyReply = z.object({apikey: apikeyV1APIKeyInfo.optional(), plaintextKey: z.string().optional()}); 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 }; /** * POST /v1/me/apikeys */ export const createApiV1MeApikeys = async (args: CreateApiV1MeApikeysArgs, options: CreateApiV1MeApikeysOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/me/apikeys${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(apikeyV1CreateAPIKeyReply, await res.json()) };