@aeriajs/http 0.0.97 → 0.0.98
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/routing.d.ts +7 -8
- package/dist/routing.js +19 -7
- package/dist/routing.mjs +19 -8
- package/package.json +5 -5
package/dist/routing.d.ts
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
import type { RouteContext, GenericRequest, GenericResponse, RequestMethod, RouteUri,
|
|
1
|
+
import type { RouteContext, GenericRequest, GenericResponse, RequestMethod, RouteUri, InferProperties, PackReferences, ContractWithRoles, ApiConfig, RoleFromAccessCondition } from '@aeriajs/types';
|
|
2
2
|
export type RouterOptions = {
|
|
3
3
|
exhaust?: boolean;
|
|
4
4
|
base?: RouteUri;
|
|
5
5
|
};
|
|
6
6
|
export type RoutesMeta = Record<RouteUri, Partial<Record<RequestMethod, ContractWithRoles | null> | undefined>>;
|
|
7
|
-
export type Middleware = (context: RouteContext) => any;
|
|
8
7
|
export type RouteGroupOptions = {
|
|
9
8
|
base?: RouteUri;
|
|
10
9
|
};
|
|
11
|
-
type TypedContext<TContractWithRoles extends ContractWithRoles> = RouteContext<RoleFromAccessCondition<TContractWithRoles['roles']
|
|
10
|
+
type TypedContext<TContractWithRoles extends ContractWithRoles> = Omit<RouteContext<RoleFromAccessCondition<TContractWithRoles['roles']>>, 'request'> & {
|
|
12
11
|
request: Omit<RouteContext['request'], 'payload' | 'query'> & {
|
|
13
12
|
payload: TContractWithRoles extends {
|
|
14
13
|
payload: infer Payload;
|
|
15
|
-
} ? PackReferences<
|
|
14
|
+
} ? PackReferences<InferProperties<Payload>> : any;
|
|
16
15
|
query: TContractWithRoles extends {
|
|
17
16
|
query: infer Query;
|
|
18
|
-
} ?
|
|
17
|
+
} ? InferProperties<Query> : any;
|
|
19
18
|
};
|
|
20
19
|
};
|
|
21
20
|
export type ProxiedRouter<TRouter> = TRouter & Record<RequestMethod, <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
|
|
22
21
|
response: infer Response;
|
|
23
|
-
} ?
|
|
22
|
+
} ? InferProperties<Response> extends infer InferredResponse ? InferredResponse | Promise<InferredResponse> : never : any) extends infer Response ? (context: TypedContext<TContractWithRoles>) => Response : never>(exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => ReturnType<typeof registerRoute>>;
|
|
24
23
|
export declare const matches: <TRequest extends GenericRequest>(req: TRequest, method: RequestMethod | RequestMethod[] | null, exp: string | RegExp, options?: RouterOptions, config?: ApiConfig) => {
|
|
25
24
|
fragments: string[];
|
|
26
25
|
} | undefined;
|
|
@@ -29,13 +28,13 @@ export declare const wrapRouteExecution: (response: GenericResponse, cb: () => a
|
|
|
29
28
|
export declare const createRouter: (options?: Partial<RouterOptions>) => ProxiedRouter<{
|
|
30
29
|
route: <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
|
|
31
30
|
response: infer Response;
|
|
32
|
-
} ?
|
|
31
|
+
} ? InferProperties<Response> : any) extends infer Response ? (context: TypedContext<TContractWithRoles>) => Response : never>(method: RequestMethod | RequestMethod[], exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => void;
|
|
33
32
|
routes: ((_: unknown, context: RouteContext, groupOptions?: RouteGroupOptions) => ReturnType<typeof registerRoute>)[];
|
|
34
33
|
routesMeta: RoutesMeta;
|
|
35
34
|
group: <TRouter extends {
|
|
36
35
|
install: (context: RouteContext, options?: RouterOptions) => any;
|
|
37
36
|
routesMeta: RoutesMeta;
|
|
38
|
-
}>(exp: RouteUri, router: TRouter, middleware?:
|
|
37
|
+
}>(exp: RouteUri, router: TRouter, middleware?: (context: RouteContext) => any) => void;
|
|
39
38
|
install: (_context: RouteContext, _options?: RouterOptions) => ReturnType<(value: unknown, context: RouteContext, groupOptions?: RouteGroupOptions | undefined) => Promise<any>>;
|
|
40
39
|
}>;
|
|
41
40
|
export {};
|
package/dist/routing.js
CHANGED
|
@@ -8,7 +8,20 @@ const validation_1 = require("@aeriajs/validation");
|
|
|
8
8
|
const entrypoint_1 = require("@aeriajs/entrypoint");
|
|
9
9
|
const payload_js_1 = require("./payload.js");
|
|
10
10
|
const next_js_1 = require("./next.js");
|
|
11
|
-
const checkUnprocessable = (
|
|
11
|
+
const checkUnprocessable = (what, schema, context, validateOptions = {}) => {
|
|
12
|
+
let result;
|
|
13
|
+
if (Array.isArray(schema)) {
|
|
14
|
+
for (const property of schema) {
|
|
15
|
+
result = (0, validation_1.validate)(what, property, validateOptions);
|
|
16
|
+
if (!result.error) {
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
result = (0, validation_1.validate)(what, schema, validateOptions);
|
|
23
|
+
}
|
|
24
|
+
const { error } = result;
|
|
12
25
|
if (error) {
|
|
13
26
|
if ('code' in error) {
|
|
14
27
|
return context.error(types_1.HTTPStatus.UnprocessableContent, {
|
|
@@ -22,6 +35,7 @@ const checkUnprocessable = ({ error }, context) => {
|
|
|
22
35
|
details: error,
|
|
23
36
|
});
|
|
24
37
|
}
|
|
38
|
+
return types_1.Result.result({});
|
|
25
39
|
};
|
|
26
40
|
const matches = (req, method, exp, options = {}, config) => {
|
|
27
41
|
const base = config?.baseUrl && config.baseUrl !== '/'
|
|
@@ -81,19 +95,17 @@ const registerRoute = async (context, method, exp, cb, contract, options = {}) =
|
|
|
81
95
|
}
|
|
82
96
|
}
|
|
83
97
|
if ('payload' in contract && contract.payload) {
|
|
84
|
-
const
|
|
85
|
-
const error = checkUnprocessable(validationEither, context);
|
|
98
|
+
const { error } = checkUnprocessable(context.request.query, contract.payload, context);
|
|
86
99
|
if (error) {
|
|
87
|
-
return error;
|
|
100
|
+
return types_1.Result.error(error);
|
|
88
101
|
}
|
|
89
102
|
}
|
|
90
103
|
if ('query' in contract && contract.query) {
|
|
91
|
-
const
|
|
104
|
+
const { error } = checkUnprocessable(context.request.query, contract.query, context, {
|
|
92
105
|
coerce: true,
|
|
93
106
|
});
|
|
94
|
-
const error = checkUnprocessable(validationEither, context);
|
|
95
107
|
if (error) {
|
|
96
|
-
return error;
|
|
108
|
+
return types_1.Result.error(error);
|
|
97
109
|
}
|
|
98
110
|
}
|
|
99
111
|
}
|
package/dist/routing.mjs
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
import { Stream } from "stream";
|
|
3
|
-
import { ACError, HTTPStatus, REQUEST_METHODS, STREAMED_RESPONSE } from "@aeriajs/types";
|
|
3
|
+
import { Result, ACError, HTTPStatus, REQUEST_METHODS, STREAMED_RESPONSE } from "@aeriajs/types";
|
|
4
4
|
import { pipe, isGranted, deepMerge, endpointError } 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
8
|
import { isNext } from "./next.mjs";
|
|
9
|
-
const checkUnprocessable = (
|
|
9
|
+
const checkUnprocessable = (what, schema, context, validateOptions = {}) => {
|
|
10
|
+
let result;
|
|
11
|
+
if (Array.isArray(schema)) {
|
|
12
|
+
for (const property of schema) {
|
|
13
|
+
result = validate(what, property, validateOptions);
|
|
14
|
+
if (!result.error) {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
result = validate(what, schema, validateOptions);
|
|
20
|
+
}
|
|
21
|
+
const { error } = result;
|
|
10
22
|
if (error) {
|
|
11
23
|
if ("code" in error) {
|
|
12
24
|
return context.error(HTTPStatus.UnprocessableContent, {
|
|
@@ -20,6 +32,7 @@ const checkUnprocessable = ({ error }, context) => {
|
|
|
20
32
|
details: error
|
|
21
33
|
});
|
|
22
34
|
}
|
|
35
|
+
return Result.result({});
|
|
23
36
|
};
|
|
24
37
|
export const matches = (req, method, exp, options = {}, config) => {
|
|
25
38
|
const base = config?.baseUrl && config.baseUrl !== "/" ? options.base ? `${config.baseUrl}${options.base}` : config.baseUrl : options.base ? options.base : "";
|
|
@@ -79,19 +92,17 @@ export const registerRoute = async (context, method, exp, cb, contract, options
|
|
|
79
92
|
}
|
|
80
93
|
}
|
|
81
94
|
if ("payload" in contract && contract.payload) {
|
|
82
|
-
const
|
|
83
|
-
const error = checkUnprocessable(validationEither, context);
|
|
95
|
+
const { error } = checkUnprocessable(context.request.query, contract.payload, context);
|
|
84
96
|
if (error) {
|
|
85
|
-
return error;
|
|
97
|
+
return Result.error(error);
|
|
86
98
|
}
|
|
87
99
|
}
|
|
88
100
|
if ("query" in contract && contract.query) {
|
|
89
|
-
const
|
|
101
|
+
const { error } = checkUnprocessable(context.request.query, contract.query, context, {
|
|
90
102
|
coerce: true
|
|
91
103
|
});
|
|
92
|
-
const error = checkUnprocessable(validationEither, context);
|
|
93
104
|
if (error) {
|
|
94
|
-
return error;
|
|
105
|
+
return Result.error(error);
|
|
95
106
|
}
|
|
96
107
|
}
|
|
97
108
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.98",
|
|
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.87",
|
|
32
|
+
"@aeriajs/entrypoint": "^0.0.89",
|
|
33
|
+
"@aeriajs/types": "^0.0.75",
|
|
34
|
+
"@aeriajs/validation": "^0.0.90"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"test": "TS_NODE_COMPILER_OPTIONS=\"$(cat ../compilerOptions.json)\" mocha -r ts-node/register tests/*.spec.ts",
|