import {z} from 'zod' export type GetApiV2EnrollmentsArgs = {'filter[student.id]'?: (string | null) | undefined, 'filter[student.email]'?: (string | null) | undefined, 'filter[course.id]'?: (string | null) | undefined, 'filter[enrolled_gte]'?: (string | null) | undefined, 'filter[enrolled_lte]'?: (string | null) | undefined, 'filter[completed_gte]'?: (string | null) | undefined, 'filter[completed_lte]'?: (string | null) | undefined, 'filter[progress_status]'?: (string | null) | undefined, 'filter[domains]'?: (string | null) | undefined, 'filter[active]'?: (boolean | null) | undefined, include?: (string | null) | undefined, 'page[size]'?: number | undefined, 'page[cursor]'?: (string | null) | undefined}; export type GetApiV2EnrollmentsOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const enrollmentAttributes = z.object({enrolled_at: z.string(), expires_at: z.string().nullable().optional(), due_at: z.string().nullable().optional(), active: z.boolean(), channel: z.string(), source: z.string(), progress_status: z.string(), completed_at: z.string().nullable().optional(), score: z.number().int().nullable().optional(), max_score: z.number().int().nullable().optional(), success_status: z.string(), domain_name: z.string(), has_certificate: z.boolean(), rating: z.number().int().nullable().optional(), rating_scale: z.number().int().nullable().optional(), feedback: z.string().nullable().optional()}); export const studentIdentifier = z.object({type: z.string().optional(), id: z.string()}); export const studentRelationship = z.object({data: studentIdentifier.nullable().optional()}); export const publishedCourseIdentifier = z.object({type: z.string().optional(), id: z.string()}); export const publishedCourseRelationship = z.object({data: publishedCourseIdentifier}); export const purchaseIdentifier = z.object({type: z.string().optional(), id: z.string()}); export const purchaseRelationship = z.object({data: purchaseIdentifier.nullable().optional()}); export const certificateIdentifier = z.object({type: z.string().optional(), id: z.string()}); export const certificateRelationship = z.object({data: certificateIdentifier.nullable().optional()}); export const enrollmentRelationships = z.object({student: studentRelationship, "published-course": publishedCourseRelationship, purchase: purchaseRelationship, certificate: certificateRelationship}); export const enrollmentResource = z.object({type: z.string().optional(), id: z.string(), attributes: enrollmentAttributes, relationships: enrollmentRelationships, 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 money = z.object({amount: z.string(), currency_code: z.string()}); export const purchaseAttributes = z.object({order_id: z.string(), offer_price: money.nullable().optional(), purchase_price: money, quantity: z.number().int(), purchase_state: z.string(), payment_processor: z.string(), purchased_at: z.string(), tax_price_cents: z.number().int().nullable().optional()}); export const purchaseResource = z.object({type: z.string().optional(), id: z.string(), attributes: purchaseAttributes}); 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 certificateAttributes = z.object({code: z.string(), issued_at: z.string(), expires_at: z.string().nullable().optional(), score_as_percent: z.number().int().nullable().optional()}); export const courseIdentifier = z.object({type: z.literal("courses"), id: z.string()}); export const courseRelationship = z.object({data: courseIdentifier}); export const certificateRelationships = z.object({student: studentRelationship, course: courseRelationship}); export const certificateResource = z.object({type: z.string().optional(), id: z.string(), attributes: certificateAttributes, relationships: certificateRelationships, links: z.record(z.string(), z.unknown()).nullable().optional()}); export const enrollmentListResponse = z.object({data: z.array(enrollmentResource), meta: z.record(z.string(), z.unknown()).nullable().optional(), links: paginationLinks.nullable().optional(), included: z.array(z.union([purchaseResource, studentResource, certificateResource])).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 enrollments * * GET /v2/enrollments/ */ export const getApiV2Enrollments = async (args: GetApiV2EnrollmentsArgs, options: GetApiV2EnrollmentsOptions) => { 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[student.id]'] != null) query.append("filter[student.id]", String(args['filter[student.id]'])) if (args['filter[student.email]'] != null) query.append("filter[student.email]", String(args['filter[student.email]'])) if (args['filter[course.id]'] != null) query.append("filter[course.id]", String(args['filter[course.id]'])) if (args['filter[enrolled_gte]'] != null) query.append("filter[enrolled_gte]", String(args['filter[enrolled_gte]'])) if (args['filter[enrolled_lte]'] != null) query.append("filter[enrolled_lte]", String(args['filter[enrolled_lte]'])) if (args['filter[completed_gte]'] != null) query.append("filter[completed_gte]", String(args['filter[completed_gte]'])) if (args['filter[completed_lte]'] != null) query.append("filter[completed_lte]", String(args['filter[completed_lte]'])) if (args['filter[progress_status]'] != null) query.append("filter[progress_status]", String(args['filter[progress_status]'])) if (args['filter[domains]'] != null) query.append("filter[domains]", String(args['filter[domains]'])) if (args['filter[active]'] != null) query.append("filter[active]", String(args['filter[active]'])) if (args.include != null) query.append("include", String(args.include)) 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/enrollments/${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(enrollmentListResponse, await res.json()) };