mirror of
https://github.com/zoriya/elysia-swagger.git
synced 2025-12-06 00:36:10 +00:00
🔧 fix: duplicate object reference
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
# 1.1.14 - 9 Oct 2024
|
||||||
|
Bug fix:
|
||||||
|
- Fix duplicate object reference
|
||||||
|
|
||||||
# 1.1.2 - 5 Sep 2024
|
# 1.1.2 - 5 Sep 2024
|
||||||
Feature:
|
Feature:
|
||||||
- add provenance publish
|
- add provenance publish
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@elysiajs/swagger",
|
"name": "@elysiajs/swagger",
|
||||||
"version": "1.1.3",
|
"version": "1.1.4",
|
||||||
"description": "Plugin for Elysia to auto-generate Swagger page",
|
"description": "Plugin for Elysia to auto-generate Swagger page",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "saltyAom",
|
"name": "saltyAom",
|
||||||
@@ -62,7 +62,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@apidevtools/swagger-parser": "^10.1.0",
|
"@apidevtools/swagger-parser": "^10.1.0",
|
||||||
"@types/bun": "1.1.6",
|
"@types/bun": "1.1.6",
|
||||||
"elysia": ">= 1.1.0-rc.2",
|
"elysia": "1.1.18",
|
||||||
"eslint": "9.6.0",
|
"eslint": "9.6.0",
|
||||||
"tsup": "^8.1.0",
|
"tsup": "^8.1.0",
|
||||||
"typescript": "^5.5.3"
|
"typescript": "^5.5.3"
|
||||||
|
|||||||
71
src/utils.ts
71
src/utils.ts
@@ -69,7 +69,9 @@ const mapTypesResponse = (
|
|||||||
|
|
||||||
const responses: Record<string, OpenAPIV3.MediaTypeObject> = {}
|
const responses: Record<string, OpenAPIV3.MediaTypeObject> = {}
|
||||||
|
|
||||||
for (const type of types)
|
for (const type of types) {
|
||||||
|
// console.log(schema)
|
||||||
|
|
||||||
responses[type] = {
|
responses[type] = {
|
||||||
schema:
|
schema:
|
||||||
typeof schema === 'string'
|
typeof schema === 'string'
|
||||||
@@ -78,6 +80,7 @@ const mapTypesResponse = (
|
|||||||
}
|
}
|
||||||
: { ...(schema as any) }
|
: { ...(schema as any) }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return responses
|
return responses
|
||||||
}
|
}
|
||||||
@@ -101,6 +104,12 @@ export const generateOperationId = (method: string, paths: string) => {
|
|||||||
return operationId
|
return operationId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cloneHook = <T>(hook: T) => {
|
||||||
|
if (!hook) return
|
||||||
|
|
||||||
|
return { ...hook }
|
||||||
|
}
|
||||||
|
|
||||||
export const registerSchemaPath = ({
|
export const registerSchemaPath = ({
|
||||||
schema,
|
schema,
|
||||||
path,
|
path,
|
||||||
@@ -114,7 +123,7 @@ export const registerSchemaPath = ({
|
|||||||
method: HTTPMethod
|
method: HTTPMethod
|
||||||
hook?: LocalHook<any, any, any, any, any, any, any>
|
hook?: LocalHook<any, any, any, any, any, any, any>
|
||||||
models: Record<string, TSchema>
|
models: Record<string, TSchema>
|
||||||
}) => {
|
}) => {
|
||||||
const contentType = hook?.type ?? [
|
const contentType = hook?.type ?? [
|
||||||
'application/json',
|
'application/json',
|
||||||
'multipart/form-data',
|
'multipart/form-data',
|
||||||
@@ -126,13 +135,13 @@ export const registerSchemaPath = ({
|
|||||||
const contentTypes =
|
const contentTypes =
|
||||||
typeof contentType === 'string'
|
typeof contentType === 'string'
|
||||||
? [contentType]
|
? [contentType]
|
||||||
: contentType ?? ['application/json']
|
: (contentType ?? ['application/json'])
|
||||||
|
|
||||||
const bodySchema = hook?.body
|
const bodySchema = cloneHook(hook?.body)
|
||||||
const paramsSchema = hook?.params
|
const paramsSchema = cloneHook(hook?.params)
|
||||||
const headerSchema = hook?.headers
|
const headerSchema = cloneHook(hook?.headers)
|
||||||
const querySchema = hook?.query
|
const querySchema = cloneHook(hook?.query)
|
||||||
let responseSchema = hook?.response as unknown as OpenAPIV3.ResponsesObject
|
let responseSchema: OpenAPIV3.ResponsesObject = cloneHook(hook?.response)
|
||||||
|
|
||||||
if (typeof responseSchema === 'object') {
|
if (typeof responseSchema === 'object') {
|
||||||
if (Kind in responseSchema) {
|
if (Kind in responseSchema) {
|
||||||
@@ -292,36 +301,36 @@ export const registerSchemaPath = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const filterPaths = (
|
export const filterPaths = (
|
||||||
paths: Record<string, any>,
|
paths: Record<string, any>,
|
||||||
docsPath: string,
|
docsPath: string,
|
||||||
{
|
{
|
||||||
excludeStaticFile = true,
|
excludeStaticFile = true,
|
||||||
exclude = []
|
exclude = []
|
||||||
}: {
|
}: {
|
||||||
excludeStaticFile: boolean
|
excludeStaticFile: boolean
|
||||||
exclude: (string | RegExp)[]
|
exclude: (string | RegExp)[]
|
||||||
}
|
}
|
||||||
) => {
|
) => {
|
||||||
const newPaths: Record<string, any> = {}
|
const newPaths: Record<string, any> = {}
|
||||||
|
|
||||||
// exclude docs path and OpenAPI json path
|
// exclude docs path and OpenAPI json path
|
||||||
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
|
const excludePaths = [`/${docsPath}`, `/${docsPath}/json`].map((p) =>
|
||||||
normalize(p)
|
normalize(p)
|
||||||
)
|
)
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(paths))
|
for (const [key, value] of Object.entries(paths))
|
||||||
if (
|
if (
|
||||||
!exclude.some((x) => {
|
!exclude.some((x) => {
|
||||||
if (typeof x === 'string') return key === x
|
if (typeof x === 'string') return key === x
|
||||||
|
|
||||||
return x.test(key)
|
return x.test(key)
|
||||||
}) &&
|
}) &&
|
||||||
!excludePaths.includes(key) &&
|
!excludePaths.includes(key) &&
|
||||||
!key.includes('*') &&
|
!key.includes('*') &&
|
||||||
(excludeStaticFile ? !key.includes('.') : true)
|
(excludeStaticFile ? !key.includes('.') : true)
|
||||||
) {
|
) {
|
||||||
Object.keys(value).forEach((method) => {
|
Object.keys(value).forEach((method) => {
|
||||||
const schema = value[method]
|
const schema = value[method]
|
||||||
|
|
||||||
if (key.includes('{')) {
|
if (key.includes('{')) {
|
||||||
if (!schema.parameters) schema.parameters = []
|
if (!schema.parameters) schema.parameters = []
|
||||||
|
|||||||
Reference in New Issue
Block a user