import {z} from 'zod' export type GetApiV1Args = {skip?: number | undefined, take?: number | undefined, order?: ({[key: string]: 'ASC' | 'DESC'}) | undefined, userTypes?: (Array<'ADMIN' | 'INNOVATOR' | 'ACCESSOR' | 'ASSESSMENT' | 'QUALIFYING_ACCESSOR'>) | undefined, organisationUnitId?: string | undefined, onlyActive?: boolean | undefined, fields?: Array<'email'> | undefined}; export type GetApiV1Options = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1Response = z.object({id: 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 }; /** * Get users list * * GET /v1 */ export const getApiV1 = async (args: GetApiV1Args, options: GetApiV1Options) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.skip != null) query.append("skip", String(args.skip)) if (args.take != null) query.append("take", String(args.take)) if (args.order != null) query.append("order", JSON.stringify(args.order)) for (const item of args.userTypes ?? []) query.append("userTypes", String(item)) if (args.organisationUnitId != null) query.append("organisationUnitId", String(args.organisationUnitId)) if (args.onlyActive != null) query.append("onlyActive", String(args.onlyActive)) for (const item of args.fields ?? []) query.append("fields", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/v1${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1Response, await res.json()) };