import {z} from 'zod' export type APIKeyCreateRequest = {description?: string | undefined, read_only?: boolean | undefined, expires_at?: (string | null) | undefined, workspaces?: (Array | null) | undefined, role_id?: (string | null) | undefined, org_role_id?: (string | null) | undefined, default_workspace_id?: (string | null) | undefined}; export type CreateApiApiV1OrgsCurrentPersonalAccessTokensArgs = {body: APIKeyCreateRequest}; export type CreateApiApiV1OrgsCurrentPersonalAccessTokensOptions = {baseUrl: string, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const accessScope = z.enum(["organization", "workspace"]); export const aPIKeyCreateResponse = z.object({created_at: z.string().nullable().optional(), id: z.string(), short_key: z.string(), description: z.string(), read_only: z.boolean().optional(), last_used_at: z.string().nullable().optional(), expires_at: z.string().nullable().optional(), revoked_at: z.string().nullable().optional(), workspace_names: z.array(z.string()).nullable().optional(), default_workspace_name: z.string().nullable().optional(), role_id: z.string().nullable().optional(), org_role_id: z.string().nullable().optional(), access_scope: accessScope.nullable().optional(), created_by: z.string().nullable().optional(), is_tracing_key: z.boolean().optional(), workspace_ids: z.array(z.string()).optional(), can_delete: z.boolean().optional(), key: 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 Org Personal Access Token * * POST /api/v1/orgs/current/personal-access-tokens * * Also accepts auth: Organization ID, Bearer Auth */ export const createApiApiV1OrgsCurrentPersonalAccessTokens = async (args: CreateApiApiV1OrgsCurrentPersonalAccessTokensArgs, options: CreateApiApiV1OrgsCurrentPersonalAccessTokensOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/orgs/current/personal-access-tokens${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(aPIKeyCreateResponse, await res.json()) };