@fy-tools/rpc-server-expressjs 0.0.119-alpha.10381

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/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # rpc-server-express
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build rpc-server-express` to build the library.
@@ -0,0 +1,5 @@
1
+ export { App } from './lib/app';
2
+ export { Controller } from './lib/controller';
3
+ export { Route } from './lib/route';
4
+ export type { RouteToContext } from './lib/types';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Route = exports.Controller = exports.App = void 0;
4
+ var app_1 = require("./lib/app");
5
+ Object.defineProperty(exports, "App", { enumerable: true, get: function () { return app_1.App; } });
6
+ var controller_1 = require("./lib/controller");
7
+ Object.defineProperty(exports, "Controller", { enumerable: true, get: function () { return controller_1.Controller; } });
8
+ var route_1 = require("./lib/route");
9
+ Object.defineProperty(exports, "Route", { enumerable: true, get: function () { return route_1.Route; } });
@@ -0,0 +1,15 @@
1
+ import type { AnyApp, ControllerByFullPath, ControllerFullPath } from '@fy-tools/rpc-server';
2
+ import type { Application } from 'express';
3
+ import { Controller } from './controller';
4
+ export declare class App<ExpressApp extends Application = Application, Schema extends AnyApp = AnyApp> {
5
+ _app: ExpressApp;
6
+ _schema: Schema;
7
+ /**
8
+ * Controllers.
9
+ * @description Map of all controllers in the schema.
10
+ * */
11
+ C: { [key in ControllerFullPath<Schema["_controllers"][number]>]: Controller<ExpressApp, ControllerByFullPath<Schema["_controllers"][number], key>>; };
12
+ constructor(_app: ExpressApp, _schema: Schema);
13
+ build<T extends Application>(fn: (app: ExpressApp) => T): App<T, Schema>;
14
+ }
15
+ //# sourceMappingURL=app.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"app.d.ts","sourceRoot":"","sources":["../../src/lib/app.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,MAAM,EACN,oBAAoB,EACpB,kBAAkB,EACnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,qBAAa,GAAG,CAAC,UAAU,SAAS,WAAW,GAAG,WAAW,EAAE,MAAM,SAAS,MAAM,GAAG,MAAM;IAYxE,IAAI,EAAE,UAAU;IAAS,OAAO,EAAE,MAAM;IAX3D;;;SAGK;IACE,CAAC,EAAS,GACd,GAAG,IAAI,kBAAkB,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,UAAU,CACrE,UAAU,EACV,oBAAoB,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC,CAC1D,GACF,CAAC;gBAEiB,IAAI,EAAE,UAAU,EAAS,OAAO,EAAE,MAAM;IAS3D,KAAK,CAAC,CAAC,SAAS,WAAW,EAAE,EAAE,EAAE,CAAC,GAAG,EAAE,UAAU,KAAK,CAAC;CAGxD"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.App = void 0;
4
+ const controller_1 = require("./controller");
5
+ class App {
6
+ _app;
7
+ _schema;
8
+ /**
9
+ * Controllers.
10
+ * @description Map of all controllers in the schema.
11
+ * */
12
+ C = {};
13
+ constructor(_app, _schema) {
14
+ this._app = _app;
15
+ this._schema = _schema;
16
+ for (const i in _schema._controllers_map) {
17
+ this.C[i] = new controller_1.Controller(this._app, _schema._controllers[_schema._controllers_map[i]]);
18
+ }
19
+ }
20
+ build(fn) {
21
+ return new App(fn(this._app), this._schema);
22
+ }
23
+ }
24
+ exports.App = App;
@@ -0,0 +1,15 @@
1
+ import type { AnyController, RouteByFullPath, RouteFullPath } from '@fy-tools/rpc-server';
2
+ import type { Application, Router } from 'express';
3
+ import { Route } from './route';
4
+ export declare class Controller<App extends Application = Application, Schema extends AnyController = AnyController> {
5
+ _app: App;
6
+ _schema: Schema;
7
+ /**
8
+ * Requests.
9
+ * @description Map of all requests in the schema.
10
+ * */
11
+ R: { [key in RouteFullPath<Schema["_routes"][number]>]: Route<Router, RouteByFullPath<Schema["_routes"][number], key>>; };
12
+ constructor(_app: App, _schema: Schema);
13
+ build<T extends Application>(fn: (app: App) => T): Controller<T, Schema>;
14
+ }
15
+ //# sourceMappingURL=controller.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../../src/lib/controller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,aAAa,EACb,eAAe,EACf,aAAa,EACd,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAGnD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,qBAAa,UAAU,CACrB,GAAG,SAAS,WAAW,GAAG,WAAW,EACrC,MAAM,SAAS,aAAa,GAAG,aAAa;IAQzB,IAAI,EAAE,GAAG;IAAS,OAAO,EAAE,MAAM;IANpD;;;SAGK;IACE,CAAC,KAkBD,GAAG,iHAlBD;gBAEU,IAAI,EAAE,GAAG,EAAS,OAAO,EAAE,MAAM;IA6BpD,KAAK,CAAC,CAAC,SAAS,WAAW,EAAE,EAAE,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;CAGjD"}
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Controller = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const express_1 = tslib_1.__importDefault(require("express"));
6
+ const route_1 = require("./route");
7
+ class Controller {
8
+ _app;
9
+ _schema;
10
+ /**
11
+ * Requests.
12
+ * @description Map of all requests in the schema.
13
+ * */
14
+ R;
15
+ constructor(_app, _schema) {
16
+ this._app = _app;
17
+ this._schema = _schema;
18
+ const router = express_1.default.Router();
19
+ const routes = {};
20
+ for (const i in _schema._routes_map) {
21
+ routes[i] = new route_1.Route(router, _schema._routes[_schema._routes_map[i]]);
22
+ }
23
+ const basePath = this._schema._basePath ? `/${this._schema._basePath}` : '/';
24
+ this._app.use(basePath, router);
25
+ this.R = new Proxy(routes, {
26
+ get(target, p) {
27
+ return routes[p];
28
+ },
29
+ });
30
+ }
31
+ build(fn) {
32
+ return new Controller(fn(this._app), this._schema);
33
+ }
34
+ }
35
+ exports.Controller = Controller;
@@ -0,0 +1,11 @@
1
+ import type { AnyRoute, Response } from '@fy-tools/rpc-server';
2
+ import type { Router } from 'express';
3
+ import type { RouteToContext } from './types';
4
+ export declare class Route<App extends Router = Router, Schema extends AnyRoute = AnyRoute> {
5
+ _app: App;
6
+ _schema: Schema;
7
+ constructor(_app: App, _schema: Schema);
8
+ build<T extends Router>(fn: (app: App) => T): Route<T, Schema>;
9
+ handler(fn: (ctx: RouteToContext<Schema>) => Promise<Response<Schema>>): this;
10
+ }
11
+ //# sourceMappingURL=route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/lib/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,KAAK,EAIV,MAAM,EACP,MAAM,SAAS,CAAC;AAEjB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE9C,qBAAa,KAAK,CAChB,GAAG,SAAS,MAAM,GAAG,MAAM,EAC3B,MAAM,SAAS,QAAQ,GAAG,QAAQ;IAEf,IAAI,EAAE,GAAG;IAAS,OAAO,EAAE,MAAM;gBAAjC,IAAI,EAAE,GAAG,EAAS,OAAO,EAAE,MAAM;IAEpD,KAAK,CAAC,CAAC,SAAS,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC;IAI3C,OAAO,CAAC,EAAE,EAAE,CAAC,GAAG,EAAE,cAAc,CAAC,MAAM,CAAC,KAAK,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;CA0EvE"}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Route = void 0;
4
+ class Route {
5
+ _app;
6
+ _schema;
7
+ constructor(_app, _schema) {
8
+ this._app = _app;
9
+ this._schema = _schema;
10
+ }
11
+ build(fn) {
12
+ return new Route(fn(this._app), this._schema);
13
+ }
14
+ handler(fn) {
15
+ const path = this._schema._path ? `/${this._schema._path}` : '/';
16
+ const method = this._schema._method;
17
+ this._app[method](path, async (req, res, next) => {
18
+ try {
19
+ let body = req.body;
20
+ let params = req.params;
21
+ let query = req.query;
22
+ if (this._schema._body) {
23
+ const result = await this._schema._body['~standard'].validate(req.body);
24
+ if (result.issues) {
25
+ res
26
+ .status(400)
27
+ .json({ error: 'Validation error', issues: result.issues });
28
+ return;
29
+ }
30
+ body = result.value;
31
+ }
32
+ if (this._schema._params) {
33
+ const result = await this._schema._params['~standard'].validate(req.params);
34
+ if (result.issues) {
35
+ res
36
+ .status(400)
37
+ .json({ error: 'Validation error', issues: result.issues });
38
+ return;
39
+ }
40
+ params = result.value;
41
+ }
42
+ if (this._schema._query) {
43
+ const result = await this._schema._query['~standard'].validate(req.query);
44
+ if (result.issues) {
45
+ res
46
+ .status(400)
47
+ .json({ error: 'Validation error', issues: result.issues });
48
+ return;
49
+ }
50
+ query = result.value;
51
+ }
52
+ const response = await fn({
53
+ body,
54
+ params,
55
+ query,
56
+ req,
57
+ res,
58
+ });
59
+ if (!res.headersSent)
60
+ res.json(response);
61
+ }
62
+ catch (err) {
63
+ next(err);
64
+ }
65
+ });
66
+ return this;
67
+ }
68
+ }
69
+ exports.Route = Route;
@@ -0,0 +1,10 @@
1
+ import type { AnyRoute, Body, Params, Query } from '@fy-tools/rpc-server';
2
+ import type { Request, Response } from 'express';
3
+ export type RouteToContext<Schema extends AnyRoute> = {
4
+ body: Body<Schema>;
5
+ params: Params<Schema>;
6
+ query: Query<Schema>;
7
+ req: Request;
8
+ res: Response;
9
+ };
10
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AACzE,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,MAAM,cAAc,CAAC,MAAM,SAAS,QAAQ,IAAI;IACpD,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC;IACvB,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACrB,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@fy-tools/rpc-server-expressjs",
3
+ "version": "0.0.119-alpha.10381",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/festusyuma/fy-tools.git",
7
+ "directory": "packages/rpc-server-expressjs"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ "./package.json": "./package.json",
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "!**/*.tsbuildinfo"
23
+ ],
24
+ "dependencies": {
25
+ "@fy-tools/rpc-server": "0.0.119-alpha.10381",
26
+ "express": "^4.21.2",
27
+ "tslib": "^2.3.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/express": "^4.17.21"
31
+ },
32
+ "peerDependencies": {
33
+ "express": "^4.21.2"
34
+ }
35
+ }