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

feat(di): allow controllers to be scoped to environment #2551

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions docs/docs/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ See the following example:

In order to avoid such side-effects, simply move `findAll()` method above `findOne()`.

### Environment scoped controllers

Sometimes you have controllers that you want to only serve locally for development purposes, for this you can specify the environments in which they should be run.
These environments are then checked against the `NODE_ENV`

<<< @/docs/snippets/controllers/development-only-controller.ts

## Request

### Decorators
Expand Down
13 changes: 13 additions & 0 deletions docs/docs/snippets/controllers/development-only-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Get} from "@tsed/schema";
import {Controller} from "@tsed/di";

@Controller({
path: "local",
environments: ["development"]
})
export class LocalCtrl {
@Get()
index(): string {
return [];
}
}
37 changes: 36 additions & 1 deletion packages/di/src/common/decorators/controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Test {}
class Dep {}

describe("@Controller", () => {
afterAll(() => {
afterEach(() => {
GlobalProviders.delete(Test);
});
it("should register a controller with his path and Dependency", () => {
Expand Down Expand Up @@ -40,4 +40,39 @@ describe("@Controller", () => {
expect(provider.path).toEqual("/test");
expect(provider.children).toEqual([Dep]);
});

describe("environments", () => {
it("should not register the controller on an invalid environment", () => {
Controller({
path: "/test",
environments: ["development"]
})(Test);

const provider = GlobalProviders.get(Test);

expect(provider).toBeUndefined();
});

it("should register the controller for the current environment", () => {
Controller({
path: "/test",
environments: [process.env.NODE_ENV]
})(Test);

const provider = GlobalProviders.get(Test);

expect(provider).not.toBeUndefined();
});

it("should register the controller when at least one environment matches", () => {
Controller({
path: "/test",
environments: ["development", process.env.NODE_ENV]
})(Test);

const provider = GlobalProviders.get(Test);

expect(provider).not.toBeUndefined();
});
});
});
9 changes: 8 additions & 1 deletion packages/di/src/common/decorators/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ControllerOptions extends Partial<ProviderOpts<any>> {
path?: PathType;
children?: Type<any>[];
middlewares?: Partial<ControllerMiddlewares>;
environments?: string[];
}

function mapOptions(options: any): ControllerOptions {
Expand Down Expand Up @@ -56,7 +57,13 @@ function mapOptions(options: any): ControllerOptions {
* @classDecorator
*/
export function Controller(options: PathType | ControllerOptions): ClassDecorator {
const {children = [], path, ...opts} = mapOptions(options);
const {children = [], path, environments = [process.env.NODE_ENV as string], ...opts} = mapOptions(options);

const controllerIsForCurrentEnvironment = environments.includes(process.env.NODE_ENV as string);

if (!controllerIsForCurrentEnvironment) {
return useDecorators();
}

return useDecorators(
(target: Type) => {
Expand Down
1 change: 1 addition & 0 deletions packages/third-parties/bullmq/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ jest.config.js
.idea
packages
.tsbuildinfo
*.tsbuildinfo
Loading