import {z} from 'zod' export type Authentication1 = {authType: 'NO_AUTH' | 'BASIC_AUTH' | 'API_KEY' | 'OAUTH2' | 'OAUTH2_BASIC', apiKey?: string | undefined, username?: string | undefined, password?: string | undefined, tokenUrl?: string | undefined, clientId?: string | undefined, clientSecret?: string | undefined, grantType?: string | undefined, contentType?: string | undefined}; export type Link = {rel?: string | undefined, href?: string | undefined, hreflang?: string | undefined, media?: string | undefined, title?: string | undefined, type?: string | undefined, deprecation?: string | undefined, profile?: string | undefined, name?: string | undefined}; export type WebhookRequest = {hookId?: string | undefined, uri: string, companyId?: string | undefined, authentication: Authentication1, domains: Array, links?: Array | undefined}; export type CreateApiManagementHooksArgs = {body: WebhookRequest}; export type CreateApiManagementHooksOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookResponse = z.object({hookId: z.string().optional(), companyId: z.string().optional(), uri: z.string().optional(), authType: z.string().optional(), createdDate: z.string().optional(), active: z.boolean().optional(), domains: z.array(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 }; /** * Add Webhook * * POST /management/hooks */ export const createApiManagementHooks = async (args: CreateApiManagementHooksArgs, options?: CreateApiManagementHooksOptions) => { const {baseUrl = "https://api.paychex.com", 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(/\/+$/, '')}/management/hooks${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(webhookResponse, await res.json()) };