npm package diff
Package: @forge/events
Versions: 1.0.3-next.0-experimental-f44d099 - 1.0.3-next.0-experimental-786d4ac
File: package/src/appEvents.ts
Index: package/src/appEvents.ts
===================================================================
--- package/src/appEvents.ts
+++ package/src/appEvents.ts
@@ -4,15 +4,36 @@
export interface AppEvent {
key: string;
}
-export type AppEventResult = AppEventSuccess | AppEventError;
+export type AppEventPublishResult = AppEventPublishSuccess | AppEventPublishError;
-export type AppEventSuccess = {
+/**
+ * Represents the successful result of the publishing operation.
+ *
+ * It may happen that publishing some of the events fails even though the request was generally successful.
+ * Inspect the `failedEvents` field to see which events failed to be published and why
+ * (if the list is empty then the operation was fully successful).
+ */
+export type AppEventPublishSuccess = {
type: 'success';
+ /** A list of events that failed to be published along with error messages. */
+ failedEvents: AppEventPublishFailure[];
};
-export interface AppEventError {
+/**
+ * A publishing failure of a specific event.
+ */
+export interface AppEventPublishFailure {
+ event: AppEvent;
+ errorMessage: string;
+}
+
+/**
+ * A general error of the event publishing operation.
+ * No events were published if this is returned.
+ */
+export interface AppEventPublishError {
type: 'error';
errorType: AppEventErrorType;
errorMessage: string;
}
@@ -30,8 +51,12 @@
errorMessages: string[];
errors: Map<string, string>;
}
+interface AppEventSuccessResponse {
+ failedEvents: AppEventPublishFailure[];
+}
+
const errorTypes: Record<number, AppEventErrorType> = {
400: 'VALIDATION_ERROR',
401: 'AUTHENTICATION_ERROR',
403: 'AUTHORIZATION_ERROR',
@@ -42,29 +67,30 @@
const endpoint = '/forge/events/v1/app-events';
export const appEvents = {
- async publish(events: AppEvent | AppEvent[]): Promise<AppEventResult> {
+ async publish(events: AppEvent | AppEvent[]): Promise<AppEventPublishResult> {
const eventsArray = Array.isArray(events) ? events : [events];
const body = {
events: eventsArray.map((e) => ({
eventKey: e.key
}))
};
const response = await post(endpoint, body, __requestAtlassianAsApp);
+ const responseBody = await response.json();
if (!response.ok) {
- const responseBody = await response.json();
return {
type: 'error',
errorType: errorTypes[response.status] ?? 'OTHER',
errorMessage: getErrorMessage(responseBody)
};
}
return {
- type: 'success'
+ type: 'success',
+ failedEvents: (responseBody as AppEventSuccessResponse).failedEvents ?? []
};
}
};