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 Tsbv2ServiceAccount = {fqn?: string | undefined, displayName?: string | undefined, etag?: string | undefined, description?: string | undefined, keys?: Array | undefined}; export type CreateApiV2OrganizationsOrganizationServiceaccountsArgs = {organization: string, body: {keyEncoding?: KeyPairEncoding | undefined, name: string, serviceAccount: Tsbv2ServiceAccount}}; export type CreateApiV2OrganizationsOrganizationServiceaccountsOptions = {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 }; /** * Create Service Account in TSB. * Service Accounts are local to TSB and can be used to access the platform using * JWT tokens signed with the Service Account's private key for authentication. * * POST /v2/organizations/{organization}/serviceaccounts */ export const createApiV2OrganizationsOrganizationServiceaccounts = async (args: CreateApiV2OrganizationsOrganizationServiceaccountsArgs, options: CreateApiV2OrganizationsOrganizationServiceaccountsOptions) => { 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${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(tsbv2ServiceAccount, await res.json()) };