import {z} from 'zod' export type GetApiOrgunitArgs = {search?: string | undefined, pageNumber: number, pageSize: number, defaultOrgunit?: boolean | undefined}; export type GetApiOrgunitOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const address = z.object({address1: z.string().nullable().optional(), state: z.string().nullable().optional(), postalCode: z.string().nullable().optional(), countryCode: z.string().nullable().optional()}); export const commCode = z.object({type: z.string().nullable().optional(), value: z.string().nullable().optional()}); export const communicationCodes = z.object({commCode: z.array(commCode).nullable().optional()}); export const orgunitResponse = z.object({id: z.string().nullable().optional(), accountId: z.string().nullable().optional(), name: z.string().nullable().optional(), shortName: z.string().nullable().optional(), email: z.string().nullable().optional(), contactNo: z.string().nullable().optional(), imageURL: z.string().nullable().optional(), website: z.string().nullable().optional(), details: z.string().nullable().optional(), address: address.optional(), groups: z.array(z.string()).nullable().optional(), defaultCampus: z.boolean().optional(), shortenEnabled: z.boolean().optional(), shortenUrlLength: z.number().int().optional(), communicationCodes: communicationCodes.optional(), autoResponseMessage: z.string().nullable().optional()}); export const orgunitResponseListPaginatedResponse = z.object({totalCount: z.number().int().optional(), filterCount: z.number().int().optional(), data: z.array(orgunitResponse).nullable().optional()}); export const orgunitResponseListPaginatedResponseExternalApiResponse = z.object({data: orgunitResponseListPaginatedResponse.optional(), appCode: z.number().int().optional(), message: z.string().nullable().optional(), isError: z.boolean().optional(), error: z.string().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 Orgunit * * GET /orgunit */ export const getApiOrgunit = async (args: GetApiOrgunitArgs, options: GetApiOrgunitOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.defaultOrgunit != null) query.append("defaultOrgunit", String(args.defaultOrgunit)) const url = `${baseUrl.replace(/\/+$/, '')}/orgunit${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(orgunitResponseListPaginatedResponseExternalApiResponse, await res.json()) };