import {z} from 'zod' export type ManagementProjectsCustomEventSchemasUpdateRequestBody = {schema?: ({properties: {[key: string]: {type: 'string' | 'number' | 'date' | 'datetime' | 'boolean', optional: boolean, deleted?: boolean | undefined}}}) | undefined}; export type UpdateApiManagementV1ProjectsProjectIdCustomEventSchemasCustomEventSchemaIdArgs = {body: ManagementProjectsCustomEventSchemasUpdateRequestBody}; export type UpdateApiManagementV1ProjectsProjectIdCustomEventSchemasCustomEventSchemaIdOptions = {baseUrl?: string | undefined, XManagementId?: string | undefined, XManagementToken?: string | undefined, fetch?: typeof fetch | undefined}; export const managementProjectsCustomEventSchemasUpdateResponseBody = 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 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 }; /** * Update Custom Event Schema * * PUT /management/v1/projects/{projectId}/custom-event-schemas/{customEventSchemaId} */ export const updateApiManagementV1ProjectsProjectIdCustomEventSchemasCustomEventSchemaId = async (args: UpdateApiManagementV1ProjectsProjectIdCustomEventSchemasCustomEventSchemaIdArgs, options?: UpdateApiManagementV1ProjectsProjectIdCustomEventSchemasCustomEventSchemaIdOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XManagementId, XManagementToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/${encodeURIComponent(String(args.customEventSchemaId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(managementProjectsCustomEventSchemasUpdateResponseBody, await res.json()) };