From 4a9f0ba8ae0fe77c999f7ed2fb8f0fa09cec6508 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Mon, 23 Jan 2023 17:54:11 +0300 Subject: [PATCH 1/6] Add HttpContext variable and skip it http interceptor logic when it is true --- .../core/src/lib/clients/http.client.ts | 30 +++++++++++++++++++ .../packages/core/src/lib/clients/index.ts | 1 + .../src/lib/interceptors/api.interceptor.ts | 4 +++ .../core/src/lib/tokens/http-context.token.ts | 3 ++ npm/ng-packs/packages/core/src/public-api.ts | 1 + 5 files changed, 39 insertions(+) create mode 100644 npm/ng-packs/packages/core/src/lib/clients/http.client.ts create mode 100644 npm/ng-packs/packages/core/src/lib/clients/index.ts create mode 100644 npm/ng-packs/packages/core/src/lib/tokens/http-context.token.ts diff --git a/npm/ng-packs/packages/core/src/lib/clients/http.client.ts b/npm/ng-packs/packages/core/src/lib/clients/http.client.ts new file mode 100644 index 0000000000..c6e9017be7 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/clients/http.client.ts @@ -0,0 +1,30 @@ +import { HttpClient, HttpContext, HttpRequest } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { Observable } from 'rxjs'; +import { IS_EXTERNAL_REQUEST } from '../tokens/http-context.token'; + +// source : https://github.com/armanozak/demo-angular-server-specific-interceptors +@Injectable({ + providedIn: 'root', +}) +export class ExternalHttpClient extends HttpClient { + override request( + first: string | HttpRequest, + url?: string, + options: RequestOptions = {}, + ): Observable { + if (typeof first === 'string') { + this.#setPlaceholderContext(options); + return super.request(first, url, options); + } + + this.#setPlaceholderContext(first); + return super.request(first); + } + #setPlaceholderContext(optionsOrRequest: { context?: HttpContext }) { + optionsOrRequest.context ??= new HttpContext(); + optionsOrRequest.context.set(IS_EXTERNAL_REQUEST, true); + } +} + +type RequestOptions = Parameters[2]; diff --git a/npm/ng-packs/packages/core/src/lib/clients/index.ts b/npm/ng-packs/packages/core/src/lib/clients/index.ts new file mode 100644 index 0000000000..c9aba4d030 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/clients/index.ts @@ -0,0 +1 @@ +export * from './http.client'; diff --git a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts index d3dbe19ec0..aef5b9e706 100644 --- a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts +++ b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts @@ -5,6 +5,7 @@ import { finalize } from 'rxjs/operators'; import { SessionStateService } from '../services/session-state.service'; import { HttpWaitService } from '../services/http-wait.service'; import { TENANT_KEY } from '../tokens/tenant-key.token'; +import { IS_EXTERNAL_REQUEST } from '../tokens/http-context.token'; @Injectable({ providedIn: 'root', @@ -18,6 +19,9 @@ export class ApiInterceptor implements HttpInterceptor { ) {} intercept(request: HttpRequest, next: HttpHandler) { + if (request.context?.get(IS_EXTERNAL_REQUEST)) { + return next.handle(request); + } this.httpWaitService.addRequest(request); return next .handle( diff --git a/npm/ng-packs/packages/core/src/lib/tokens/http-context.token.ts b/npm/ng-packs/packages/core/src/lib/tokens/http-context.token.ts new file mode 100644 index 0000000000..e935232b08 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tokens/http-context.token.ts @@ -0,0 +1,3 @@ +import { HttpContextToken } from '@angular/common/http'; + +export const IS_EXTERNAL_REQUEST = new HttpContextToken(() => false); diff --git a/npm/ng-packs/packages/core/src/public-api.ts b/npm/ng-packs/packages/core/src/public-api.ts index a5cbd78e4e..e740b4c4df 100644 --- a/npm/ng-packs/packages/core/src/public-api.ts +++ b/npm/ng-packs/packages/core/src/public-api.ts @@ -23,3 +23,4 @@ export * from './lib/strategies'; export * from './lib/tokens'; export * from './lib/utils'; export * from './lib/validators'; +export * from './lib/clients'; From 395369f968a329e0a49e3b0b69ed2ee59d3d4c31 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Mon, 23 Jan 2023 19:06:27 +0300 Subject: [PATCH 2/6] HttpWait service always must call. with or without `IS_EXTERNAL_REQUEST`. --- .../core/src/lib/interceptors/api.interceptor.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts index aef5b9e706..e09bb6292e 100644 --- a/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts +++ b/npm/ng-packs/packages/core/src/lib/interceptors/api.interceptor.ts @@ -19,16 +19,15 @@ export class ApiInterceptor implements HttpInterceptor { ) {} intercept(request: HttpRequest, next: HttpHandler) { - if (request.context?.get(IS_EXTERNAL_REQUEST)) { - return next.handle(request); - } this.httpWaitService.addRequest(request); - return next - .handle( - request.clone({ + const isExternalRequest = request.context?.get(IS_EXTERNAL_REQUEST); + const newRequest = isExternalRequest + ? request + : request.clone({ setHeaders: this.getAdditionalHeaders(request.headers), - }), - ) + }); + return next + .handle(newRequest) .pipe(finalize(() => this.httpWaitService.deleteRequest(request))); } From d361802de2b4512ae80ca8168aab23a6ba86d665 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Mon, 23 Jan 2023 19:08:05 +0300 Subject: [PATCH 3/6] Add documentation --- .../How-To-Skip-ABP-Headers-And-Interceptors.md | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md diff --git a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md new file mode 100644 index 0000000000..b52f72b27c --- /dev/null +++ b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md @@ -0,0 +1,10 @@ +# How to Skip HTTP interceptors and ABP headers + +The ABP Framework adds several HTTP headers to the HttpClient, such as the "Auth token" or "tenant Id." +ABP Server must possess the information but the ABP user may not want to send this informations to a external server. +ExternalHttpClient and IS EXTERNAL REQUEST HttpContext Token were added in V6.0.4. +THe ABP Http interceptors check value of `` token. Adding headers is skipped by the interceptor if it is present and its value is true. +The `ExternalHttpClient` extends from `HTTPClient` and set `IS_EXTERNAL_REQUEST` Context token to true. +When you are using `ExternalHttpClient` as HttpClient in your components, it does not add ABP-specific headers. + +Note: With `IS_EXTERNAL_REQUEST` or without, ABP loading service work. From 8c2f37bd3da4fb3f07f995d22e2dba4caf83b0a3 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Tue, 24 Jan 2023 09:54:47 +0300 Subject: [PATCH 4/6] Update doc --- docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md index b52f72b27c..2f3d51643b 100644 --- a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md +++ b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md @@ -3,7 +3,7 @@ The ABP Framework adds several HTTP headers to the HttpClient, such as the "Auth token" or "tenant Id." ABP Server must possess the information but the ABP user may not want to send this informations to a external server. ExternalHttpClient and IS EXTERNAL REQUEST HttpContext Token were added in V6.0.4. -THe ABP Http interceptors check value of `` token. Adding headers is skipped by the interceptor if it is present and its value is true. +THe ABP Http interceptors check value of `IS_EXTERNAL_REQUEST` token. When the token is True, ABP-specific headers don't add to Http Request. The `ExternalHttpClient` extends from `HTTPClient` and set `IS_EXTERNAL_REQUEST` Context token to true. When you are using `ExternalHttpClient` as HttpClient in your components, it does not add ABP-specific headers. From 857626a52324bb2568da64bb227f40568f4a058f Mon Sep 17 00:00:00 2001 From: Hamza Albreem <94292623+braim23@users.noreply.github.com> Date: Tue, 24 Jan 2023 10:00:48 +0300 Subject: [PATCH 5/6] quick fix for the How-To-Skip-ABP-Headers-And-Interceptors doc --- .../How-To-Skip-ABP-Headers-And-Interceptors.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md index 2f3d51643b..4c8a09f136 100644 --- a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md +++ b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md @@ -1,10 +1,10 @@ # How to Skip HTTP interceptors and ABP headers -The ABP Framework adds several HTTP headers to the HttpClient, such as the "Auth token" or "tenant Id." -ABP Server must possess the information but the ABP user may not want to send this informations to a external server. +The ABP Framework adds several HTTP headers to the HttpClient, such as the "Auth token" or "tenant Id". +The ABP Server must possess the information but the ABP user may not want to send this informations to an external server. ExternalHttpClient and IS EXTERNAL REQUEST HttpContext Token were added in V6.0.4. -THe ABP Http interceptors check value of `IS_EXTERNAL_REQUEST` token. When the token is True, ABP-specific headers don't add to Http Request. -The `ExternalHttpClient` extends from `HTTPClient` and set `IS_EXTERNAL_REQUEST` Context token to true. +The ABP Http interceptors check the value of the `IS_EXTERNAL_REQUEST` token. If the token is True then ABP-specific headers won't be added to Http Request. +The `ExternalHttpClient` extends from `HTTPClient` and sets the `IS_EXTERNAL_REQUEST` context token to true. When you are using `ExternalHttpClient` as HttpClient in your components, it does not add ABP-specific headers. -Note: With `IS_EXTERNAL_REQUEST` or without, ABP loading service work. +Note: With `IS_EXTERNAL_REQUEST` or without it, ABP loading service works. From 4a0704566a528460f1b89d068e7f92fcbabdf820 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Tue, 24 Jan 2023 10:51:16 +0300 Subject: [PATCH 6/6] add token index.ts, move doc HTTP Requests file --- docs/en/UI/Angular/HTTP-Requests.md | 12 ++++++++++++ .../How-To-Skip-ABP-Headers-And-Interceptors.md | 10 ---------- npm/ng-packs/packages/core/src/lib/tokens/index.ts | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) delete mode 100644 docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md diff --git a/docs/en/UI/Angular/HTTP-Requests.md b/docs/en/UI/Angular/HTTP-Requests.md index 57a058ef36..a0e53df9c9 100644 --- a/docs/en/UI/Angular/HTTP-Requests.md +++ b/docs/en/UI/Angular/HTTP-Requests.md @@ -298,3 +298,15 @@ export function handleHttpErrors(injector: Injector, httpError: HttpErrorRespons return throwError(httpError) } ``` + + +### How to Skip HTTP interceptors and ABP headers + +The ABP Framework adds several HTTP headers to the HttpClient, such as the "Auth token" or "tenant Id". +The ABP Server must possess the information but the ABP user may not want to send this informations to an external server. +ExternalHttpClient and IS EXTERNAL REQUEST HttpContext Token were added in V6.0.4. +The ABP Http interceptors check the value of the `IS_EXTERNAL_REQUEST` token. If the token is True then ABP-specific headers won't be added to Http Request. +The `ExternalHttpClient` extends from `HTTPClient` and sets the `IS_EXTERNAL_REQUEST` context token to true. +When you are using `ExternalHttpClient` as HttpClient in your components, it does not add ABP-specific headers. + +Note: With `IS_EXTERNAL_REQUEST` or without it, ABP loading service works. diff --git a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md b/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md deleted file mode 100644 index 4c8a09f136..0000000000 --- a/docs/en/UI/Angular/How-To-Skip-ABP-Headers-And-Interceptors.md +++ /dev/null @@ -1,10 +0,0 @@ -# How to Skip HTTP interceptors and ABP headers - -The ABP Framework adds several HTTP headers to the HttpClient, such as the "Auth token" or "tenant Id". -The ABP Server must possess the information but the ABP user may not want to send this informations to an external server. -ExternalHttpClient and IS EXTERNAL REQUEST HttpContext Token were added in V6.0.4. -The ABP Http interceptors check the value of the `IS_EXTERNAL_REQUEST` token. If the token is True then ABP-specific headers won't be added to Http Request. -The `ExternalHttpClient` extends from `HTTPClient` and sets the `IS_EXTERNAL_REQUEST` context token to true. -When you are using `ExternalHttpClient` as HttpClient in your components, it does not add ABP-specific headers. - -Note: With `IS_EXTERNAL_REQUEST` or without it, ABP loading service works. diff --git a/npm/ng-packs/packages/core/src/lib/tokens/index.ts b/npm/ng-packs/packages/core/src/lib/tokens/index.ts index 98985107f3..e0116e007a 100644 --- a/npm/ng-packs/packages/core/src/lib/tokens/index.ts +++ b/npm/ng-packs/packages/core/src/lib/tokens/index.ts @@ -7,3 +7,4 @@ export * from './manage-profile.token'; export * from './options.token'; export * from './queue.token'; export * from './tenant-key.token'; +export * from './http-context.token';