@sepo27/servicio 0.0.0-stage → 0.2.4
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/CHANGELOG.md +41 -0
- package/README.md +42 -2
- package/dist/spec/index.d.ts +1 -0
- package/dist/spec/index.js +1 -0
- package/dist/spec/mocks/expressRequestMock.d.ts +2 -0
- package/dist/spec/mocks/expressRequestMock.js +2 -0
- package/dist/spec/mocks/expressResponseMock.d.ts +8 -0
- package/dist/spec/mocks/expressResponseMock.js +5 -0
- package/dist/spec/mocks/index.d.ts +2 -0
- package/dist/spec/mocks/index.js +2 -0
- package/dist/src/Servicio.d.ts +8 -0
- package/dist/src/Servicio.js +50 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +3 -0
- package/dist/src/lib/common/isType.d.ts +2 -0
- package/dist/src/lib/common/isType.js +2 -0
- package/dist/src/lib/common/string.d.ts +1 -0
- package/dist/src/lib/common/string.js +1 -0
- package/dist/src/lib/common/types.d.ts +4 -0
- package/dist/src/lib/common/types.js +1 -0
- package/dist/src/lib/express/types.d.ts +28 -0
- package/dist/src/lib/express/types.js +38 -0
- package/dist/src/lib/http-meta/HttpMethod.d.ts +29 -0
- package/dist/src/lib/http-meta/HttpMethod.js +33 -0
- package/dist/src/lib/http-meta/index.d.ts +5 -0
- package/dist/src/lib/http-meta/index.js +4 -0
- package/dist/src/lib/http-meta/response/HttpResponse.d.ts +7 -0
- package/dist/src/lib/http-meta/response/HttpResponse.js +1 -0
- package/dist/src/lib/http-meta/response/SuccessResponse.d.ts +11 -0
- package/dist/src/lib/http-meta/response/SuccessResponse.js +29 -0
- package/dist/src/lib/http-meta/response/errors/HttpError.d.ts +15 -0
- package/dist/src/lib/http-meta/response/errors/HttpError.js +20 -0
- package/dist/src/lib/http-meta/response/errors/errors.d.ts +122 -0
- package/dist/src/lib/http-meta/response/errors/errors.js +243 -0
- package/dist/src/lib/http-meta/response/errors/index.d.ts +2 -0
- package/dist/src/lib/http-meta/response/errors/index.js +2 -0
- package/dist/src/lib/http-meta/response/httpStatus.d.ts +83 -0
- package/dist/src/lib/http-meta/response/httpStatus.js +94 -0
- package/dist/src/lib/http-meta/response/types.d.ts +3 -0
- package/dist/src/lib/http-meta/response/types.js +1 -0
- package/dist/src/main/Route.d.ts +9 -0
- package/dist/src/main/Route.js +21 -0
- package/dist/src/main/Route.spec.d.ts +1 -0
- package/dist/src/main/Route.spec.js +26 -0
- package/dist/src/main/ServicioApp.d.ts +10 -0
- package/dist/src/main/ServicioApp.js +39 -0
- package/dist/src/main/ServicioApp.spec.d.ts +1 -0
- package/dist/src/main/ServicioApp.spec.js +85 -0
- package/dist/src/main/index.d.ts +1 -0
- package/dist/src/main/index.js +2 -0
- package/dist/src/main/servicio.d.ts +2 -0
- package/dist/src/main/servicio.js +41 -0
- package/dist/src/main/servicio.spec.d.ts +1 -0
- package/dist/src/main/servicio.spec.js +71 -0
- package/dist/src/main/types.d.ts +13 -0
- package/dist/src/main/types.js +1 -0
- package/package.json +54 -3
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { ServicioApp } from './ServicioApp.js';
|
|
3
|
+
export const servicio = () => {
|
|
4
|
+
const expressApp = express();
|
|
5
|
+
const servicio = new ServicioApp(expressApp);
|
|
6
|
+
return makeServicioAppProxy(expressApp, servicio);
|
|
7
|
+
};
|
|
8
|
+
/*** Private ***/
|
|
9
|
+
function makeServicioAppProxy(expressApp, servicioApp) {
|
|
10
|
+
const expressRecord = expressApp;
|
|
11
|
+
const servicioRecord = servicioApp;
|
|
12
|
+
return new Proxy(expressApp, {
|
|
13
|
+
get(_, prop) {
|
|
14
|
+
if (prop in servicioRecord) {
|
|
15
|
+
const value = servicioRecord[prop];
|
|
16
|
+
// Bind methods to the target instance so they have access to `this`
|
|
17
|
+
if (typeof value === 'function') {
|
|
18
|
+
return value.bind(servicioApp);
|
|
19
|
+
}
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
// Otherwise, pass through to the Express application
|
|
23
|
+
const value = expressRecord[prop];
|
|
24
|
+
return typeof value === 'function' ? value.bind(expressApp) : value;
|
|
25
|
+
},
|
|
26
|
+
set(_, prop, value) {
|
|
27
|
+
// If the property exists on the ServicioApp instance, set it there
|
|
28
|
+
if (prop in servicioRecord) {
|
|
29
|
+
servicioRecord[prop] = value;
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
// Otherwise, pass through to the Express application
|
|
33
|
+
expressRecord[prop] = value;
|
|
34
|
+
return true;
|
|
35
|
+
},
|
|
36
|
+
has(_, prop) {
|
|
37
|
+
// Check both the Servicio instance and the Express application
|
|
38
|
+
return prop in servicioRecord || prop in expressRecord;
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import supertest from 'supertest';
|
|
11
|
+
import { servicio } from './servicio.js';
|
|
12
|
+
import { Route } from './Route.js';
|
|
13
|
+
import { BadRequestError, HttpStatus, SuccessResponse } from '../lib/http-meta/index.js';
|
|
14
|
+
describe('servicio', () => {
|
|
15
|
+
let app, agent;
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
app = servicio();
|
|
18
|
+
agent = supertest.agent(app);
|
|
19
|
+
});
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
jest.resetAllMocks();
|
|
22
|
+
});
|
|
23
|
+
it('handles simple express like native route', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
app.get('/foo', (req, res) => {
|
|
25
|
+
res.status(200).send();
|
|
26
|
+
});
|
|
27
|
+
yield agent.get('/foo').expect(200);
|
|
28
|
+
}));
|
|
29
|
+
it('handles simple route', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
const response = new SuccessResponse({
|
|
31
|
+
status: HttpStatus.OK,
|
|
32
|
+
body: {
|
|
33
|
+
mary: 'Had a little lamb',
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
const route = new Route('GET /abc', () => response);
|
|
37
|
+
app.addRoute(route);
|
|
38
|
+
yield agent
|
|
39
|
+
.get('/abc')
|
|
40
|
+
.expect(HttpStatus.OK)
|
|
41
|
+
.expect(res => {
|
|
42
|
+
expect(res.body).toEqual(response.body);
|
|
43
|
+
});
|
|
44
|
+
}));
|
|
45
|
+
it('handles simple response error', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
+
const error = new BadRequestError(), route = new Route('GET /xyz', () => {
|
|
47
|
+
throw error;
|
|
48
|
+
});
|
|
49
|
+
app.addRoute(route);
|
|
50
|
+
yield agent.get('/xyz').expect(error.status);
|
|
51
|
+
}));
|
|
52
|
+
xit('handles response error with body', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const error = new BadRequestError({
|
|
54
|
+
body: { message: 'Test error occurred' },
|
|
55
|
+
}), route = new Route('GET /baz', () => {
|
|
56
|
+
throw error;
|
|
57
|
+
});
|
|
58
|
+
app.addRoute(route);
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
app.use((err, req, res, next) => {
|
|
61
|
+
console.log('=== err', err);
|
|
62
|
+
return next(err);
|
|
63
|
+
});
|
|
64
|
+
yield agent
|
|
65
|
+
.get('/baz')
|
|
66
|
+
.expect(error.status)
|
|
67
|
+
.expect(res => {
|
|
68
|
+
expect(res.body).toEqual(error.body);
|
|
69
|
+
});
|
|
70
|
+
}));
|
|
71
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import { HttpMethod, HttpResponse } from '../lib/http-meta/index.js';
|
|
3
|
+
import { ExpressPathParams } from '../lib/express/types.js';
|
|
4
|
+
import { ServicioApp } from './ServicioApp.js';
|
|
5
|
+
export type Servicio = ServicioApp & express.Express;
|
|
6
|
+
export interface Request extends express.Request {
|
|
7
|
+
}
|
|
8
|
+
export type RouteEndpointStruct = {
|
|
9
|
+
method: HttpMethod;
|
|
10
|
+
path: ExpressPathParams;
|
|
11
|
+
};
|
|
12
|
+
export type RouteEndpoint = string | RouteEndpointStruct;
|
|
13
|
+
export type RouteHandler = (req: Request) => HttpResponse | Promise<HttpResponse>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sepo27/servicio",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Backend Service Frame",
|
|
4
|
+
"version": "0.2.4",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/src/index.js",
|
|
7
|
+
"types": "./dist/src/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/",
|
|
10
|
+
"README.md",
|
|
11
|
+
"CHANGELOG.md"
|
|
12
|
+
],
|
|
13
|
+
"devEngines": {
|
|
14
|
+
"packageManager": {
|
|
15
|
+
"name": "pnpm",
|
|
16
|
+
"version": "12.18.0",
|
|
17
|
+
"onFail": "download"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@commitlint/cli": "^21.2.4",
|
|
22
|
+
"@commitlint/config-conventional": "^21.2.4",
|
|
23
|
+
"@eslint/js": "^10.0.1",
|
|
24
|
+
"@types/express": "^5.0.6",
|
|
25
|
+
"@types/jest": "^30.0.0",
|
|
26
|
+
"@types/node": "^26.6.6",
|
|
27
|
+
"@types/supertest": "^7.2.1",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "^8.72.1",
|
|
29
|
+
"@typescript-eslint/parser": "^8.72.1",
|
|
30
|
+
"conventional-changelog": "^8.1.3",
|
|
31
|
+
"eslint": "^10.14.0",
|
|
32
|
+
"globals": "^17.14.0",
|
|
33
|
+
"husky": "^9.1.7",
|
|
34
|
+
"jest": "^30.5.2",
|
|
35
|
+
"jest-extended": "^7.0.0",
|
|
36
|
+
"standard-version": "^9.5.0",
|
|
37
|
+
"supertest": "^7.3.2",
|
|
38
|
+
"ts-jest": "^29.4.15",
|
|
39
|
+
"tsx": "^4.23.15",
|
|
40
|
+
"typescript": "^6.0.3"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"express": "^5.3.0"
|
|
44
|
+
},
|
|
45
|
+
"keywords": [],
|
|
46
|
+
"author": "Sergey Poskachey",
|
|
47
|
+
"license": "ISC",
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsc -p ./tsconfig.build.json",
|
|
50
|
+
"lint": "eslint ./src",
|
|
51
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
52
|
+
"test:base": "jest -c ./jest.config.js",
|
|
53
|
+
"test": "pnpm test:base ./src",
|
|
54
|
+
"server.sand": "tsx .sandbox/server",
|
|
55
|
+
"version": "conventional-changelog -i CHANGELOG.md -s -n .versionrc.js && git add CHANGELOG.md"
|
|
56
|
+
}
|
|
6
57
|
}
|