MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / defineRoute

Function defineRoute

packages/server/src/middleware/defineRoute.ts:233–314  ·  view source on GitHub ↗
(
  options: DefineRouteOptions<TBody, TParams, TQuery, TSuccessData>,
  handler: RouteDefinition<TBody, TParams, TQuery>['handler'],
)

Source from the content-addressed store, hash-verified

231 * app verb, e.g. `app.post(route.path, route.options, route.handler)`.
232 */
233export function defineRoute<
234 TBody extends z.ZodTypeAny | undefined,
235 TParams extends z.ZodTypeAny | undefined,
236 TQuery extends z.ZodTypeAny | undefined,
237 TSuccessData extends z.ZodTypeAny | undefined,
238>(
239 options: DefineRouteOptions<TBody, TParams, TQuery, TSuccessData>,
240 handler: RouteDefinition<TBody, TParams, TQuery>['handler'],
241): RouteDefinition<TBody, TParams, TQuery> {
242 // -- runtime validators ----------------------------------------------------
243 const preHandler: unknown[] = [];
244
245 if (options.params) {
246 preHandler.push(validateParams(options.params));
247 }
248 if (options.body) {
249 preHandler.push(validateBody(options.body));
250 }
251 if (options.querystring) {
252 preHandler.push(validateQuery(options.querystring));
253 }
254
255 // -- swagger schema --------------------------------------------------------
256 const schema: Record<string, unknown> = {};
257
258 if (options.body) {
259 schema['body'] = jsonSchema(options.body);
260 }
261 if (options.params) {
262 schema['params'] = jsonSchema(options.params);
263 }
264 if (options.querystring) {
265 schema['querystring'] = jsonSchema(options.querystring);
266 }
267
268 const hasResponse =
269 options.success !== undefined ||
270 (options.errors !== undefined && Object.keys(options.errors).length > 0) ||
271 options.rawResponse !== undefined;
272
273 if (hasResponse) {
274 const responses: Record<string, unknown> = {};
275
276 if (options.success || options.errors) {
277 responses['200'] = buildUnifiedResponseSchema(
278 options.success?.data ?? z.null(),
279 options.errors ?? {},
280 );
281 }
282
283 if (options.rawResponse) {
284 for (const [code, rawSchema] of Object.entries(options.rawResponse)) {
285 responses[String(code)] = rawSchema;
286 }
287 }
288
289 schema['response'] = responses;
290 }

Callers 15

registerSkillsRoutesFunction · 0.90
registerShutdownRoutesFunction · 0.90
registerGuiStoreRoutesFunction · 0.90
registerOAuthRoutesFunction · 0.90
registerPromptsRoutesFunction · 0.90
registerTerminalsRoutesFunction · 0.90
registerTasksRoutesFunction · 0.90
registerToolsRoutesFunction · 0.90
registerApprovalsRoutesFunction · 0.90
registerConfigRoutesFunction · 0.90

Calls 8

validateParamsFunction · 0.90
validateBodyFunction · 0.90
validateQueryFunction · 0.90
jsonSchemaFunction · 0.90
toFastifyPathFunction · 0.85
keysMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected