Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Interceptor to enrich requests with api sandbox header for api … #226

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ We've provided the `AuthEventsHandlerService` via the `APP_INITIALIZER` which wi

We've also provided an example implementation of an `AuthInterceptor` in the app module. The purpose of this interceptor is to catch 401 errors and attempt to refresh the user's access token. If this refresh is successful the original request will be replayed with the new access token. If the refresh fails, or the original error was not a 401, then we surface the original error to the calling code.

### How to use API Sandbox endpoints

Since API Sandbox requires an individual API Key to allow requests to go through the services, you need to request a new API Key for yourself. You can do this by sending an email to `api-sandbox-support@backbase.com`.

When you receive your API Key, you can add it to your environment file. For example, in `environment.ts`:

```ts
export const environment: Environment = {
apiSandboxKey: 'YOUR_API_KEY'
}
```

## Generate an application

Run `ng g @nx/angular:app my-app` to generate an application.
Expand Down
6 changes: 6 additions & 0 deletions apps/golden-sample-app/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { LocaleSelectorModule } from './locale-selector/locale-selector.module';
import { TrackerModule } from '@backbase/foundation-ang/observability';
import { UserContextInterceptor } from './user-context/user-context.interceptor';
import { ActivityMonitorModule } from './auth/activity-monitor';
import { ApiSandboxInterceptor } from '../environments/api-sandbox-interceptor';
import packageInfo from 'package-json';
import { ThemeSwitcherModule } from './theme-switcher/theme-switcher.component.module';
import { ThemeManagerService } from './theme-switcher/theme-service';
Expand Down Expand Up @@ -169,6 +170,11 @@ import { ThemeManagerService } from './theme-switcher/theme-service';
accessControlBasePath: `${environment.apiRoot}/access-control`,
},
},
{
provide: HTTP_INTERCEPTORS,
useClass: ApiSandboxInterceptor,
multi: true,
},
{
provide: ErrorHandler,
useClass: AppErrorHandler,
Expand Down
23 changes: 23 additions & 0 deletions apps/golden-sample-app/src/environments/api-sandbox-interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { environment } from './environment';

@Injectable()
export class ApiSandboxInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const apiSandboxKey = environment.apiSandboxKey;

if (!req.url.includes(environment.apiRoot) || !apiSandboxKey) {
return next.handle(req);
}

const newReq = req.clone({
setHeaders: {
'X-SDBXAZ-API-KEY': apiSandboxKey,
}
});

return next.handle(newReq);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const environment: Environment = {
production: true,
apiRoot: '${API_ROOT}',
locales: '${LOCALES}'.split(','),
apiSandboxKey: '${X-SDBXAZ-API-KEY}',
common: {
designSlimMode: false,
},
Expand Down
1 change: 1 addition & 0 deletions apps/golden-sample-app/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const environment: Environment = {
apiRoot: 'https://app.stg.sdbxaz.azure.backbaseservices.com/api',
mockProviders,
locales: ['en-US', 'nl-NL'],
apiSandboxKey: 'sandboxApiKey',
common: {
designSlimMode: false,
},
Expand Down
1 change: 1 addition & 0 deletions apps/golden-sample-app/src/environments/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Environment {
mockProviders?: Provider[];
mockEnabled?: boolean;
common: sharedJourneyConfiguration;
apiSandboxKey?: string;
isTelemetryTracerEnabled?: boolean;
bbApiKey?: string;
telemetryCollectorURL?: string;
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ services:
AUTH_CLIENT_ID: bb-web-client
LOCALES: en,nl
FALLBACK_LOCALE: nl
X-SDBXAZ-API-KEY: sandboxApiKey
Loading