Skip to content

Commit 107aba1

Browse files
committed
feat(@sirutils/wizard): adjust swagger automaticly when an action is created
1 parent 64fc3ae commit 107aba1

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

packages/wizard/src/definitions/wizard.ts

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ declare global {
147147
Hr,
148148
>(
149149
meta: {
150+
description?: string
150151
body?: B
151152
params?: P
152153
queries?: Q

packages/wizard/src/utils/internals/action.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export const actionActions = createActions(
1111
const queriesSchema = meta.queries && createAsyncSchema(meta.queries)
1212

1313
return serviceOptions => ({
14-
...((meta.rest ? { rest: meta.rest } : {}) as BlobType),
14+
// We dont have access to service's name in here
15+
// so we return meta datas for adding these to swagger in service module
16+
meta: meta,
1517
handler: capsule(
1618
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: Redundant
1719
async ctx => {

packages/wizard/src/utils/internals/service.ts

+72-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ export const serviceActions = createActions(
1919
) as BlobType
2020

2121
for (const [key, value] of Object.entries(serviceOptions.actions ?? {})) {
22-
if (value.rest) {
23-
aliases[
24-
typeof value.rest === 'boolean' ? `${toMethod(key)} /` : (value.rest as string)
25-
] = async (req: IncomingRequest, res: GatewayResponse) => {
22+
if (value.meta.rest) {
23+
// Create alias for redirecting
24+
const alias =
25+
typeof value.meta.rest === 'boolean'
26+
? `${toMethod(key)} /`
27+
: (value.meta.rest as string)
28+
29+
aliases[alias] = async (req: IncomingRequest, res: GatewayResponse) => {
2630
const result = await group(() =>
2731
// biome-ignore lint/style/noNonNullAssertion: Redundant
2832
serviceOptions.actions![key]?.handler!(req.$ctx)
@@ -42,6 +46,70 @@ export const serviceActions = createActions(
4246
res.end(result.error.stringify())
4347
}
4448
}
49+
50+
// Extract method and path from alias
51+
const [method, path] = alias.split(' ') as [string, string]
52+
53+
// Format param variables
54+
const formattedPath = path
55+
.substring(1)
56+
.split('/')
57+
.map(val => (val.startsWith(':') ? `\{${val.substring(1)}\}` : val))
58+
.join('/')
59+
60+
// Get the paths object of swagger
61+
const paths = context.api.gateway.settings.swagger.paths
62+
63+
// Original path
64+
const oriPath = `/${serviceOptions.name}\/${serviceOptions.version}\/${formattedPath}`
65+
66+
// Omit $$async to get body content
67+
// and create swagger struct
68+
const { $$async: _$b, ...omittedBody } = value.meta.body ?? {}
69+
// biome-ignore lint/complexity/noForEach: <explanation>
70+
Object.keys(omittedBody).forEach(key => {
71+
omittedBody[key] = {
72+
type: omittedBody[key],
73+
}
74+
})
75+
76+
// Omit $$async to get params
77+
// and create swagger struct
78+
const { $$async: _$p, ...omittedParams } = value.meta.queries ?? {}
79+
// biome-ignore lint/complexity/noForEach: <explanation>
80+
Object.keys(omittedParams).forEach(key => {
81+
omittedParams[key] = {
82+
name: key,
83+
in: 'path',
84+
required: true,
85+
schema: {
86+
type: omittedParams[key],
87+
},
88+
}
89+
})
90+
91+
// Create request body struct for swagger
92+
const bodyDef = value.meta.body
93+
? {
94+
required: true,
95+
content: {
96+
'application/json': {
97+
schema: {
98+
type: 'object',
99+
properties: omittedBody,
100+
},
101+
},
102+
},
103+
}
104+
: {}
105+
106+
// Add struct of the endpoint to swagger
107+
paths[oriPath] = {}
108+
paths[oriPath][method.toLowerCase()] = {
109+
...(value.meta.description ? { description: value.meta.description } : {}),
110+
parameters: Object.values(omittedParams),
111+
requestBody: bodyDef,
112+
}
45113
}
46114
}
47115

0 commit comments

Comments
 (0)