import {z} from 'zod' export type GetApi202409CouriersLyocArgs = {page?: number | undefined, per_page?: number | undefined, country_alpha2?: string | undefined, country_id?: string | undefined}; export type GetApi202409CouriersLyocOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const lYOC = z.object({key: z.string().optional(), name: z.string().optional(), logo_url: z.string().optional(), supported_countries: z.array(z.object({id: z.number().optional(), alpha2: z.string().optional(), creation: z.object({contact: z.boolean().optional(), ui: z.boolean().optional(), api: z.boolean().optional()}).optional(), courier_id: z.string().nullable().optional()})).optional()}); export const pagination = z.object({next: z.number().int().nullable().optional(), count: z.number().int().nullable().optional(), page: z.number().int().optional()}); export const metaWithPagination = z.object({pagination: pagination.optional(), request_id: z.string().optional()}); export const lyocList = z.object({couriers: z.array(lYOC).optional(), meta: metaWithPagination.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 all LYOC * * GET /2024-09/couriers/lyoc */ export const getApi202409CouriersLyoc = async (args: GetApi202409CouriersLyocArgs, options?: GetApi202409CouriersLyocOptions) => { const {baseUrl = "https://public-api.easyship.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.country_alpha2 != null) query.append("country_alpha2", String(args.country_alpha2)) if (args.country_id != null) query.append("country_id", String(args.country_id)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/2024-09/couriers/lyoc${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(lyocList, await res.json()) };