import {z} from 'zod' export type GetApiApiUsersServiceUserIdPersonalAccessTokensPatIdArgs = {userId: string | null, patId: string}; export type GetApiApiUsersServiceUserIdPersonalAccessTokensPatIdOptions = {baseUrl: string, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiUsersServiceUserIdPersonalAccessTokensPatIdResponse = 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()}); 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 }; /** * Get token * * GET /api/users/service/{userId}/personal-access-tokens/{patId} * * Also accepts auth: internalAuth */ export const getApiApiUsersServiceUserIdPersonalAccessTokensPatId = async (args: GetApiApiUsersServiceUserIdPersonalAccessTokensPatIdArgs, options: GetApiApiUsersServiceUserIdPersonalAccessTokensPatIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/users/service/${encodeURIComponent(String(args.userId))}/personal-access-tokens/${encodeURIComponent(String(args.patId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiUsersServiceUserIdPersonalAccessTokensPatIdResponse, await res.json()) };