Skip to content

Commit a50b5eb

Browse files
committed
added interceptor controller
1 parent 30bce63 commit a50b5eb

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
2+
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
3+
import { OpenAPI } from '@umbraco-cms/backoffice/external/backend-api';
4+
import {
5+
extractUmbNotificationColor,
6+
isUmbNotifications,
7+
UMB_NOTIFICATION_CONTEXT,
8+
UMB_NOTIFICATION_HEADER,
9+
} from '@umbraco-cms/backoffice/notification';
10+
11+
export class UmbApiInterceptorController extends UmbControllerBase {
12+
constructor(host: UmbControllerHost) {
13+
super(host);
14+
this.#addApiInterceptor();
15+
}
16+
17+
#addApiInterceptor() {
18+
OpenAPI.interceptors.response.use((response) => {
19+
const umbNotifications = response.headers.get(UMB_NOTIFICATION_HEADER);
20+
if (!umbNotifications) return response;
21+
22+
const notifications = JSON.parse(umbNotifications);
23+
if (!isUmbNotifications(notifications)) return response;
24+
25+
this.getContext(UMB_NOTIFICATION_CONTEXT).then((notificationContext) => {
26+
for (const notification of notifications) {
27+
notificationContext.peek(extractUmbNotificationColor(notification.type), {
28+
data: { headline: notification.category, message: notification.message },
29+
});
30+
}
31+
});
32+
33+
const newHeader = new Headers();
34+
for (const header of response.headers.entries()) {
35+
const [key, value] = header;
36+
if (key !== UMB_NOTIFICATION_HEADER) newHeader.set(key, value);
37+
}
38+
39+
const newResponse = new Response(response.body, {
40+
headers: newHeader,
41+
status: response.status,
42+
statusText: response.statusText,
43+
});
44+
45+
return newResponse;
46+
});
47+
}
48+
}

src/apps/app/app.element.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { UmbAppErrorElement } from './app-error.element.js';
33
import { UmbAppContext } from './app.context.js';
44
import { UmbServerConnection } from './server-connection.js';
55
import { UmbAppAuthController } from './app-auth.controller.js';
6+
import { UmbApiInterceptorController } from './api-interceptor.controller.js';
67
import type { UMB_AUTH_CONTEXT } from '@umbraco-cms/backoffice/auth';
78
import { UmbAuthContext } from '@umbraco-cms/backoffice/auth';
89
import { css, html, customElement, property } from '@umbraco-cms/backoffice/external/lit';
@@ -19,12 +20,6 @@ import {
1920
} from '@umbraco-cms/backoffice/extension-registry';
2021
import { filter, first, firstValueFrom } from '@umbraco-cms/backoffice/external/rxjs';
2122
import { hasOwnOpener, retrieveStoredPath } from '@umbraco-cms/backoffice/utils';
22-
import {
23-
extractUmbNotificationColor,
24-
isUmbNotifications,
25-
UMB_NOTIFICATION_CONTEXT,
26-
UMB_NOTIFICATION_HEADER,
27-
} from '@umbraco-cms/backoffice/notification';
2823

2924
@customElement('umb-app')
3025
export class UmbAppElement extends UmbLitElement {
@@ -152,7 +147,8 @@ export class UmbAppElement extends UmbLitElement {
152147
super();
153148

154149
OpenAPI.BASE = window.location.origin;
155-
this.#attachApiInterceptor();
150+
151+
new UmbApiInterceptorController(this);
156152

157153
new UmbBundleExtensionInitializer(this, umbExtensionsRegistry);
158154

@@ -218,6 +214,7 @@ export class UmbAppElement extends UmbLitElement {
218214
}
219215
}
220216

217+
/*
221218
#attachApiInterceptor() {
222219
OpenAPI.interceptors.response.use((response) => {
223220
const umbNotifications = response.headers.get(UMB_NOTIFICATION_HEADER);
@@ -249,6 +246,7 @@ export class UmbAppElement extends UmbLitElement {
249246
return newResponse;
250247
});
251248
}
249+
*/
252250

253251
// TODO: move set initial auth state into auth context
254252
async #setAuthStatus() {

0 commit comments

Comments
 (0)