@aeriajs/http 0.0.30 → 0.0.32
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/dist/constants.mjs +2 -1
- package/dist/cors.mjs +27 -73
- package/dist/index.mjs +1 -0
- package/dist/options.mjs +10 -6
- package/dist/payload.mjs +11 -10
- package/dist/routing.mjs +185 -473
- package/package.json +6 -6
package/dist/constants.mjs
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
export const DEFAULT_BASE_URI = "/api";
|
package/dist/cors.mjs
CHANGED
|
@@ -1,74 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (_d) throw _e;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return _arr;
|
|
32
|
-
}
|
|
33
|
-
function _non_iterable_rest() {
|
|
34
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
35
|
-
}
|
|
36
|
-
function _sliced_to_array(arr, i) {
|
|
37
|
-
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
38
|
-
}
|
|
39
|
-
function _unsupported_iterable_to_array(o, minLen) {
|
|
40
|
-
if (!o) return;
|
|
41
|
-
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
42
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
43
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
44
|
-
if (n === "Map" || n === "Set") return Array.from(n);
|
|
45
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
46
|
-
}
|
|
47
|
-
export var cors = function(req, res) {
|
|
48
|
-
var allowedHeaders = [
|
|
49
|
-
"Accept",
|
|
50
|
-
"Accept-Version",
|
|
51
|
-
"Authorization",
|
|
52
|
-
"Content-Length",
|
|
53
|
-
"Content-MD5",
|
|
54
|
-
"Content-Type",
|
|
55
|
-
"Date",
|
|
56
|
-
"X-Api-Version",
|
|
57
|
-
"X-Stream-Request"
|
|
58
|
-
];
|
|
59
|
-
var headers = {
|
|
60
|
-
"Access-Control-Allow-Origin": "*",
|
|
61
|
-
"Access-Control-Allow-Methods": "*",
|
|
62
|
-
"Access-Control-Allow-Headers": allowedHeaders.join(","),
|
|
63
|
-
"Access-Control-Max-Age": "2592000"
|
|
64
|
-
};
|
|
65
|
-
if (req.method === "OPTIONS") {
|
|
66
|
-
res.writeHead(204, headers);
|
|
67
|
-
res.end();
|
|
68
|
-
return null;
|
|
69
|
-
}
|
|
70
|
-
Object.entries(headers).forEach(function(param) {
|
|
71
|
-
var _param = _sliced_to_array(param, 2), key = _param[0], value = _param[1];
|
|
72
|
-
res.setHeader(key, value);
|
|
73
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
export const cors = (req, res) => {
|
|
3
|
+
const allowedHeaders = [
|
|
4
|
+
"Accept",
|
|
5
|
+
"Accept-Version",
|
|
6
|
+
"Authorization",
|
|
7
|
+
"Content-Length",
|
|
8
|
+
"Content-MD5",
|
|
9
|
+
"Content-Type",
|
|
10
|
+
"Date",
|
|
11
|
+
"X-Api-Version",
|
|
12
|
+
"X-Stream-Request"
|
|
13
|
+
];
|
|
14
|
+
const headers = {
|
|
15
|
+
"Access-Control-Allow-Origin": "*",
|
|
16
|
+
"Access-Control-Allow-Methods": "*",
|
|
17
|
+
"Access-Control-Allow-Headers": allowedHeaders.join(","),
|
|
18
|
+
"Access-Control-Max-Age": "2592000"
|
|
19
|
+
};
|
|
20
|
+
if (req.method === "OPTIONS") {
|
|
21
|
+
res.writeHead(204, headers);
|
|
22
|
+
res.end();
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
26
|
+
res.setHeader(key, value);
|
|
27
|
+
});
|
|
74
28
|
};
|
package/dist/index.mjs
CHANGED
package/dist/options.mjs
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
export const defineServerOptions = (options) => {
|
|
3
|
+
const {
|
|
4
|
+
host = "0.0.0.0",
|
|
5
|
+
port = 3e3
|
|
6
|
+
} = options || {};
|
|
7
|
+
return {
|
|
8
|
+
host,
|
|
9
|
+
port
|
|
10
|
+
};
|
|
7
11
|
};
|
package/dist/payload.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
export const safeJson = (candidate) => {
|
|
3
|
+
if (!candidate || typeof candidate !== "string") {
|
|
4
|
+
return candidate;
|
|
5
|
+
}
|
|
6
|
+
const json = JSON.parse(candidate);
|
|
7
|
+
if (json && typeof json === "object") {
|
|
8
|
+
delete json.constructor;
|
|
9
|
+
delete json.__proto__;
|
|
10
|
+
}
|
|
11
|
+
return json;
|
|
11
12
|
};
|
package/dist/routing.mjs
CHANGED
|
@@ -1,491 +1,203 @@
|
|
|
1
|
-
|
|
2
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
-
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
-
return arr2;
|
|
5
|
-
}
|
|
6
|
-
function _array_without_holes(arr) {
|
|
7
|
-
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
|
8
|
-
}
|
|
9
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
10
|
-
try {
|
|
11
|
-
var info = gen[key](arg);
|
|
12
|
-
var value = info.value;
|
|
13
|
-
} catch (error) {
|
|
14
|
-
reject(error);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
if (info.done) {
|
|
18
|
-
resolve(value);
|
|
19
|
-
} else {
|
|
20
|
-
Promise.resolve(value).then(_next, _throw);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function _async_to_generator(fn) {
|
|
24
|
-
return function() {
|
|
25
|
-
var self = this, args = arguments;
|
|
26
|
-
return new Promise(function(resolve, reject) {
|
|
27
|
-
var gen = fn.apply(self, args);
|
|
28
|
-
function _next(value) {
|
|
29
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
30
|
-
}
|
|
31
|
-
function _throw(err) {
|
|
32
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
33
|
-
}
|
|
34
|
-
_next(undefined);
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
function _instanceof(left, right) {
|
|
39
|
-
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
40
|
-
return !!right[Symbol.hasInstance](left);
|
|
41
|
-
} else {
|
|
42
|
-
return left instanceof right;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
function _iterable_to_array(iter) {
|
|
46
|
-
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
|
47
|
-
}
|
|
48
|
-
function _non_iterable_spread() {
|
|
49
|
-
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
50
|
-
}
|
|
51
|
-
function _to_consumable_array(arr) {
|
|
52
|
-
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
|
53
|
-
}
|
|
54
|
-
function _unsupported_iterable_to_array(o, minLen) {
|
|
55
|
-
if (!o) return;
|
|
56
|
-
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
57
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
58
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
59
|
-
if (n === "Map" || n === "Set") return Array.from(n);
|
|
60
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
61
|
-
}
|
|
62
|
-
function _ts_generator(thisArg, body) {
|
|
63
|
-
var f, y, t, g, _ = {
|
|
64
|
-
label: 0,
|
|
65
|
-
sent: function() {
|
|
66
|
-
if (t[0] & 1) throw t[1];
|
|
67
|
-
return t[1];
|
|
68
|
-
},
|
|
69
|
-
trys: [],
|
|
70
|
-
ops: []
|
|
71
|
-
};
|
|
72
|
-
return g = {
|
|
73
|
-
next: verb(0),
|
|
74
|
-
"throw": verb(1),
|
|
75
|
-
"return": verb(2)
|
|
76
|
-
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
77
|
-
return this;
|
|
78
|
-
}), g;
|
|
79
|
-
function verb(n) {
|
|
80
|
-
return function(v) {
|
|
81
|
-
return step([
|
|
82
|
-
n,
|
|
83
|
-
v
|
|
84
|
-
]);
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
function step(op) {
|
|
88
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
89
|
-
while(_)try {
|
|
90
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
91
|
-
if (y = 0, t) op = [
|
|
92
|
-
op[0] & 2,
|
|
93
|
-
t.value
|
|
94
|
-
];
|
|
95
|
-
switch(op[0]){
|
|
96
|
-
case 0:
|
|
97
|
-
case 1:
|
|
98
|
-
t = op;
|
|
99
|
-
break;
|
|
100
|
-
case 4:
|
|
101
|
-
_.label++;
|
|
102
|
-
return {
|
|
103
|
-
value: op[1],
|
|
104
|
-
done: false
|
|
105
|
-
};
|
|
106
|
-
case 5:
|
|
107
|
-
_.label++;
|
|
108
|
-
y = op[1];
|
|
109
|
-
op = [
|
|
110
|
-
0
|
|
111
|
-
];
|
|
112
|
-
continue;
|
|
113
|
-
case 7:
|
|
114
|
-
op = _.ops.pop();
|
|
115
|
-
_.trys.pop();
|
|
116
|
-
continue;
|
|
117
|
-
default:
|
|
118
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
119
|
-
_ = 0;
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
123
|
-
_.label = op[1];
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
127
|
-
_.label = t[1];
|
|
128
|
-
t = op;
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
if (t && _.label < t[2]) {
|
|
132
|
-
_.label = t[2];
|
|
133
|
-
_.ops.push(op);
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
if (t[2]) _.ops.pop();
|
|
137
|
-
_.trys.pop();
|
|
138
|
-
continue;
|
|
139
|
-
}
|
|
140
|
-
op = body.call(thisArg, _);
|
|
141
|
-
} catch (e) {
|
|
142
|
-
op = [
|
|
143
|
-
6,
|
|
144
|
-
e
|
|
145
|
-
];
|
|
146
|
-
y = 0;
|
|
147
|
-
} finally{
|
|
148
|
-
f = t = 0;
|
|
149
|
-
}
|
|
150
|
-
if (op[0] & 5) throw op[1];
|
|
151
|
-
return {
|
|
152
|
-
value: op[0] ? op[1] : void 0,
|
|
153
|
-
done: true
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
}
|
|
1
|
+
"use strict";
|
|
157
2
|
import { Stream } from "stream";
|
|
158
3
|
import { ACErrors, REQUEST_METHODS } from "@aeriajs/types";
|
|
159
4
|
import { pipe, arraysIntersects, left, isLeft, unwrapEither, deepMerge } from "@aeriajs/common";
|
|
160
5
|
import { validate } from "@aeriajs/validation";
|
|
161
6
|
import { safeJson } from "./payload.mjs";
|
|
162
7
|
import { DEFAULT_BASE_URI } from "./constants.mjs";
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
});
|
|
168
|
-
return validationEither;
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
var unsufficientRoles = function(context) {
|
|
172
|
-
context.response.writeHead(403, {
|
|
173
|
-
"content-type": "application/json"
|
|
8
|
+
const checkUnprocessable = (validationEither, context) => {
|
|
9
|
+
if (isLeft(validationEither)) {
|
|
10
|
+
context.response.writeHead(422, {
|
|
11
|
+
"content-type": "application/json"
|
|
174
12
|
});
|
|
13
|
+
return validationEither;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const unsufficientRoles = (context) => {
|
|
17
|
+
context.response.writeHead(403, {
|
|
18
|
+
"content-type": "application/json"
|
|
19
|
+
});
|
|
20
|
+
return {
|
|
21
|
+
error: ACErrors.AuthorizationError
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export const matches = (req, method, exp, options) => {
|
|
25
|
+
const { url } = req;
|
|
26
|
+
const { base = DEFAULT_BASE_URI } = options;
|
|
27
|
+
if (method && method !== req.method) {
|
|
28
|
+
if (!Array.isArray(method) || !method.includes(req.method)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const regexp = exp instanceof RegExp ? exp : new RegExp(`^${base}${exp}$`);
|
|
33
|
+
const matches2 = url.split("?")[0].match(regexp);
|
|
34
|
+
if (matches2) {
|
|
35
|
+
const fragments = matches2.splice(1);
|
|
175
36
|
return {
|
|
176
|
-
|
|
37
|
+
fragments
|
|
177
38
|
};
|
|
39
|
+
}
|
|
178
40
|
};
|
|
179
|
-
export
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
if (
|
|
183
|
-
|
|
184
|
-
|
|
41
|
+
export const registerRoute = async (context, method, exp, cb, contract, options = {}) => {
|
|
42
|
+
const match = matches(context.request, method, exp, options);
|
|
43
|
+
if (match) {
|
|
44
|
+
if (context.request.headers["content-type"] === "application/json") {
|
|
45
|
+
try {
|
|
46
|
+
context.request.payload = deepMerge(
|
|
47
|
+
safeJson(context.request.body),
|
|
48
|
+
context.request.payload || {},
|
|
49
|
+
{
|
|
50
|
+
arrays: false
|
|
51
|
+
}
|
|
52
|
+
);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
context.response.writeHead(500);
|
|
55
|
+
context.response.end(left({
|
|
56
|
+
httpCode: 500,
|
|
57
|
+
message: "Invalid JSON"
|
|
58
|
+
}));
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
Object.assign(context.request, match);
|
|
63
|
+
if (contract) {
|
|
64
|
+
if (contract.roles) {
|
|
65
|
+
if (!context.token.authenticated) {
|
|
66
|
+
if (!contract.roles.includes("guest")) {
|
|
67
|
+
return unsufficientRoles(context);
|
|
68
|
+
}
|
|
69
|
+
} else if (!arraysIntersects(context.token.roles, contract.roles)) {
|
|
70
|
+
return unsufficientRoles(context);
|
|
185
71
|
}
|
|
72
|
+
}
|
|
73
|
+
if ("payload" in contract && contract.payload) {
|
|
74
|
+
const validationEither = validate(context.request.payload, contract.payload);
|
|
75
|
+
const error = checkUnprocessable(validationEither, context);
|
|
76
|
+
if (error) {
|
|
77
|
+
return error;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if ("query" in contract && contract.query) {
|
|
81
|
+
const validationEither = validate(context.request.query, contract.query, {
|
|
82
|
+
coerce: true
|
|
83
|
+
});
|
|
84
|
+
const error = checkUnprocessable(validationEither, context);
|
|
85
|
+
if (error) {
|
|
86
|
+
return error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const result = await cb(context);
|
|
91
|
+
return result === void 0 ? null : result;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
export const wrapRouteExecution = async (res, cb) => {
|
|
95
|
+
try {
|
|
96
|
+
const result = await cb();
|
|
97
|
+
if (result === null) {
|
|
98
|
+
if (!res.headersSent) {
|
|
99
|
+
res.writeHead(204);
|
|
100
|
+
res.end();
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
186
103
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
fragments: fragments
|
|
193
|
-
};
|
|
104
|
+
if (!res.headersSent && result && isLeft(result)) {
|
|
105
|
+
const error = unwrapEither(result);
|
|
106
|
+
if (error.httpCode) {
|
|
107
|
+
res.writeHead(error.httpCode);
|
|
108
|
+
}
|
|
194
109
|
}
|
|
110
|
+
if (result instanceof Stream) {
|
|
111
|
+
try {
|
|
112
|
+
result.pipe(res);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
}
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (!res.writableEnded) {
|
|
118
|
+
res.end(result);
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
} catch (e) {
|
|
122
|
+
console.trace(e);
|
|
123
|
+
if (!res.headersSent) {
|
|
124
|
+
res.writeHead(500);
|
|
125
|
+
}
|
|
126
|
+
if (!res.writableEnded) {
|
|
127
|
+
const error = left({
|
|
128
|
+
httpCode: 500,
|
|
129
|
+
message: "Internal server error"
|
|
130
|
+
});
|
|
131
|
+
res.end(error);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
195
134
|
};
|
|
196
|
-
export
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
});
|
|
214
|
-
} catch (err) {
|
|
215
|
-
context.response.writeHead(500);
|
|
216
|
-
context.response.end(left({
|
|
217
|
-
httpCode: 500,
|
|
218
|
-
message: "Invalid JSON"
|
|
219
|
-
}));
|
|
220
|
-
return [
|
|
221
|
-
2,
|
|
222
|
-
null
|
|
223
|
-
];
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
Object.assign(context.request, match);
|
|
227
|
-
if (contract) {
|
|
228
|
-
if (contract.roles) {
|
|
229
|
-
if (!context.token.authenticated) {
|
|
230
|
-
if (!contract.roles.includes("guest")) {
|
|
231
|
-
return [
|
|
232
|
-
2,
|
|
233
|
-
unsufficientRoles(context)
|
|
234
|
-
];
|
|
235
|
-
}
|
|
236
|
-
} else if (!arraysIntersects(context.token.roles, contract.roles)) {
|
|
237
|
-
return [
|
|
238
|
-
2,
|
|
239
|
-
unsufficientRoles(context)
|
|
240
|
-
];
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
if ("payload" in contract && contract.payload) {
|
|
244
|
-
validationEither = validate(context.request.payload, contract.payload);
|
|
245
|
-
error = checkUnprocessable(validationEither, context);
|
|
246
|
-
if (error) {
|
|
247
|
-
return [
|
|
248
|
-
2,
|
|
249
|
-
error
|
|
250
|
-
];
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
if ("query" in contract && contract.query) {
|
|
254
|
-
validationEither1 = validate(context.request.query, contract.query, {
|
|
255
|
-
coerce: true
|
|
256
|
-
});
|
|
257
|
-
error1 = checkUnprocessable(validationEither1, context);
|
|
258
|
-
if (error1) {
|
|
259
|
-
return [
|
|
260
|
-
2,
|
|
261
|
-
error1
|
|
262
|
-
];
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return [
|
|
267
|
-
4,
|
|
268
|
-
cb(context)
|
|
269
|
-
];
|
|
270
|
-
case 1:
|
|
271
|
-
result = _state.sent();
|
|
272
|
-
return [
|
|
273
|
-
2,
|
|
274
|
-
result === undefined ? null : result
|
|
275
|
-
];
|
|
276
|
-
case 2:
|
|
277
|
-
return [
|
|
278
|
-
2
|
|
279
|
-
];
|
|
280
|
-
}
|
|
281
|
-
});
|
|
135
|
+
export const createRouter = (options = {}) => {
|
|
136
|
+
const { exhaust } = options;
|
|
137
|
+
options.base ??= DEFAULT_BASE_URI;
|
|
138
|
+
const routes = [];
|
|
139
|
+
const routesMeta = {};
|
|
140
|
+
const route = (method, exp, cb, contract) => {
|
|
141
|
+
routesMeta[exp] ??= {};
|
|
142
|
+
routesMeta[exp][Array.isArray(method) ? method[0] : method] = contract || null;
|
|
143
|
+
routes.push((_, context, groupOptions) => {
|
|
144
|
+
return registerRoute(
|
|
145
|
+
context,
|
|
146
|
+
method,
|
|
147
|
+
exp,
|
|
148
|
+
cb,
|
|
149
|
+
contract,
|
|
150
|
+
groupOptions || options
|
|
151
|
+
);
|
|
282
152
|
});
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
};
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
]);
|
|
299
|
-
return [
|
|
300
|
-
4,
|
|
301
|
-
cb()
|
|
302
|
-
];
|
|
303
|
-
case 1:
|
|
304
|
-
result = _state.sent();
|
|
305
|
-
if (result === null) {
|
|
306
|
-
if (!res.headersSent) {
|
|
307
|
-
res.writeHead(204);
|
|
308
|
-
res.end();
|
|
309
|
-
}
|
|
310
|
-
return [
|
|
311
|
-
2
|
|
312
|
-
];
|
|
313
|
-
}
|
|
314
|
-
if (!res.headersSent && result && isLeft(result)) {
|
|
315
|
-
error = unwrapEither(result);
|
|
316
|
-
if (error.httpCode) {
|
|
317
|
-
res.writeHead(error.httpCode);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
if (_instanceof(result, Stream)) {
|
|
321
|
-
try {
|
|
322
|
-
result.pipe(res);
|
|
323
|
-
} catch (err) {}
|
|
324
|
-
return [
|
|
325
|
-
2
|
|
326
|
-
];
|
|
327
|
-
}
|
|
328
|
-
if (!res.writableEnded) {
|
|
329
|
-
res.end(result);
|
|
330
|
-
}
|
|
331
|
-
return [
|
|
332
|
-
2,
|
|
333
|
-
result
|
|
334
|
-
];
|
|
335
|
-
case 2:
|
|
336
|
-
e = _state.sent();
|
|
337
|
-
console.trace(e);
|
|
338
|
-
if (!res.headersSent) {
|
|
339
|
-
res.writeHead(500);
|
|
340
|
-
}
|
|
341
|
-
if (!res.writableEnded) {
|
|
342
|
-
error1 = left({
|
|
343
|
-
httpCode: 500,
|
|
344
|
-
message: "Internal server error"
|
|
345
|
-
});
|
|
346
|
-
res.end(error1);
|
|
347
|
-
}
|
|
348
|
-
return [
|
|
349
|
-
3,
|
|
350
|
-
3
|
|
351
|
-
];
|
|
352
|
-
case 3:
|
|
353
|
-
return [
|
|
354
|
-
2
|
|
355
|
-
];
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
});
|
|
359
|
-
return function wrapRouteExecution(res, cb) {
|
|
360
|
-
return _ref.apply(this, arguments);
|
|
361
|
-
};
|
|
362
|
-
}();
|
|
363
|
-
export var createRouter = function() {
|
|
364
|
-
var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
365
|
-
var _options;
|
|
366
|
-
var exhaust = options.exhaust;
|
|
367
|
-
var _base;
|
|
368
|
-
(_base = (_options = options).base) !== null && _base !== void 0 ? _base : _options.base = DEFAULT_BASE_URI;
|
|
369
|
-
var routes = [];
|
|
370
|
-
var routesMeta = {};
|
|
371
|
-
var route = function(method, exp, cb, contract) {
|
|
372
|
-
var _routesMeta, _exp;
|
|
373
|
-
var _;
|
|
374
|
-
(_ = (_routesMeta = routesMeta)[_exp = exp]) !== null && _ !== void 0 ? _ : _routesMeta[_exp] = {};
|
|
375
|
-
routesMeta[exp][Array.isArray(method) ? method[0] : method] = contract || null;
|
|
376
|
-
routes.push(function(_, context, groupOptions) {
|
|
377
|
-
return registerRoute(context, method, exp, cb, contract, groupOptions || options);
|
|
378
|
-
});
|
|
379
|
-
};
|
|
380
|
-
var group = function(exp, router, middleware) {
|
|
381
|
-
var newOptions = Object.assign({}, options);
|
|
382
|
-
for(var route in router.routesMeta){
|
|
383
|
-
routesMeta["".concat(exp).concat(route)] = router.routesMeta[route];
|
|
384
|
-
}
|
|
385
|
-
routes.push(function() {
|
|
386
|
-
var _ref = _async_to_generator(function(_, context, groupOptions) {
|
|
387
|
-
var match, result;
|
|
388
|
-
return _ts_generator(this, function(_state) {
|
|
389
|
-
switch(_state.label){
|
|
390
|
-
case 0:
|
|
391
|
-
newOptions.base = groupOptions ? "".concat(groupOptions.base).concat(exp) : "".concat(options.base).concat(exp);
|
|
392
|
-
match = matches(context.request, null, new RegExp("^".concat(newOptions.base, "/")), newOptions);
|
|
393
|
-
if (!match) return [
|
|
394
|
-
3,
|
|
395
|
-
3
|
|
396
|
-
];
|
|
397
|
-
if (!middleware) return [
|
|
398
|
-
3,
|
|
399
|
-
2
|
|
400
|
-
];
|
|
401
|
-
return [
|
|
402
|
-
4,
|
|
403
|
-
middleware(context)
|
|
404
|
-
];
|
|
405
|
-
case 1:
|
|
406
|
-
result = _state.sent();
|
|
407
|
-
if (result) {
|
|
408
|
-
return [
|
|
409
|
-
2,
|
|
410
|
-
result
|
|
411
|
-
];
|
|
412
|
-
}
|
|
413
|
-
_state.label = 2;
|
|
414
|
-
case 2:
|
|
415
|
-
return [
|
|
416
|
-
2,
|
|
417
|
-
router.install(context, newOptions)
|
|
418
|
-
];
|
|
419
|
-
case 3:
|
|
420
|
-
return [
|
|
421
|
-
2
|
|
422
|
-
];
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
});
|
|
426
|
-
return function(_, context, groupOptions) {
|
|
427
|
-
return _ref.apply(this, arguments);
|
|
428
|
-
};
|
|
429
|
-
}());
|
|
430
|
-
};
|
|
431
|
-
var routerPipe = pipe(routes, {
|
|
432
|
-
returnFirst: true
|
|
433
|
-
});
|
|
434
|
-
var router = {
|
|
435
|
-
route: route,
|
|
436
|
-
routes: routes,
|
|
437
|
-
routesMeta: routesMeta,
|
|
438
|
-
group: group,
|
|
439
|
-
install: function(_context, _options) {
|
|
440
|
-
return {};
|
|
441
|
-
}
|
|
442
|
-
};
|
|
443
|
-
router.install = function() {
|
|
444
|
-
var _ref = _async_to_generator(function(context, options) {
|
|
445
|
-
var result;
|
|
446
|
-
return _ts_generator(this, function(_state) {
|
|
447
|
-
switch(_state.label){
|
|
448
|
-
case 0:
|
|
449
|
-
return [
|
|
450
|
-
4,
|
|
451
|
-
routerPipe(undefined, context, options)
|
|
452
|
-
];
|
|
453
|
-
case 1:
|
|
454
|
-
result = _state.sent();
|
|
455
|
-
if (exhaust && result === undefined) {
|
|
456
|
-
return [
|
|
457
|
-
2,
|
|
458
|
-
left({
|
|
459
|
-
httpCode: 404,
|
|
460
|
-
message: "Not found"
|
|
461
|
-
})
|
|
462
|
-
];
|
|
463
|
-
}
|
|
464
|
-
return [
|
|
465
|
-
2,
|
|
466
|
-
result
|
|
467
|
-
];
|
|
468
|
-
}
|
|
469
|
-
});
|
|
470
|
-
});
|
|
471
|
-
return function(context, options) {
|
|
472
|
-
return _ref.apply(this, arguments);
|
|
473
|
-
};
|
|
474
|
-
}();
|
|
475
|
-
return new Proxy(router, {
|
|
476
|
-
get: function(target, key) {
|
|
477
|
-
if (REQUEST_METHODS.includes(key)) {
|
|
478
|
-
var _target;
|
|
479
|
-
return function() {
|
|
480
|
-
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){
|
|
481
|
-
args[_key] = arguments[_key];
|
|
482
|
-
}
|
|
483
|
-
return (_target = target).route.apply(_target, [
|
|
484
|
-
key
|
|
485
|
-
].concat(_to_consumable_array(args)));
|
|
486
|
-
};
|
|
487
|
-
}
|
|
488
|
-
return target[key];
|
|
153
|
+
};
|
|
154
|
+
const group = (exp, router2, middleware) => {
|
|
155
|
+
const newOptions = Object.assign({}, options);
|
|
156
|
+
for (const route2 in router2.routesMeta) {
|
|
157
|
+
routesMeta[`${exp}${route2}`] = router2.routesMeta[route2];
|
|
158
|
+
}
|
|
159
|
+
routes.push(async (_, context, groupOptions) => {
|
|
160
|
+
newOptions.base = groupOptions ? `${groupOptions.base}${exp}` : `${options.base}${exp}`;
|
|
161
|
+
const match = matches(context.request, null, new RegExp(`^${newOptions.base}/`), newOptions);
|
|
162
|
+
if (match) {
|
|
163
|
+
if (middleware) {
|
|
164
|
+
const result = await middleware(context);
|
|
165
|
+
if (result) {
|
|
166
|
+
return result;
|
|
167
|
+
}
|
|
489
168
|
}
|
|
169
|
+
return router2.install(context, newOptions);
|
|
170
|
+
}
|
|
490
171
|
});
|
|
172
|
+
};
|
|
173
|
+
const routerPipe = pipe(routes, {
|
|
174
|
+
returnFirst: true
|
|
175
|
+
});
|
|
176
|
+
const router = {
|
|
177
|
+
route,
|
|
178
|
+
routes,
|
|
179
|
+
routesMeta,
|
|
180
|
+
group,
|
|
181
|
+
install: (_context, _options) => {
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
router.install = async (context, options2) => {
|
|
186
|
+
const result = await routerPipe(void 0, context, options2);
|
|
187
|
+
if (exhaust && result === void 0) {
|
|
188
|
+
return left({
|
|
189
|
+
httpCode: 404,
|
|
190
|
+
message: "Not found"
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
};
|
|
195
|
+
return new Proxy(router, {
|
|
196
|
+
get: (target, key) => {
|
|
197
|
+
if (REQUEST_METHODS.includes(key)) {
|
|
198
|
+
return (...args) => target.route(key, ...args);
|
|
199
|
+
}
|
|
200
|
+
return target[key];
|
|
201
|
+
}
|
|
202
|
+
});
|
|
491
203
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.32",
|
|
4
4
|
"description": "## Installation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@aeriajs/access-control": "^0.0.
|
|
26
|
-
"@aeriajs/common": "^0.0.
|
|
27
|
-
"@aeriajs/types": "^0.0.
|
|
28
|
-
"@aeriajs/validation": "^0.0.
|
|
25
|
+
"@aeriajs/access-control": "^0.0.25",
|
|
26
|
+
"@aeriajs/common": "^0.0.24",
|
|
27
|
+
"@aeriajs/types": "^0.0.21",
|
|
28
|
+
"@aeriajs/validation": "^0.0.27"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
31
|
"test": "echo skipping",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"lint:fix": "eslint src --fix",
|
|
34
34
|
"build": "pnpm build:cjs && pnpm build:esm",
|
|
35
35
|
"build:cjs": "tsc",
|
|
36
|
-
"build:esm": "
|
|
36
|
+
"build:esm": "esbuild './src/**/*.ts' --outdir=dist --out-extension:.js=.mjs && pnpm build:esm-transform",
|
|
37
37
|
"build:esm-transform": "pnpm -w esm-transform $PWD/dist"
|
|
38
38
|
}
|
|
39
39
|
}
|