import {z} from 'zod' export type GetApiApiV1OrganizationsOrganizationIdCollectionsArgs = {offset?: number | undefined, limit?: number | undefined, organizationId: string, type?: ('ROUTING_COLLECTION' | 'LOGISTIC_COLLECTION' | 'LABELLED_COLLECTION') | undefined, label?: string | undefined, labelPrefix?: string | undefined, property?: Array | undefined}; export type GetApiApiV1OrganizationsOrganizationIdCollectionsOptions = {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 }; /** * Get collections of organization * * GET /api/v1/organizations/{organizationId}/collections */ export const getApiApiV1OrganizationsOrganizationIdCollections = async (args: GetApiApiV1OrganizationsOrganizationIdCollectionsArgs, options: GetApiApiV1OrganizationsOrganizationIdCollectionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.type != null) query.append("type", String(args.type)) if (args.label != null) query.append("label", String(args.label)) if (args.labelPrefix != null) query.append("labelPrefix", String(args.labelPrefix)) for (const item of args.property ?? []) query.append("property", String(item)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/organizations/${encodeURIComponent(String(args.organizationId))}/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()) };