import {z} from 'zod' export type CreateApiApiUsersServiceUserIdPersonalAccessTokensArgs = {userId: string | null, body: {note: string, allProjects: boolean, projects?: Array | undefined, permissions?: (Array<'owner' | 'documents' | 'releases' | 'releases:manage' | 'releases:deploy' | 'releases:delete' | 'integrations:manage' | 'documents:full' | 'documents:view-content' | 'documents:edit-content' | 'documents:edit-view' | 'branches' | 'branches:create' | 'branches:merge' | 'branches:delete' | 'project:manage' | 'environments' | 'environments:manage' | 'environments:delete'>) | undefined, expiry: 'week' | 'month' | 'three_months' | 'year' | 'indefinite'}}; export type CreateApiApiUsersServiceUserIdPersonalAccessTokensOptions = {baseUrl: string, internalAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiUsersServiceUserIdPersonalAccessTokensResponse = z.object({id: z.string(), note: z.string(), permissions: z.array(z.enum(["owner", "documents", "releases", "releases:manage", "releases:deploy", "releases:delete", "integrations:manage", "documents:full", "documents:view-content", "documents:edit-content", "documents:edit-view", "branches", "branches:create", "branches:merge", "branches:delete", "project:manage", "environments", "environments:manage", "environments:delete"])), allProjects: z.boolean(), projects: z.array(z.object({id: z.string(), name: z.string(), key: z.string().optional(), organisationId: z.string(), version: z.string(), createdAt: z.string(), updatedAt: z.string(), protected: z.boolean().optional()})), user: z.object({id: z.string(), email: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), type: z.string(), status: z.string(), isService: z.boolean().optional(), createdAt: z.string(), updatedAt: z.string()}), createdAt: z.string(), updatedAt: z.string(), expiresAt: z.string().nullable().optional(), token: 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 token * * POST /api/users/service/{userId}/personal-access-tokens */ export const createApiApiUsersServiceUserIdPersonalAccessTokens = async (args: CreateApiApiUsersServiceUserIdPersonalAccessTokensArgs, options: CreateApiApiUsersServiceUserIdPersonalAccessTokensOptions) => { const {baseUrl, fetch: fetchFn = fetch, internalAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (internalAuth !== undefined) headers.set('authorization', `internal ${internalAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/users/service/${encodeURIComponent(String(args.userId))}/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(createApiApiUsersServiceUserIdPersonalAccessTokensResponse, await res.json()) };