committed by
Fredrik Adelöw
parent
9076fab703
commit
6f4b8d9933
@@ -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<DeepWriteable<typeof spec>>(
|
||||
spec as DeepWriteable<typeof spec>,
|
||||
);
|
||||
const router = Router() as ApiRouter<DeepWriteable<typeof spec>>;
|
||||
router.use(express.json());
|
||||
|
||||
const readonlyEnabled =
|
||||
@@ -312,7 +310,7 @@ export async function createRouter(
|
||||
}
|
||||
|
||||
router.use(errorHandler());
|
||||
return router.build();
|
||||
return router;
|
||||
}
|
||||
|
||||
function getBearerToken(
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ValueOf<RequestBodySchema<Doc, Path, Method>>>
|
||||
>[number];
|
||||
export type RequestHandlerParams<
|
||||
P,
|
||||
ResBody,
|
||||
ReqBody,
|
||||
ReqQuery,
|
||||
LocalsObj extends Record<string, any>,
|
||||
> =
|
||||
| RequestHandler<P, ResBody, ReqBody, ReqQuery, LocalsObj>
|
||||
| ErrorRequestHandler<P, ResBody, ReqBody, ReqQuery, LocalsObj>;
|
||||
|
||||
type DocRequestHandler<
|
||||
Doc extends RequiredDoc,
|
||||
@@ -283,112 +291,63 @@ type DocRequestHandler<
|
||||
Record<string, string>
|
||||
>;
|
||||
|
||||
export type RequestHandlerParams<
|
||||
P = ParamsDictionary,
|
||||
ResBody = any,
|
||||
ReqBody = any,
|
||||
ReqQuery = ParsedQs,
|
||||
LocalsObj extends Record<string, any> = Record<string, any>,
|
||||
> =
|
||||
| RequestHandler<P, ResBody, ReqBody, ReqQuery, LocalsObj>
|
||||
| ErrorRequestHandler<P, ResBody, ReqBody, ReqQuery, LocalsObj>
|
||||
| Array<RequestHandler<P> | ErrorRequestHandler<P>>;
|
||||
|
||||
export class ApiRouter<Doc extends RequiredDoc> {
|
||||
private _router = Router();
|
||||
|
||||
constructor(private spec: OpenAPIV3_1.Document | OpenAPIV3.Document) {}
|
||||
|
||||
static fromSpec<Doc extends RequiredDoc>(
|
||||
spec: OpenAPIV3_1.Document | OpenAPIV3.Document,
|
||||
) {
|
||||
return new ApiRouter<Doc>(spec);
|
||||
}
|
||||
export interface ApiRouterMatcher<
|
||||
Doc extends RequiredDoc,
|
||||
Path extends DocPathTemplate<Doc>,
|
||||
Method extends DocPathMethod<Doc, Path>,
|
||||
T,
|
||||
> {
|
||||
(
|
||||
path: MethodAwareDocPath<Doc, Path, Method>,
|
||||
...handlers: Array<DocRequestHandler<Doc, Path, Method>>
|
||||
): T;
|
||||
}
|
||||
|
||||
export interface ApiRouter<Doc extends RequiredDoc> extends Router {
|
||||
get<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'get'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'get'>[]
|
||||
) {
|
||||
console.log(path, this.spec);
|
||||
this._router.get(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
): this;
|
||||
|
||||
post<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'post'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'post'>[]
|
||||
) {
|
||||
console.log(path);
|
||||
this._router.post(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
): this;
|
||||
|
||||
all<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'all'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'all'>[]
|
||||
) {
|
||||
console.log(path);
|
||||
this._router.all(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
): this;
|
||||
|
||||
put<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'put'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'put'>[]
|
||||
) {
|
||||
console.log(path);
|
||||
this._router.put(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
): this;
|
||||
delete<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'delete'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'delete'>[]
|
||||
) {
|
||||
console.log(path);
|
||||
this._router.delete(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
): this;
|
||||
patch<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'patch'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'patch'>[]
|
||||
) {
|
||||
console.log(path);
|
||||
this._router.patch(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
): this;
|
||||
options<
|
||||
Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'options'>,
|
||||
>(path: Path, ...handlers: DocRequestHandler<Doc, Path, 'options'>[]) {
|
||||
console.log(path);
|
||||
this._router.options(path, ...handlers);
|
||||
return this;
|
||||
}
|
||||
>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'options'>[]
|
||||
): this;
|
||||
head<Path extends MethodAwareDocPath<Doc, DocPathTemplate<Doc>, 'head'>>(
|
||||
path: Path,
|
||||
...handlers: DocRequestHandler<Doc, Path, 'head'>[]
|
||||
) {
|
||||
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<DeepWriteable<typeof doc>>(
|
||||
// As const forces the doc to readonly which conflicts with imported types.
|
||||
doc as DeepWriteable<typeof doc>,
|
||||
);
|
||||
const router = Router() as ApiRouter<DeepWriteable<typeof doc>>;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user