Skip to content

Commit

Permalink
feat(di): allow controllers to be scoped to environment
Browse files Browse the repository at this point in the history
  • Loading branch information
abenerd committed Dec 12, 2023
1 parent 79b855d commit 1fb96fc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
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

0 comments on commit 1fb96fc

Please sign in to comment.