import {z} from 'zod' export type UpdateDomainRouteDto = {agentId?: string | undefined, type?: ('agent' | 'webhook') | undefined, data?: {[key: string]: string} | undefined}; export type PatchApiV1DomainsDomainRoutesAddressArgs = {domain: string, address: string, 'idempotency-key'?: string | undefined, body: UpdateDomainRouteDto}; export type PatchApiV1DomainsDomainRoutesAddressOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const domainRouteResponseDto = z.object({_id: z.string(), _domainId: z.string(), address: z.string(), agentId: z.string().optional(), type: z.enum(["agent", "webhook"]), _environmentId: z.string(), _organizationId: z.string(), createdAt: z.string(), updatedAt: z.string(), data: z.record(z.string(), 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 }; /** * Update a route * * PATCH /v1/domains/{domain}/routes/{address} */ export const patchApiV1DomainsDomainRoutesAddress = async (args: PatchApiV1DomainsDomainRoutesAddressArgs, options?: PatchApiV1DomainsDomainRoutesAddressOptions) => { const {baseUrl = "https://api.novu.co", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (secretKey !== undefined) headers.set("Authorization", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/${encodeURIComponent(String(args.domain))}/routes/${encodeURIComponent(String(args.address))}${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(domainRouteResponseDto, await res.json()) };