import {z} from 'zod' export type GetApiOfferingsOfferingIdBadgesArgs = {offeringId: string}; export type GetApiOfferingsOfferingIdBadgesOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const criteria = z.object({hasCompletedCourse: z.boolean().optional(), hasPassedMandatoryAssessedQuizzes: z.boolean().optional()}); export const badgeExpiry = z.object({expires: z.boolean().optional(), expiryType: z.enum(["date", "timeframe"]).optional(), expirationDate: z.string().optional(), timeframeUnit: z.enum(["days", "months", "years"]).optional(), timeframeAmount: z.number().optional()}); export const openBadgeClass = z.object({criteria: z.object({narrative: z.string().optional()}).optional(), description: z.string().optional(), id: z.string().optional(), image: z.string().optional(), issuer: z.string().optional(), name: z.string().optional(), type: z.string().optional()}); export const badge = z.object({title: z.string().optional(), description: z.string().optional(), criterias: criteria.optional(), badgeExpiry: badgeExpiry.optional(), badgeUrl: z.string().optional(), openBadge: openBadgeClass.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 }; /** * Find offering badges * * GET /offerings/{offeringId}/badges */ export const getApiOfferingsOfferingIdBadges = async (args: GetApiOfferingsOfferingIdBadgesArgs, options?: GetApiOfferingsOfferingIdBadgesOptions) => { const {baseUrl = "https://api.iqualify.com/v1", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/offerings/${encodeURIComponent(String(args.offeringId))}/badges${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(badge, await res.json()) };