@adonisjs/http-server 5.9.0 → 5.11.0

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
  /**
@@ -127,7 +129,9 @@ declare module '@ioc:Adonis/Core/Route' {
127
129
  * them as spread options to the context.
128
130
  */
129
131
  export type MatchedRoute = {
130
- route: RouteNode;
132
+ route: RouteNode & {
133
+ params: string[];
134
+ };
131
135
  /**
132
136
  * A unique key for the looked up route
133
137
  */
@@ -194,6 +198,14 @@ declare module '@ioc:Adonis/Core/Route' {
194
198
  * Define controller namespace for a given route
195
199
  */
196
200
  namespace(namespace: string): this;
201
+ /**
202
+ * Get the route pattern
203
+ */
204
+ getPattern(): string;
205
+ /**
206
+ * Update route pattern
207
+ */
208
+ setPattern(pattern: string): this;
197
209
  /**
198
210
  * Returns [[RouteDefinition]] that can be passed to the [[Store]] for
199
211
  * registering the route
@@ -238,6 +250,10 @@ declare module '@ioc:Adonis/Core/Route' {
238
250
  * Define namespace for all the routes inside a given resource
239
251
  */
240
252
  namespace(namespace: string): this;
253
+ /**
254
+ * Set the param name for a given resource
255
+ */
256
+ paramFor(resource: string, param: string): this;
241
257
  /**
242
258
  * Prepend name to the routes names
243
259
  */
@@ -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';
@@ -27,6 +27,13 @@ export declare class RouteResource extends Macroable implements RouteResourceCon
27
27
  private shallow;
28
28
  protected static macros: {};
29
29
  protected static getters: {};
30
+ /**
31
+ * The param names used to create the resource URLs.
32
+ *
33
+ * We need these later when someone explicitly wants to remap
34
+ * param name for a given resource using the "paramFor" method.
35
+ */
36
+ private resourceParamNames;
30
37
  /**
31
38
  * A copy of routes that belongs to this resource
32
39
  */
@@ -77,6 +84,10 @@ export declare class RouteResource extends Macroable implements RouteResourceCon
77
84
  * Define namespace for all the routes inside a given resource
78
85
  */
79
86
  namespace(namespace: string): this;
87
+ /**
88
+ * Set the param name for a given resource
89
+ */
90
+ paramFor(resource: string, param: string): this;
80
91
  /**
81
92
  * Prepend name to the routes names
82
93
  */
@@ -31,6 +31,13 @@ class RouteResource extends macroable_1.Macroable {
31
31
  this.controller = controller;
32
32
  this.globalMatchers = globalMatchers;
33
33
  this.shallow = shallow;
34
+ /**
35
+ * The param names used to create the resource URLs.
36
+ *
37
+ * We need these later when someone explicitly wants to remap
38
+ * param name for a given resource using the "paramFor" method.
39
+ */
40
+ this.resourceParamNames = {};
34
41
  /**
35
42
  * A copy of routes that belongs to this resource
36
43
  */
@@ -59,8 +66,16 @@ class RouteResource extends macroable_1.Macroable {
59
66
  this.resource = this.resource.replace(/^\//, '').replace(/\/$/, '');
60
67
  const resourceTokens = this.resource.split('.');
61
68
  const mainResource = resourceTokens.pop();
69
+ /**
70
+ * The main resource always uses ids
71
+ */
72
+ this.resourceParamNames[mainResource] = ':id';
62
73
  const fullUrl = `${resourceTokens
63
- .map((token) => `${token}/:${helpers_1.string.snakeCase((0, pluralize_1.singular)(token))}_id`)
74
+ .map((token) => {
75
+ const paramName = `:${helpers_1.string.snakeCase((0, pluralize_1.singular)(token))}_id`;
76
+ this.resourceParamNames[token] = paramName;
77
+ return `${token}/${paramName}`;
78
+ })
64
79
  .join('/')}/${mainResource}`;
65
80
  this.makeRoute(fullUrl, ['GET', 'HEAD'], 'index');
66
81
  this.makeRoute(`${fullUrl}/create`, ['GET', 'HEAD'], 'create');
@@ -136,6 +151,20 @@ class RouteResource extends macroable_1.Macroable {
136
151
  });
137
152
  return this;
138
153
  }
154
+ /**
155
+ * Set the param name for a given resource
156
+ */
157
+ paramFor(resource, param) {
158
+ const existingParam = this.resourceParamNames[resource];
159
+ this.resourceParamNames[resource] = `:${param}`;
160
+ this.routes.forEach((route) => {
161
+ /**
162
+ * Update the pattern for the route with the new param name
163
+ */
164
+ route.setPattern(route.getPattern().replace(`${resource}/${existingParam}`, `${resource}/:${param}`));
165
+ });
166
+ return this;
167
+ }
139
168
  /**
140
169
  * Prepend name to the routes names
141
170
  */
@@ -77,7 +77,7 @@ export declare class Route extends Macroable implements RouteContract {
77
77
  /**
78
78
  * Returns a normalized pattern string by prefixing the `prefix` (if defined).
79
79
  */
80
- private getPattern;
80
+ private computePattern;
81
81
  /**
82
82
  * Define Regex matcher for a given param. If a matcher exists, then we do not
83
83
  * override that, since the routes inside a group will set matchers before
@@ -122,6 +122,14 @@ export declare class Route extends Macroable implements RouteContract {
122
122
  * Define controller namespace for a given route
123
123
  */
124
124
  namespace(namespace: string, overwrite?: boolean): this;
125
+ /**
126
+ * Get the route pattern
127
+ */
128
+ getPattern(): string;
129
+ /**
130
+ * Set the route pattern
131
+ */
132
+ setPattern(pattern: string): this;
125
133
  /**
126
134
  * Returns [[RouteDefinition]] that can be passed to the [[Store]] for
127
135
  * registering the route
@@ -75,7 +75,7 @@ class Route extends macroable_1.Macroable {
75
75
  /**
76
76
  * Returns a normalized pattern string by prefixing the `prefix` (if defined).
77
77
  */
78
- getPattern() {
78
+ computePattern() {
79
79
  const pattern = (0, helpers_2.dropSlash)(this.pattern);
80
80
  const prefix = this.prefixes
81
81
  .slice()
@@ -167,6 +167,19 @@ class Route extends macroable_1.Macroable {
167
167
  }
168
168
  return this;
169
169
  }
170
+ /**
171
+ * Get the route pattern
172
+ */
173
+ getPattern() {
174
+ return this.pattern;
175
+ }
176
+ /**
177
+ * Set the route pattern
178
+ */
179
+ setPattern(pattern) {
180
+ this.pattern = pattern;
181
+ return this;
182
+ }
170
183
  /**
171
184
  * Returns [[RouteDefinition]] that can be passed to the [[Store]] for
172
185
  * registering the route
@@ -174,7 +187,7 @@ class Route extends macroable_1.Macroable {
174
187
  toJSON() {
175
188
  return {
176
189
  domain: this.routeDomain,
177
- pattern: this.getPattern(),
190
+ pattern: this.computePattern(),
178
191
  matchers: this.getMatchers(),
179
192
  meta: {
180
193
  namespace: this.routeNamespace,
@@ -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);
@@ -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.9.0",
3
+ "version": "5.11.0",
4
4
  "description": "AdonisJS HTTP server with support packed with Routing and Cookies",
5
5
  "main": "build/providers/HttpServerProvider.js",
6
6
  "files": [
@@ -36,18 +36,18 @@
36
36
  "author": "virk,adonisjs",
37
37
  "license": "MIT",
38
38
  "devDependencies": {
39
- "@adonisjs/application": "^5.2.4",
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",
42
+ "@adonisjs/require-ts": "^2.0.12",
43
43
  "@japa/assert": "^1.3.4",
44
44
  "@japa/run-failed-tests": "^1.0.7",
45
- "@japa/runner": "^2.0.7",
45
+ "@japa/runner": "^2.0.9",
46
46
  "@japa/spec-reporter": "^1.1.12",
47
47
  "@poppinss/dev-utils": "^2.0.3",
48
48
  "@types/cookie": "^0.5.1",
49
49
  "@types/ms": "^0.7.31",
50
- "@types/node": "^17.0.31",
50
+ "@types/node": "^17.0.42",
51
51
  "@types/pluralize": "0.0.29",
52
52
  "@types/proxy-addr": "^2.0.0",
53
53
  "@types/qs": "^6.9.7",
@@ -57,23 +57,23 @@
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.15.0",
60
+ "eslint": "^8.17.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.29.0",
64
+ "fastify": "^4.0.1",
65
65
  "github-label-sync": "^2.2.0",
66
66
  "http-status-codes": "^2.2.0",
67
- "husky": "^8.0.0",
68
- "middie": "^6.0.0",
67
+ "husky": "^8.0.1",
68
+ "middie": "^7.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
74
  "supertest": "^6.2.3",
75
- "ts-node": "^10.7.0",
76
- "typescript": "^4.6.4"
75
+ "ts-node": "^10.8.1",
76
+ "typescript": "^4.7.3"
77
77
  },
78
78
  "peerDependencies": {
79
79
  "@adonisjs/application": "^5.0.0",
@@ -114,7 +114,7 @@
114
114
  "on-finished": "^2.4.1",
115
115
  "pluralize": "^8.0.0",
116
116
  "proxy-addr": "^2.0.7",
117
- "qs": "^6.10.3",
117
+ "qs": "^6.10.5",
118
118
  "tmp-cache": "^1.1.0",
119
119
  "type-is": "^1.6.18",
120
120
  "vary": "^1.1.2"