import {z} from 'zod' export type PatchApiOauthRegisterIdArgs = {id: string, 'if-match': string, body: {redirect_uris: Array, token_endpoint_auth_method?: ('none' | 'client_secret_post' | 'client_secret_basic') | undefined, grant_types?: (Array<'authorization_code' | 'refresh_token' | 'implicit'>) | undefined, response_types?: (Array<'code' | 'token'>) | undefined, client_name?: string | undefined, client_uri?: string | undefined, logo_uri?: string | undefined, scope?: string | undefined, contacts?: Array | undefined, tos_uri?: string | undefined, policy_uri?: string | undefined, jwks_uri?: string | undefined, jwks?: ({[key: string]: string | number | boolean | null}) | undefined, software_id?: string | undefined, software_version?: string | undefined, software_statement?: string | undefined}}; export type PatchApiOauthRegisterIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApiOauthRegisterIdResponse = z.object({redirect_uris: z.array(z.string()), token_endpoint_auth_method: z.enum(["none", "client_secret_post", "client_secret_basic"]).optional(), grant_types: z.array(z.enum(["authorization_code", "refresh_token", "implicit"])).optional(), response_types: z.array(z.enum(["code", "token"])).optional(), client_name: z.string().min(1).optional(), client_uri: z.string().optional(), logo_uri: z.string().optional(), scope: z.string().optional(), contacts: z.array(z.string()).optional(), tos_uri: z.string().optional(), policy_uri: z.string().optional(), jwks_uri: z.string().optional(), jwks: z.record(z.string(), z.union([z.string(), z.number(), z.boolean()]).nullable()).optional(), software_id: z.string().min(1).optional(), software_version: z.string().min(1).optional(), software_statement: z.string().min(1).optional(), client_id: z.string().min(1), client_secret: z.string().min(1).optional(), client_id_issued_at: z.number().int().gte(0).lte(2147483647).optional(), client_secret_expires_at: z.number().int().gte(0).lte(2147483647).optional(), registration_client_uri: z.string().optional(), registration_access_token: z.string().min(1).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 a register * * PATCH /oauth/register/{id} * * Also accepts auth: OAuth2 */ export const patchApiOauthRegisterId = async (args: PatchApiOauthRegisterIdArgs, options?: PatchApiOauthRegisterIdOptions) => { const {baseUrl = "https://core.sivo.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['if-match'] != null) headers.set("if-match", String(args['if-match'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/oauth/register/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiOauthRegisterIdResponse, await res.json()) };