@aeriajs/http 0.0.36 → 0.0.38

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 CHANGED
@@ -1,4 +1,3 @@
1
- export * from './constants.js';
2
1
  export * from './cors.js';
3
2
  export * from './options.js';
4
3
  export * from './routing.js';
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
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
- export * from "./constants.mjs";
3
2
  export * from "./cors.mjs";
4
3
  export * from "./options.mjs";
5
4
  export * from "./routing.mjs";
package/dist/routing.d.ts CHANGED
@@ -1,16 +1,15 @@
1
- import type { Context, GenericRequest, GenericResponse, RequestMethod, InferProperty, InferResponse, PackReferences, ContractWithRoles } from '@aeriajs/types';
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: Context) => any;
7
+ export type Middleware = (context: RouteContext) => any;
9
8
  export type RouteGroupOptions = {
10
9
  base?: RouteUri;
11
10
  };
12
- type TypedContext<TContractWithRoles extends ContractWithRoles> = Omit<Context, 'request'> & {
13
- request: Omit<Context['request'], 'payload' | 'query'> & {
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: Context, method: RequestMethod | RequestMethod[], exp: RouteUri, cb: (context: Context) => any, contract?: ContractWithRoles, options?: RouterOptions) => Promise<any>;
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: Omit<Context, "request"> & {
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: Context, groupOptions?: RouteGroupOptions) => ReturnType<typeof registerRoute>)[];
50
+ routes: ((_: unknown, context: RouteContext, groupOptions?: RouteGroupOptions) => ReturnType<typeof registerRoute>)[];
52
51
  routesMeta: RoutesMeta;
53
52
  group: <TRouter extends {
54
- install: (context: Context, options?: RouterOptions) => any;
53
+ install: (context: RouteContext, options?: RouterOptions) => any;
55
54
  routesMeta: RoutesMeta;
56
55
  }>(exp: RouteUri, router: TRouter, middleware?: Middleware) => void;
57
- install: (_context: Context, _options?: RouterOptions) => Promise<true | Promise<any>>;
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,17 @@ const unsufficientRoles = (context) => {
23
23
  error: types_1.ACErrors.AuthorizationError,
24
24
  };
25
25
  };
26
- const matches = (req, method, exp, options) => {
27
- const { url } = req;
28
- const { base = constants_js_1.DEFAULT_BASE_URI } = options;
26
+ const matches = (req, method, exp, options, config) => {
27
+ let base = '';
28
+ if (config?.apiBase) {
29
+ base += config.apiBase;
30
+ }
31
+ if (options.base) {
32
+ base += `/${options.base}`;
33
+ }
34
+ base = base
35
+ .replace('//', '/')
36
+ .replace(/\/$/, '');
29
37
  if (method && method !== req.method) {
30
38
  if (!Array.isArray(method) || !method.includes(req.method)) {
31
39
  return;
@@ -34,9 +42,10 @@ const matches = (req, method, exp, options) => {
34
42
  const regexp = exp instanceof RegExp
35
43
  ? exp
36
44
  : new RegExp(`^${base}${exp}$`);
37
- const matches = url.split('?')[0].match(regexp);
38
- if (matches) {
39
- const fragments = matches.splice(1);
45
+ const url = new URL(`http://0.com${req.url}`).pathname;
46
+ const expMatches = url.match(regexp);
47
+ if (expMatches) {
48
+ const fragments = expMatches.splice(1);
40
49
  return {
41
50
  fragments,
42
51
  };
@@ -44,7 +53,8 @@ const matches = (req, method, exp, options) => {
44
53
  };
45
54
  exports.matches = matches;
46
55
  const registerRoute = async (context, method, exp, cb, contract, options = {}) => {
47
- const match = (0, exports.matches)(context.request, method, exp, options);
56
+ const config = await (0, entrypoint_1.getConfig)();
57
+ const match = (0, exports.matches)(context.request, method, exp, options, config);
48
58
  if (match) {
49
59
  if (context.request.headers['content-type'] === 'application/json') {
50
60
  try {
@@ -143,7 +153,6 @@ const wrapRouteExecution = async (res, cb) => {
143
153
  exports.wrapRouteExecution = wrapRouteExecution;
144
154
  const createRouter = (options = {}) => {
145
155
  const { exhaust } = options;
146
- options.base ??= constants_js_1.DEFAULT_BASE_URI;
147
156
  const routes = [];
148
157
  const routesMeta = {};
149
158
  const route = (method, exp, cb, contract) => {
@@ -161,10 +170,14 @@ const createRouter = (options = {}) => {
161
170
  routesMeta[`${exp}${route}`] = router.routesMeta[route];
162
171
  }
163
172
  routes.push(async (_, context, groupOptions) => {
164
- newOptions.base = groupOptions
165
- ? `${groupOptions.base}${exp}`
166
- : `${options.base}${exp}`;
167
- const match = (0, exports.matches)(context.request, null, new RegExp(`^${newOptions.base}/`), newOptions);
173
+ const config = await (0, entrypoint_1.getConfig)();
174
+ const base = groupOptions
175
+ ? groupOptions.base
176
+ : options.base;
177
+ newOptions.base = base
178
+ ? `${base}${exp}`
179
+ : exp;
180
+ const match = (0, exports.matches)(context.request, null, new RegExp(`^${config.apiBase}${newOptions.base}/`), newOptions);
168
181
  if (match) {
169
182
  if (middleware) {
170
183
  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,25 +21,39 @@ const unsufficientRoles = (context) => {
21
21
  error: ACErrors.AuthorizationError
22
22
  };
23
23
  };
24
- export const matches = (req, method, exp, options) => {
25
- const { url } = req;
26
- const { base = DEFAULT_BASE_URI } = options;
24
+ export const matches = (req, method, exp, options, config) => {
25
+ let base = "";
26
+ if (config?.apiBase) {
27
+ base += config.apiBase;
28
+ }
29
+ if (options.base) {
30
+ base += `/${options.base}`;
31
+ }
32
+ base = base.replace("//", "/").replace(/\/$/, "");
27
33
  if (method && method !== req.method) {
28
34
  if (!Array.isArray(method) || !method.includes(req.method)) {
29
35
  return;
30
36
  }
31
37
  }
32
38
  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);
39
+ const url = new URL(`http://0.com${req.url}`).pathname;
40
+ const expMatches = url.match(regexp);
41
+ if (expMatches) {
42
+ const fragments = expMatches.splice(1);
36
43
  return {
37
44
  fragments
38
45
  };
39
46
  }
40
47
  };
41
48
  export const registerRoute = async (context, method, exp, cb, contract, options = {}) => {
42
- const match = matches(context.request, method, exp, options);
49
+ const config = await getConfig();
50
+ const match = matches(
51
+ context.request,
52
+ method,
53
+ exp,
54
+ options,
55
+ config
56
+ );
43
57
  if (match) {
44
58
  if (context.request.headers["content-type"] === "application/json") {
45
59
  try {
@@ -134,7 +148,6 @@ export const wrapRouteExecution = async (res, cb) => {
134
148
  };
135
149
  export const createRouter = (options = {}) => {
136
150
  const { exhaust } = options;
137
- options.base ??= DEFAULT_BASE_URI;
138
151
  const routes = [];
139
152
  const routesMeta = {};
140
153
  const route = (method, exp, cb, contract) => {
@@ -157,8 +170,15 @@ export const createRouter = (options = {}) => {
157
170
  routesMeta[`${exp}${route2}`] = router2.routesMeta[route2];
158
171
  }
159
172
  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);
173
+ const config = await getConfig();
174
+ const base = groupOptions ? groupOptions.base : options.base;
175
+ newOptions.base = base ? `${base}${exp}` : exp;
176
+ const match = matches(
177
+ context.request,
178
+ null,
179
+ new RegExp(`^${config.apiBase}${newOptions.base}/`),
180
+ newOptions
181
+ );
162
182
  if (match) {
163
183
  if (middleware) {
164
184
  const result = await middleware(context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aeriajs/http",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
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.29",
26
- "@aeriajs/common": "^0.0.29",
27
- "@aeriajs/types": "^0.0.26",
28
- "@aeriajs/validation": "^0.0.32"
25
+ "@aeriajs/access-control": "^0.0.31",
26
+ "@aeriajs/common": "^0.0.31",
27
+ "@aeriajs/entrypoint": "^0.0.31",
28
+ "@aeriajs/types": "^0.0.28",
29
+ "@aeriajs/validation": "^0.0.34"
29
30
  },
30
31
  "peerDependencies": {
31
- "@aeriajs/access-control": "^0.0.29",
32
- "@aeriajs/common": "^0.0.29",
33
- "@aeriajs/types": "^0.0.26",
34
- "@aeriajs/validation": "^0.0.32"
32
+ "@aeriajs/access-control": "^0.0.31",
33
+ "@aeriajs/common": "^0.0.31",
34
+ "@aeriajs/entrypoint": "^0.0.31",
35
+ "@aeriajs/types": "^0.0.28",
36
+ "@aeriajs/validation": "^0.0.34"
35
37
  },
36
38
  "scripts": {
37
39
  "test": "echo skipping",
@@ -1 +0,0 @@
1
- export declare const DEFAULT_BASE_URI = "/api";
package/dist/constants.js DELETED
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_BASE_URI = void 0;
4
- exports.DEFAULT_BASE_URI = '/api';
@@ -1,2 +0,0 @@
1
- "use strict";
2
- export const DEFAULT_BASE_URI = "/api";