import { ApiPayload, EventSourcePayload, GetEventSource, GetEventSourceInput, SdkError, } from '@atlassian/forge-graphql-types'; import { CompassRequests } from '../../compass-requests'; import { mapGqlErrors, mapQueryErrors, parsingResponseError, } from '../../helpers'; declare module '../../compass-requests' { interface CompassRequests { /** * Retrieves a single event source by its external event source ID and event type. * **Required Oauth Scopes:** `read:event:compass` */ getEventSource( input: GetEventSourceInput, ): Promise>; } } CompassRequests.prototype.getEventSource = async function ( this: CompassRequests, { cloudId, eventType, externalEventSourceId, options = {}, }: GetEventSourceInput, ) { let eventSource = null; let errorsResp: SdkError[] = []; const { includeEvents = false, eventsQuery = {} } = options; try { const resp = await this.api.requestGraph( GetEventSource, { cloudId, eventType, externalEventSourceId, includeEvents, eventsQuery }, 'getEventSource', ); const { data, errors: gqlErrors } = await resp.json(); errorsResp = mapGqlErrors(gqlErrors); errorsResp = errorsResp.concat( mapQueryErrors(data.compass, ['eventSource', 'eventSource.events']), ); if (errorsResp.length === 0) { eventSource = data.compass.eventSource; } } catch (e) { if (errorsResp.length === 0) { errorsResp.push(parsingResponseError(e)); } } return { errors: errorsResp, success: errorsResp.length === 0, data: { eventSource }, }; };