diff --git a/plugins/catalog-backend/src/service/createRouter.ts b/plugins/catalog-backend/src/service/createRouter.ts index 8b912ae6f1..5da996546d 100644 --- a/plugins/catalog-backend/src/service/createRouter.ts +++ b/plugins/catalog-backend/src/service/createRouter.ts @@ -24,7 +24,7 @@ import { } from '@backstage/catalog-model'; import { Config } from '@backstage/config'; import { NotFoundError, serializeError } from '@backstage/errors'; -import express from 'express'; +import express, { Router } from 'express'; import { Logger } from 'winston'; import yn from 'yn'; import { z } from 'zod'; @@ -86,9 +86,7 @@ export async function createRouter( logger, permissionIntegrationRouter, } = options; - const router = new ApiRouter>( - spec as DeepWriteable, - ); + const router = Router() as ApiRouter>; router.use(express.json()); const readonlyEnabled = @@ -312,7 +310,7 @@ export async function createRouter( } router.use(errorHandler()); - return router.build(); + return router; } function getBearerToken( diff --git a/plugins/openapi-router-common/package.json b/plugins/openapi-router-common/package.json index b6dbd8cb63..06a4c4e8ee 100644 --- a/plugins/openapi-router-common/package.json +++ b/plugins/openapi-router-common/package.json @@ -36,6 +36,6 @@ "openapi3-ts": "^3.1.2", "ts-node": "^10.9.1", "winston": "^3.8.2", - "yn": "^5.0.0" + "yn": "^4.0.0" } } diff --git a/plugins/openapi-router-common/src/router.ts b/plugins/openapi-router-common/src/router.ts index 3e0a08ffd6..62e116727b 100644 --- a/plugins/openapi-router-common/src/router.ts +++ b/plugins/openapi-router-common/src/router.ts @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'; import { ErrorRequestHandler, RequestHandler, Router } from 'express'; import core, { ParamsDictionary } from 'express-serve-static-core'; import { FromSchema, JSONSchema7 } from 'json-schema-to-ts'; @@ -269,6 +268,15 @@ type RequestBodyToJsonSchema< > = ConvertAll< TuplifyUnion>> >[number]; +export type RequestHandlerParams< + P, + ResBody, + ReqBody, + ReqQuery, + LocalsObj extends Record, +> = + | RequestHandler + | ErrorRequestHandler; type DocRequestHandler< Doc extends RequiredDoc, @@ -283,112 +291,63 @@ type DocRequestHandler< Record >; -export type RequestHandlerParams< - P = ParamsDictionary, - ResBody = any, - ReqBody = any, - ReqQuery = ParsedQs, - LocalsObj extends Record = Record, -> = - | RequestHandler - | ErrorRequestHandler - | Array | ErrorRequestHandler

>; - -export class ApiRouter { - private _router = Router(); - - constructor(private spec: OpenAPIV3_1.Document | OpenAPIV3.Document) {} - - static fromSpec( - spec: OpenAPIV3_1.Document | OpenAPIV3.Document, - ) { - return new ApiRouter(spec); - } +export interface ApiRouterMatcher< + Doc extends RequiredDoc, + Path extends DocPathTemplate, + Method extends DocPathMethod, + T, +> { + ( + path: MethodAwareDocPath, + ...handlers: Array> + ): T; +} +export interface ApiRouter extends Router { get, 'get'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path, this.spec); - this._router.get(path, ...handlers); - return this; - } + ): this; post, 'post'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path); - this._router.post(path, ...handlers); - return this; - } + ): this; all, 'all'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path); - this._router.all(path, ...handlers); - return this; - } + ): this; put, 'put'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path); - this._router.put(path, ...handlers); - return this; - } + ): this; delete, 'delete'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path); - this._router.delete(path, ...handlers); - return this; - } + ): this; patch, 'patch'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path); - this._router.patch(path, ...handlers); - return this; - } + ): this; options< Path extends MethodAwareDocPath, 'options'>, - >(path: Path, ...handlers: DocRequestHandler[]) { - console.log(path); - this._router.options(path, ...handlers); - return this; - } + >( + path: Path, + ...handlers: DocRequestHandler[] + ): this; head, 'head'>>( path: Path, ...handlers: DocRequestHandler[] - ) { - console.log(path); - this._router.head(path, ...handlers); - return this; - } - - use(...handlers: RequestHandlerParams[]) { - return this._router.use(...handlers); - } - - build() { - return this._router; - } + ): this; } interface RouterOptions {} export async function createRouter(options: RouterOptions) { console.log(options); - const router = ApiRouter.fromSpec>( - // As const forces the doc to readonly which conflicts with imported types. - doc as DeepWriteable, - ); + const router = Router() as ApiRouter>; router.get('/pets/:uid', (req, res) => { res.json({ @@ -400,10 +359,10 @@ export async function createRouter(options: RouterOptions) { // router.get('/pet') will complain with a TS error router.post('/pets', (req, res) => { - res.json({ + res.send({ message: req.path, code: 1, }); }); - return router.build(); + return router; } diff --git a/yarn.lock b/yarn.lock index b32c3fbf2f..9810ede5b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7458,7 +7458,7 @@ __metadata: openapi3-ts: ^3.1.2 ts-node: ^10.9.1 winston: ^3.8.2 - yn: ^5.0.0 + yn: ^4.0.0 languageName: unknown linkType: soft @@ -24966,16 +24966,16 @@ __metadata: languageName: node linkType: hard -"glob@npm:*, glob@npm:8.0.3": - version: 8.0.3 - resolution: "glob@npm:8.0.3" +"glob@npm:*, glob@npm:^8.0.0, glob@npm:^8.0.1, glob@npm:^8.0.3": + version: 8.1.0 + resolution: "glob@npm:8.1.0" dependencies: fs.realpath: ^1.0.0 inflight: ^1.0.4 inherits: 2 minimatch: ^5.0.1 once: ^1.3.0 - checksum: 50bcdea19d8e79d8de5f460b1939ffc2b3299eac28deb502093fdca22a78efebc03e66bf54f0abc3d3d07d8134d19a32850288b7440d77e072aa55f9d33b18c5 + checksum: 92fbea3221a7d12075f26f0227abac435de868dd0736a17170663783296d0dd8d3d532a5672b4488a439bf5d7fb85cdd07c11185d6cd39184f0385cbdfb86a47 languageName: node linkType: hard @@ -24993,6 +24993,19 @@ __metadata: languageName: node linkType: hard +"glob@npm:8.0.3": + version: 8.0.3 + resolution: "glob@npm:8.0.3" + dependencies: + fs.realpath: ^1.0.0 + inflight: ^1.0.4 + inherits: 2 + minimatch: ^5.0.1 + once: ^1.3.0 + checksum: 50bcdea19d8e79d8de5f460b1939ffc2b3299eac28deb502093fdca22a78efebc03e66bf54f0abc3d3d07d8134d19a32850288b7440d77e072aa55f9d33b18c5 + languageName: node + linkType: hard + "glob@npm:^7.0.0, glob@npm:^7.1.1, glob@npm:^7.1.3, glob@npm:^7.1.4, glob@npm:^7.1.6, glob@npm:^7.1.7, glob@npm:^7.2.0": version: 7.2.3 resolution: "glob@npm:7.2.3" @@ -25007,19 +25020,6 @@ __metadata: languageName: node linkType: hard -"glob@npm:^8.0.0, glob@npm:^8.0.1, glob@npm:^8.0.3": - version: 8.1.0 - resolution: "glob@npm:8.1.0" - dependencies: - fs.realpath: ^1.0.0 - inflight: ^1.0.4 - inherits: 2 - minimatch: ^5.0.1 - once: ^1.3.0 - checksum: 92fbea3221a7d12075f26f0227abac435de868dd0736a17170663783296d0dd8d3d532a5672b4488a439bf5d7fb85cdd07c11185d6cd39184f0385cbdfb86a47 - languageName: node - linkType: hard - "global-agent@npm:^3.0.0": version: 3.0.0 resolution: "global-agent@npm:3.0.0" @@ -38649,20 +38649,13 @@ __metadata: languageName: node linkType: hard -"ts-log@npm:^2.1.4": +"ts-log@npm:^2.1.4, ts-log@npm:^2.2.3": version: 2.2.5 resolution: "ts-log@npm:2.2.5" checksum: 28f78ab15b8555d56c089dbc243327d8ce4331219956242a29fc4cb3bad6bb0cb8234dd17a292381a1b1dba99a7e4849a2181b2e1a303e8247e9f4ca4e284f2d languageName: node linkType: hard -"ts-log@npm:^2.2.3": - version: 2.2.3 - resolution: "ts-log@npm:2.2.3" - checksum: 8aa34a2724d7915ddc6de8d0c27eb48f67206b52c42656f1ea2a7ffb080fa664db5fb22b42bd3c72b5b95cc6eb4612196b7d1e28f4bc146191ebc2a9683af548 - languageName: node - linkType: hard - "ts-morph@npm:^17.0.0": version: 17.0.1 resolution: "ts-morph@npm:17.0.1" @@ -40618,7 +40611,7 @@ __metadata: languageName: node linkType: hard -"yaml@npm:^2.0.0, yaml@npm:^2.1.1, yaml@npm:^2.2.1": +"yaml@npm:^2.0.0, yaml@npm:^2.1.1, yaml@npm:^2.1.3, yaml@npm:^2.2.1": version: 2.2.1 resolution: "yaml@npm:2.2.1" checksum: 84f68cbe462d5da4e7ded4a8bded949ffa912bc264472e5a684c3d45b22d8f73a3019963a32164023bdf3d83cfb6f5b58ff7b2b10ef5b717c630f40bd6369a23