Skip to content

Commit 6e53bfa

Browse files
committed
handle empty files
1 parent 76f86a9 commit 6e53bfa

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

packages/nestjs-endpoints/src/fns.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ export function endpoint<
471471
'handler',
472472
);
473473
methodDecorator(cls.prototype, 'handler', descriptor);
474+
Reflect.defineMetadata('endpoints:path', httpPath, cls);
474475
});
475476

476477
return cls;

packages/nestjs-endpoints/src/router-module.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,27 @@ export class EndpointsRouterModule {
4242
rootDirectory = path.join(path.dirname(calledFrom), rootDirectory);
4343
}
4444
settings.rootDirectory = rootDirectory;
45-
const endpoints: Type[] = [];
45+
let endpoints: Type[] = [];
4646
if (params.autoLoadEndpoints ?? true) {
4747
const endopointFiles = findEndpoints(rootDirectory);
4848
for (const f of endopointFiles) {
4949
// eslint-disable-next-line @typescript-eslint/no-require-imports
50-
endpoints.push(require(f).default);
50+
const endpoint = require(f).default;
51+
if (endpoint) {
52+
endpoints.push(endpoint);
53+
}
5154
}
5255
}
5356
for (const fn of settings.decorateEndpointFns) {
5457
fn();
5558
}
59+
if (endpoints.length > 0) {
60+
endpoints = endpoints.filter((e) => {
61+
return Reflect.getMetadataKeys(e).some(
62+
(k) => k === 'endpoints:path',
63+
);
64+
});
65+
}
5666

5767
return {
5868
module: EndpointsRouterModule,

packages/test-endpoints-router-module/test-app-express-cjs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"build": "rm -rf ./dist && pnpm tsc --project tsconfig.build.json",
1010
"start": "pnpm build && pnpm start:prod",
11+
"start:dev": "nest start --watch",
1112
"start:prod": "node dist/main",
1213
"dev": "tsx watch --inspect=0 --clear-screen=false ./src/main.ts",
1314
"test:e2e": "jest --config ./test/jest-e2e.json"

scripts/build.sh

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,5 @@ echo '{"type": "commonjs"}' > dist/cjs/package.json
99
pnpm tsc --project tsconfig.esm.json && pnpm tsc-alias --project tsconfig.esm.json
1010
echo '{"type": "module"}' > dist/esm/package.json
1111
# Modify the ESM router-module.js to use dynamic import instead of require
12-
if [[ "$OSTYPE" == "darwin"* ]]; then
13-
SED_OPTS="-i ''"
14-
else
15-
SED_OPTS="-i"
16-
fi
17-
sed $SED_OPTS 's/require(f).default/await import(f).then((m) => m.default)/g' dist/esm/router-module.js
12+
sed -i.bak 's/require(f).default/await import(f).then((m) => m.default)/g' dist/esm/router-module.js
13+
rm -f dist/esm/router-module.js.bak

0 commit comments

Comments
 (0)