@aeriajs/http 0.0.57 → 0.0.59
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/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/next.d.ts +9 -0
- package/dist/next.js +15 -0
- package/dist/next.mjs +11 -0
- package/dist/routing.d.ts +1 -1
- package/dist/routing.js +26 -25
- package/dist/routing.mjs +33 -32
- package/package.json +5 -5
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./cors.js"), exports);
|
|
18
|
+
__exportStar(require("./next.js"), exports);
|
|
18
19
|
__exportStar(require("./options.js"), exports);
|
|
19
20
|
__exportStar(require("./routing.js"), exports);
|
|
20
21
|
__exportStar(require("./payload.js"), exports);
|
package/dist/index.mjs
CHANGED
package/dist/next.d.ts
ADDED
package/dist/next.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isNext = exports.next = exports.NEXT_SYMBOL = void 0;
|
|
4
|
+
exports.NEXT_SYMBOL = Symbol('NEXT_SYMBOL');
|
|
5
|
+
const next = () => {
|
|
6
|
+
return {
|
|
7
|
+
[exports.NEXT_SYMBOL]: null,
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
exports.next = next;
|
|
11
|
+
const isNext = (object) => {
|
|
12
|
+
const sym = Object.getOwnPropertyDescriptor(object, exports.NEXT_SYMBOL);
|
|
13
|
+
return !!sym;
|
|
14
|
+
};
|
|
15
|
+
exports.isNext = isNext;
|
package/dist/next.mjs
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
export const NEXT_SYMBOL = Symbol("NEXT_SYMBOL");
|
|
3
|
+
export const next = () => {
|
|
4
|
+
return {
|
|
5
|
+
[NEXT_SYMBOL]: null
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export const isNext = (object) => {
|
|
9
|
+
const sym = Object.getOwnPropertyDescriptor(object, NEXT_SYMBOL);
|
|
10
|
+
return !!sym;
|
|
11
|
+
};
|
package/dist/routing.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export declare const matches: <TRequest extends GenericRequest>(req: TRequest, m
|
|
|
29
29
|
fragments: string[];
|
|
30
30
|
} | undefined;
|
|
31
31
|
export declare const registerRoute: (context: RouteContext, method: RequestMethod | RequestMethod[], exp: RouteUri, cb: (context: RouteContext) => any, contract?: ContractWithRoles, options?: RouterOptions) => Promise<any>;
|
|
32
|
-
export declare const wrapRouteExecution: (
|
|
32
|
+
export declare const wrapRouteExecution: (response: GenericResponse, cb: () => any | Promise<any>) => Promise<any>;
|
|
33
33
|
export declare const createRouter: (options?: Partial<RouterOptions>) => ProxiedRouter<{
|
|
34
34
|
route: <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
|
|
35
35
|
response: infer Response;
|
package/dist/routing.js
CHANGED
|
@@ -7,6 +7,7 @@ const common_1 = require("@aeriajs/common");
|
|
|
7
7
|
const validation_1 = require("@aeriajs/validation");
|
|
8
8
|
const entrypoint_1 = require("@aeriajs/entrypoint");
|
|
9
9
|
const payload_js_1 = require("./payload.js");
|
|
10
|
+
const next_js_1 = require("./next.js");
|
|
10
11
|
const checkUnprocessable = (validationEither, context) => {
|
|
11
12
|
if ((0, common_1.isLeft)(validationEither)) {
|
|
12
13
|
context.response.writeHead(422, {
|
|
@@ -60,12 +61,11 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
|
|
|
60
61
|
});
|
|
61
62
|
}
|
|
62
63
|
catch (err) {
|
|
63
|
-
context.
|
|
64
|
-
context.response.end((0, common_1.left)({
|
|
64
|
+
return context.error({
|
|
65
65
|
httpCode: 500,
|
|
66
|
+
code: 'INVALID_JSON',
|
|
66
67
|
message: 'Invalid JSON',
|
|
67
|
-
})
|
|
68
|
-
return null;
|
|
68
|
+
});
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
Object.assign(context.request, match);
|
|
@@ -100,46 +100,42 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
|
|
|
100
100
|
}
|
|
101
101
|
};
|
|
102
102
|
exports.registerRoute = registerRoute;
|
|
103
|
-
const wrapRouteExecution = async (
|
|
103
|
+
const wrapRouteExecution = async (response, cb) => {
|
|
104
104
|
try {
|
|
105
105
|
const result = await cb();
|
|
106
106
|
if (result === null) {
|
|
107
|
-
if (!
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
if (!response.headersSent) {
|
|
108
|
+
response.writeHead(204);
|
|
109
|
+
response.end();
|
|
110
110
|
}
|
|
111
111
|
return;
|
|
112
112
|
}
|
|
113
|
-
if (!res.headersSent && result && (0, common_1.isLeft)(result)) {
|
|
114
|
-
const error = (0, common_1.unwrapEither)(result);
|
|
115
|
-
if (error.httpCode) {
|
|
116
|
-
res.writeHead(error.httpCode);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
113
|
if (result instanceof stream_1.Stream) {
|
|
120
114
|
try {
|
|
121
|
-
result.pipe(
|
|
115
|
+
result.pipe(response);
|
|
122
116
|
}
|
|
123
117
|
catch (err) {
|
|
124
118
|
}
|
|
125
119
|
return;
|
|
126
120
|
}
|
|
127
|
-
if (!
|
|
128
|
-
|
|
121
|
+
if (!response.writableEnded) {
|
|
122
|
+
response.end(result);
|
|
129
123
|
}
|
|
130
124
|
return result;
|
|
131
125
|
}
|
|
132
126
|
catch (e) {
|
|
133
127
|
console.trace(e);
|
|
134
|
-
if (!
|
|
135
|
-
|
|
128
|
+
if (!response.headersSent) {
|
|
129
|
+
response.writeHead(500);
|
|
136
130
|
}
|
|
137
|
-
if (!
|
|
138
|
-
|
|
131
|
+
if (!response.writableEnded) {
|
|
132
|
+
return (0, common_1.error)({
|
|
139
133
|
httpCode: 500,
|
|
134
|
+
code: 'UNKNOWN_ERROR',
|
|
140
135
|
message: 'Internal server error',
|
|
136
|
+
}, {
|
|
137
|
+
response,
|
|
141
138
|
});
|
|
142
|
-
res.end(error);
|
|
143
139
|
}
|
|
144
140
|
}
|
|
145
141
|
};
|
|
@@ -185,7 +181,11 @@ const createRouter = (options = {}) => {
|
|
|
185
181
|
});
|
|
186
182
|
};
|
|
187
183
|
const routerPipe = (0, common_1.pipe)(routes, {
|
|
188
|
-
returnFirst:
|
|
184
|
+
returnFirst: (value) => {
|
|
185
|
+
if (value && !(0, next_js_1.isNext)(value)) {
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
},
|
|
189
189
|
});
|
|
190
190
|
const router = {
|
|
191
191
|
route,
|
|
@@ -198,9 +198,10 @@ const createRouter = (options = {}) => {
|
|
|
198
198
|
};
|
|
199
199
|
router.install = async (context, options) => {
|
|
200
200
|
const result = await routerPipe(undefined, context, options);
|
|
201
|
-
if (exhaust && result === undefined) {
|
|
202
|
-
return
|
|
201
|
+
if (exhaust && (result === undefined || (0, next_js_1.isNext)(result))) {
|
|
202
|
+
return context.error({
|
|
203
203
|
httpCode: 404,
|
|
204
|
+
code: 'NOT_FOUND',
|
|
204
205
|
message: 'Not found',
|
|
205
206
|
});
|
|
206
207
|
}
|
package/dist/routing.mjs
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { Stream } from "stream";
|
|
3
3
|
import { ACErrors, REQUEST_METHODS } from "@aeriajs/types";
|
|
4
|
-
import { pipe, isGranted,
|
|
4
|
+
import { pipe, isGranted, isLeft, deepMerge, error } from "@aeriajs/common";
|
|
5
5
|
import { validate } from "@aeriajs/validation";
|
|
6
6
|
import { getConfig } from "@aeriajs/entrypoint";
|
|
7
7
|
import { safeJson } from "./payload.mjs";
|
|
8
|
+
import { isNext } from "./next.mjs";
|
|
8
9
|
const checkUnprocessable = (validationEither, context) => {
|
|
9
10
|
if (isLeft(validationEither)) {
|
|
10
11
|
context.response.writeHead(422, {
|
|
@@ -58,12 +59,11 @@ export const registerRoute = async (context, method, exp, cb, contract, options
|
|
|
58
59
|
}
|
|
59
60
|
);
|
|
60
61
|
} catch (err) {
|
|
61
|
-
context.
|
|
62
|
-
context.response.end(left({
|
|
62
|
+
return context.error({
|
|
63
63
|
httpCode: 500,
|
|
64
|
+
code: "INVALID_JSON",
|
|
64
65
|
message: "Invalid JSON"
|
|
65
|
-
})
|
|
66
|
-
return null;
|
|
66
|
+
});
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
Object.assign(context.request, match);
|
|
@@ -76,18 +76,18 @@ export const registerRoute = async (context, method, exp, cb, contract, options
|
|
|
76
76
|
}
|
|
77
77
|
if ("payload" in contract && contract.payload) {
|
|
78
78
|
const validationEither = validate(context.request.payload, contract.payload);
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
81
|
-
return
|
|
79
|
+
const error2 = checkUnprocessable(validationEither, context);
|
|
80
|
+
if (error2) {
|
|
81
|
+
return error2;
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
if ("query" in contract && contract.query) {
|
|
85
85
|
const validationEither = validate(context.request.query, contract.query, {
|
|
86
86
|
coerce: true
|
|
87
87
|
});
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
return
|
|
88
|
+
const error2 = checkUnprocessable(validationEither, context);
|
|
89
|
+
if (error2) {
|
|
90
|
+
return error2;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
93
|
}
|
|
@@ -95,44 +95,40 @@ export const registerRoute = async (context, method, exp, cb, contract, options
|
|
|
95
95
|
return result === void 0 ? null : result;
|
|
96
96
|
}
|
|
97
97
|
};
|
|
98
|
-
export const wrapRouteExecution = async (
|
|
98
|
+
export const wrapRouteExecution = async (response, cb) => {
|
|
99
99
|
try {
|
|
100
100
|
const result = await cb();
|
|
101
101
|
if (result === null) {
|
|
102
|
-
if (!
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
if (!response.headersSent) {
|
|
103
|
+
response.writeHead(204);
|
|
104
|
+
response.end();
|
|
105
105
|
}
|
|
106
106
|
return;
|
|
107
107
|
}
|
|
108
|
-
if (!res.headersSent && result && isLeft(result)) {
|
|
109
|
-
const error = unwrapEither(result);
|
|
110
|
-
if (error.httpCode) {
|
|
111
|
-
res.writeHead(error.httpCode);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
108
|
if (result instanceof Stream) {
|
|
115
109
|
try {
|
|
116
|
-
result.pipe(
|
|
110
|
+
result.pipe(response);
|
|
117
111
|
} catch (err) {
|
|
118
112
|
}
|
|
119
113
|
return;
|
|
120
114
|
}
|
|
121
|
-
if (!
|
|
122
|
-
|
|
115
|
+
if (!response.writableEnded) {
|
|
116
|
+
response.end(result);
|
|
123
117
|
}
|
|
124
118
|
return result;
|
|
125
119
|
} catch (e) {
|
|
126
120
|
console.trace(e);
|
|
127
|
-
if (!
|
|
128
|
-
|
|
121
|
+
if (!response.headersSent) {
|
|
122
|
+
response.writeHead(500);
|
|
129
123
|
}
|
|
130
|
-
if (!
|
|
131
|
-
|
|
124
|
+
if (!response.writableEnded) {
|
|
125
|
+
return error({
|
|
132
126
|
httpCode: 500,
|
|
127
|
+
code: "UNKNOWN_ERROR",
|
|
133
128
|
message: "Internal server error"
|
|
129
|
+
}, {
|
|
130
|
+
response
|
|
134
131
|
});
|
|
135
|
-
res.end(error);
|
|
136
132
|
}
|
|
137
133
|
}
|
|
138
134
|
};
|
|
@@ -181,7 +177,11 @@ export const createRouter = (options = {}) => {
|
|
|
181
177
|
});
|
|
182
178
|
};
|
|
183
179
|
const routerPipe = pipe(routes, {
|
|
184
|
-
returnFirst:
|
|
180
|
+
returnFirst: (value) => {
|
|
181
|
+
if (value && !isNext(value)) {
|
|
182
|
+
return value;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
185
|
});
|
|
186
186
|
const router = {
|
|
187
187
|
route,
|
|
@@ -194,9 +194,10 @@ export const createRouter = (options = {}) => {
|
|
|
194
194
|
};
|
|
195
195
|
router.install = async (context, options2) => {
|
|
196
196
|
const result = await routerPipe(void 0, context, options2);
|
|
197
|
-
if (exhaust && result === void 0) {
|
|
198
|
-
return
|
|
197
|
+
if (exhaust && (result === void 0 || isNext(result))) {
|
|
198
|
+
return context.error({
|
|
199
199
|
httpCode: 404,
|
|
200
|
+
code: "NOT_FOUND",
|
|
200
201
|
message: "Not found"
|
|
201
202
|
});
|
|
202
203
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.59",
|
|
4
4
|
"description": "## Installation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -28,10 +28,10 @@
|
|
|
28
28
|
"@aeriajs/validation": "link:../validation"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"@aeriajs/common": "^0.0.
|
|
32
|
-
"@aeriajs/entrypoint": "^0.0.
|
|
33
|
-
"@aeriajs/types": "^0.0.
|
|
34
|
-
"@aeriajs/validation": "^0.0.
|
|
31
|
+
"@aeriajs/common": "^0.0.51",
|
|
32
|
+
"@aeriajs/entrypoint": "^0.0.51",
|
|
33
|
+
"@aeriajs/types": "^0.0.48",
|
|
34
|
+
"@aeriajs/validation": "^0.0.54"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"test": "echo skipping",
|