import {z} from 'zod' export type GetApiV2StudentsIdRelationshipsDomainMembershipsArgs = {id: string, 'filter[active]'?: (boolean | null) | undefined, 'page[size]'?: number | undefined, 'page[cursor]'?: (string | null) | undefined}; export type GetApiV2StudentsIdRelationshipsDomainMembershipsOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const domainMembershipAttributes = z.object({active: z.boolean(), expires_at: z.string().nullable().optional(), created_at: z.string(), modified_at: z.string()}); export const domainIdentifier = z.object({type: z.literal("domains"), id: z.string()}); export const domainRelationship = z.object({data: domainIdentifier}); export const domainMembershipRelationships = z.object({domain: domainRelationship}); export const domainMembershipResource = z.object({type: z.literal("domain-memberships").optional(), id: z.string(), attributes: domainMembershipAttributes, relationships: domainMembershipRelationships, links: z.record(z.string(), z.string()).nullable().optional()}); export const paginationLinks = z.object({self: z.string().nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const paginatedListResponseDomainMembershipResource = z.object({data: z.array(domainMembershipResource), 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 a student's domain memberships * * GET /v2/students/{id}/relationships/domain-memberships/ */ export const getApiV2StudentsIdRelationshipsDomainMemberships = async (args: GetApiV2StudentsIdRelationshipsDomainMembershipsArgs, options: GetApiV2StudentsIdRelationshipsDomainMembershipsOptions) => { 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[active]'] != null) query.append("filter[active]", String(args['filter[active]'])) 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/${encodeURIComponent(String(args.id))}/relationships/domain-memberships/${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(paginatedListResponseDomainMembershipResource, await res.json()) };