@aeriajs/http 0.0.36 → 0.0.37
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 +0 -1
- package/dist/index.js +0 -1
- package/dist/index.mjs +0 -1
- package/dist/routing.d.ts +10 -11
- package/dist/routing.js +11 -6
- package/dist/routing.mjs +19 -6
- package/package.json +11 -9
- package/dist/constants.d.ts +0 -1
- package/dist/constants.js +0 -4
- package/dist/constants.mjs +0 -2
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -14,7 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./constants.js"), exports);
|
|
18
17
|
__exportStar(require("./cors.js"), exports);
|
|
19
18
|
__exportStar(require("./options.js"), exports);
|
|
20
19
|
__exportStar(require("./routing.js"), exports);
|
package/dist/index.mjs
CHANGED
package/dist/routing.d.ts
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export type RouteUri = `/${string}`;
|
|
1
|
+
import type { RouteContext, GenericRequest, GenericResponse, RequestMethod, RouteUri, InferProperty, InferResponse, PackReferences, ContractWithRoles, ApiConfig } from '@aeriajs/types';
|
|
3
2
|
export type RouterOptions = {
|
|
4
3
|
exhaust?: boolean;
|
|
5
4
|
base?: RouteUri;
|
|
6
5
|
};
|
|
7
6
|
export type RoutesMeta = Record<RouteUri, Partial<Record<RequestMethod, ContractWithRoles | null> | undefined>>;
|
|
8
|
-
export type Middleware = (context:
|
|
7
|
+
export type Middleware = (context: RouteContext) => any;
|
|
9
8
|
export type RouteGroupOptions = {
|
|
10
9
|
base?: RouteUri;
|
|
11
10
|
};
|
|
12
|
-
type TypedContext<TContractWithRoles extends ContractWithRoles> =
|
|
13
|
-
request: Omit<
|
|
11
|
+
type TypedContext<TContractWithRoles extends ContractWithRoles> = RouteContext & {
|
|
12
|
+
request: Omit<RouteContext['request'], 'payload' | 'query'> & {
|
|
14
13
|
payload: TContractWithRoles extends {
|
|
15
14
|
payload: infer Payload;
|
|
16
15
|
} ? PackReferences<InferProperty<Payload>> : any;
|
|
@@ -26,15 +25,15 @@ export type ProxiedRouter<TRouter> = TRouter & Record<RequestMethod, <const TCon
|
|
|
26
25
|
authenticated: true;
|
|
27
26
|
};
|
|
28
27
|
}) => Response : never : (context: TypedContext<TContractWithRoles>) => Response : never>(exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => ReturnType<typeof registerRoute>>;
|
|
29
|
-
export declare const matches: <TRequest extends GenericRequest>(req: TRequest, method: RequestMethod | RequestMethod[] | null, exp: string | RegExp, options: RouterOptions) => {
|
|
28
|
+
export declare const matches: <TRequest extends GenericRequest>(req: TRequest, method: RequestMethod | RequestMethod[] | null, exp: string | RegExp, options: RouterOptions, config: ApiConfig) => {
|
|
30
29
|
fragments: string[];
|
|
31
30
|
} | undefined;
|
|
32
|
-
export declare const registerRoute: (context:
|
|
31
|
+
export declare const registerRoute: (context: RouteContext, method: RequestMethod | RequestMethod[], exp: RouteUri, cb: (context: RouteContext) => any, contract?: ContractWithRoles, options?: RouterOptions) => Promise<any>;
|
|
33
32
|
export declare const wrapRouteExecution: (res: GenericResponse, cb: () => any | Promise<any>) => Promise<any>;
|
|
34
33
|
export declare const createRouter: (options?: Partial<RouterOptions>) => ProxiedRouter<{
|
|
35
34
|
route: <const TContractWithRoles extends ContractWithRoles, TCallback extends (TContractWithRoles extends {
|
|
36
35
|
response: infer Response;
|
|
37
|
-
} ? InferResponse<Response> : any) extends infer Response_1 ? TContractWithRoles["roles"] extends unknown[] ? TContractWithRoles["roles"][number] extends infer Role ? "guest" extends Role ? (context: TypedContext<TContractWithRoles>) => Response_1 : (context:
|
|
36
|
+
} ? InferResponse<Response> : any) extends infer Response_1 ? TContractWithRoles["roles"] extends unknown[] ? TContractWithRoles["roles"][number] extends infer Role ? "guest" extends Role ? (context: TypedContext<TContractWithRoles>) => Response_1 : (context: RouteContext & {
|
|
38
37
|
request: Omit<GenericRequest, "payload" | "query"> & {
|
|
39
38
|
payload: TContractWithRoles extends {
|
|
40
39
|
payload: infer Payload;
|
|
@@ -48,12 +47,12 @@ export declare const createRouter: (options?: Partial<RouterOptions>) => Proxied
|
|
|
48
47
|
authenticated: true;
|
|
49
48
|
};
|
|
50
49
|
}) => Response_1 : never : (context: TypedContext<TContractWithRoles>) => Response_1 : never>(method: RequestMethod | RequestMethod[], exp: RouteUri, cb: TCallback, contract?: TContractWithRoles) => void;
|
|
51
|
-
routes: ((_: unknown, context:
|
|
50
|
+
routes: ((_: unknown, context: RouteContext, groupOptions?: RouteGroupOptions) => ReturnType<typeof registerRoute>)[];
|
|
52
51
|
routesMeta: RoutesMeta;
|
|
53
52
|
group: <TRouter extends {
|
|
54
|
-
install: (context:
|
|
53
|
+
install: (context: RouteContext, options?: RouterOptions) => any;
|
|
55
54
|
routesMeta: RoutesMeta;
|
|
56
55
|
}>(exp: RouteUri, router: TRouter, middleware?: Middleware) => void;
|
|
57
|
-
install: (_context:
|
|
56
|
+
install: (_context: RouteContext, _options?: RouterOptions) => Promise<true | Promise<any>>;
|
|
58
57
|
}>;
|
|
59
58
|
export {};
|
package/dist/routing.js
CHANGED
|
@@ -5,8 +5,8 @@ const stream_1 = require("stream");
|
|
|
5
5
|
const types_1 = require("@aeriajs/types");
|
|
6
6
|
const common_1 = require("@aeriajs/common");
|
|
7
7
|
const validation_1 = require("@aeriajs/validation");
|
|
8
|
+
const entrypoint_1 = require("@aeriajs/entrypoint");
|
|
8
9
|
const payload_js_1 = require("./payload.js");
|
|
9
|
-
const constants_js_1 = require("./constants.js");
|
|
10
10
|
const checkUnprocessable = (validationEither, context) => {
|
|
11
11
|
if ((0, common_1.isLeft)(validationEither)) {
|
|
12
12
|
context.response.writeHead(422, {
|
|
@@ -23,9 +23,13 @@ const unsufficientRoles = (context) => {
|
|
|
23
23
|
error: types_1.ACErrors.AuthorizationError,
|
|
24
24
|
};
|
|
25
25
|
};
|
|
26
|
-
const matches = (req, method, exp, options) => {
|
|
26
|
+
const matches = (req, method, exp, options, config) => {
|
|
27
27
|
const { url } = req;
|
|
28
|
-
const
|
|
28
|
+
const base = `${config.apiBase}${options.base
|
|
29
|
+
? `/${options.base}`
|
|
30
|
+
: ''}`
|
|
31
|
+
.replace('//', '/')
|
|
32
|
+
.replace(/\/$/, '');
|
|
29
33
|
if (method && method !== req.method) {
|
|
30
34
|
if (!Array.isArray(method) || !method.includes(req.method)) {
|
|
31
35
|
return;
|
|
@@ -44,7 +48,8 @@ const matches = (req, method, exp, options) => {
|
|
|
44
48
|
};
|
|
45
49
|
exports.matches = matches;
|
|
46
50
|
const registerRoute = async (context, method, exp, cb, contract, options = {}) => {
|
|
47
|
-
const
|
|
51
|
+
const config = await (0, entrypoint_1.getConfig)();
|
|
52
|
+
const match = (0, exports.matches)(context.request, method, exp, options, config);
|
|
48
53
|
if (match) {
|
|
49
54
|
if (context.request.headers['content-type'] === 'application/json') {
|
|
50
55
|
try {
|
|
@@ -143,7 +148,6 @@ const wrapRouteExecution = async (res, cb) => {
|
|
|
143
148
|
exports.wrapRouteExecution = wrapRouteExecution;
|
|
144
149
|
const createRouter = (options = {}) => {
|
|
145
150
|
const { exhaust } = options;
|
|
146
|
-
options.base ??= constants_js_1.DEFAULT_BASE_URI;
|
|
147
151
|
const routes = [];
|
|
148
152
|
const routesMeta = {};
|
|
149
153
|
const route = (method, exp, cb, contract) => {
|
|
@@ -161,10 +165,11 @@ const createRouter = (options = {}) => {
|
|
|
161
165
|
routesMeta[`${exp}${route}`] = router.routesMeta[route];
|
|
162
166
|
}
|
|
163
167
|
routes.push(async (_, context, groupOptions) => {
|
|
168
|
+
const config = await (0, entrypoint_1.getConfig)();
|
|
164
169
|
newOptions.base = groupOptions
|
|
165
170
|
? `${groupOptions.base}${exp}`
|
|
166
171
|
: `${options.base}${exp}`;
|
|
167
|
-
const match = (0, exports.matches)(context.request, null, new RegExp(`^${newOptions.base}/`), newOptions);
|
|
172
|
+
const match = (0, exports.matches)(context.request, null, new RegExp(`^${newOptions.base}/`), newOptions, config);
|
|
168
173
|
if (match) {
|
|
169
174
|
if (middleware) {
|
|
170
175
|
const result = await middleware(context);
|
package/dist/routing.mjs
CHANGED
|
@@ -3,8 +3,8 @@ import { Stream } from "stream";
|
|
|
3
3
|
import { ACErrors, REQUEST_METHODS } from "@aeriajs/types";
|
|
4
4
|
import { pipe, arraysIntersects, left, isLeft, unwrapEither, deepMerge } from "@aeriajs/common";
|
|
5
5
|
import { validate } from "@aeriajs/validation";
|
|
6
|
+
import { getConfig } from "@aeriajs/entrypoint";
|
|
6
7
|
import { safeJson } from "./payload.mjs";
|
|
7
|
-
import { DEFAULT_BASE_URI } from "./constants.mjs";
|
|
8
8
|
const checkUnprocessable = (validationEither, context) => {
|
|
9
9
|
if (isLeft(validationEither)) {
|
|
10
10
|
context.response.writeHead(422, {
|
|
@@ -21,9 +21,9 @@ const unsufficientRoles = (context) => {
|
|
|
21
21
|
error: ACErrors.AuthorizationError
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
|
-
export const matches = (req, method, exp, options) => {
|
|
24
|
+
export const matches = (req, method, exp, options, config) => {
|
|
25
25
|
const { url } = req;
|
|
26
|
-
const
|
|
26
|
+
const base = `${config.apiBase}${options.base ? `/${options.base}` : ""}`.replace("//", "/").replace(/\/$/, "");
|
|
27
27
|
if (method && method !== req.method) {
|
|
28
28
|
if (!Array.isArray(method) || !method.includes(req.method)) {
|
|
29
29
|
return;
|
|
@@ -39,7 +39,14 @@ export const matches = (req, method, exp, options) => {
|
|
|
39
39
|
}
|
|
40
40
|
};
|
|
41
41
|
export const registerRoute = async (context, method, exp, cb, contract, options = {}) => {
|
|
42
|
-
const
|
|
42
|
+
const config = await getConfig();
|
|
43
|
+
const match = matches(
|
|
44
|
+
context.request,
|
|
45
|
+
method,
|
|
46
|
+
exp,
|
|
47
|
+
options,
|
|
48
|
+
config
|
|
49
|
+
);
|
|
43
50
|
if (match) {
|
|
44
51
|
if (context.request.headers["content-type"] === "application/json") {
|
|
45
52
|
try {
|
|
@@ -134,7 +141,6 @@ export const wrapRouteExecution = async (res, cb) => {
|
|
|
134
141
|
};
|
|
135
142
|
export const createRouter = (options = {}) => {
|
|
136
143
|
const { exhaust } = options;
|
|
137
|
-
options.base ??= DEFAULT_BASE_URI;
|
|
138
144
|
const routes = [];
|
|
139
145
|
const routesMeta = {};
|
|
140
146
|
const route = (method, exp, cb, contract) => {
|
|
@@ -157,8 +163,15 @@ export const createRouter = (options = {}) => {
|
|
|
157
163
|
routesMeta[`${exp}${route2}`] = router2.routesMeta[route2];
|
|
158
164
|
}
|
|
159
165
|
routes.push(async (_, context, groupOptions) => {
|
|
166
|
+
const config = await getConfig();
|
|
160
167
|
newOptions.base = groupOptions ? `${groupOptions.base}${exp}` : `${options.base}${exp}`;
|
|
161
|
-
const match = matches(
|
|
168
|
+
const match = matches(
|
|
169
|
+
context.request,
|
|
170
|
+
null,
|
|
171
|
+
new RegExp(`^${newOptions.base}/`),
|
|
172
|
+
newOptions,
|
|
173
|
+
config
|
|
174
|
+
);
|
|
162
175
|
if (match) {
|
|
163
176
|
if (middleware) {
|
|
164
177
|
const result = await middleware(context);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aeriajs/http",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.37",
|
|
4
4
|
"description": "## Installation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -22,16 +22,18 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@aeriajs/access-control": "^0.0.
|
|
26
|
-
"@aeriajs/common": "^0.0.
|
|
27
|
-
"@aeriajs/
|
|
28
|
-
"@aeriajs/
|
|
25
|
+
"@aeriajs/access-control": "^0.0.30",
|
|
26
|
+
"@aeriajs/common": "^0.0.30",
|
|
27
|
+
"@aeriajs/entrypoint": "^0.0.30",
|
|
28
|
+
"@aeriajs/types": "^0.0.27",
|
|
29
|
+
"@aeriajs/validation": "^0.0.33"
|
|
29
30
|
},
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"@aeriajs/access-control": "^0.0.
|
|
32
|
-
"@aeriajs/common": "^0.0.
|
|
33
|
-
"@aeriajs/
|
|
34
|
-
"@aeriajs/
|
|
32
|
+
"@aeriajs/access-control": "^0.0.30",
|
|
33
|
+
"@aeriajs/common": "^0.0.30",
|
|
34
|
+
"@aeriajs/entrypoint": "^0.0.30",
|
|
35
|
+
"@aeriajs/types": "^0.0.27",
|
|
36
|
+
"@aeriajs/validation": "^0.0.33"
|
|
35
37
|
},
|
|
36
38
|
"scripts": {
|
|
37
39
|
"test": "echo skipping",
|
package/dist/constants.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const DEFAULT_BASE_URI = "/api";
|
package/dist/constants.js
DELETED
package/dist/constants.mjs
DELETED