import {z} from 'zod' export type DimensionInputSerializer = {depth?: (number | null) | undefined, width?: (number | null) | undefined, height?: (number | null) | undefined}; export type IdentifierInputSerializer = {type: string, value: string}; export type WeightedItemInfoInputSerializer = {weightPerItem?: (number | null) | undefined, weightUnit?: (string | null) | undefined}; export type SizeSpecificationInputSerializer = {value?: (number | null) | undefined, description?: (string | null) | undefined}; export type PackSizeSpecificationInputSerializer = {value?: (string | null) | undefined, description?: (string | null) | undefined}; export type DetailsInputSerializer = {weightedItemInfo?: (WeightedItemInfoInputSerializer | null) | undefined, sizeSpecification?: (SizeSpecificationInputSerializer | null) | undefined, packSizeSpecification?: (PackSizeSpecificationInputSerializer | null) | undefined, skillsRequired?: (Array | null) | undefined, dropoffServiceTimeSeconds?: (number | null) | undefined}; export type ProviderConfigInputSerializer = {provider: string, serviceType: string, categories?: (Array | null) | undefined, imageUrls?: (Array | null) | undefined, enabled?: (boolean | null) | undefined}; export type ProductInputSerializer = {externalIdentifier?: (string | null) | undefined, sku?: (string | null) | undefined, name: string, description?: (string | null) | undefined, imageUrls?: (Array | null) | undefined, categories: Array, weight: number, dimensions: DimensionInputSerializer, identifiers: Array, attributes?: (Array | null) | undefined, details?: (DetailsInputSerializer | null) | undefined, providerConfigs?: (Array | null) | undefined}; export type ProductsInputSerializer = {products: Array}; export type CreateApiV1ProductsArgs = {body: ProductsInputSerializer}; export type CreateApiV1ProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dimensionOutputSerializer = z.object({depth: z.number().nullable().optional(), width: z.number().nullable().optional(), height: z.number().nullable().optional()}); export const identifierOutputSerializer = z.object({type: z.string(), value: z.string()}); export const weightedItemInfoOutputSerializer = z.object({weightPerItem: z.number().nullable().optional(), weightUnit: z.string().nullable().optional()}); export const sizeSpecificationOutputSerializer = z.object({value: z.number().nullable().optional(), description: z.string().nullable().optional()}); export const packSizeSpecificationOutputSerializer = z.object({value: z.string().nullable().optional(), description: z.string().nullable().optional()}); export const detailsOutputSerializer = z.object({weightedItemInfo: weightedItemInfoOutputSerializer.nullable().optional(), sizeSpecification: sizeSpecificationOutputSerializer.nullable().optional(), packSizeSpecification: packSizeSpecificationOutputSerializer.nullable().optional()}); export const providerConfigOutputSerializer = z.object({provider: z.string(), serviceType: z.string(), categories: z.array(z.string()).nullable().optional(), imageUrls: z.array(z.string()).nullable().optional(), enabled: z.boolean().nullable().optional()}); export const productOutputSerializer = z.object({id: z.string(), externalIdentifier: z.string().nullable().optional(), sku: z.string().nullable().optional(), name: z.string(), description: z.string().nullable().optional(), imageUrls: z.array(z.string()).nullable().optional(), categories: z.array(z.string()).nullable().optional(), weight: z.number().nullable().optional(), dimensions: dimensionOutputSerializer.nullable().optional(), identifiers: z.array(identifierOutputSerializer).nullable().optional(), attributes: z.array(z.string()).nullable().optional(), details: detailsOutputSerializer.nullable().optional(), providerConfigs: z.array(providerConfigOutputSerializer).nullable().optional()}); export const productsOutputSerializer = z.array(productOutputSerializer); 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 }; /** * Create or update products * * POST /v1/products */ export const createApiV1Products = async (args: CreateApiV1ProductsArgs, options?: CreateApiV1ProductsOptions) => { const {baseUrl = "https://api.sandbox.usenash.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/products${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(productsOutputSerializer, await res.json()) };