import {z} from 'zod' export type GetApiWsSavArgs = {last_id?: number | undefined, state?: (0 | 9) | undefined, orderline?: number | undefined, last_modification_date?: string | undefined, last_message_date?: string | undefined, page?: number | undefined}; export type GetApiWsSavOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const careFolder = z.object({id: z.number().int().optional(), client: z.number().int().optional(), merchant: z.number().int().optional(), orderline: z.number().int().optional(), date_creation: z.string().optional(), date_modification: z.string().optional(), date_last_message: z.string().optional(), date_closed: z.string().optional(), state: z.number().int().optional(), lines: z.array(z.number().int()).optional(), country_code: z.string().optional(), backcare: z.boolean().optional()}); export const getCareFolderListResponse = z.object({count: z.number().int().optional(), next: z.string().optional(), previous: z.string().optional(), results: z.array(careFolder).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 a list of Care Folders attached to a Seller * * GET /ws/sav * * Also accepts auth: BasicAuth */ export const getApiWsSav = async (args: GetApiWsSavArgs, options?: GetApiWsSavOptions) => { const {baseUrl = "https://preprod.backmarket.co.jp", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.last_id != null) query.append("last_id", String(args.last_id)) if (args.state != null) query.append("state", String(args.state)) if (args.orderline != null) query.append("orderline", String(args.orderline)) if (args.last_modification_date != null) query.append("last_modification_date", String(args.last_modification_date)) if (args.last_message_date != null) query.append("last_message_date", String(args.last_message_date)) if (args.page != null) query.append("page", String(args.page)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/ws/sav${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(getCareFolderListResponse, await res.json()) };