import {z} from 'zod' export type GatewayUserState = 'STATE_UNSPECIFIED' | 'CREATING' | 'READY' | 'UPDATING' | 'DELETING'; export type GatewayCode = 'OK' | 'CANCELLED' | 'UNKNOWN' | 'INVALID_ARGUMENT' | 'DEADLINE_EXCEEDED' | 'NOT_FOUND' | 'ALREADY_EXISTS' | 'PERMISSION_DENIED' | 'UNAUTHENTICATED' | 'RESOURCE_EXHAUSTED' | 'FAILED_PRECONDITION' | 'ABORTED' | 'OUT_OF_RANGE' | 'UNIMPLEMENTED' | 'INTERNAL' | 'UNAVAILABLE' | 'DATA_LOSS'; export type GatewayStatus = {code?: GatewayCode | undefined, message?: string | undefined}; export type GatewayUser = {name?: string | undefined, displayName?: string | undefined, serviceAccount?: boolean | undefined, createTime?: string | undefined, role: string, email?: string | undefined, state?: GatewayUserState | undefined, status?: GatewayStatus | undefined, updateTime?: string | undefined, permissionPreset?: string | undefined}; export type CreateApiV1AccountsAccountIdUsersArgs = {userId?: string | undefined, account_id: string, body: GatewayUser}; export type CreateApiV1AccountsAccountIdUsersOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewayUserState = z.enum(["STATE_UNSPECIFIED", "CREATING", "READY", "UPDATING", "DELETING"]); export const gatewayCode = z.enum(["OK", "CANCELLED", "UNKNOWN", "INVALID_ARGUMENT", "DEADLINE_EXCEEDED", "NOT_FOUND", "ALREADY_EXISTS", "PERMISSION_DENIED", "UNAUTHENTICATED", "RESOURCE_EXHAUSTED", "FAILED_PRECONDITION", "ABORTED", "OUT_OF_RANGE", "UNIMPLEMENTED", "INTERNAL", "UNAVAILABLE", "DATA_LOSS"]); export const gatewayStatus = z.object({code: gatewayCode.optional(), message: z.string().optional()}); export const gatewayUser = z.object({name: z.string().optional(), displayName: z.string().optional(), serviceAccount: z.boolean().optional(), createTime: z.string().optional(), role: z.string(), email: z.string().optional(), state: gatewayUserState.optional(), status: gatewayStatus.optional(), updateTime: z.string().optional(), permissionPreset: 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 }; /** * Create User * * POST /v1/accounts/{account_id}/users */ export const createApiV1AccountsAccountIdUsers = async (args: CreateApiV1AccountsAccountIdUsersArgs, options: CreateApiV1AccountsAccountIdUsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.userId != null) query.append("userId", String(args.userId)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args.account_id))}/users${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(gatewayUser, await res.json()) };