vovk 3.0.0-draft.21 → 3.0.0-draft.23
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/Segment.js +5 -2
- package/client/defaultHandler.js +2 -1
- package/package.json +1 -1
- package/types.d.ts +2 -0
- package/utils/getSchema.js +1 -1
- package/utils/reqForm.d.ts +3 -0
- package/utils/reqForm.js +13 -0
- package/utils/setClientValidatorsForHandler.d.ts +1 -1
- package/utils/setClientValidatorsForHandler.js +2 -5
package/Segment.js
CHANGED
|
@@ -10,6 +10,7 @@ const HttpException_1 = require("./HttpException");
|
|
|
10
10
|
const StreamJSONResponse_1 = require("./StreamJSONResponse");
|
|
11
11
|
const reqQuery_1 = __importDefault(require("./utils/reqQuery"));
|
|
12
12
|
const reqMeta_1 = __importDefault(require("./utils/reqMeta"));
|
|
13
|
+
const reqForm_1 = __importDefault(require("./utils/reqForm"));
|
|
13
14
|
class _Segment {
|
|
14
15
|
static getHeadersFromOptions(options) {
|
|
15
16
|
if (!options)
|
|
@@ -50,8 +51,9 @@ class _Segment {
|
|
|
50
51
|
},
|
|
51
52
|
});
|
|
52
53
|
};
|
|
53
|
-
#respondWithError = (statusCode, message, options) => {
|
|
54
|
+
#respondWithError = (statusCode, message, options, cause) => {
|
|
54
55
|
return this.respond(statusCode, {
|
|
56
|
+
cause,
|
|
55
57
|
statusCode,
|
|
56
58
|
message,
|
|
57
59
|
isError: true,
|
|
@@ -127,6 +129,7 @@ class _Segment {
|
|
|
127
129
|
body: () => req.json(),
|
|
128
130
|
query: () => (0, reqQuery_1.default)(req),
|
|
129
131
|
meta: (meta) => (0, reqMeta_1.default)(req, meta),
|
|
132
|
+
form: () => (0, reqForm_1.default)(req),
|
|
130
133
|
};
|
|
131
134
|
try {
|
|
132
135
|
const result = await staticMethod.call(controller, req, methodParams);
|
|
@@ -172,7 +175,7 @@ class _Segment {
|
|
|
172
175
|
}
|
|
173
176
|
if (err.message !== 'NEXT_REDIRECT' && err.message !== 'NEXT_NOT_FOUND') {
|
|
174
177
|
const statusCode = err.statusCode ?? types_1._HttpStatus.INTERNAL_SERVER_ERROR;
|
|
175
|
-
return this.#respondWithError(statusCode, err.message, staticMethod._options);
|
|
178
|
+
return this.#respondWithError(statusCode, err.message, staticMethod._options, err.cause);
|
|
176
179
|
}
|
|
177
180
|
throw e; // if NEXT_REDIRECT or NEXT_NOT_FOUND, rethrow it
|
|
178
181
|
}
|
package/client/defaultHandler.js
CHANGED
|
@@ -14,7 +14,8 @@ const _defaultHandler = async (response) => {
|
|
|
14
14
|
}
|
|
15
15
|
if (!response.ok) {
|
|
16
16
|
// handle server errors
|
|
17
|
-
|
|
17
|
+
const errorResponse = result;
|
|
18
|
+
throw new HttpException_1._HttpException(response.status, errorResponse?.message ?? exports.DEFAULT_ERROR_MESSAGE, errorResponse?.cause);
|
|
18
19
|
}
|
|
19
20
|
return result;
|
|
20
21
|
};
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export type _VovkSchema = {
|
|
|
10
10
|
controllers: Record<string, _VovkControllerSchema>;
|
|
11
11
|
};
|
|
12
12
|
export type _VovkErrorResponse = {
|
|
13
|
+
cause?: unknown;
|
|
13
14
|
statusCode: _HttpStatus;
|
|
14
15
|
message: string;
|
|
15
16
|
isError: true;
|
|
@@ -73,6 +74,7 @@ export interface _VovkRequest<BODY = undefined, QUERY extends object | undefined
|
|
|
73
74
|
body: () => Promise<BODY>;
|
|
74
75
|
query: () => QUERY;
|
|
75
76
|
meta: <T = Record<_KnownAny, _KnownAny>>(meta?: T | null) => T;
|
|
77
|
+
form: <T = _KnownAny>() => Promise<T>;
|
|
76
78
|
};
|
|
77
79
|
}
|
|
78
80
|
export type _ControllerStaticMethod<REQ extends _VovkRequest<_KnownAny, _KnownAny> = _VovkRequest<undefined, Record<string, string | string[]>>, PARAMS extends {
|
package/utils/getSchema.js
CHANGED
|
@@ -20,7 +20,7 @@ function getSchema(options) {
|
|
|
20
20
|
handlers: {
|
|
21
21
|
...(exposeValidation
|
|
22
22
|
? controller._handlers
|
|
23
|
-
: Object.fromEntries(Object.entries(controller._handlers).map(([key, value]) => [
|
|
23
|
+
: Object.fromEntries(Object.entries(controller._handlers ?? {}).map(([key, value]) => [
|
|
24
24
|
key,
|
|
25
25
|
{ ...value, validation: undefined },
|
|
26
26
|
]))),
|
package/utils/reqForm.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = reqForm;
|
|
4
|
+
const formMap = new WeakMap();
|
|
5
|
+
async function reqForm(req) {
|
|
6
|
+
if (formMap.has(req)) {
|
|
7
|
+
return formMap.get(req);
|
|
8
|
+
}
|
|
9
|
+
const body = await req.formData();
|
|
10
|
+
const formData = Object.fromEntries(body.entries());
|
|
11
|
+
formMap.set(req, formData);
|
|
12
|
+
return formData;
|
|
13
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { _KnownAny as KnownAny } from '../types';
|
|
2
|
-
export default function setClientValidatorsForHandler(h: (...args: KnownAny[]) => KnownAny,
|
|
2
|
+
export default function setClientValidatorsForHandler(h: (...args: KnownAny[]) => KnownAny, validation: {
|
|
3
3
|
body: unknown;
|
|
4
4
|
query: unknown;
|
|
5
5
|
}): Promise<void>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.default = setClientValidatorsForHandler;
|
|
4
|
-
function setClientValidatorsForHandler(h,
|
|
4
|
+
function setClientValidatorsForHandler(h, validation) {
|
|
5
5
|
return new Promise((resolve) => {
|
|
6
6
|
setTimeout(() => {
|
|
7
7
|
const controller = h._controller;
|
|
@@ -16,10 +16,7 @@ function setClientValidatorsForHandler(h, validators) {
|
|
|
16
16
|
...controller._handlers,
|
|
17
17
|
[handlerName]: {
|
|
18
18
|
...controller._handlers[handlerName],
|
|
19
|
-
validation
|
|
20
|
-
body: validators.body,
|
|
21
|
-
query: validators.query,
|
|
22
|
-
},
|
|
19
|
+
validation,
|
|
23
20
|
},
|
|
24
21
|
};
|
|
25
22
|
resolve();
|