///
import {
AddEventSourceInput,
ApiPayload,
EventSourcePayload,
SdkError,
} from '@atlassian/forge-graphql-types';
import { CompassRequests } from '../../compass-requests';
declare module '../../compass-requests' {
interface CompassRequests {
/**
* Creates a new event source and attaches that event source to a
* specified component. **Note: This method is unstable.**
*
* **Required Oauth Scopes:** `write:component:compass`, `write:event:compass`
*/
addEventSource(
input: AddEventSourceInput,
): Promise>;
}
}
CompassRequests.prototype.addEventSource = async function ({
componentId,
eventSource,
}) {
let errorsAcc = [] as Array;
let eventSourceId;
const { errors: createErrors, data } = await this.createEventSource(
eventSource,
);
if (createErrors.length === 0) {
try {
const {
eventSource: { id },
} = data;
const { errors: attachErrors } = await this.attachEventSource({
eventSourceId: id,
componentId,
});
eventSourceId = id;
errorsAcc = errorsAcc.concat(attachErrors);
} catch {
console.error('Error: Could not find id in createEventSource response');
}
} else {
errorsAcc = errorsAcc.concat(createErrors);
}
return {
errors: errorsAcc,
success: errorsAcc.length === 0,
data: {
eventSource:
errorsAcc.length > 0
? {}
: {
eventType: eventSource.eventType,
externalEventSourceId: eventSource.externalEventSourceId,
id: eventSourceId,
},
},
} as ApiPayload;
};