keryx 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions/swagger.ts +63 -5
- package/classes/Action.ts +0 -1
- package/package.json +1 -1
package/actions/swagger.ts
CHANGED
|
@@ -63,7 +63,20 @@ export class Swagger implements Action {
|
|
|
63
63
|
|
|
64
64
|
async run() {
|
|
65
65
|
const paths: Record<string, any> = {};
|
|
66
|
-
const components: {
|
|
66
|
+
const components: {
|
|
67
|
+
schemas: Record<string, any>;
|
|
68
|
+
securitySchemes?: Record<string, any>;
|
|
69
|
+
} = {
|
|
70
|
+
schemas: {},
|
|
71
|
+
securitySchemes: {
|
|
72
|
+
sessionCookie: {
|
|
73
|
+
type: "apiKey",
|
|
74
|
+
in: "cookie",
|
|
75
|
+
name: config.session.cookieName,
|
|
76
|
+
description: "Session cookie set by session:create",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
};
|
|
67
80
|
|
|
68
81
|
for (const action of api.actions.actions) {
|
|
69
82
|
if (!action.web?.route || !action.web?.method) continue;
|
|
@@ -81,19 +94,63 @@ export class Swagger implements Action {
|
|
|
81
94
|
const description = action.description;
|
|
82
95
|
|
|
83
96
|
// Extract path parameters from the original route
|
|
84
|
-
const
|
|
97
|
+
const parameters: any[] = [];
|
|
85
98
|
const pathParamMatches = action.web.route.match(/:\w+/g) || [];
|
|
99
|
+
const pathParamNames = new Set<string>();
|
|
100
|
+
|
|
101
|
+
// Pre-compute Zod JSON Schema for enriching path param types
|
|
102
|
+
let zodProperties: Record<string, any> = {};
|
|
103
|
+
let zodDescriptions: Record<string, string> = {};
|
|
104
|
+
if (action.inputs && typeof action.inputs.parse === "function") {
|
|
105
|
+
const jsonSchema = z.toJSONSchema(action.inputs, {
|
|
106
|
+
io: "input",
|
|
107
|
+
unrepresentable: "any",
|
|
108
|
+
}) as any;
|
|
109
|
+
zodProperties = jsonSchema.properties ?? {};
|
|
110
|
+
for (const [name, propSchema] of Object.entries<any>(zodProperties)) {
|
|
111
|
+
if (propSchema.description) {
|
|
112
|
+
zodDescriptions[name] = propSchema.description;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
86
117
|
for (const paramMatch of pathParamMatches) {
|
|
87
118
|
const paramName = paramMatch.slice(1); // Remove the colon
|
|
88
|
-
|
|
119
|
+
pathParamNames.add(paramName);
|
|
120
|
+
parameters.push({
|
|
89
121
|
name: paramName,
|
|
90
122
|
in: "path",
|
|
91
123
|
required: true,
|
|
92
124
|
schema: { type: "string" },
|
|
93
|
-
description:
|
|
125
|
+
description:
|
|
126
|
+
zodDescriptions[paramName] ?? `The ${paramName} parameter`,
|
|
94
127
|
});
|
|
95
128
|
}
|
|
96
129
|
|
|
130
|
+
// For GET/HEAD, convert remaining Zod inputs into query parameters
|
|
131
|
+
if (
|
|
132
|
+
(method === "get" || method === "head") &&
|
|
133
|
+
Object.keys(zodProperties).length > 0
|
|
134
|
+
) {
|
|
135
|
+
const fullSchema = z.toJSONSchema(action.inputs!, {
|
|
136
|
+
io: "input",
|
|
137
|
+
unrepresentable: "any",
|
|
138
|
+
}) as any;
|
|
139
|
+
const requiredFields = new Set<string>(fullSchema.required ?? []);
|
|
140
|
+
for (const [name, propSchema] of Object.entries<any>(zodProperties)) {
|
|
141
|
+
if (pathParamNames.has(name)) continue; // already a path param
|
|
142
|
+
parameters.push({
|
|
143
|
+
name,
|
|
144
|
+
in: "query",
|
|
145
|
+
required: requiredFields.has(name),
|
|
146
|
+
schema: propSchema,
|
|
147
|
+
...(propSchema.description
|
|
148
|
+
? { description: propSchema.description }
|
|
149
|
+
: {}),
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
97
154
|
// Build requestBody if Zod inputs exist and method supports body
|
|
98
155
|
let requestBody: any = undefined;
|
|
99
156
|
if (
|
|
@@ -145,7 +202,7 @@ export class Swagger implements Action {
|
|
|
145
202
|
operationId,
|
|
146
203
|
summary,
|
|
147
204
|
...(description ? { description } : {}),
|
|
148
|
-
...(
|
|
205
|
+
...(parameters.length > 0 ? { parameters } : {}),
|
|
149
206
|
...(requestBody ? { requestBody } : {}),
|
|
150
207
|
responses,
|
|
151
208
|
tags: [tag],
|
|
@@ -168,6 +225,7 @@ export class Swagger implements Action {
|
|
|
168
225
|
],
|
|
169
226
|
paths,
|
|
170
227
|
components,
|
|
228
|
+
security: [{ sessionCookie: [] }],
|
|
171
229
|
};
|
|
172
230
|
return document;
|
|
173
231
|
}
|
package/classes/Action.ts
CHANGED