import {z} from 'zod' export type Coupon = {id?: string | undefined, name: string, amount: number, currency: string, discount_scheme?: ('%' | '$') | undefined, duration_scheme?: ('one_time' | 'forever' | 'limited') | undefined, duration_times?: (number | null) | undefined, valid_until?: (string | null) | undefined, apply_on?: ('line' | 'invoice') | undefined, status?: ('active' | 'archived') | undefined, archived?: (string | null) | undefined, created?: string | undefined, updated?: string | undefined}; export type CreateApiCouponsArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, body: Coupon}; export type CreateApiCouponsOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coupon = z.object({id: z.string().optional(), name: z.string().max(56), amount: z.number(), currency: z.string(), discount_scheme: z.enum(["%", "$"]).optional(), duration_scheme: z.enum(["one_time", "forever", "limited"]).optional(), duration_times: z.number().int().nullable().optional(), valid_until: z.string().nullable().optional(), apply_on: z.enum(["line", "invoice"]).optional(), status: z.enum(["active", "archived"]).optional(), archived: z.string().nullable().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 }; /** * Create coupon * * POST /coupons/ */ export const createApiCoupons = async (args: CreateApiCouponsArgs, options?: CreateApiCouponsOptions) => { 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 (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/coupons/${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(coupon, await res.json()) };