Skip to content

Commit 79afbdc

Browse files
committed
🔧 fix: Bug fix: type generator: collapse path when trying to access from dist
1 parent 371c376 commit 79afbdc

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.3.3 - 3 Sep 2025
2+
Bug fix:
3+
- type generator: collapse path when trying to access from dist
4+
15
# 1.3.2 - 2 Sep 2025
26
Feature:
37
- add `withHeader` for adding custom headers to response schema

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elysiajs/openapi",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "Plugin for Elysia to auto-generate API documentation",
55
"author": {
66
"name": "saltyAom",

src/gen/index.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
writeFileSync,
66
rmSync,
77
existsSync,
8-
cpSync
8+
cpSync,
9+
exists
910
} from 'fs'
1011
import { TypeBox } from '@sinclair/typemap'
1112

@@ -46,6 +47,14 @@ interface OpenAPIGeneratorOptions {
4647
* @default process.cwd()
4748
*/
4849
projectRoot?: string
50+
51+
/**
52+
* Override output path
53+
*
54+
* Under any circumstance, that Elysia failed to find a correct schema,
55+
* Put your own schema in this path
56+
*/
57+
overrideOutputPath?(tempDir: string): string
4958
}
5059

5160
/**
@@ -66,7 +75,8 @@ export const fromTypes =
6675
{
6776
tsconfigPath = 'tsconfig.json',
6877
instanceName,
69-
projectRoot = process.cwd()
78+
projectRoot = process.cwd(),
79+
overrideOutputPath
7080
}: OpenAPIGeneratorOptions = {}
7181
) =>
7282
() => {
@@ -107,16 +117,22 @@ export const fromTypes =
107117
exec(`tsc`, tmpRoot)
108118

109119
try {
110-
const declaration = readFileSync(
111-
join(
120+
const fileName = targetFilePath
121+
.replace(/.tsx$/, '.ts')
122+
.replace(/.ts$/, '.d.ts')
123+
124+
let targetFile =
125+
overrideOutputPath?.(tmpRoot) ?? join(tmpRoot, 'dist', fileName)
126+
127+
// Sometime TypeScript doesn't include the first level directory, eg. src/file.ts -> dist/file.d.ts
128+
if (!existsSync(targetFile))
129+
targetFile = join(
112130
tmpRoot,
113131
'dist',
114-
targetFilePath
115-
.replace(/.tsx$/, '.ts')
116-
.replace(/.ts$/, '.d.ts')
117-
),
118-
'utf8'
119-
)
132+
fileName.slice(fileName.indexOf('/') + 1)
133+
)
134+
135+
const declaration = readFileSync(targetFile, 'utf8')
120136

121137
// Check just in case of race-condition
122138
if (existsSync(tmpRoot))
@@ -184,7 +200,7 @@ export const fromTypes =
184200

185201
return routes
186202
} catch (error) {
187-
console.warn('Failed to generate OpenAPI schema')
203+
console.warn('[@elysiajs/openapi/gen] Failed to generate OpenAPI schema')
188204
console.warn(error)
189205

190206
return

0 commit comments

Comments
 (0)