import {z} from 'zod' export type JsonWebKey = {alg: string, crv?: string | undefined, d?: string | undefined, dp?: string | undefined, dq?: string | undefined, e?: string | undefined, k?: string | undefined, kid: string, kty: string, n?: string | undefined, p?: string | undefined, q?: string | undefined, qi?: string | undefined, use: string, x?: string | undefined, x5c?: Array | undefined, y?: string | undefined}; export type TrustOAuth2JwtGrantIssuer = {allow_any_subject?: boolean | undefined, expires_at: string, issuer: string, jwk: JsonWebKey, scope: Array, subject?: string | undefined}; export type CreateApiAdminTrustGrantsJwtBearerIssuersArgs = {body: TrustOAuth2JwtGrantIssuer}; export type CreateApiAdminTrustGrantsJwtBearerIssuersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const trustedOAuth2JwtGrantJsonWebKey = z.object({kid: z.string().optional(), set: z.string().optional()}); export const trustedOAuth2JwtGrantIssuer = z.object({allow_any_subject: z.boolean().optional(), created_at: z.string().optional(), expires_at: z.string().optional(), id: z.string().optional(), issuer: z.string().optional(), public_key: trustedOAuth2JwtGrantJsonWebKey.optional(), scope: z.array(z.string()).optional(), subject: 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 }; /** * Trust OAuth2 JWT Bearer Grant Type Issuer * * POST /admin/trust/grants/jwt-bearer/issuers */ export const createApiAdminTrustGrantsJwtBearerIssuers = async (args: CreateApiAdminTrustGrantsJwtBearerIssuersArgs, options: CreateApiAdminTrustGrantsJwtBearerIssuersOptions) => { 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(/\/+$/, '')}/admin/trust/grants/jwt-bearer/issuers${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(trustedOAuth2JwtGrantIssuer, await res.json()) };