import {z} from 'zod' export type GetApiV1MetersArgs = {limit?: number | undefined, starting_after?: string | undefined, ending_before?: string | undefined, include_archived?: boolean | undefined}; export type GetApiV1MetersOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const filterClauseApiResponseDto = z.object({property: z.string(), operator: z.string(), value: z.union([z.string(), z.number(), z.boolean()])}); export const meterFilterApiResponseDto = z.object({conjunction: z.enum(["and", "or"]), clauses: z.array(filterClauseApiResponseDto)}); export const meterApiResponseDto = z.object({id: z.string(), store_id: z.string(), name: z.string(), event_name: z.string(), aggregation: z.enum(["count", "sum", "average", "min", "max", "unique"]), aggregation_property: z.string().nullable(), filter: meterFilterApiResponseDto, unit_label: z.string(), status: z.enum(["active", "archived"]), created_at: z.string(), updated_at: z.string()}); export const meterListApiResponseDto = z.object({object: z.string(), data: z.array(meterApiResponseDto), has_more: z.boolean()}); 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 }; /** * List meters * * GET /v1/meters */ export const getApiV1Meters = async (args: GetApiV1MetersArgs, options?: GetApiV1MetersOptions) => { const {baseUrl = "https://api.creem.io", fetch: fetchFn = fetch, ApiKey} = 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.starting_after != null) query.append("starting_after", String(args.starting_after)) if (args.ending_before != null) query.append("ending_before", String(args.ending_before)) if (args.include_archived != null) query.append("include_archived", String(args.include_archived)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/meters${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(meterListApiResponseDto, await res.json()) };