import {z} from 'zod' export type GetApiUsersIdOauthDelegationsArgs = {Accept: string, 'Content-Type': 'application/json', id: string, delegation_type?: ('mobile' | 'web' | 'integration') | undefined, status?: ('issued' | 'revoked') | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiUsersIdOauthDelegationsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const oAuthDelegation = z.object({id: z.string(), status: z.enum(["issued", "revoked"]), client_id: z.string(), delegation_type: z.enum(["web", "mobile", "integration"]), scope: z.string(), created_at: z.string(), expires_at: z.string(), self: z.string()}); export const userOAuthDelegations = z.object({oauth_delegations: z.array(oAuthDelegation), limit: z.number().int(), more: z.boolean(), next_cursor: 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 }; /** * List a user's delegations * * GET /users/{id}/oauth_delegations */ export const getApiUsersIdOauthDelegations = async (args: GetApiUsersIdOauthDelegationsArgs, options?: GetApiUsersIdOauthDelegationsOptions) => { const {baseUrl = "https://api.pagerduty.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (args.delegation_type != null) query.append("delegation_type", String(args.delegation_type)) if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.id))}/oauth_delegations${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(userOAuthDelegations, await res.json()) };