@adonisjs/http-server 5.8.0 → 5.10.1

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.
@@ -31,7 +31,9 @@ declare module '@ioc:Adonis/Core/HttpContext' {
31
31
  response: ResponseContract;
32
32
  logger: LoggerContract;
33
33
  profiler: ProfilerRowContract;
34
- route?: RouteNode;
34
+ route?: RouteNode & {
35
+ params: string[];
36
+ };
35
37
  routeKey: string;
36
38
  params: Record<string, any>;
37
39
  subdomains: Record<string, any>;
@@ -7,6 +7,7 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  /// <reference types="node" />
10
+ /// <reference types="node" />
10
11
  declare module '@ioc:Adonis/Core/Server' {
11
12
  import { Server as HttpsServer } from 'https';
12
13
  import { RouterContract } from '@ioc:Adonis/Core/Route';
@@ -7,6 +7,7 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  /// <reference types="node" />
10
+ /// <reference types="node" />
10
11
  declare module '@ioc:Adonis/Core/Request' {
11
12
  import { UrlWithStringQuery } from 'url';
12
13
  import { MacroableConstructorContract } from 'macroable';
@@ -7,6 +7,8 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  /// <reference types="node" />
10
+ /// <reference types="node" />
11
+ /// <reference types="node" />
10
12
  declare module '@ioc:Adonis/Core/Response' {
11
13
  import { ServerResponse, IncomingMessage } from 'http';
12
14
  import { MacroableConstructorContract } from 'macroable';
@@ -89,7 +89,9 @@ declare module '@ioc:Adonis/Core/Route' {
89
89
  export type MethodNode = {
90
90
  tokens: any[];
91
91
  routes: {
92
- [pattern: string]: RouteNode;
92
+ [pattern: string]: RouteNode & {
93
+ params: string[];
94
+ };
93
95
  };
94
96
  };
95
97
  /**
@@ -116,27 +118,20 @@ declare module '@ioc:Adonis/Core/Route' {
116
118
  domain?: string;
117
119
  matchers: RouteMatchersNode;
118
120
  };
119
- /**
120
- * Shape of the identifier node of the URL builder
121
- */
122
- export type LookupStoreIdentifier = {
123
- handler: RouteHandler;
124
- methods: string[];
125
- pattern: string;
126
- name?: string;
127
- };
128
121
  /**
129
122
  * Shape of the routes tree maintained by the UrlBuilder
130
123
  */
131
124
  export type LookupStoreTree = {
132
- [domain: string]: LookupStoreIdentifier[];
125
+ [domain: string]: RouteJSON[];
133
126
  };
134
127
  /**
135
128
  * Shape of the matched route for a pattern, method and domain. We set
136
129
  * them as spread options to the context.
137
130
  */
138
131
  export type MatchedRoute = {
139
- route: RouteNode;
132
+ route: RouteNode & {
133
+ params: string[];
134
+ };
140
135
  /**
141
136
  * A unique key for the looked up route
142
137
  */
@@ -490,6 +485,21 @@ declare module '@ioc:Adonis/Core/Route' {
490
485
  * directly
491
486
  */
492
487
  export interface LookupStoreContract {
488
+ /**
489
+ * Find a route by indentifier. Optionally one can find routes inside
490
+ * a given domain
491
+ */
492
+ find(routeIdentifier: string, domainPattern?: string): RouteJSON | null;
493
+ /**
494
+ * Find a route by indentifier or fail. Optionally one can find routes inside
495
+ * a given domain
496
+ */
497
+ findOrFail(routeIdentifier: string, domainPattern?: string): RouteJSON;
498
+ /**
499
+ * Find if a route for given identifier exists. Optionally one can find routes inside
500
+ * a given domain
501
+ */
502
+ has(routeIdentifier: string, domainPattern?: string): boolean;
493
503
  /**
494
504
  * Get the builder instance for the main domain
495
505
  */
@@ -68,7 +68,9 @@ export declare class HttpContext extends Macroable implements HttpContextContrac
68
68
  * Reference to the current route. Not available inside
69
69
  * server hooks
70
70
  */
71
- route?: RouteNode;
71
+ route?: RouteNode & {
72
+ params: string[];
73
+ };
72
74
  /**
73
75
  * Required by macroable
74
76
  */
@@ -7,12 +7,16 @@
7
7
  * For the full copyright and license information, please view the LICENSE
8
8
  * file that was distributed with this source code.
9
9
  */
10
+ var __importDefault = (this && this.__importDefault) || function (mod) {
11
+ return (mod && mod.__esModule) ? mod : { "default": mod };
12
+ };
10
13
  Object.defineProperty(exports, "__esModule", { value: true });
11
14
  exports.HttpContext = void 0;
12
15
  /// <reference path="../../adonis-typings/index.ts" />
13
16
  const net_1 = require("net");
14
17
  const util_1 = require("util");
15
18
  const macroable_1 = require("macroable");
19
+ const matchit_1 = __importDefault(require("@poppinss/matchit"));
16
20
  const utils_1 = require("@poppinss/utils");
17
21
  const http_1 = require("http");
18
22
  const Request_1 = require("../Request");
@@ -149,11 +153,15 @@ class HttpContext extends macroable_1.Macroable {
149
153
  middleware: [],
150
154
  handler: async () => 'handled',
151
155
  meta: {},
156
+ params: matchit_1.default
157
+ .parse(routePattern, {})
158
+ .filter((token) => [1, 3].includes(token.type))
159
+ .map((token) => token.val),
152
160
  };
153
161
  /*
154
162
  * Defining route key
155
163
  */
156
- ctx.routeKey = `${request.method()}-${ctx.route.pattern}`;
164
+ ctx.routeKey = `${request.method() || 'GET'}-${ctx.route.pattern}`;
157
165
  /*
158
166
  * Attaching params to the ctx
159
167
  */
@@ -8,6 +8,7 @@
8
8
  */
9
9
  /// <reference path="../../adonis-typings/index.d.ts" />
10
10
  /// <reference types="node" />
11
+ /// <reference types="node" />
11
12
  import { Macroable } from 'macroable';
12
13
  import { UrlWithStringQuery } from 'url';
13
14
  import { ServerResponse, IncomingMessage, IncomingHttpHeaders } from 'http';
@@ -8,6 +8,7 @@
8
8
  */
9
9
  /// <reference path="../../adonis-typings/index.d.ts" />
10
10
  /// <reference types="node" />
11
+ /// <reference types="node" />
11
12
  import { Macroable } from 'macroable';
12
13
  import { ServerResponse, IncomingMessage } from 'http';
13
14
  import { CookieOptions, CastableHeader, ResponseConfig, ResponseStream, ResponseContract, RedirectContract } from '@ioc:Adonis/Core/Response';
@@ -1,5 +1,24 @@
1
- import { RouteJSON, LookupStoreTree, UrlBuilderContract, LookupStoreIdentifier } from '@ioc:Adonis/Core/Route';
1
+ import { RouteJSON, LookupStoreTree, UrlBuilderContract, LookupStoreContract } from '@ioc:Adonis/Core/Route';
2
2
  import { EncryptionContract } from '@ioc:Adonis/Core/Encryption';
3
+ /**
4
+ * A class to encapsulate finding routes
5
+ */
6
+ declare class Routes {
7
+ private routes;
8
+ constructor(routes: RouteJSON[]);
9
+ /**
10
+ * Find a route by indentifier
11
+ */
12
+ find(routeIdentifier: string): RouteJSON | null;
13
+ /**
14
+ * Find a route by indentifier or fail
15
+ */
16
+ findOrFail(routeIdentifier: string): RouteJSON;
17
+ /**
18
+ * Find if a route exists
19
+ */
20
+ has(routeIdentifier: string): boolean;
21
+ }
3
22
  /**
4
23
  * Url builder is responsible for building the URLs
5
24
  */
@@ -23,16 +42,11 @@ export declare class UrlBuilder implements UrlBuilderContract {
23
42
  * A baseUrl to prefix to the endpoint
24
43
  */
25
44
  private baseUrl;
26
- constructor(encryption: EncryptionContract, routes: LookupStoreIdentifier[]);
45
+ constructor(encryption: EncryptionContract, routes: Routes);
27
46
  /**
28
47
  * Processes the pattern against the params
29
48
  */
30
49
  private processPattern;
31
- /**
32
- * Finds the route inside the list of registered routes and
33
- * raises exception when unable to
34
- */
35
- private findRouteOrFail;
36
50
  /**
37
51
  * Suffix the query string to the URL
38
52
  */
@@ -70,7 +84,7 @@ export declare class UrlBuilder implements UrlBuilderContract {
70
84
  * The look up store to make URLs for a given route by looking
71
85
  * it by its name, route handler or the pattern directly.
72
86
  */
73
- export declare class LookupStore {
87
+ export declare class LookupStore implements LookupStoreContract {
74
88
  private encryption;
75
89
  /**
76
90
  * Shape of the registered routes. Optimized for lookups
@@ -89,4 +103,20 @@ export declare class LookupStore {
89
103
  * Returns the route builder a given domain.
90
104
  */
91
105
  builderForDomain(domainPattern: string): UrlBuilder;
106
+ /**
107
+ * Find a route by indentifier. Optionally one can find routes inside
108
+ * a given domain
109
+ */
110
+ find(routeIdentifier: string, domainPattern?: string): RouteJSON | null;
111
+ /**
112
+ * Find a route by indentifier or fail. Optionally one can find routes inside
113
+ * a given domain
114
+ */
115
+ findOrFail(routeIdentifier: string, domainPattern?: string): RouteJSON;
116
+ /**
117
+ * Find if a route for given identifier exists. Optionally one can find routes inside
118
+ * a given domain
119
+ */
120
+ has(routeIdentifier: string, domainPattern?: string): boolean;
92
121
  }
122
+ export {};
@@ -15,6 +15,38 @@ exports.LookupStore = exports.UrlBuilder = void 0;
15
15
  const qs_1 = __importDefault(require("qs"));
16
16
  const encodeurl_1 = __importDefault(require("encodeurl"));
17
17
  const RouterException_1 = require("../Exceptions/RouterException");
18
+ /**
19
+ * A class to encapsulate finding routes
20
+ */
21
+ class Routes {
22
+ constructor(routes) {
23
+ this.routes = routes;
24
+ }
25
+ /**
26
+ * Find a route by indentifier
27
+ */
28
+ find(routeIdentifier) {
29
+ return (this.routes.find(({ name, pattern, handler }) => {
30
+ return (name === routeIdentifier || pattern === routeIdentifier || handler === routeIdentifier);
31
+ }) || null);
32
+ }
33
+ /**
34
+ * Find a route by indentifier or fail
35
+ */
36
+ findOrFail(routeIdentifier) {
37
+ const route = this.find(routeIdentifier);
38
+ if (!route) {
39
+ throw RouterException_1.RouterException.cannotLookupRoute(routeIdentifier);
40
+ }
41
+ return route;
42
+ }
43
+ /**
44
+ * Find if a route exists
45
+ */
46
+ has(routeIdentifier) {
47
+ return !!this.find(routeIdentifier);
48
+ }
49
+ }
18
50
  /**
19
51
  * Url builder is responsible for building the URLs
20
52
  */
@@ -80,19 +112,6 @@ class UrlBuilder {
80
112
  }
81
113
  return url.join('/');
82
114
  }
83
- /**
84
- * Finds the route inside the list of registered routes and
85
- * raises exception when unable to
86
- */
87
- findRouteOrFail(identifier) {
88
- const route = this.routes.find(({ name, pattern, handler }) => {
89
- return name === identifier || pattern === identifier || handler === identifier;
90
- });
91
- if (!route) {
92
- throw RouterException_1.RouterException.cannotLookupRoute(identifier);
93
- }
94
- return route;
95
- }
96
115
  /**
97
116
  * Suffix the query string to the URL
98
117
  */
@@ -144,7 +163,7 @@ class UrlBuilder {
144
163
  make(identifier) {
145
164
  let url;
146
165
  if (this.lookupRoute) {
147
- const route = this.findRouteOrFail(identifier);
166
+ const route = this.routes.findOrFail(identifier);
148
167
  url = this.processPattern(route.pattern);
149
168
  }
150
169
  else {
@@ -158,7 +177,7 @@ class UrlBuilder {
158
177
  makeSigned(identifier, options) {
159
178
  let url;
160
179
  if (this.lookupRoute) {
161
- const route = this.findRouteOrFail(identifier);
180
+ const route = this.routes.findOrFail(identifier);
162
181
  url = this.processPattern(route.pattern);
163
182
  }
164
183
  else {
@@ -199,12 +218,7 @@ class LookupStore {
199
218
  register(route) {
200
219
  const domain = route.domain || 'root';
201
220
  this.tree[domain] = this.tree[domain] || [];
202
- this.tree[domain].push({
203
- methods: route.methods,
204
- name: route.name,
205
- handler: route.handler,
206
- pattern: route.pattern,
207
- });
221
+ this.tree[domain].push(route);
208
222
  }
209
223
  /**
210
224
  * Returns the route builder for the root domain
@@ -220,7 +234,31 @@ class LookupStore {
220
234
  if (!domainRoutes && domainPattern !== 'root') {
221
235
  throw RouterException_1.RouterException.cannotLookupDomain(domainPattern);
222
236
  }
223
- return new UrlBuilder(this.encryption, domainRoutes || []);
237
+ return new UrlBuilder(this.encryption, new Routes(domainRoutes || []));
238
+ }
239
+ /**
240
+ * Find a route by indentifier. Optionally one can find routes inside
241
+ * a given domain
242
+ */
243
+ find(routeIdentifier, domainPattern) {
244
+ const routes = this.tree[domainPattern || 'root'] || [];
245
+ return new Routes(routes || []).find(routeIdentifier);
246
+ }
247
+ /**
248
+ * Find a route by indentifier or fail. Optionally one can find routes inside
249
+ * a given domain
250
+ */
251
+ findOrFail(routeIdentifier, domainPattern) {
252
+ const routes = this.tree[domainPattern || 'root'] || [];
253
+ return new Routes(routes || []).findOrFail(routeIdentifier);
254
+ }
255
+ /**
256
+ * Find if a route for given identifier exists. Optionally one can find routes inside
257
+ * a given domain
258
+ */
259
+ has(routeIdentifier, domainPattern) {
260
+ const routes = this.tree[domainPattern || 'root'] || [];
261
+ return new Routes(routes || []).has(routeIdentifier);
224
262
  }
225
263
  }
226
264
  exports.LookupStore = LookupStore;
@@ -143,6 +143,7 @@ class Store {
143
143
  }
144
144
  }
145
145
  }
146
+ routeJSON.params = new Array(...collectedParams);
146
147
  collectedParams.clear();
147
148
  route.methods.forEach((method) => {
148
149
  const methodRoutes = this.getMethodRoutes(route.domain || 'root', method);
@@ -14,6 +14,7 @@ import { RouteGroup } from './Group';
14
14
  import { BriskRoute } from './BriskRoute';
15
15
  import { RouteResource } from './Resource';
16
16
  import { RouteMatchers } from './Matchers';
17
+ import { LookupStore } from './LookupStore';
17
18
  /**
18
19
  * Router class exposes unified API to create new routes, group them or
19
20
  * create route resources.
@@ -27,8 +28,7 @@ import { RouteMatchers } from './Matchers';
27
28
  * })
28
29
  * ```
29
30
  */
30
- export declare class Router implements RouterContract {
31
- private encryption;
31
+ export declare class Router extends LookupStore implements RouterContract {
32
32
  private routeProcessor?;
33
33
  /**
34
34
  * Collection of routes, including route resource and route
@@ -52,10 +52,6 @@ export declare class Router implements RouterContract {
52
52
  * Global matchers to test route params against regular expressions.
53
53
  */
54
54
  private paramMatchers;
55
- /**
56
- * The lookup store instance
57
- */
58
- private lookupStore;
59
55
  /**
60
56
  * Store with tokenized routes
61
57
  */
@@ -133,14 +129,6 @@ export declare class Router implements RouterContract {
133
129
  * Find route for a given URL, method and optionally domain
134
130
  */
135
131
  match(url: string, method: string, domain?: string): null | MatchedRoute;
136
- /**
137
- * Access to the URL builder
138
- */
139
- builder(): import("./LookupStore").UrlBuilder;
140
- /**
141
- * Access to the URL builder for a specific domain
142
- */
143
- builderForDomain(domainPattern: string): import("./LookupStore").UrlBuilder;
144
132
  /**
145
133
  * Makes url to a registered route by looking it up with the route pattern,
146
134
  * name or the controller.method
@@ -33,9 +33,9 @@ const LookupStore_1 = require("./LookupStore");
33
33
  * })
34
34
  * ```
35
35
  */
36
- class Router {
36
+ class Router extends LookupStore_1.LookupStore {
37
37
  constructor(encryption, routeProcessor) {
38
- this.encryption = encryption;
38
+ super(encryption);
39
39
  this.routeProcessor = routeProcessor;
40
40
  /**
41
41
  * Collection of routes, including route resource and route
@@ -59,10 +59,6 @@ class Router {
59
59
  * Global matchers to test route params against regular expressions.
60
60
  */
61
61
  this.paramMatchers = {};
62
- /**
63
- * The lookup store instance
64
- */
65
- this.lookupStore = new LookupStore_1.LookupStore(this.encryption);
66
62
  /**
67
63
  * Store with tokenized routes
68
64
  */
@@ -224,7 +220,7 @@ class Router {
224
220
  * Returns a flat list of routes JSON
225
221
  */
226
222
  toJSON() {
227
- const lookupStoreRoutes = this.lookupStore.tree;
223
+ const lookupStoreRoutes = this.tree;
228
224
  const domains = Object.keys(lookupStoreRoutes);
229
225
  return domains.reduce((result, domain) => {
230
226
  result[domain] = lookupStoreRoutes[domain].map((route) => {
@@ -264,7 +260,7 @@ class Router {
264
260
  /**
265
261
  * Register the route with the lookup store
266
262
  */
267
- this.lookupStore.register(route);
263
+ this.register(route);
268
264
  this.store.add(route);
269
265
  });
270
266
  this.routes = [];
@@ -303,18 +299,6 @@ class Router {
303
299
  }
304
300
  return response;
305
301
  }
306
- /**
307
- * Access to the URL builder
308
- */
309
- builder() {
310
- return this.lookupStore.builder();
311
- }
312
- /**
313
- * Access to the URL builder for a specific domain
314
- */
315
- builderForDomain(domainPattern) {
316
- return this.lookupStore.builderForDomain(domainPattern);
317
- }
318
302
  /**
319
303
  * Makes url to a registered route by looking it up with the route pattern,
320
304
  * name or the controller.method
@@ -44,7 +44,15 @@ class PreCompiler {
44
44
  returnValue = await routeHandler.handler(ctx);
45
45
  }
46
46
  else {
47
- returnValue = await this.resolver.call(routeHandler, undefined, [ctx]);
47
+ returnValue = await this.resolver.call(routeHandler, undefined, (controller) => {
48
+ /**
49
+ * Allowing controller to provide the controller method argument
50
+ */
51
+ if (typeof controller['getHandlerArguments'] === 'function') {
52
+ return controller['getHandlerArguments'](ctx);
53
+ }
54
+ return [ctx];
55
+ });
48
56
  }
49
57
  if ((0, helpers_2.useReturnValue)(returnValue, ctx)) {
50
58
  ctx.response.send(returnValue);
@@ -8,6 +8,7 @@
8
8
  */
9
9
  /// <reference path="../../adonis-typings/index.d.ts" />
10
10
  /// <reference types="node" />
11
+ /// <reference types="node" />
11
12
  import { Server as HttpsServer } from 'https';
12
13
  import { EncryptionContract } from '@ioc:Adonis/Core/Encryption';
13
14
  import { ApplicationContract } from '@ioc:Adonis/Core/Application';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/http-server",
3
- "version": "5.8.0",
3
+ "version": "5.10.1",
4
4
  "description": "AdonisJS HTTP server with support packed with Routing and Cookies",
5
5
  "main": "build/providers/HttpServerProvider.js",
6
6
  "files": [
@@ -36,44 +36,44 @@
36
36
  "author": "virk,adonisjs",
37
37
  "license": "MIT",
38
38
  "devDependencies": {
39
- "@adonisjs/application": "^5.2.1",
39
+ "@adonisjs/application": "^5.2.5",
40
40
  "@adonisjs/encryption": "^4.0.8",
41
41
  "@adonisjs/mrm-preset": "^5.0.3",
42
- "@adonisjs/require-ts": "^2.0.11",
43
- "@japa/assert": "^1.3.3",
42
+ "@adonisjs/require-ts": "^2.0.12",
43
+ "@japa/assert": "^1.3.4",
44
44
  "@japa/run-failed-tests": "^1.0.7",
45
- "@japa/runner": "^2.0.6",
45
+ "@japa/runner": "^2.0.8",
46
46
  "@japa/spec-reporter": "^1.1.12",
47
47
  "@poppinss/dev-utils": "^2.0.3",
48
- "@types/cookie": "^0.4.1",
48
+ "@types/cookie": "^0.5.1",
49
49
  "@types/ms": "^0.7.31",
50
- "@types/node": "^17.0.23",
50
+ "@types/node": "^17.0.35",
51
51
  "@types/pluralize": "0.0.29",
52
52
  "@types/proxy-addr": "^2.0.0",
53
53
  "@types/qs": "^6.9.7",
54
54
  "@types/supertest": "^2.0.12",
55
- "autocannon": "^7.8.1",
55
+ "autocannon": "^7.9.0",
56
56
  "commitizen": "^4.2.4",
57
57
  "cross-env": "^7.0.3",
58
58
  "cz-conventional-changelog": "^3.3.0",
59
59
  "del-cli": "^4.0.1",
60
- "eslint": "^8.12.0",
60
+ "eslint": "^8.16.0",
61
61
  "eslint-config-prettier": "^8.5.0",
62
62
  "eslint-plugin-adonis": "^2.1.0",
63
63
  "eslint-plugin-prettier": "^4.0.0",
64
- "fastify": "^3.27.4",
64
+ "fastify": "^3.29.0",
65
65
  "github-label-sync": "^2.2.0",
66
66
  "http-status-codes": "^2.2.0",
67
- "husky": "^7.0.4",
68
- "middie": "^6.0.0",
67
+ "husky": "^8.0.1",
68
+ "middie": "^6.1.0",
69
69
  "mrm": "^4.0.0",
70
70
  "np": "^7.6.1",
71
71
  "pem": "^1.14.6",
72
72
  "prettier": "^2.6.2",
73
73
  "reflect-metadata": "^0.1.13",
74
- "supertest": "^6.2.2",
75
- "ts-node": "^10.7.0",
76
- "typescript": "^4.6.3"
74
+ "supertest": "^6.2.3",
75
+ "ts-node": "^10.8.0",
76
+ "typescript": "^4.7.2"
77
77
  },
78
78
  "peerDependencies": {
79
79
  "@adonisjs/application": "^5.0.0",
@@ -98,11 +98,11 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "@poppinss/matchit": "^3.1.2",
101
- "@poppinss/utils": "^4.0.3",
101
+ "@poppinss/utils": "^4.0.4",
102
102
  "accepts": "^1.3.8",
103
103
  "co-compose": "^7.0.2",
104
104
  "content-disposition": "^0.5.4",
105
- "cookie": "^0.4.2",
105
+ "cookie": "^0.5.0",
106
106
  "destroy": "^1.2.0",
107
107
  "encodeurl": "^1.0.2",
108
108
  "etag": "^1.8.1",