import {z} from 'zod' export type GetApiApiV1Id4nsId4nCollectionsArgs = {id4n: string, organizationId?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiApiV1Id4nsId4nCollectionsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const guidCollection = z.object({createdTimestamp: z.number().int().optional(), holderOrganizationId: z.string().optional(), id4n: z.string().optional(), label: z.string().min(5).max(128).optional(), ownerOrganizationId: z.string().optional(), physicalState: z.enum(["UNATTACHED", "ATTACHED", "DETACHED"]).optional(), type: z.enum(["ROUTING_COLLECTION", "LOGISTIC_COLLECTION", "LABELLED_COLLECTION"]).optional()}); export const paginatedResponseOfGuidCollection = z.object({elements: z.array(guidCollection), limit: z.number().int(), offset: z.number().int(), total: z.number().int().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 }; /** * Retrieve collections of an ID * * GET /api/v1/id4ns/{id4n}/collections */ export const getApiApiV1Id4nsId4nCollections = async (args: GetApiApiV1Id4nsId4nCollectionsArgs, options: GetApiApiV1Id4nsId4nCollectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.organizationId != null) query.append("organizationId", String(args.organizationId)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/id4ns/${encodeURIComponent(String(args.id4n))}/collections${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(paginatedResponseOfGuidCollection, await res.json()) };