import {z} from 'zod' export type SortOrder = string; export type OauthClientListMetadata = {filter?: string | undefined, kind?: string | undefined, sort_order?: SortOrder | undefined, offset?: number | undefined, length?: number | undefined, sort_attribute?: string | undefined}; export type CreateApiOauthClientListArgs = {body: OauthClientListMetadata}; export type CreateApiOauthClientListOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const oauthClientResponse = z.object({redirect_uris: z.array(z.string()), client_id: z.string(), client_secret: z.string(), client_name: z.string().max(64), client_description: z.string().max(1000), default_scopes: z.array(z.string())}); export const sortOrder = z.string(); export const oauthClientListMetadata = z.object({filter: z.string().optional(), kind: z.string().optional(), sort_order: sortOrder.optional(), offset: z.number().int().gte(0).optional(), length: z.number().int().gte(1).optional(), sort_attribute: z.string().optional()}); export const oauthClientList = z.object({entities: z.array(oauthClientResponse), metadata: oauthClientListMetadata}); 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 }; /** * Retrieves all oauth client * * POST /oauth/client/list */ export const createApiOauthClientList = async (args: CreateApiOauthClientListArgs, options: CreateApiOauthClientListOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/oauth/client/list${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(oauthClientList, await res.json()) };