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

Feature/global base route path #3

Merged
merged 3 commits into from
Oct 10, 2023
Merged
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
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,10 @@
},
"scripts": {
"prepare": "husky install",
"win-clean": "if exist lib (del /q lib\\*)",
"linux-clean": "rm -rf lib/*",
"win-cpy": "xcopy package.json lib\\ && xcopy README.md lib\\ && xcopy CHANGELOG.md lib\\ /Y",
"linux-cpy": "cp package.json README.md CHANGELOG.md lib/",
"build:win": "npm run win-clean && npm run build:esm && npm run build:cjs && npm run win-cpy",
"build:linux": "npm run linux-clean && npm run build:esm && npm run build:cjs && npm run linux-cpy",
"build:esm": "tsc -p tsconfig.esm.json && mv lib/esm/index.js lib/esm/index.mjs ",
"clean": "node scripts/rm.js lib",
"copy": "node scripts/copy.js package.json README.md CHANGELOG.md lib",
"build": "npm run clean && npm run build:cjs && npm run copy",
"build:cjs": "tsc -p tsconfig.cjs.json",
"git": "powershell -Command \"git add .; git commit -m \"%1\"; npm run release\"",
"release": "release-it",
"test": "jest",
"format": "prettier --write \"src/**/*.ts\" --cache",
Expand Down
35 changes: 35 additions & 0 deletions scripts/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require("fs");
const path = require("path");

function copyRecursiveSync(src, dest) {
const exists = fs.existsSync(src);
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();

if (isDirectory) {
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function (childItemName) {
copyRecursiveSync(
path.join(src, childItemName),
path.join(dest, childItemName),
);
});
} else {
fs.copyFileSync(src, dest);
}
}

if (process.argv.length < 4) {
process.stderr.write(
"Usage: node copy.js <origin1> <origin2> ... <destination>\n",
);
process.exit(1);
}

const destination = process.argv[process.argv.length - 1];

for (let i = 2; i < process.argv.length - 1; i++) {
const origin = process.argv[i];
copyRecursiveSync(origin, path.join(destination, path.basename(origin)));
process.stdout.write(`Copied: ${origin} to ${destination}\n`);
}
30 changes: 30 additions & 0 deletions scripts/mv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require("fs").promises;

const moveFile = async (origin, destination) => {
try {
await fs.access(origin);
} catch (error) {
process.stderr.write(`Error: Origin '${origin}' not found\n`);
process.exit(1);
}

try {
await fs.rename(origin, destination);
process.stdout.write(`Move: ${origin} to ${destination}\n`);
} catch (error) {
process.stderr.write(
`Error: Unable to move '${origin}' to '${destination}'\n`,
);
process.exit(1);
}
};

if (process.argv.length !== 4) {
process.stderr.write("Usage: node mv.js <origin> <destination>\n");
process.exit(1);
}

const origin = process.argv[2];
const destination = process.argv[3];

moveFile(origin, destination);
19 changes: 19 additions & 0 deletions scripts/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require("fs").promises;

const removeTarget = async (target) => {
try {
await fs.rm(target, { recursive: true, force: true });
process.stdout.write(`Removed: ${target}\n`);
} catch (error) {
process.stderr.write(`Error: Unable to remove '${target}'\n`);
process.exit(1);
}
};

if (process.argv.length !== 3) {
process.stderr.write("Usage: node rm.js <dir/file>\n");
process.exit(1);
}

const target = process.argv[2];
removeTarget(target);
43 changes: 39 additions & 4 deletions src/adapter-express/application-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
private environment: ServerEnvironment;
private container: Container;
private middlewares: Array<ExpressHandler> = [];
private globalPrefix: string | undefined;

protected configureServices(): void | Promise<void> {}
protected postServerInitialization(): void | Promise<void> {}
Expand All @@ -64,12 +65,10 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
* @param middlewares - An array of Express middlewares to be applied.
* @returns The configured Application instance.
*/
public async create(
private async init(
container: Container,
middlewares: Array<express.RequestHandler> = [],
): Promise<ApplicationExpress> {
this.container = container;

await Promise.resolve(this.configureServices());

const middleware = container.get<IMiddleware>(Middleware);
Expand All @@ -80,7 +79,9 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress

const allMiddlewareEntries: Array<ExpressHandler | MiddlewareConfig> = [...this.middlewares];

const expressServer = new InversifyExpressServer(container);
const expressServer = new InversifyExpressServer(container, null, {
rootPath: this.globalPrefix as string,
});

expressServer.setConfig((app: express.Application) => {
allMiddlewareEntries.forEach((entry) => {
Expand Down Expand Up @@ -109,6 +110,23 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
return this;
}

/**
* Create and configure the Express application.
* @param container - The InversifyJS container.
* @param middlewares - An array of Express middlewares to be applied.
* @returns The configured Application instance.
*/
public async create(
container: Container,
middlewares: Array<ExpressHandler> = [],
): Promise<ApplicationExpress> {
this.container = container;
this.middlewares = middlewares;
this.globalPrefix = this.globalPrefix || "/";

return this;
}

/**
* Start listening on the given port and environment.
* @param port - The port number to listen on.
Expand All @@ -120,6 +138,10 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
environment: ServerEnvironment,
consoleMessage?: IApplicationMessageToConsole,
): Promise<void> {
/* Initializes the application and executes the middleware pipeline */
await this.init(this.container, this.middlewares as Array<express.RequestHandler>);

/* Sets the port and environment */
this.port = port;
this.environment = environment;
this.app.set("env", environment);
Expand All @@ -138,6 +160,19 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
await Promise.resolve(this.postServerInitialization());
}

/**
* Sets the global route prefix for the application.
*
* @public
* @method setGlobalRoutePrefix
*
* @param {string} prefix - The prefix to use for all routes.
*
*/
public setGlobalRoutePrefix(prefix: string): void {
this.globalPrefix = prefix;
}

/**
* Configures the application's view engine based on the provided configuration options.
*
Expand Down
21 changes: 6 additions & 15 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames":true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"strictNullChecks": false,
"checkJs": true,
"allowJs": true,
"declaration": true,
"declarationMap": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"types": [
"node",
"reflect-metadata",
"jest"
],
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
]
}
"types": ["node", "reflect-metadata", "jest"]
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
11 changes: 0 additions & 11 deletions tsconfig.esm.json

This file was deleted.

Loading