import {z} from 'zod' export type ShippingAccountsSortBy = 'Name' | 'Alias' | 'LastModified' | 'CarrierAccountNumber'; export type OrderDirection = 'Ascending' | 'Descending'; export type GetApiV4ShippingLocationsShippingLocationIdShippingAccountsArgs = {shippingLocationId: string, searchTerm?: string | undefined, pageSize?: number | undefined, pageNumber?: number | undefined, sortBy?: ShippingAccountsSortBy | undefined, sortDir?: OrderDirection | undefined}; export type GetApiV4ShippingLocationsShippingLocationIdShippingAccountsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const accountType = z.enum(["Sandbox", "Production"]); export const shippingAccount = z.object({ShippingAccountId: z.string(), CarrierCode: z.string().min(1), AccountNumber: z.string().min(1), AccountType: accountType, AccountName: z.string().min(1).max(35), AccountAlias: z.string().min(1).max(30), AccountStatus: z.string().min(1), ContactName: z.string().min(1).max(100), ContactNumber: z.string().min(1).max(20), LastUpdatedBy: z.string().min(1), LastUpdatedDateUtc: z.string()}); export const shippingAccountsPagedResponse = z.object({ShippingAccounts: z.array(shippingAccount), TotalCount: z.number().int()}); 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 Associated Accounts * * GET /v4/shippingLocations/{shippingLocationId}/shippingAccounts */ export const getApiV4ShippingLocationsShippingLocationIdShippingAccounts = async (args: GetApiV4ShippingLocationsShippingLocationIdShippingAccountsArgs, options?: GetApiV4ShippingLocationsShippingLocationIdShippingAccountsOptions) => { const {baseUrl = "https://api.intersoftsapient.net", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.searchTerm != null) query.append("searchTerm", String(args.searchTerm)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageNumber != null) query.append("pageNumber", String(args.pageNumber)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/shippingLocations/${encodeURIComponent(String(args.shippingLocationId))}/shippingAccounts${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(shippingAccountsPagedResponse, await res.json()) };