import {z} from 'zod' export type CreateApiApiCdpSettingsV1AppAppIdWebhookImportArgs = {body: {id?: string | undefined, name: string, identifiers?: Record | undefined, attributes?: Record | undefined, status: 'active' | 'inactive', test_request_body?: string | undefined}}; export type CreateApiApiCdpSettingsV1AppAppIdWebhookImportOptions = {baseUrl?: string | undefined, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiCdpSettingsV1AppAppIdWebhookImportResponse = z.object({id: z.string(), author: z.object({email: z.string()}), name: z.string().min(1).max(255), identifiers: z.object({}).optional(), attributes: z.object({}).optional(), status: z.enum(["active", "inactive"]), test_request_body: z.string().optional(), 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 webhook import * * POST /api/cdp/settings/v1/app/{app_id}/webhook-import */ export const createApiApiCdpSettingsV1AppAppIdWebhookImport = async (args: CreateApiApiCdpSettingsV1AppAppIdWebhookImportArgs, options?: CreateApiApiCdpSettingsV1AppAppIdWebhookImportOptions) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.api+json") if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/cdp/settings/v1/app/${encodeURIComponent(String(args.app_id))}/webhook-import${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(createApiApiCdpSettingsV1AppAppIdWebhookImportResponse, await res.json()) };