Skip to content

Commit

Permalink
Revert "Feature/cls-interceptor (#6)"
Browse files Browse the repository at this point in the history
This reverts commit a281248.
  • Loading branch information
Papooch authored Oct 24, 2021
1 parent a281248 commit a793880
Show file tree
Hide file tree
Showing 24 changed files with 150 additions and 603 deletions.
185 changes: 72 additions & 113 deletions README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"cls",
"continuation-local-storage",
"als",
"AsyncLocalStorage",
"async_hooks",
"request context"
],
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './lib/cls-service-manager';
export * from './lib/cls.constants';
export * from './lib/cls.middleware';
export * from './lib/cls.interceptor';
export * from './lib/cls.module';
export * from './lib/cls.service';
export * from './lib/cls.decorators';
1 change: 0 additions & 1 deletion src/lib/cls.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ export const CLS_ID = 'CLS_ID';
export const CLS_DEFAULT_NAMESPACE = 'CLS_DEFAULT_NAMESPACE';
export const CLS_MIDDLEWARE_OPTIONS = 'ClsMiddlewareOptions';
export const CLS_GUARD_OPTIONS = 'ClsGuardOptions';
export const CLS_INTERCEPTOR_OPTIONS = 'ClsInterceptorOptions';
44 changes: 0 additions & 44 deletions src/lib/cls.interceptor.ts

This file was deleted.

34 changes: 0 additions & 34 deletions src/lib/cls.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ export class ClsModuleOptions {
* Cls guard options
*/
guard?: ClsGuardOptions = null;

/**
* Cls interceptor options
*/
interceptor?: ClsInterceptorOptions = null;
}

export class ClsMiddlewareOptions {
Expand Down Expand Up @@ -108,32 +103,3 @@ export class ClsGuardOptions {

readonly namespaceName?: string;
}

export class ClsInterceptorOptions {
/**
* whether to mount the interceptor globally
*/
mount?: boolean; // default false

/**
* whether to automatically generate request ids
*/
generateId?: boolean; // default false

/**
* the function to generate request ids inside the interceptor
*/
idGenerator?: (context: ExecutionContext) => string | Promise<string> =
() => Math.random().toString(36).slice(-8);

/**
* Function that executes after the CLS context has been initialised.
* It can be used to put additional variables in the CLS context.
*/
setup?: (
cls: ClsService,
context: ExecutionContext,
) => void | Promise<void>;

readonly namespaceName?: string;
}
36 changes: 5 additions & 31 deletions src/lib/cls.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,12 @@ import {
NestModule,
Provider,
} from '@nestjs/common';
import {
APP_GUARD,
APP_INTERCEPTOR,
HttpAdapterHost,
ModuleRef,
} from '@nestjs/core';
import { ClsInterceptor } from '..';
import { APP_GUARD, HttpAdapterHost, ModuleRef } from '@nestjs/core';
import { ClsServiceManager, getClsServiceToken } from './cls-service-manager';
import {
CLS_GUARD_OPTIONS,
CLS_INTERCEPTOR_OPTIONS,
CLS_MIDDLEWARE_OPTIONS,
} from './cls.constants';
import { CLS_GUARD_OPTIONS, CLS_MIDDLEWARE_OPTIONS } from './cls.constants';
import { ClsGuard } from './cls.guard';
import {
ClsGuardOptions,
ClsInterceptorOptions,
ClsMiddlewareOptions,
ClsModuleOptions,
} from './cls.interfaces';
Expand Down Expand Up @@ -91,11 +80,6 @@ export class ClsModule implements NestModule {
...options.guard,
namespaceName: options.namespaceName,
};
const clsInterceptorOptions = {
...new ClsInterceptorOptions(),
...options.interceptor,
namespaceName: options.namespaceName,
};
const providers: Provider[] = [
...ClsServiceManager.getClsServicesAsProviders(),
{
Expand All @@ -106,28 +90,18 @@ export class ClsModule implements NestModule {
provide: CLS_GUARD_OPTIONS,
useValue: clsGuardOptions,
},
{
provide: CLS_INTERCEPTOR_OPTIONS,
useValue: clsInterceptorOptions,
},
];
const enhancerArr = [];
const guardArr = [];
if (clsGuardOptions.mount) {
enhancerArr.push({
guardArr.push({
provide: APP_GUARD,
useClass: ClsGuard,
});
}
if (clsInterceptorOptions.mount) {
enhancerArr.push({
provide: APP_INTERCEPTOR,
useClass: ClsInterceptor,
});
}

return {
module: ClsModule,
providers: providers.concat(...enhancerArr),
providers: providers.concat(...guardArr),
exports: providers,
global: options.global,
};
Expand Down
3 changes: 1 addition & 2 deletions src/lib/cls.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ClsServiceManager } from './cls-service-manager';
import { CLS_DEFAULT_NAMESPACE } from './cls.constants';
import { ClsServiceManager, CLS_DEFAULT_NAMESPACE } from '..';
import { ClsService } from './cls.service';

describe('ClsService', () => {
Expand Down
22 changes: 2 additions & 20 deletions src/lib/cls.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ClsService<S = Record<string, any>> {
}

/**
* Run the callback with a shared CLS context.
* Run the callback in a shared CLS context.
* @param callback function to run
* @returns whatever the callback returns
*/
Expand All @@ -59,30 +59,12 @@ export class ClsService<S = Record<string, any>> {
}

/**
* Run the callbacks with a shared CLS context.
* @param store the default context contents
* @param callback function to run
* @returns whatever the callback returns
*/
runWith<T = any>(store: any, callback: () => T) {
return this.namespace.run(store ?? {}, callback);
}

/**
* Run any following code with a shared CLS context.
* Run any following code in a shared CLS context.
*/
enter() {
return this.namespace.enterWith({});
}

/**
* Run any following code with a shared ClS context
* @param store the default context contents
*/
enterWith(store: any = {}) {
return this.namespace.enterWith(store);
}

/**
* Run the callback outside of a shared CLS context
* @param callback function to run
Expand Down
8 changes: 0 additions & 8 deletions test/common/test.exception.ts

This file was deleted.

4 changes: 2 additions & 2 deletions test/common/test.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class TestGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
if (this.cls.isActive()) this.cls.set('FROM_GUARD', this.cls.getId());
return true;
this.cls.set('FROM_GUARD', this.cls.getId());
return this.cls.isActive();
}
}
37 changes: 3 additions & 34 deletions test/gql/expect-ids-gql.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { INestApplication } from '@nestjs/common';
import request from 'supertest';

export const expectOkIdsGql = (
app: INestApplication,
options = { skipGuard: false },
) =>
export const expectIdsGql = (app: INestApplication) =>
request(app.getHttpServer())
.post('/graphql')
.send({
Expand All @@ -22,36 +19,8 @@ export const expectOkIdsGql = (
.expect(200)
.then((r) => {
const body = r.body.data?.items[0];
const id = body.id ?? 'no-id';
if (!options.skipGuard) expect(body.fromGuard).toEqual(id);
expect(body.fromInterceptor).toEqual(id);
expect(body.fromInterceptorAfter).toEqual(id);
expect(body.fromResolver).toEqual(id);
expect(body.fromService).toEqual(id);
});

export const expectErrorIdsGql = (
app: INestApplication,
options = { skipGuard: false },
) =>
request(app.getHttpServer())
.post('/graphql')
.send({
query: `query {
error {
id
fromGuard
fromInterceptor
fromInterceptorAfter
fromResolver
fromService
}
}`,
})
.then((r) => {
const body = r.body.errors?.[0].extensions.exception?.response;
const id = body.id ?? 'no-id';
if (!options.skipGuard) expect(body.fromGuard).toEqual(id);
const id = body.id;
expect(body.fromGuard).toEqual(id);
expect(body.fromInterceptor).toEqual(id);
expect(body.fromInterceptorAfter).toEqual(id);
expect(body.fromResolver).toEqual(id);
Expand Down
Loading

0 comments on commit a793880

Please sign in to comment.