import {z} from 'zod' export type GetApiV2EnrollmentsIdArgs = {id: string, include?: (string | null) | undefined}; export type GetApiV2EnrollmentsIdOptions = {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 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 enrollmentDetailResponse = z.object({data: enrollmentResource, 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 }; /** * Get enrollment * * GET /v2/enrollments/{id} */ export const getApiV2EnrollmentsId = async (args: GetApiV2EnrollmentsIdArgs, options: GetApiV2EnrollmentsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.include != null) query.append("include", String(args.include)) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/enrollments/${encodeURIComponent(String(args.id))}${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(enrollmentDetailResponse, await res.json()) };