Skip to content

Commit 6726465

Browse files
committed
🔧 fix: #230, #265 do not inline Response if is Cloudflare Worker
1 parent ebdf2f5 commit 6726465

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Improvement:
88
- type gen: use `process.getBuiltinModule` to import native node module conditionally
99
- type gen: `fromTypes` now accept direct declaration
1010
- export `fromTypes` from index
11+
- [#230](https://github.com/elysiajs/elysia-openapi/issues/230) do not inline Response if is Cloudflare Worker
1112

1213
Bug fix:
1314
- [#226](https://github.com/elysiajs/elysia-openapi/issues/266) accept operationId

src/index.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ import type { OpenAPIV3 } from 'openapi-types'
99
import type { ApiReferenceConfiguration } from '@scalar/types'
1010
import type { ElysiaOpenAPIConfig } from './types'
1111

12+
function isCloudflareWorker() {
13+
try {
14+
// Check for the presence of caches.default, which is a global in Workers
15+
if (
16+
typeof caches !== 'undefined' &&
17+
// @ts-ignore
18+
typeof caches.default !== 'undefined'
19+
)
20+
return true
21+
22+
// @ts-ignore
23+
if (typeof WebSocketPair !== 'undefined') {
24+
return true
25+
}
26+
} catch (e) {
27+
// If accessing these globals throws an error, it's likely not a Worker
28+
return false
29+
}
30+
31+
return false
32+
}
33+
1234
/**
1335
* Plugin for [elysia](https://github.com/elysiajs/elysia) that auto-generate OpenAPI documentation page.
1436
*
@@ -47,8 +69,7 @@ export const openapi = <
4769
.use((app) => {
4870
if (provider === null) return app
4971

50-
return app.get(
51-
path,
72+
const page = () =>
5273
new Response(
5374
provider === 'swagger-ui'
5475
? SwaggerUIRender(info, {
@@ -70,13 +91,13 @@ export const openapi = <
7091
'content-type': 'text/html; charset=utf8'
7192
}
7293
}
73-
),
74-
{
75-
detail: {
76-
hide: true
77-
}
94+
)
95+
96+
return app.get(path, isCloudflareWorker() ? page : page(), {
97+
detail: {
98+
hide: true
7899
}
79-
)
100+
})
80101
})
81102
.get(
82103
specPath,

0 commit comments

Comments
 (0)