Add interceptor to inject correlation id in http requests.

This commit is contained in:
Florian THIERRY
2024-09-26 10:38:52 +02:00
parent 5647a5a959
commit dee473fe46
5 changed files with 77 additions and 4 deletions

View File

@@ -20,6 +20,7 @@
"@angular/router": "^18.2.5", "@angular/router": "^18.2.5",
"rxjs": "~7.8.0", "rxjs": "~7.8.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"uuid": "^10.0.0",
"zone.js": "~0.14.10" "zone.js": "~0.14.10"
}, },
"devDependencies": { "devDependencies": {
@@ -28,6 +29,7 @@
"@angular/compiler-cli": "^18.2.5", "@angular/compiler-cli": "^18.2.5",
"@angular/localize": "^18.2.5", "@angular/localize": "^18.2.5",
"@types/jasmine": "~5.1.0", "@types/jasmine": "~5.1.0",
"@types/uuid": "^10.0.0",
"jasmine-core": "~5.1.0", "jasmine-core": "~5.1.0",
"karma": "~6.4.0", "karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0", "karma-chrome-launcher": "~3.2.0",
@@ -4655,6 +4657,13 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/@types/uuid": {
"version": "10.0.0",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz",
"integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/wrap-ansi": { "node_modules/@types/wrap-ansi": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/wrap-ansi/-/wrap-ansi-3.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/wrap-ansi/-/wrap-ansi-3.0.0.tgz",
@@ -12427,6 +12436,16 @@
"websocket-driver": "^0.7.4" "websocket-driver": "^0.7.4"
} }
}, },
"node_modules/sockjs/node_modules/uuid": {
"version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==",
"dev": true,
"license": "MIT",
"bin": {
"uuid": "dist/bin/uuid"
}
},
"node_modules/socks": { "node_modules/socks": {
"version": "2.8.3", "version": "2.8.3",
"resolved": "https://registry.npmjs.org/socks/-/socks-2.8.3.tgz", "resolved": "https://registry.npmjs.org/socks/-/socks-2.8.3.tgz",
@@ -13315,10 +13334,13 @@
} }
}, },
"node_modules/uuid": { "node_modules/uuid": {
"version": "8.3.2", "version": "10.0.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-10.0.0.tgz",
"integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", "integrity": "sha512-8XkAphELsDnEGrDxUOHB3RGvXz6TeuYSGEZBOjtTtPm2lwhGBjLgOzLHB63IUWfBpNucQjND6d3AOudO+H3RWQ==",
"dev": true, "funding": [
"https://github.com/sponsors/broofa",
"https://github.com/sponsors/ctavan"
],
"license": "MIT", "license": "MIT",
"bin": { "bin": {
"uuid": "dist/bin/uuid" "uuid": "dist/bin/uuid"

View File

@@ -29,6 +29,7 @@
"@angular/router": "^18.2.5", "@angular/router": "^18.2.5",
"rxjs": "~7.8.0", "rxjs": "~7.8.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"uuid": "^10.0.0",
"zone.js": "~0.14.10" "zone.js": "~0.14.10"
}, },
"devDependencies": { "devDependencies": {
@@ -37,6 +38,7 @@
"@angular/compiler-cli": "^18.2.5", "@angular/compiler-cli": "^18.2.5",
"@angular/localize": "^18.2.5", "@angular/localize": "^18.2.5",
"@types/jasmine": "~5.1.0", "@types/jasmine": "~5.1.0",
"@types/uuid": "^10.0.0",
"jasmine-core": "~5.1.0", "jasmine-core": "~5.1.0",
"karma": "~6.4.0", "karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0", "karma-chrome-launcher": "~3.2.0",

View File

@@ -5,6 +5,7 @@ import { HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi } from '@a
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async'; import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
import { routes } from './app.routes'; import { routes } from './app.routes';
import { JwtInterceptor } from './core/interceptor/jwt.interceptor'; import { JwtInterceptor } from './core/interceptor/jwt.interceptor';
import { CorrelationIdInterceptor } from './core/interceptor/correlation-id.interceptor';
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
@@ -18,5 +19,6 @@ export const appConfig: ApplicationConfig = {
provideAnimationsAsync(), provideAnimationsAsync(),
provideHttpClient(withInterceptorsFromDi()), provideHttpClient(withInterceptorsFromDi()),
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true }, { provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: CorrelationIdInterceptor, multi: true },
] ]
}; };

View File

@@ -0,0 +1,20 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { inject, Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { CorrelationIdService } from "../service/correlation-id.service";
@Injectable()
export class CorrelationIdInterceptor implements HttpInterceptor {
private readonly correlationIdService = inject(CorrelationIdService);
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const correlationId = this.correlationIdService.getCorrelationId();
const requestWithCorrelationId = request.clone({
headers: request.headers.set('x-correlation-id', correlationId)
});
return next.handle(requestWithCorrelationId);
}
}

View File

@@ -0,0 +1,27 @@
import { Injectable } from "@angular/core";
import * as uuid from 'uuid';
const CORRELATION_ID_KEY = 'correlationId';
@Injectable({
providedIn: 'root'
})
export class CorrelationIdService {
getCorrelationId(): string {
let correlationId = this.getCorrelationFromLocalStorage();
if (correlationId === undefined) {
correlationId = this.createNewCorrelationId();
}
return correlationId;
}
private getCorrelationFromLocalStorage(): string | undefined {
return localStorage.getItem(CORRELATION_ID_KEY) ?? undefined;
}
private createNewCorrelationId(): string {
const newCorrelationId = uuid.v4();
localStorage.setItem(CORRELATION_ID_KEY, newCorrelationId);
return newCorrelationId;
}
}