import {z} from 'zod' export type KeyPairEncoding = 'PEM' | 'JWK'; export type ServiceAccountKeyPair = {id?: string | undefined, publicKey?: string | undefined, privateKey?: string | undefined, encoding?: KeyPairEncoding | undefined, defaultToken?: string | undefined}; export type UpdateApiV2OrganizationsOrganizationServiceaccountsServiceaccountArgs = {organization: string, serviceaccount: string, body: {description?: string | undefined, displayName?: string | undefined, etag?: string | undefined, keys?: Array | undefined}}; export type UpdateApiV2OrganizationsOrganizationServiceaccountsServiceaccountOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const keyPairEncoding = z.enum(["PEM", "JWK"]); export const serviceAccountKeyPair = z.object({id: z.string().optional(), publicKey: z.string().optional(), privateKey: z.string().optional(), encoding: keyPairEncoding.optional(), defaultToken: z.string().optional()}); export const tsbv2ServiceAccount = z.object({fqn: z.string().optional(), displayName: z.string().optional(), etag: z.string().optional(), description: z.string().optional(), keys: z.array(serviceAccountKeyPair).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 }; /** * Update the details of a service account. * Updating the details of the service account does not regenerate its keys. * * PUT /v2/organizations/{organization}/serviceaccounts/{serviceaccount} */ export const updateApiV2OrganizationsOrganizationServiceaccountsServiceaccount = async (args: UpdateApiV2OrganizationsOrganizationServiceaccountsServiceaccountArgs, options: UpdateApiV2OrganizationsOrganizationServiceaccountsServiceaccountOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/organizations/${encodeURIComponent(String(args.organization))}/serviceaccounts/${encodeURIComponent(String(args.serviceaccount))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(tsbv2ServiceAccount, await res.json()) };