import {z} from 'zod' export type GetApiAdminRedirectsJsonArgs = {limit?: number | undefined, since_id?: number | undefined, path?: string | undefined, target?: string | undefined, fields?: string | undefined}; export type GetApiAdminRedirectsJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const redirect = z.object({from_url: z.string().optional(), id: z.number().int().optional(), to_url: z.string().optional()}); export const redirectsSwaggerResponse = z.object({redirects: z.array(redirect).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 }; /** * Retrieves a list of URL redirects * * GET /admin/redirects.json */ export const getApiAdminRedirectsJson = async (args: GetApiAdminRedirectsJsonArgs, options?: GetApiAdminRedirectsJsonOptions) => { const {baseUrl = "https://shop-name.onshopbase.com", fetch: fetchFn = fetch, APPACCESSTOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.since_id != null) query.append("since_id", String(args.since_id)) if (args.path != null) query.append("path", String(args.path)) if (args.target != null) query.append("target", String(args.target)) if (args.fields != null) query.append("fields", String(args.fields)) if (APPACCESSTOKEN !== undefined) headers.set("APP_ACCESS_TOKEN", APPACCESSTOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/admin/redirects.json${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(redirectsSwaggerResponse, await res.json()) };