Skip to content

Commit

Permalink
feat(platform-router): add @tsed/platform-router package
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Ts.ED use a new abstracted router instead of Express.Router/KoaRouter.
  • Loading branch information
semantic-release-bot authored and Romakita committed Jul 25, 2022
1 parent 520778b commit 166dede
Show file tree
Hide file tree
Showing 146 changed files with 3,843 additions and 3,691 deletions.
5 changes: 0 additions & 5 deletions docs/docs/snippets/controllers/inject-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import {Controller} from "@tsed/di";
export class CalendarCtrl {
constructor(router: PlatformRouter) {
router.get("/", this.myMethod);

// GET raw router (Express.Router or Koa.Router)
console.log(router.callback());
console.log(router.raw);
console.log(router.getRouter());
}

myMethod(req: any, res: any, next: any) {}
Expand Down
8 changes: 4 additions & 4 deletions packages/di/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module.exports = {
...require("@tsed/jest-config")(__dirname, "di"),
coverageThreshold: {
global: {
statements: 98.3,
branches: 92.63,
functions: 96.44,
lines: 98.39
statements: 99.23,
branches: 92.91,
functions: 99.23,
lines: 99.34
}
}
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {getEnumerableKeys} from "@tsed/core";
import {ProviderScope} from "@tsed/di";
import {ControllerProvider} from "./ControllerProvider";

Expand All @@ -11,7 +10,6 @@ describe("ControllerProvider", () => {
controllerProvider = new ControllerProvider(Test);
controllerProvider.path = "/";
controllerProvider.scope = ProviderScope.REQUEST;
controllerProvider.routerOptions = {};
controllerProvider.middlewares = {
useBefore: [new Function()],
use: [new Function()],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import {ControllerMiddlewares, Provider, ProviderType, TokenProvider} from "@tsed/di";
import {ROUTER_OPTIONS} from "../constants/routerOptions";

let AUTO_INC = 0;
import {ControllerMiddlewares} from "../decorators/controller";
import {TokenProvider} from "../interfaces/TokenProvider";
import {Provider} from "./Provider";
import {ProviderType} from "./ProviderType";

export class ControllerProvider<T = any> extends Provider<T> {
readonly tokenRouter: string;
public tokenRouter: string;

constructor(provide: TokenProvider, options: Partial<Provider> = {}) {
super(provide, options);
this.type = ProviderType.CONTROLLER;
this.tokenRouter = `${this.name}_ROUTER_${AUTO_INC++}`;
}

/**
*
*/
get routerOptions(): any {
return this.store.get(ROUTER_OPTIONS) || ({} as any);
}

/**
*
* @param value
*/
set routerOptions(value: any) {
this.store.set(ROUTER_OPTIONS, value);
}

/**
Expand Down Expand Up @@ -53,6 +37,7 @@ export class ControllerProvider<T = any> extends Provider<T> {
Object.keys(middlewares).forEach((key: string) => {
concat(key, mdlwrs, middlewares);
});

this.store.set("middlewares", mdlwrs);
}
}
3 changes: 3 additions & 0 deletions packages/di/src/domain/DIContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ describe("DIContext", () => {
maxStackSize: 0
});

context.next = jest.fn();

expect(context.id).toEqual("id");
expect(context.dateStart).toBeInstanceOf(Date);
expect(context.container).toBeInstanceOf(Map);
expect(context.env).toEqual("test");

context.next();
context.logger.info("test");

expect(logger.info).toHaveBeenCalled();
Expand Down
1 change: 1 addition & 0 deletions packages/di/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from "./decorators/useOpts";
export * from "./decorators/value";
export * from "./domain/Container";
export * from "./domain/ContextLogger";
export * from "./domain/ControllerProvider";
export * from "./domain/DIContext";
export * from "./domain/InjectablePropertyType";
export * from "./domain/LocalsContainer";
Expand Down
48 changes: 48 additions & 0 deletions packages/di/src/services/DIConfiguration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
import {Env} from "@tsed/core";
import {DIConfiguration} from "../../src";

describe("DIConfiguration", () => {
describe("version()", () => {
it("should get version", () => {
// GIVEN
const configuration = new DIConfiguration();

configuration.version = "1.0.0";
expect(configuration.version).toEqual("1.0.0");
});
});
describe("env()", () => {
it("should get env", () => {
// GIVEN
const configuration = new DIConfiguration();

configuration.env = Env.DEV;
expect(configuration.env).toEqual(Env.DEV);
});
});
describe("debug()", () => {
it("should return debug", () => {
// GIVEN
const configuration = new DIConfiguration();

configuration.debug = true;
expect(configuration.debug).toEqual(true);

configuration.debug = false;
expect(configuration.debug).toEqual(false);
});
});
describe("forEach()", () => {
it("should return all key, value", () => {
// GIVEN
const configuration = new DIConfiguration();
const obj: any = {};
configuration.forEach((value, key) => {
obj[key] = value;
});
expect(obj).toEqual({
imports: [],
logger: {},
resolvers: [],
routes: [],
scopes: {}
});
});
});
describe("scopes()", () => {
it("should get scopes", () => {
// GIVEN
Expand Down
10 changes: 9 additions & 1 deletion packages/di/src/utils/runInContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ export function runInContext(ctx: DIContext, cb: any) {
}

export function bindContext(cb: any) {
return AsyncResource.bind(cb);
let localArgs: any;
const newCB: any = AsyncResource.bind(() => cb(...localArgs));

// FIXME: remove this hack when the support of v14 will be removed
// see issue: https://github.com/nodejs/node/issues/36051
return (...args: any) => {
localArgs = args;
return newCB(...args);
};
}
14 changes: 11 additions & 3 deletions packages/graphql/apollo/src/services/ApolloService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {PlatformTest} from "@tsed/common";
import {jest} from "@jest/globals";
import {PlatformApplication, PlatformTest} from "@tsed/common";
import {ApolloServer} from "apollo-server-express";
import {ApolloService} from "./ApolloService";
import {jest} from "@jest/globals";

jest.mock("apollo-server-express");

Expand All @@ -19,7 +19,14 @@ describe("ApolloService", () => {
describe("when server options isn't given", () => {
it("should create a server", async () => {
// GIVEN
const service = PlatformTest.get(ApolloService);
const service = PlatformTest.get<ApolloService>(ApolloService);
const app = PlatformTest.get(PlatformApplication);

const middleware = function middleware() {};
// @ts-ignore
ApolloServer.prototype.getMiddleware = jest.fn().mockReturnValue(middleware);

jest.spyOn(app, "use").mockReturnThis();

// WHEN
const result1 = await service.createServer("key", {
Expand All @@ -35,6 +42,7 @@ describe("ApolloService", () => {
expect(result1.getMiddleware).toHaveBeenCalledWith({
path: "/path"
});
expect(app.use).toHaveBeenCalledWith(middleware);
});
});
});
Expand Down
10 changes: 5 additions & 5 deletions packages/graphql/apollo/src/services/ApolloService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PlatformApplication} from "@tsed/common";
import {Constant, Inject, Service} from "@tsed/di";
import {Logger} from "@tsed/logger";
import {PlatformApplication} from "@tsed/common";
import type {Config} from "apollo-server-core";
import {
ApolloServerBase,
Expand All @@ -9,10 +9,10 @@ import {
ApolloServerPluginLandingPageGraphQLPlayground
} from "apollo-server-core";
import type {GraphQLSchema} from "graphql";
import type {ApolloServer, ApolloSettings} from "../interfaces/ApolloSettings";
import {ApolloCustomServerCB} from "../interfaces/ApolloSettings";
import Http from "http";
import Https from "https";
import type {ApolloServer, ApolloSettings} from "../interfaces/ApolloSettings";
import {ApolloCustomServerCB} from "../interfaces/ApolloSettings";

@Service()
export class ApolloService {
Expand All @@ -36,7 +36,7 @@ export class ApolloService {
> = new Map();

@Inject()
private app: PlatformApplication<any, any>;
private app: PlatformApplication<any>;

@Inject(Http.Server)
private httpServer: Http.Server | null;
Expand Down Expand Up @@ -76,7 +76,7 @@ export class ApolloService {
...middlewareOptions
});

this.app.getRouter().use(middleware);
this.app.use(middleware);

return server;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tslib": "2.4.0"
},
"devDependencies": {
"@prisma/client": "^3.10.0",
"@prisma/client": "^4.0.0",
"@tsed/core": "7.0.0-beta.13",
"@tsed/di": "7.0.0-beta.13",
"@tsed/json-mapper": "7.0.0-beta.13",
Expand Down
3 changes: 2 additions & 1 deletion packages/platform/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@tsed/platform-params": "7.0.0-beta.13",
"@tsed/platform-response-filter": "7.0.0-beta.13",
"@tsed/platform-views": "7.0.0-beta.13",
"@tsed/platform-router": "7.0.0-beta.13",
"@tsed/schema": "7.0.0-beta.13",
"@types/json-schema": "7.0.11",
"@types/on-finished": "2.3.1",
Expand All @@ -102,4 +103,4 @@
}
},
"devDependencies": {}
}
}
Loading

0 comments on commit 166dede

Please sign in to comment.