import {z} from 'zod' export type APIKeyRequest = {name: string, description?: (string | null) | undefined}; export type CreateApiApiV1ServiceAccountsServiceAccountIdApiKeysArgs = {service_account_id: string, body: APIKeyRequest}; export type CreateApiApiV1ServiceAccountsServiceAccountIdApiKeysOptions = {baseUrl: string, CookieOAuth2TokenBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const serviceAccountResponseBody = z.object({created: z.string(), updated: z.string(), active: z.boolean().optional()}); export const serviceAccountResponseMetadata = z.object({description: z.string().max(65535).optional()}); export const serviceAccountResponseResources = z.record(z.string(), z.unknown()); export const serviceAccountResponse = z.object({body: serviceAccountResponseBody.nullable().optional(), metadata: serviceAccountResponseMetadata.nullable().optional(), resources: serviceAccountResponseResources.nullable().optional(), id: z.string(), permission_denied: z.boolean().optional(), name: z.string().max(255)}); export const aPIKeyResponseBody = z.object({created: z.string(), updated: z.string(), key: z.string().nullable().optional(), active: z.boolean().optional(), service_account: serviceAccountResponse}); export const aPIKeyResponseMetadata = z.object({description: z.string().max(65535).optional(), retain_period_minutes: z.number().int(), last_login: z.string().nullable().optional(), last_rotated: z.string().nullable().optional()}); export const aPIKeyResponseResources = z.record(z.string(), z.unknown()); export const aPIKeyResponse = z.object({body: aPIKeyResponseBody.nullable().optional(), metadata: aPIKeyResponseMetadata.nullable().optional(), resources: aPIKeyResponseResources.nullable().optional(), id: z.string(), permission_denied: z.boolean().optional(), name: z.string().max(255)}); 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 Api Key * * POST /api/v1/service_accounts/{service_account_id}/api_keys */ export const createApiApiV1ServiceAccountsServiceAccountIdApiKeys = async (args: CreateApiApiV1ServiceAccountsServiceAccountIdApiKeysArgs, options: CreateApiApiV1ServiceAccountsServiceAccountIdApiKeysOptions) => { const {baseUrl, fetch: fetchFn = fetch, CookieOAuth2TokenBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (CookieOAuth2TokenBearer !== undefined) headers.set('authorization', `Bearer ${CookieOAuth2TokenBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/service_accounts/${encodeURIComponent(String(args.service_account_id))}/api_keys${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(aPIKeyResponse, await res.json()) };