From ca6156fe718fc09ba05b085ce60b8bc05f828fcc Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Fri, 16 Sep 2022 21:50:05 +0200 Subject: [PATCH] WIP: Add additional handling of endpoints with v2 options --- packages/backend/src/server/api/endpoints.ts | 18 ++++++++++++++++++ packages/backend/src/server/api/index.ts | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/packages/backend/src/server/api/endpoints.ts b/packages/backend/src/server/api/endpoints.ts index 5a70d1a4f..b03c9fbf8 100644 --- a/packages/backend/src/server/api/endpoints.ts +++ b/packages/backend/src/server/api/endpoints.ts @@ -703,6 +703,24 @@ export interface IEndpointMeta { * 正常応答をキャッシュ (Cache-Control: public) する秒数 */ readonly cacheSec?: number; + + /** + * API v2 options + */ + readonly v2?: { + + /** + * HTTP verb this endpoint supports + */ + readonly method: string; + + /** + * Path alias for v2 endpoint + * + * @example (v0) /api/notes/create -> /api/v2/notes + */ + readonly alias?: string; + }; } export interface IEndpoint { diff --git a/packages/backend/src/server/api/index.ts b/packages/backend/src/server/api/index.ts index 83ece51f5..f27c082e1 100644 --- a/packages/backend/src/server/api/index.ts +++ b/packages/backend/src/server/api/index.ts @@ -74,6 +74,11 @@ for (const endpoint of endpoints) { } else { router.get(`/${endpoint.name}`, async ctx => { ctx.status = 405; }); } + + if (endpoint.meta.v2) { + const path = endpoint.meta.v2.alias ?? endpoint.name.replace(/-/g, '_'); + router[endpoint.meta.v2.method](`/v2/${path}`, handler.bind(null, endpoint)); + } } }