import {z} from 'zod' export type PlanTier = {amount: number, amount_scheme?: ('flat' | 'per_unit') | undefined, quantity_from: number, quantity_to?: (number | null) | undefined, unit_scheme?: ('all_units' | 'tier_units' | 'overage' | null) | undefined}; export type Plan = {id?: string | undefined, product: string, sku?: (string | null) | undefined, name: string, description: string, frequency: string, currency: string, amount: number, exempt?: boolean | undefined, usage_scheme?: ('licensed' | 'metered') | undefined, usage_aggregation?: ('last_record' | 'max' | 'sum' | null) | undefined, tiers_mode?: ('package' | 'volume' | 'graduated' | 'package_overage' | 'unit' | 'range' | null) | undefined, tiers?: Array | undefined, unit_label?: (string | null) | undefined, status?: ('active' | 'archived') | undefined, archived?: (string | null) | undefined, created?: string | undefined, updated?: string | undefined}; export type Product = {id?: string | undefined, name: string, unit_label: string, status?: ('active' | 'archived') | undefined, archived?: (string | null) | undefined, plans?: Array | undefined, test_mode?: boolean | undefined, created?: string | undefined, updated?: string | undefined}; export type PatchApiProductsIdArgs = {body: Product}; export type PatchApiProductsIdOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const planTier = z.object({amount: z.number(), amount_scheme: z.enum(["flat", "per_unit"]).optional(), quantity_from: z.number().int(), quantity_to: z.number().int().nullable().optional(), unit_scheme: z.enum(["all_units", "tier_units", "overage"]).nullable().optional()}); export const plan = z.object({id: z.string().optional(), product: z.string(), sku: z.string().max(32).nullable().optional(), name: z.string().max(56), description: z.string().max(200), frequency: z.string(), currency: z.string(), amount: z.number(), exempt: z.boolean().optional(), usage_scheme: z.enum(["licensed", "metered"]).optional(), usage_aggregation: z.enum(["last_record", "max", "sum"]).nullable().optional(), tiers_mode: z.enum(["package", "volume", "graduated", "package_overage", "unit", "range"]).nullable().optional(), tiers: z.array(planTier).optional(), unit_label: z.string().max(4).nullable().optional(), status: z.enum(["active", "archived"]).optional(), archived: z.string().nullable().optional(), created: z.string().optional(), updated: z.string().optional()}); export const product = z.object({id: z.string().optional(), name: z.string().max(80), unit_label: z.string().max(12), status: z.enum(["active", "archived"]).optional(), archived: z.string().nullable().optional(), plans: z.array(plan).optional(), test_mode: z.boolean().optional(), created: z.string().optional(), updated: z.string().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 }; /** * Partial update product * * PATCH /products/{id}/ */ export const patchApiProductsId = async (args: PatchApiProductsIdArgs, options?: PatchApiProductsIdOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/products/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(product, await res.json()) };