import {z} from 'zod' export type GetApiProductsIdArgs = {id: number}; export type GetApiProductsIdOptions = {baseUrl?: string | undefined, accessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const productCountryMappingItemDto = z.object({id: z.number(), productId: z.number(), countryId: z.number(), countryName: z.string(), currencyCode: z.string(), currencySymbol: z.string(), stateId: z.number().nullable().optional(), stateName: z.string().nullable().optional(), price: z.number().nullable().optional()}); export const productResponseDto = z.object({id: z.number(), productName: z.string(), productNameForFrench: z.string().nullable().optional(), productNameForSpanish: z.string().nullable().optional(), productDescription: z.string().optional(), productDescriptionForFrench: z.string().nullable().optional(), productDescriptionForSpanish: z.string().nullable().optional(), productDescriptionForGerman: z.string().nullable().optional(), upSellDescription: z.string().nullable().optional(), upSellDescriptionForFrench: z.string().nullable().optional(), upSellDescriptionForSpanish: z.string().nullable().optional(), upSellDescriptionForGerman: z.string().nullable().optional(), productSortOrder: z.number(), isPhysical: z.boolean(), isFastTrack: z.boolean(), isPaperCopy: z.boolean(), productCountryPrices: z.array(productCountryMappingItemDto)}); 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 product by ID * * GET /products/{id} */ export const getApiProductsId = async (args: GetApiProductsIdArgs, options?: GetApiProductsIdOptions) => { const {baseUrl = "https://api.findmydocument.net", fetch: fetchFn = fetch, accessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (accessToken !== undefined) headers.set('authorization', `Bearer ${accessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/products/${encodeURIComponent(String(args.id))}${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(productResponseDto, await res.json()) };