import {z} from 'zod' export type PagerdutyRegion = 'us' | 'eu'; export type GetApiV1IntegrationsPagerdutyServicesPagerdutyServiceIdArgs = {pagerduty_service_id: string, account_subdomain: string, region?: PagerdutyRegion | undefined}; export type GetApiV1IntegrationsPagerdutyServicesPagerdutyServiceIdOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pagerDutyServiceView = z.object({install_id: z.string(), service_id: z.string(), pagerduty_service_id: z.string(), service_name: 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 }; /** * Resolve PagerDuty service * * GET /v1/integrations/pagerduty/services/{pagerduty_service_id}/ */ export const getApiV1IntegrationsPagerdutyServicesPagerdutyServiceId = async (args: GetApiV1IntegrationsPagerdutyServicesPagerdutyServiceIdArgs, options: GetApiV1IntegrationsPagerdutyServicesPagerdutyServiceIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account_subdomain != null) query.append("account_subdomain", String(args.account_subdomain)) if (args.region != null) query.append("region", String(args.region)) if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/integrations/pagerduty/services/${encodeURIComponent(String(args.pagerduty_service_id))}/${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(pagerDutyServiceView, await res.json()) };