import {z} from 'zod' export type GetApiV2StudentsArgs = {'filter[email]'?: (string | null) | undefined, 'filter[first_name]'?: (string | null) | undefined, 'filter[last_name]'?: (string | null) | undefined, 'filter[is_inactive]'?: (boolean | null) | undefined, 'page[size]'?: number | undefined, 'page[cursor]'?: (string | null) | undefined}; export type GetApiV2StudentsOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const studentAttributes = z.object({email: z.string(), first_name: z.string(), last_name: z.string(), is_inactive: z.boolean(), external_id: z.string(), date_joined: z.string()}); export const studentResource = z.object({type: z.string().optional(), id: z.string(), attributes: studentAttributes, relationships: z.record(z.string(), z.unknown()).nullable().optional(), links: z.record(z.string(), z.unknown()).nullable().optional()}); export const paginationLinks = z.object({self: z.string().nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const paginatedListResponseStudentResource = z.object({data: z.array(studentResource), meta: z.record(z.string(), z.unknown()).nullable().optional(), links: paginationLinks.nullable().optional(), included: z.array(z.record(z.string(), z.unknown())).nullable().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 }; /** * List students * * GET /v2/students/ */ export const getApiV2Students = async (args: GetApiV2StudentsArgs, options: GetApiV2StudentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['filter[email]'] != null) query.append("filter[email]", String(args['filter[email]'])) if (args['filter[first_name]'] != null) query.append("filter[first_name]", String(args['filter[first_name]'])) if (args['filter[last_name]'] != null) query.append("filter[last_name]", String(args['filter[last_name]'])) if (args['filter[is_inactive]'] != null) query.append("filter[is_inactive]", String(args['filter[is_inactive]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[cursor]'] != null) query.append("page[cursor]", String(args['page[cursor]'])) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/students/${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(paginatedListResponseStudentResource, await res.json()) };