import {z} from 'zod' export type GetApiIntegrationsArgs = {page?: string | undefined, per_page?: string | undefined}; export type GetApiIntegrationsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const integrationsGetResponse = z.object({integration: z.object({id: z.string().optional(), service_name: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), template_id: z.unknown(), enabled: z.boolean().optional(), callback_token: z.string().optional(), updated_at: z.string().optional(), last_error_at: z.unknown(), last_webhook_request_at: z.unknown(), url: z.string().optional(), resource: z.string().optional(), jql_filter: z.unknown(), owner: z.object({type: z.string().optional(), id: z.string().optional(), url: z.string().optional(), resource: z.string().optional()}).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 }; /** * List integrations for an account * * GET /integrations * * Also accepts auth: ApiKeyAuth, CookieAuth */ export const getApiIntegrations = async (args: GetApiIntegrationsArgs, options?: GetApiIntegrationsOptions) => { const {baseUrl = "https://company.aha.io/api/v1", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=utf-8") if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/integrations${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(integrationsGetResponse, await res.json()) };