@quatrain/api-server-express 1.1.6 → 1.1.7
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
-
import { ServerAdapter, ApiHandler, EndpointHandler, EndpointOptions } from '@quatrain/api';
|
|
2
|
+
import { ServerAdapter, ApiHandler, EndpointHandler, EndpointOptions, ApiMiddleware } from '@quatrain/api';
|
|
3
3
|
export declare class ExpressAdapter implements ServerAdapter {
|
|
4
4
|
private appOrRouter;
|
|
5
5
|
private config;
|
|
@@ -12,6 +12,7 @@ export declare class ExpressAdapter implements ServerAdapter {
|
|
|
12
12
|
put(path: string, handler: ApiHandler): void;
|
|
13
13
|
delete(path: string, handler: ApiHandler): void;
|
|
14
14
|
use(middleware: any): void;
|
|
15
|
+
addMiddleware(middleware: ApiMiddleware): void;
|
|
15
16
|
createRouter(path: string): ServerAdapter;
|
|
16
17
|
start(port: number, callback?: () => void): void;
|
|
17
18
|
getNativeInstance(): any;
|
|
@@ -32,7 +32,8 @@ class ExpressAdapter {
|
|
|
32
32
|
const apiReq = {
|
|
33
33
|
body: req.body,
|
|
34
34
|
params: req.params,
|
|
35
|
-
query: req.query
|
|
35
|
+
query: req.query,
|
|
36
|
+
headers: req.headers
|
|
36
37
|
};
|
|
37
38
|
const apiRes = {
|
|
38
39
|
status: (code) => {
|
|
@@ -77,6 +78,47 @@ class ExpressAdapter {
|
|
|
77
78
|
use(middleware) {
|
|
78
79
|
this.appOrRouter.use(middleware);
|
|
79
80
|
}
|
|
81
|
+
addMiddleware(middleware) {
|
|
82
|
+
const expressMiddleware = async (req, res, next) => {
|
|
83
|
+
const apiReq = {
|
|
84
|
+
body: req.body,
|
|
85
|
+
params: req.params,
|
|
86
|
+
query: req.query,
|
|
87
|
+
headers: req.headers
|
|
88
|
+
};
|
|
89
|
+
const apiRes = {
|
|
90
|
+
status: (code) => {
|
|
91
|
+
res.status(code);
|
|
92
|
+
return apiRes;
|
|
93
|
+
},
|
|
94
|
+
json: (data) => {
|
|
95
|
+
res.json(data);
|
|
96
|
+
},
|
|
97
|
+
send: (data) => {
|
|
98
|
+
res.send(data);
|
|
99
|
+
},
|
|
100
|
+
setHeader: (name, value) => {
|
|
101
|
+
res.setHeader(name, value);
|
|
102
|
+
},
|
|
103
|
+
write: (data) => {
|
|
104
|
+
res.write(data);
|
|
105
|
+
},
|
|
106
|
+
end: () => {
|
|
107
|
+
res.end();
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
try {
|
|
111
|
+
const shouldContinue = await middleware(apiReq, apiRes);
|
|
112
|
+
if (shouldContinue) {
|
|
113
|
+
next();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
next(err);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
this.appOrRouter.use(expressMiddleware);
|
|
121
|
+
}
|
|
80
122
|
createRouter(path) {
|
|
81
123
|
const router = express_1.default.Router();
|
|
82
124
|
this.appOrRouter.use(path, router);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/api-server-express",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"description": "API Server Utilities for Quatrain",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"typescript": "^5.2.2"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@quatrain/api": "^1.1.
|
|
33
|
-
"@quatrain/backend": "^1.2.
|
|
32
|
+
"@quatrain/api": "^1.1.4",
|
|
33
|
+
"@quatrain/backend": "^1.2.6",
|
|
34
34
|
"@quatrain/core": "^1.2.5",
|
|
35
35
|
"express": "^4.19.2"
|
|
36
36
|
},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import express from 'express'
|
|
2
|
-
import { ServerAdapter, ApiHandler, ApiRequest, ApiResponse, EndpointHandler, EndpointOptions } from '@quatrain/api'
|
|
2
|
+
import { ServerAdapter, ApiHandler, ApiRequest, ApiResponse, EndpointHandler, EndpointOptions, ApiMiddleware } from '@quatrain/api'
|
|
3
3
|
|
|
4
4
|
export class ExpressAdapter implements ServerAdapter {
|
|
5
5
|
constructor(
|
|
@@ -28,7 +28,8 @@ export class ExpressAdapter implements ServerAdapter {
|
|
|
28
28
|
const apiReq: ApiRequest = {
|
|
29
29
|
body: req.body,
|
|
30
30
|
params: req.params,
|
|
31
|
-
query: req.query
|
|
31
|
+
query: req.query,
|
|
32
|
+
headers: req.headers
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
const apiRes: ApiResponse = {
|
|
@@ -80,6 +81,49 @@ export class ExpressAdapter implements ServerAdapter {
|
|
|
80
81
|
(this.appOrRouter as express.Router).use(middleware)
|
|
81
82
|
}
|
|
82
83
|
|
|
84
|
+
addMiddleware(middleware: ApiMiddleware): void {
|
|
85
|
+
const expressMiddleware = async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
86
|
+
const apiReq: ApiRequest = {
|
|
87
|
+
body: req.body,
|
|
88
|
+
params: req.params,
|
|
89
|
+
query: req.query,
|
|
90
|
+
headers: req.headers as Record<string, string | string[] | undefined>
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const apiRes: ApiResponse = {
|
|
94
|
+
status: (code: number) => {
|
|
95
|
+
res.status(code)
|
|
96
|
+
return apiRes
|
|
97
|
+
},
|
|
98
|
+
json: (data: any) => {
|
|
99
|
+
res.json(data)
|
|
100
|
+
},
|
|
101
|
+
send: (data: string) => {
|
|
102
|
+
res.send(data)
|
|
103
|
+
},
|
|
104
|
+
setHeader: (name: string, value: string) => {
|
|
105
|
+
res.setHeader(name, value)
|
|
106
|
+
},
|
|
107
|
+
write: (data: string) => {
|
|
108
|
+
res.write(data)
|
|
109
|
+
},
|
|
110
|
+
end: () => {
|
|
111
|
+
res.end()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const shouldContinue = await middleware(apiReq, apiRes)
|
|
117
|
+
if (shouldContinue) {
|
|
118
|
+
next()
|
|
119
|
+
}
|
|
120
|
+
} catch (err) {
|
|
121
|
+
next(err)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
(this.appOrRouter as express.Router).use(expressMiddleware)
|
|
125
|
+
}
|
|
126
|
+
|
|
83
127
|
createRouter(path: string): ServerAdapter {
|
|
84
128
|
const router = express.Router()
|
|
85
129
|
;(this.appOrRouter as express.Router).use(path, router)
|