import {z} from 'zod' export type GetApiManagementV1ProjectsProjectIdCustomEventSchemasOptions = {baseUrl?: string | undefined, XManagementId?: string | undefined, XManagementToken?: string | undefined, fetch?: typeof fetch | undefined}; export const managementProjectsCustomEventSchema = z.object({id: z.string(), name: z.string(), schema: z.object({properties: z.record(z.string(), z.object({type: z.enum(["string", "number", "date", "datetime", "boolean"]), optional: z.boolean(), deleted: z.boolean().optional()}))}), created_at: z.string(), updated_at: z.string().optional(), object: z.literal("custom-event-schema")}); export const managementProjectsCustomEventSchemasListResponseBody = z.object({object: z.literal("list"), data_ref: z.literal("data"), data: z.array(managementProjectsCustomEventSchema), total: z.number().int()}); 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 Custom Event Schemas * * GET /management/v1/projects/{projectId}/custom-event-schemas */ export const getApiManagementV1ProjectsProjectIdCustomEventSchemas = async (options?: GetApiManagementV1ProjectsProjectIdCustomEventSchemasOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XManagementId, XManagementToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (XManagementId !== undefined) headers.set("X-Management-Id", XManagementId) if (XManagementToken !== undefined) headers.set("X-Management-Token", XManagementToken) const url = `${baseUrl.replace(/\/+$/, '')}/management/v1/projects/${encodeURIComponent(String(args.projectId))}/custom-event-schemas${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(managementProjectsCustomEventSchemasListResponseBody, await res.json()) };