Skip to content

Commit 5229b64

Browse files
authored
Merge pull request #13 from rhyek/fix/relative-path-for-setup
fix relative path for setup
2 parents 72d4941 + c354efe commit 5229b64

File tree

9 files changed

+22
-13
lines changed

9 files changed

+22
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ async function bootstrap() {
308308
const app = await NestFactory.create(AppModule);
309309
const { document, changed } = await setupOpenAPI(app, {
310310
configure: (builder) => builder.setTitle('My Api'),
311-
outputFile: 'openapi.json',
311+
outputFile: process.cwd() + '/openapi.json',
312312
});
313313
if (changed) {
314314
void import('orval').then(({ generate }) => generate());

packages/nestjs-endpoints/src/helpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,15 @@ export const ApiQueries = <T extends z.ZodObject<ZodRawShape>>(
101101
export function shouldJson(value: unknown) {
102102
return typeof value !== 'string';
103103
}
104+
105+
export function getCallsiteFile() {
106+
const callsite = callsites()[2];
107+
if (!callsite) {
108+
throw new Error('Callsite not found');
109+
}
110+
const result = callsite.getFileName()?.replace(/^file:\/\//, '');
111+
if (!result) {
112+
throw new Error('Callsite file not found');
113+
}
114+
return result;
115+
}

packages/nestjs-endpoints/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { z } from 'zod';
22
export { EndpointsModule } from './decorators';
3-
export { endpoint, decorated, schema } from './fns';
3+
export { endpoint, decorated, schema } from './endpoint-fn';
44
export {
55
ZodValidationException,
66
ZodSerializationException,

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import {
66
ModuleMetadata,
77
Type,
88
} from '@nestjs/common';
9-
import callsites from 'callsites';
109
import { endpointFileRegex, settings } from './consts';
10+
import { getCallsiteFile } from './helpers';
1111

1212
@Module({})
1313
export class EndpointsRouterModule {
@@ -33,12 +33,7 @@ export class EndpointsRouterModule {
3333
}): Promise<DynamicModule> {
3434
let rootDirectory = params.rootDirectory;
3535
if (!path.isAbsolute(rootDirectory)) {
36-
const calledFrom = callsites()[1]
37-
?.getFileName()
38-
?.replace(/^file:/, '');
39-
if (!calledFrom) {
40-
throw new Error('Cannot determine call site');
41-
}
36+
const calledFrom = getCallsiteFile();
4237
rootDirectory = path.join(path.dirname(calledFrom), rootDirectory);
4338
}
4439
settings.rootDirectory = rootDirectory;

packages/nestjs-endpoints/src/setup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Writable } from 'node:stream';
44
import { INestApplication } from '@nestjs/common';
55
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
66
import { patchNestJsSwagger } from 'nestjs-zod';
7+
import { getCallsiteFile } from './helpers';
78

89
export async function setupOpenAPI(
910
app: INestApplication,
@@ -36,7 +37,7 @@ export async function setupOpenAPI(
3637
} else {
3738
const documentFile = path.isAbsolute(outputFile)
3839
? outputFile
39-
: path.resolve(process.cwd(), outputFile);
40+
: path.resolve(path.dirname(getCallsiteFile()), outputFile);
4041
const currentDocument = await readFile(documentFile, 'utf-8').catch(
4142
() => '',
4243
);

packages/test-endpoints-module/test-app-express-cjs/src/codegen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AppModule } from './app.module';
66
async function bootstrap() {
77
const app = await NestFactory.create(AppModule);
88
await setupOpenAPI(app, {
9-
outputFile: 'openapi.json',
9+
outputFile: process.cwd() + '/openapi.json',
1010
});
1111
void generate();
1212
}

packages/test-endpoints-module/test-app-express-cjs/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AppModule } from './app.module';
55
async function bootstrap() {
66
const app = await NestFactory.create(AppModule);
77
await setupOpenAPI(app, {
8-
outputFile: 'openapi.json',
8+
outputFile: process.cwd() + '/openapi.json',
99
});
1010
const port = process.env.PORT || 3000;
1111
await app.listen(port, () => {

packages/test-endpoints-router-module/test-app-express-cjs/src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import path from 'node:path';
12
import { NestFactory } from '@nestjs/core';
23
import { setupOpenAPI } from 'nestjs-endpoints';
34
import { AppModule } from './app.module';
45

56
async function bootstrap() {
67
const app = await NestFactory.create(AppModule);
78
await setupOpenAPI(app, {
8-
outputFile: 'openapi.json',
9+
outputFile: path.resolve(process.cwd(), 'openapi.json'),
910
});
1011
const port = process.env.PORT || 3000;
1112
await app.listen(port, () => {

0 commit comments

Comments
 (0)