npm package diff
Package: @forge/events
Versions: 2.0.1-next.0 - 2.0.1-next.1
File: package/src/appEvents.ts
Index: package/src/appEvents.ts
===================================================================
--- package/src/appEvents.ts
+++ package/src/appEvents.ts
@@ -1,107 +0,0 @@
-import { post } from './queries';
-import { __requestAtlassianAsApp } from '@forge/api';
-
-export interface AppEvent {
- key: string;
-}
-
-export type AppEventPublishResult = AppEventPublishSuccess | AppEventPublishError;
-
-/**
- * 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[];
-};
-
-/**
- * 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;
-}
-
-export type AppEventErrorType =
- | 'VALIDATION_ERROR'
- | 'AUTHENTICATION_ERROR'
- | 'AUTHORIZATION_ERROR'
- | 'RATE_LIMIT'
- | 'SERVICE_ERROR'
- | 'SERVICE_UNAVAILABLE'
- | `OTHER`;
-
-interface AppEventErrorResponse {
- errorMessages: string[];
- errors: Map<string, string>;
-}
-
-interface AppEventSuccessResponse {
- failedEvents: AppEventPublishFailure[];
-}
-
-const errorTypes: Record<number, AppEventErrorType> = {
- 400: 'VALIDATION_ERROR',
- 401: 'AUTHENTICATION_ERROR',
- 403: 'AUTHORIZATION_ERROR',
- 429: 'RATE_LIMIT',
- 500: 'SERVICE_ERROR',
- 503: 'SERVICE_UNAVAILABLE'
-};
-
-const endpoint = '/forge/events/v1/app-events';
-
-export const appEvents = {
- async publish(events: AppEvent | AppEvent[]): Promise<AppEventPublishResult> {
- const eventsArray = Array.isArray(events) ? events : [events];
- const body = {
- events: eventsArray.map((e) => ({
- key: e.key
- }))
- };
-
- const response = await post(endpoint, body, __requestAtlassianAsApp);
- const responseBody = await response.json();
-
- if (!response.ok) {
- return {
- type: 'error',
- errorType: errorTypes[response.status] ?? 'OTHER',
- errorMessage: getErrorMessage(responseBody)
- };
- }
-
- return {
- type: 'success',
- failedEvents: (responseBody as AppEventSuccessResponse).failedEvents ?? []
- };
- }
-};
-
-function getErrorMessage(responseBody: AppEventErrorResponse) {
- if (responseBody.errorMessages && responseBody.errorMessages.length > 0) {
- return responseBody.errorMessages.join(', ');
- } else if (responseBody.errors) {
- return Object.entries(responseBody.errors)
- .map(([key, value]) => `${key}: ${value}`)
- .join(', ');
- } else {
- return JSON.stringify(responseBody);
- }
-}