import {z} from 'zod' export type UpdateApiV2CompanyCompanyIdWebhooksWebhookIdArgs = {'x-api-version'?: string | undefined, 'x-company-guid'?: string | undefined, company_id: number, webhook_id: number, body: {url: string}}; export type UpdateApiV2CompanyCompanyIdWebhooksWebhookIdOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV2CompanyCompanyIdWebhooksWebhookIdResponse = z.object({object: z.literal("webhook"), data: z.object({id: z.number().int(), company_id: z.number().int(), url: z.string(), method: z.enum(["post", "get", "put", "patch", "delete"]), event: z.string(), created: z.string(), modified: 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 }; /** * Update Webhook * * PUT /v2/company/{company_id}/webhooks/{webhook_id} * * Also accepts auth: cookieAuth */ export const updateApiV2CompanyCompanyIdWebhooksWebhookId = async (args: UpdateApiV2CompanyCompanyIdWebhooksWebhookIdArgs, options?: UpdateApiV2CompanyCompanyIdWebhooksWebhookIdOptions) => { const {baseUrl = "https://api.7shifts.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (args['x-company-guid'] != null) headers.set("x-company-guid", String(args['x-company-guid'])) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/company/${encodeURIComponent(String(args.company_id))}/webhooks/${encodeURIComponent(String(args.webhook_id))}${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(updateApiV2CompanyCompanyIdWebhooksWebhookIdResponse, await res.json()) };