import {z} from 'zod' export type Events = 'group.created' | 'group.deleted' | 'project.created' | 'project.deleted'; export type EnterpriseOrganizationWebhookCreateForm = {name: string, url: string, events: Array, requestType: 'POST' | 'GET', isActive?: boolean | undefined, batchingEnabled?: boolean | undefined, contentType?: ('multipart/form-data' | 'application/json' | 'application/x-www-form-urlencoded') | undefined, headers?: Record | undefined, payload?: ({'project.created'?: Record | undefined}) | undefined}; export type CreateApiWebhooksArgs = {body: EnterpriseOrganizationWebhookCreateForm}; export type CreateApiWebhooksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const enterpriseOrganizationWebhook = z.object({id: z.number().int().optional(), name: z.string().optional(), url: z.string().nullable().optional(), events: z.array(z.string()).optional(), headers: z.array(z.string()).optional(), payload: z.record(z.string(), z.array(z.string())).optional(), isActive: z.boolean().optional(), batchingEnabled: z.boolean().optional(), requestType: z.string().optional(), contentType: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const enterpriseOrganizationWebhookResource = z.object({data: enterpriseOrganizationWebhook}); 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 }; /** * Add Webhook * * POST /webhooks */ export const createApiWebhooks = async (args: CreateApiWebhooksArgs, options?: CreateApiWebhooksOptions) => { const {baseUrl = "https://api.crowdin.com/api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/webhooks${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(enterpriseOrganizationWebhookResource, await res.json()) };