import {z} from 'zod' export type GetApiAdminMetafieldsCountJsonArgs = {limit?: number | undefined, since_id?: number | undefined, created_at_min?: string | undefined, created_at_max?: string | undefined, updated_at_min?: string | undefined, updated_at_max?: string | undefined, namespace?: string | undefined, key?: string | undefined, value_type?: string | undefined}; export type GetApiAdminMetafieldsCountJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const aPIResponseCountDto = z.object({count: z.number().int().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 count meta fields belong to an resource with options. * * GET /admin/metafields/count.json */ export const getApiAdminMetafieldsCountJson = async (args: GetApiAdminMetafieldsCountJsonArgs, options?: GetApiAdminMetafieldsCountJsonOptions) => { 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.created_at_min != null) query.append("created_at_min", String(args.created_at_min)) if (args.created_at_max != null) query.append("created_at_max", String(args.created_at_max)) if (args.updated_at_min != null) query.append("updated_at_min", String(args.updated_at_min)) if (args.updated_at_max != null) query.append("updated_at_max", String(args.updated_at_max)) if (args.namespace != null) query.append("namespace", String(args.namespace)) if (args.key != null) query.append("key", String(args.key)) if (args.value_type != null) query.append("value_type", String(args.value_type)) if (APPACCESSTOKEN !== undefined) headers.set("APP_ACCESS_TOKEN", APPACCESSTOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/admin/metafields/count.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(aPIResponseCountDto, await res.json()) };