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

docs: add platform adapter documentation #2294

Open
wants to merge 1 commit into
base: production
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions docs/.vuepress/config.base.js
Original file line number Diff line number Diff line change
@@ -172,6 +172,10 @@ module.exports = ({title, description, base = "", url, apiRedirectUrl = "", them
{
text: "Testing",
link: `${base}/docs/testing.html`
},
{
text: "Create Platform",
link: `${base}/docs/create-platform-adapter.html`
}
]
},
524 changes: 524 additions & 0 deletions docs/docs/create-platform-adapter.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/platform/common/package.json
Original file line number Diff line number Diff line change
@@ -105,4 +105,4 @@
"optional": false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
createContext,
InjectorService,
PlatformProvider,
PlatformAdapter,
PlatformApplication,
PlatformBuilder,
@@ -10,6 +9,7 @@ import {
PlatformHandlerType,
PlatformMulter,
PlatformMulterSettings,
PlatformProvider,
PlatformStaticsOptions,
runInContext
} from "@tsed/common";
@@ -19,9 +19,8 @@ import type {PlatformViews} from "@tsed/platform-views";
import {OptionsJson, OptionsText, OptionsUrlencoded} from "body-parser";
import Express from "express";
import {IncomingMessage, ServerResponse} from "http";
import type multer from "multer";
import {promisify} from "util";
import {PlatformExpressStaticsOptions} from "../interfaces/PlatformExpressStaticsOptions";
import {multerMiddleware} from "../middlewares/multerMiddleware";
import {staticsMiddleware} from "../middlewares/staticsMiddleware";

declare module "express" {
@@ -55,11 +54,8 @@ export class PlatformExpress implements PlatformAdapter<Express.Application> {
static readonly NAME = "express";

readonly providers = [];
#multer: typeof multer;

constructor(protected injector: InjectorService) {
import("multer").then(({default: multer}) => (this.#multer = multer));
}
constructor(protected injector: InjectorService) {}

/**
* Create new serverless application. In this mode, the component scan are disabled.
@@ -87,7 +83,7 @@ export class PlatformExpress implements PlatformAdapter<Express.Application> {

async beforeLoadRoutes() {
const injector = this.injector;
const app = this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;
const app = this.getPlatformApplication()!;

// disable x-powered-by header
injector.settings.get("env") === Env.PROD && app.getApp().disable("x-powered-by");
@@ -96,7 +92,7 @@ export class PlatformExpress implements PlatformAdapter<Express.Application> {
}

async afterLoadRoutes() {
const app = this.injector.get<PlatformApplication<Express.Application>>(PlatformApplication)!;
const app = this.getPlatformApplication()!;
const platformExceptions = this.injector.get<PlatformExceptions>(PlatformExceptions)!;

// NOT FOUND
@@ -181,27 +177,7 @@ export class PlatformExpress implements PlatformAdapter<Express.Application> {
}

multipart(options: PlatformMulterSettings): PlatformMulter {
const m = this.#multer(options);
const makePromise = (multer: any, name: string) => {
// istanbul ignore next
if (!multer[name]) return;

const fn = multer[name];

multer[name] = function apply(...args: any[]) {
const middleware: any = Reflect.apply(fn, this, args);

return (req: any, res: any) => promisify(middleware)(req, res);
};
};

makePromise(m, "any");
makePromise(m, "array");
makePromise(m, "fields");
makePromise(m, "none");
makePromise(m, "single");

return m;
return multerMiddleware(options);
}

statics(endpoint: string, options: PlatformStaticsOptions) {
1 change: 1 addition & 0 deletions packages/platform/platform-express/src/index.ts
Original file line number Diff line number Diff line change
@@ -6,4 +6,5 @@ export * from "./components/PlatformExpress";
export * from "./interfaces/PlatformExpressSettings";
export * from "./interfaces/PlatformExpressStaticsOptions";
export * from "./interfaces/interfaces";
export * from "./middlewares/multerMiddleware";
export * from "./middlewares/staticsMiddleware";
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {PlatformMulterSettings} from "@tsed/common";
import type multer from "multer";
import {promisify} from "util";

let multerModule: typeof multer;

import("multer").then(({default: multer}) => (multerModule = multer));

export function multerMiddleware(options: PlatformMulterSettings) {
const m = multerModule(options);

const makePromise = (multer: any, name: string) => {
// istanbul ignore next
if (!multer[name]) return;

const fn = multer[name];

multer[name] = function apply(...args: any[]) {
const middleware: any = Reflect.apply(fn, this, args);

return (req: any, res: any) => promisify(middleware)(req, res);
};
};

makePromise(m, "any");
makePromise(m, "array");
makePromise(m, "fields");
makePromise(m, "none");
makePromise(m, "single");

return m;
}
4 changes: 2 additions & 2 deletions packages/platform/platform-koa/src/components/PlatformKoa.ts
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ import {staticsMiddleware} from "../middlewares/staticsMiddleware";
import {PlatformKoaHandler} from "../services/PlatformKoaHandler";
import {PlatformKoaRequest} from "../services/PlatformKoaRequest";
import {PlatformKoaResponse} from "../services/PlatformKoaResponse";
import {getMulter} from "../utils/multer";
import {multerMiddleware} from "../middlewares/multerMiddleware";

declare global {
namespace TsED {
@@ -186,7 +186,7 @@ export class PlatformKoa implements PlatformAdapter<Koa> {
}

multipart(options: PlatformMulterSettings): PlatformMulter {
return getMulter(options);
return multerMiddleware(options);
}

statics(endpoint: string, options: PlatformStaticsOptions) {
2 changes: 1 addition & 1 deletion packages/platform/platform-koa/src/index.ts
Original file line number Diff line number Diff line change
@@ -11,4 +11,4 @@ export * from "./middlewares/staticsMiddleware";
export * from "./services/PlatformKoaHandler";
export * from "./services/PlatformKoaRequest";
export * from "./services/PlatformKoaResponse";
export * from "./utils/multer";
export * from "./middlewares/multerMiddleware";
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Koa from "koa";
import {promisify} from "util";

let multer: any;
import("multer").then(({default: m}) => (multer = m));
let multerModule: any;
import("multer").then(({default: m}) => (multerModule = m));

/**
* @ignore
@@ -52,8 +52,8 @@ function makePromise(multer: any, name: string) {
/**
* @ignore
*/
export function getMulter(options: any) {
const m = multer(options);
export function multerMiddleware(options: any) {
const m = multerModule(options);

makePromise(m, "any");
makePromise(m, "array");
2 changes: 1 addition & 1 deletion packages/platform/platform-test-sdk/package.json
Original file line number Diff line number Diff line change
@@ -50,4 +50,4 @@
"eslint": "^8.12.0"
},
"peerDependencies": {}
}
}