import {z} from 'zod' export type FilterClauseApiRequestDto = {property: string, operator: 'equals' | 'not_equals' | 'gt' | 'gte' | 'lt' | 'lte' | 'contains' | 'not_contains', value: string | number | boolean}; export type MeterFilterApiRequestDto = {conjunction: 'and' | 'or', clauses: Array}; export type CreateMeterApiRequestDto = {name: string, event_name: string, aggregation: 'count' | 'sum' | 'average' | 'min' | 'max' | 'unique', aggregation_property?: string | undefined, filter?: MeterFilterApiRequestDto | undefined, unit_label: string}; export type CreateApiV1MetersArgs = {body: CreateMeterApiRequestDto}; export type CreateApiV1MetersOptions = {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 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 }; /** * Create a meter * * POST /v1/meters */ export const createApiV1Meters = async (args: CreateApiV1MetersArgs, options?: CreateApiV1MetersOptions) => { const {baseUrl = "https://api.creem.io", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(meterApiResponseDto, await res.json()) };