import {z} from 'zod' export type CreateApiV1CompanyWebhooksArgs = {url?: string | undefined, secret?: string | undefined, format?: string | undefined, actions?: Array | undefined, enabled?: boolean | undefined, 'X-Time-Zone'?: string | undefined, 'X-Metric-Units'?: boolean | undefined, 'X-User-Id'?: number | undefined}; export type CreateApiV1CompanyWebhooksOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1CompanyWebhooksResponse = z.object({company_webhook: z.object({id: z.number().int().optional(), url: z.string().optional(), secret: z.string().optional(), format: z.string().optional(), actions: z.array(z.string()).optional(), enabled: z.boolean().optional()}).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 a new company webhook * * POST /v1/company_webhooks */ export const createApiV1CompanyWebhooks = async (args: CreateApiV1CompanyWebhooksArgs, options?: CreateApiV1CompanyWebhooksOptions) => { const {baseUrl = "https://api.gomotive.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.url != null) query.append("url", String(args.url)) if (args.secret != null) query.append("secret", String(args.secret)) if (args.format != null) query.append("format", String(args.format)) for (const item of args.actions ?? []) query.append("actions", String(item)) if (args.enabled != null) query.append("enabled", String(args.enabled)) if (args['X-Time-Zone'] != null) headers.set("X-Time-Zone", String(args['X-Time-Zone'])) if (args['X-Metric-Units'] != null) headers.set("X-Metric-Units", String(args['X-Metric-Units'])) if (args['X-User-Id'] != null) headers.set("X-User-Id", String(args['X-User-Id'])) if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/v1/company_webhooks${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1CompanyWebhooksResponse, await res.json()) };