egg 4.1.0-beta.34 → 4.1.0-beta.36

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.
Files changed (101) hide show
  1. package/dist/agent.d.ts +7 -3
  2. package/dist/agent.js +10 -6
  3. package/dist/ajv.d.ts +1 -1
  4. package/dist/ajv.js +3 -2
  5. package/dist/aop.d.ts +1 -1
  6. package/dist/aop.js +3 -2
  7. package/dist/app/extend/context.d.ts +181 -178
  8. package/dist/app/extend/context.js +244 -259
  9. package/dist/app/extend/helper.d.ts +40 -35
  10. package/dist/app/extend/helper.js +45 -41
  11. package/dist/app/extend/request.d.ts +135 -131
  12. package/dist/app/extend/request.js +219 -258
  13. package/dist/app/extend/response.d.ts +28 -24
  14. package/dist/app/extend/response.js +36 -34
  15. package/dist/app/middleware/body_parser.d.ts +2 -2
  16. package/dist/app/middleware/body_parser.js +7 -3
  17. package/dist/app/middleware/meta.d.ts +8 -8
  18. package/dist/app/middleware/meta.js +15 -18
  19. package/dist/app/middleware/notfound.d.ts +8 -5
  20. package/dist/app/middleware/notfound.js +25 -28
  21. package/dist/app/middleware/override_method.d.ts +2 -2
  22. package/dist/app/middleware/override_method.js +7 -3
  23. package/dist/app/middleware/site_file.d.ts +11 -7
  24. package/dist/app/middleware/site_file.js +37 -52
  25. package/dist/config/config.default.d.ts +11 -6
  26. package/dist/config/config.default.js +258 -375
  27. package/dist/config/config.local.d.ts +6 -3
  28. package/dist/config/config.local.js +7 -8
  29. package/dist/config/config.unittest.d.ts +6 -3
  30. package/dist/config/config.unittest.js +10 -8
  31. package/dist/config/plugin.d.ts +6 -2
  32. package/dist/config/plugin.js +67 -131
  33. package/dist/dal.d.ts +1 -1
  34. package/dist/dal.js +3 -2
  35. package/dist/errors.d.ts +1 -1
  36. package/dist/errors.js +3 -2
  37. package/dist/helper.d.ts +1 -1
  38. package/dist/helper.js +3 -2
  39. package/dist/index.d.ts +24 -106
  40. package/dist/index.js +23 -89
  41. package/dist/lib/agent.d.ts +21 -15
  42. package/dist/lib/agent.js +53 -45
  43. package/dist/lib/application.d.ts +60 -54
  44. package/dist/lib/application.js +200 -250
  45. package/dist/lib/core/base_context_class.d.ts +23 -17
  46. package/dist/lib/core/base_context_class.js +17 -15
  47. package/dist/lib/core/base_context_logger.d.ts +39 -35
  48. package/dist/lib/core/base_context_logger.js +58 -60
  49. package/dist/lib/core/base_hook_class.d.ts +18 -11
  50. package/dist/lib/core/base_hook_class.js +26 -22
  51. package/dist/lib/core/context_httpclient.d.ts +21 -16
  52. package/dist/lib/core/context_httpclient.js +29 -26
  53. package/dist/lib/core/httpclient.d.ts +14 -12
  54. package/dist/lib/core/httpclient.js +34 -37
  55. package/dist/lib/core/logger.d.ts +7 -3
  56. package/dist/lib/core/logger.js +30 -36
  57. package/dist/lib/core/messenger/IMessenger.d.ts +53 -49
  58. package/dist/lib/core/messenger/IMessenger.js +1 -2
  59. package/dist/lib/core/messenger/base.d.ts +11 -7
  60. package/dist/lib/core/messenger/base.js +30 -29
  61. package/dist/lib/core/messenger/index.d.ts +10 -6
  62. package/dist/lib/core/messenger/index.js +11 -8
  63. package/dist/lib/core/messenger/ipc.d.ts +62 -57
  64. package/dist/lib/core/messenger/ipc.js +126 -138
  65. package/dist/lib/core/messenger/local.d.ts +63 -58
  66. package/dist/lib/core/messenger/local.js +126 -131
  67. package/dist/lib/core/utils.d.ts +5 -2
  68. package/dist/lib/core/utils.js +44 -66
  69. package/dist/lib/define.d.ts +72 -67
  70. package/dist/lib/define.js +54 -53
  71. package/dist/lib/egg.d.ts +283 -281
  72. package/dist/lib/egg.js +512 -573
  73. package/dist/lib/error/CookieLimitExceedError.d.ts +7 -4
  74. package/dist/lib/error/CookieLimitExceedError.js +15 -12
  75. package/dist/lib/error/MessageUnhandledRejectionError.d.ts +7 -4
  76. package/dist/lib/error/MessageUnhandledRejectionError.js +15 -12
  77. package/dist/lib/error/index.d.ts +3 -2
  78. package/dist/lib/error/index.js +4 -3
  79. package/dist/lib/loader/AgentWorkerLoader.d.ts +15 -10
  80. package/dist/lib/loader/AgentWorkerLoader.js +22 -18
  81. package/dist/lib/loader/AppWorkerLoader.d.ts +20 -15
  82. package/dist/lib/loader/AppWorkerLoader.js +35 -37
  83. package/dist/lib/loader/EggApplicationLoader.d.ts +7 -3
  84. package/dist/lib/loader/EggApplicationLoader.js +7 -4
  85. package/dist/lib/loader/index.d.ts +4 -3
  86. package/dist/lib/loader/index.js +5 -4
  87. package/dist/lib/start.d.ts +24 -20
  88. package/dist/lib/start.js +32 -42
  89. package/dist/lib/types.d.ts +288 -286
  90. package/dist/lib/types.js +2 -2
  91. package/dist/lib/types.plugin.d.ts +21 -21
  92. package/dist/lib/types.plugin.js +23 -24
  93. package/dist/orm.d.ts +1 -1
  94. package/dist/orm.js +3 -2
  95. package/dist/schedule.d.ts +2 -2
  96. package/dist/schedule.js +5 -5
  97. package/dist/transaction.d.ts +1 -1
  98. package/dist/transaction.js +3 -2
  99. package/dist/urllib.d.ts +1 -1
  100. package/dist/urllib.js +3 -2
  101. package/package.json +60 -64
@@ -1,259 +1,244 @@
1
- import { assign } from 'utility';
2
- import { now, diff } from 'performance-ms';
3
- import { utils, Context as EggCoreContext, Router } from '@eggjs/core';
4
- const HELPER = Symbol('ctx helper');
5
- const LOCALS = Symbol('ctx locals');
6
- const LOCALS_LIST = Symbol('ctx localsList');
7
- const COOKIES = Symbol('ctx cookies');
8
- const CONTEXT_HTTPCLIENT = Symbol('ctx httpclient');
9
- const CONTEXT_ROUTER = Symbol('ctx router');
10
- export default class Context extends EggCoreContext {
11
- /**
12
- * Request start time
13
- * @member {Number} Context#starttime
14
- */
15
- starttime;
16
- /**
17
- * Request start timer using `performance.now()`
18
- * @member {Number} Context#performanceStarttime
19
- */
20
- performanceStarttime;
21
- /**
22
- * Get the current visitor's cookies.
23
- */
24
- get cookies() {
25
- let cookies = this[COOKIES];
26
- if (!cookies) {
27
- this[COOKIES] = cookies = new this.app.ContextCookies(this, this.app.keys, this.app.config.cookies);
28
- }
29
- return cookies;
30
- }
31
- /**
32
- * Get a wrapper httpclient instance contain ctx in the hold request process
33
- *
34
- * @return {HttpClient} the wrapper httpclient instance
35
- */
36
- get httpclient() {
37
- if (!this[CONTEXT_HTTPCLIENT]) {
38
- this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this);
39
- }
40
- return this[CONTEXT_HTTPCLIENT];
41
- }
42
- /**
43
- * Alias to {@link Context#httpclient}
44
- */
45
- get httpClient() {
46
- return this.httpclient;
47
- }
48
- /**
49
- * Shortcut for httpclient.curl
50
- *
51
- * @function Context#curl
52
- * @param {String|Object} url - request url address.
53
- * @param {Object} [options] - options for request.
54
- * @return {Object} see {@link ContextHttpClient#curl}
55
- */
56
- async curl(url, options) {
57
- return await this.httpclient.curl(url, options);
58
- }
59
- /**
60
- * Alias to {@link Application#router}
61
- *
62
- * @member {Router} Context#router
63
- * @since 1.0.0
64
- * @example
65
- * ```js
66
- * this.router.pathFor('post', { id: 12 });
67
- * ```
68
- */
69
- get router() {
70
- if (this[CONTEXT_ROUTER]) {
71
- return this[CONTEXT_ROUTER];
72
- }
73
- return this.app.router;
74
- }
75
- /**
76
- * Set router to Context, only use on EggRouter
77
- * @param {Router} val router instance
78
- */
79
- set router(val) {
80
- this[CONTEXT_ROUTER] = val;
81
- }
82
- /**
83
- * Get helper instance from {@link Application#Helper}
84
- *
85
- * @member {Helper} Context#helper
86
- * @since 1.0.0
87
- */
88
- get helper() {
89
- if (!this[HELPER]) {
90
- this[HELPER] = new this.app.Helper(this);
91
- }
92
- return this[HELPER];
93
- }
94
- /**
95
- * Wrap app.loggers with context information,
96
- * if a custom logger is defined by naming aLogger, then you can `ctx.getLogger('aLogger')`
97
- *
98
- * @param {String} name - logger name
99
- */
100
- getLogger(name) {
101
- return this.app.getLogger(name);
102
- }
103
- /**
104
- * Logger for Application
105
- *
106
- * @member {Logger} Context#logger
107
- * @since 1.0.0
108
- * @example
109
- * ```js
110
- * this.logger.info('some request data: %j', this.request.body);
111
- * this.logger.warn('WARNING!!!!');
112
- * ```
113
- */
114
- get logger() {
115
- return this.getLogger('logger');
116
- }
117
- /**
118
- * Logger for frameworks and plugins
119
- *
120
- * @member {Logger} Context#coreLogger
121
- * @since 1.0.0
122
- */
123
- get coreLogger() {
124
- return this.getLogger('coreLogger');
125
- }
126
- /**
127
- * locals is an object for view, you can use `app.locals` and `ctx.locals` to set variables,
128
- * which will be used as data when view is rendering.
129
- * The difference between `app.locals` and `ctx.locals` is the context level, `app.locals` is global level, and `ctx.locals` is request level. when you get `ctx.locals`, it will merge `app.locals`.
130
- *
131
- * when you set locals, only object is available
132
- *
133
- * ```js
134
- * this.locals = {
135
- * a: 1
136
- * };
137
- * this.locals = {
138
- * b: 1
139
- * };
140
- * this.locals.c = 1;
141
- * console.log(this.locals);
142
- * {
143
- * a: 1,
144
- * b: 1,
145
- * c: 1,
146
- * };
147
- * ```
148
- *
149
- * `ctx.locals` has cache, it only merges `app.locals` once in one request.
150
- *
151
- * @member {Object} Context#locals
152
- */
153
- get locals() {
154
- if (!this[LOCALS]) {
155
- this[LOCALS] = assign({}, this.app.locals);
156
- }
157
- if (Array.isArray(this[LOCALS_LIST]) && this[LOCALS_LIST].length > 0) {
158
- assign(this[LOCALS], this[LOCALS_LIST]);
159
- this[LOCALS_LIST] = null;
160
- }
161
- return this[LOCALS];
162
- }
163
- set locals(val) {
164
- const localsList = this[LOCALS_LIST] ?? [];
165
- localsList.push(val);
166
- this[LOCALS_LIST] = localsList;
167
- }
168
- /**
169
- * alias to {@link Context#locals}, compatible with koa that use this variable
170
- * @member {Object} state
171
- * @see Context#locals
172
- */
173
- get state() {
174
- return this.locals;
175
- }
176
- set state(val) {
177
- this.locals = val;
178
- }
179
- /**
180
- * Run async function in the background
181
- * @param {Function} scope - the first args is ctx
182
- * ```js
183
- * this.body = 'hi';
184
- *
185
- * this.runInBackground(async ctx => {
186
- * await ctx.mysql.query(sql);
187
- * await ctx.curl(url);
188
- * });
189
- * ```
190
- */
191
- runInBackground(scope, taskName) {
192
- // try to use custom function name first
193
- if (!taskName) {
194
- taskName = Reflect.get(scope, '_name') || scope.name || utils.getCalleeFromStack(true);
195
- }
196
- // use setImmediate to ensure all sync logic will run async
197
- setImmediate(() => {
198
- this._runInBackground(scope, taskName);
199
- });
200
- }
201
- // let plugins or frameworks to reuse _runInBackground in some cases.
202
- // e.g.: https://github.com/eggjs/egg-mock/pull/78
203
- async _runInBackground(scope, taskName) {
204
- const startTime = now();
205
- try {
206
- await scope(this);
207
- this.coreLogger.info('[egg:background] task:%s success (%dms)', taskName, diff(startTime));
208
- }
209
- catch (err) {
210
- // background task process log
211
- this.coreLogger.info('[egg:background] task:%s fail (%dms)', taskName, diff(startTime));
212
- // emit error when promise catch, and set err.runInBackground flag
213
- err.runInBackground = true;
214
- this.app.emit('error', err, this);
215
- }
216
- }
217
- /**
218
- * @member {Boolean} Context#acceptJSON
219
- * @see Request#acceptJSON
220
- * @since 1.0.0
221
- */
222
- get acceptJSON() {
223
- return this.request.acceptJSON;
224
- }
225
- get query() {
226
- return this.request.query;
227
- }
228
- /**
229
- * @member {Array} Context#queries
230
- * @see Request#queries
231
- * @since 1.0.0
232
- */
233
- get queries() {
234
- return this.request.queries;
235
- }
236
- /**
237
- * @member {string} Context#ip
238
- * @see Request#ip
239
- * @since 1.0.0
240
- */
241
- get ip() {
242
- return this.request.ip;
243
- }
244
- set ip(val) {
245
- this.request.ip = val;
246
- }
247
- /**
248
- * @member {Number} Context#realStatus
249
- * @see Response#realStatus
250
- * @since 1.0.0
251
- */
252
- get realStatus() {
253
- return this.response.realStatus;
254
- }
255
- set realStatus(val) {
256
- this.response.realStatus = val;
257
- }
258
- }
259
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29udGV4dC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9hcHAvZXh0ZW5kL2NvbnRleHQudHMiXSw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8sQ0FBQyxPQUFPLENBQUM7SUFDOUIsQ0FBQztJQUVEOzs7O09BSUc7SUFDSCxJQUFJLEVBQUU7UUFDSixPQUFPLElBQUksQ0FBQyxPQUFPLENBQUMsRUFBRSxDQUFDO0lBQ3pCLENBQUM7SUFFRCxJQUFJLEVBQUUsQ0FBQyxHQUFXO1FBQ2hCLElBQUksQ0FBQyxPQUFPLENBQUMsRUFBRSxHQUFHLEdBQUcsQ0FBQztJQUN4QixDQUFDO0lBRUQ7Ozs7T0FJRztJQUNILElBQUksVUFBVTtRQUNaLE9BQU8sSUFBSSxDQUFDLFFBQVEsQ0FBQyxVQUFVLENBQUM7SUFDbEMsQ0FBQztJQUVELElBQUksVUFBVSxDQUFDLEdBQVc7UUFDeEIsSUFBSSxDQUFDLFFBQVEsQ0FBQyxVQUFVLEdBQUcsR0FBRyxDQUFDO0lBQ2pDLENBQUM7Q0FDRiJ9
1
+ import { Context as Context$1, Router, utils } from "@eggjs/core";
2
+ import { assign } from "utility";
3
+ import { diff, now } from "performance-ms";
4
+
5
+ //#region src/app/extend/context.ts
6
+ const HELPER = Symbol("ctx helper");
7
+ const LOCALS = Symbol("ctx locals");
8
+ const LOCALS_LIST = Symbol("ctx localsList");
9
+ const COOKIES = Symbol("ctx cookies");
10
+ const CONTEXT_HTTPCLIENT = Symbol("ctx httpclient");
11
+ const CONTEXT_ROUTER = Symbol("ctx router");
12
+ var Context = class extends Context$1 {
13
+ /**
14
+ * Request start time
15
+ * @member {Number} Context#starttime
16
+ */
17
+ starttime;
18
+ /**
19
+ * Request start timer using `performance.now()`
20
+ * @member {Number} Context#performanceStarttime
21
+ */
22
+ performanceStarttime;
23
+ /**
24
+ * Get the current visitor's cookies.
25
+ */
26
+ get cookies() {
27
+ let cookies = this[COOKIES];
28
+ if (!cookies) this[COOKIES] = cookies = new this.app.ContextCookies(this, this.app.keys, this.app.config.cookies);
29
+ return cookies;
30
+ }
31
+ /**
32
+ * Get a wrapper httpclient instance contain ctx in the hold request process
33
+ *
34
+ * @return {HttpClient} the wrapper httpclient instance
35
+ */
36
+ get httpclient() {
37
+ if (!this[CONTEXT_HTTPCLIENT]) this[CONTEXT_HTTPCLIENT] = new this.app.ContextHttpClient(this);
38
+ return this[CONTEXT_HTTPCLIENT];
39
+ }
40
+ /**
41
+ * Alias to {@link Context#httpclient}
42
+ */
43
+ get httpClient() {
44
+ return this.httpclient;
45
+ }
46
+ /**
47
+ * Shortcut for httpclient.curl
48
+ *
49
+ * @function Context#curl
50
+ * @param {String|Object} url - request url address.
51
+ * @param {Object} [options] - options for request.
52
+ * @return {Object} see {@link ContextHttpClient#curl}
53
+ */
54
+ async curl(url, options) {
55
+ return await this.httpclient.curl(url, options);
56
+ }
57
+ /**
58
+ * Alias to {@link Application#router}
59
+ *
60
+ * @member {Router} Context#router
61
+ * @since 1.0.0
62
+ * @example
63
+ * ```js
64
+ * this.router.pathFor('post', { id: 12 });
65
+ * ```
66
+ */
67
+ get router() {
68
+ if (this[CONTEXT_ROUTER]) return this[CONTEXT_ROUTER];
69
+ return this.app.router;
70
+ }
71
+ /**
72
+ * Set router to Context, only use on EggRouter
73
+ * @param {Router} val router instance
74
+ */
75
+ set router(val) {
76
+ this[CONTEXT_ROUTER] = val;
77
+ }
78
+ /**
79
+ * Get helper instance from {@link Application#Helper}
80
+ *
81
+ * @member {Helper} Context#helper
82
+ * @since 1.0.0
83
+ */
84
+ get helper() {
85
+ if (!this[HELPER]) this[HELPER] = new this.app.Helper(this);
86
+ return this[HELPER];
87
+ }
88
+ /**
89
+ * Wrap app.loggers with context information,
90
+ * if a custom logger is defined by naming aLogger, then you can `ctx.getLogger('aLogger')`
91
+ *
92
+ * @param {String} name - logger name
93
+ */
94
+ getLogger(name) {
95
+ return this.app.getLogger(name);
96
+ }
97
+ /**
98
+ * Logger for Application
99
+ *
100
+ * @member {Logger} Context#logger
101
+ * @since 1.0.0
102
+ * @example
103
+ * ```js
104
+ * this.logger.info('some request data: %j', this.request.body);
105
+ * this.logger.warn('WARNING!!!!');
106
+ * ```
107
+ */
108
+ get logger() {
109
+ return this.getLogger("logger");
110
+ }
111
+ /**
112
+ * Logger for frameworks and plugins
113
+ *
114
+ * @member {Logger} Context#coreLogger
115
+ * @since 1.0.0
116
+ */
117
+ get coreLogger() {
118
+ return this.getLogger("coreLogger");
119
+ }
120
+ /**
121
+ * locals is an object for view, you can use `app.locals` and `ctx.locals` to set variables,
122
+ * which will be used as data when view is rendering.
123
+ * The difference between `app.locals` and `ctx.locals` is the context level, `app.locals` is global level, and `ctx.locals` is request level. when you get `ctx.locals`, it will merge `app.locals`.
124
+ *
125
+ * when you set locals, only object is available
126
+ *
127
+ * ```js
128
+ * this.locals = {
129
+ * a: 1
130
+ * };
131
+ * this.locals = {
132
+ * b: 1
133
+ * };
134
+ * this.locals.c = 1;
135
+ * console.log(this.locals);
136
+ * {
137
+ * a: 1,
138
+ * b: 1,
139
+ * c: 1,
140
+ * };
141
+ * ```
142
+ *
143
+ * `ctx.locals` has cache, it only merges `app.locals` once in one request.
144
+ *
145
+ * @member {Object} Context#locals
146
+ */
147
+ get locals() {
148
+ if (!this[LOCALS]) this[LOCALS] = assign({}, this.app.locals);
149
+ if (Array.isArray(this[LOCALS_LIST]) && this[LOCALS_LIST].length > 0) {
150
+ assign(this[LOCALS], this[LOCALS_LIST]);
151
+ this[LOCALS_LIST] = null;
152
+ }
153
+ return this[LOCALS];
154
+ }
155
+ set locals(val) {
156
+ const localsList = this[LOCALS_LIST] ?? [];
157
+ localsList.push(val);
158
+ this[LOCALS_LIST] = localsList;
159
+ }
160
+ /**
161
+ * alias to {@link Context#locals}, compatible with koa that use this variable
162
+ * @member {Object} state
163
+ * @see Context#locals
164
+ */
165
+ get state() {
166
+ return this.locals;
167
+ }
168
+ set state(val) {
169
+ this.locals = val;
170
+ }
171
+ /**
172
+ * Run async function in the background
173
+ * @param {Function} scope - the first args is ctx
174
+ * ```js
175
+ * this.body = 'hi';
176
+ *
177
+ * this.runInBackground(async ctx => {
178
+ * await ctx.mysql.query(sql);
179
+ * await ctx.curl(url);
180
+ * });
181
+ * ```
182
+ */
183
+ runInBackground(scope, taskName) {
184
+ if (!taskName) taskName = Reflect.get(scope, "_name") || scope.name || utils.getCalleeFromStack(true);
185
+ setImmediate(() => {
186
+ this._runInBackground(scope, taskName);
187
+ });
188
+ }
189
+ async _runInBackground(scope, taskName) {
190
+ const startTime = now();
191
+ try {
192
+ await scope(this);
193
+ this.coreLogger.info("[egg:background] task:%s success (%dms)", taskName, diff(startTime));
194
+ } catch (err) {
195
+ this.coreLogger.info("[egg:background] task:%s fail (%dms)", taskName, diff(startTime));
196
+ err.runInBackground = true;
197
+ this.app.emit("error", err, this);
198
+ }
199
+ }
200
+ /**
201
+ * @member {Boolean} Context#acceptJSON
202
+ * @see Request#acceptJSON
203
+ * @since 1.0.0
204
+ */
205
+ get acceptJSON() {
206
+ return this.request.acceptJSON;
207
+ }
208
+ get query() {
209
+ return this.request.query;
210
+ }
211
+ /**
212
+ * @member {Array} Context#queries
213
+ * @see Request#queries
214
+ * @since 1.0.0
215
+ */
216
+ get queries() {
217
+ return this.request.queries;
218
+ }
219
+ /**
220
+ * @member {string} Context#ip
221
+ * @see Request#ip
222
+ * @since 1.0.0
223
+ */
224
+ get ip() {
225
+ return this.request.ip;
226
+ }
227
+ set ip(val) {
228
+ this.request.ip = val;
229
+ }
230
+ /**
231
+ * @member {Number} Context#realStatus
232
+ * @see Response#realStatus
233
+ * @since 1.0.0
234
+ */
235
+ get realStatus() {
236
+ return this.response.realStatus;
237
+ }
238
+ set realStatus(val) {
239
+ this.response.realStatus = val;
240
+ }
241
+ };
242
+
243
+ //#endregion
244
+ export { Context as default };
@@ -1,37 +1,42 @@
1
- import { BaseContextClass } from '../../lib/core/base_context_class.ts';
1
+ import { BaseContextClass } from "../../lib/core/base_context_class.js";
2
+
3
+ //#region src/app/extend/helper.d.ts
4
+
2
5
  /**
3
- * The Helper class which can be used as utility function.
4
- * We support developers to extend Helper through ${baseDir}/app/extend/helper.js ,
5
- * then you can use all method on `ctx.helper` that is a instance of Helper.
6
- */
7
- export default class Helper extends BaseContextClass {
8
- /**
9
- * Generate URL path(without host) for route. Takes the route name and a map of named params.
10
- * @function Helper#pathFor
11
- * @param {String} name - Router Name
12
- * @param {Object} params - Other params
13
- *
14
- * @example
15
- * ```js
16
- * app.get('home', '/index.htm', 'home.index');
17
- * ctx.helper.pathFor('home', { by: 'recent', limit: 20 })
18
- * => /index.htm?by=recent&limit=20
19
- * ```
20
- * @return {String} url path(without host)
21
- */
22
- pathFor(name: string, params: Record<string, any>): string;
23
- /**
24
- * Generate full URL(with host) for route. Takes the route name and a map of named params.
25
- * @function Helper#urlFor
26
- * @param {String} name - Router name
27
- * @param {Object} params - Other params
28
- * @example
29
- * ```js
30
- * app.get('home', '/index.htm', 'home.index');
31
- * ctx.helper.urlFor('home', { by: 'recent', limit: 20 })
32
- * => http://127.0.0.1:7001/index.htm?by=recent&limit=20
33
- * ```
34
- * @return {String} full url(with host)
35
- */
36
- urlFor(name: string, params: Record<string, any>): string;
6
+ * The Helper class which can be used as utility function.
7
+ * We support developers to extend Helper through ${baseDir}/app/extend/helper.js ,
8
+ * then you can use all method on `ctx.helper` that is a instance of Helper.
9
+ */
10
+ declare class Helper extends BaseContextClass {
11
+ /**
12
+ * Generate URL path(without host) for route. Takes the route name and a map of named params.
13
+ * @function Helper#pathFor
14
+ * @param {String} name - Router Name
15
+ * @param {Object} params - Other params
16
+ *
17
+ * @example
18
+ * ```js
19
+ * app.get('home', '/index.htm', 'home.index');
20
+ * ctx.helper.pathFor('home', { by: 'recent', limit: 20 })
21
+ * => /index.htm?by=recent&limit=20
22
+ * ```
23
+ * @return {String} url path(without host)
24
+ */
25
+ pathFor(name: string, params: Record<string, any>): string;
26
+ /**
27
+ * Generate full URL(with host) for route. Takes the route name and a map of named params.
28
+ * @function Helper#urlFor
29
+ * @param {String} name - Router name
30
+ * @param {Object} params - Other params
31
+ * @example
32
+ * ```js
33
+ * app.get('home', '/index.htm', 'home.index');
34
+ * ctx.helper.urlFor('home', { by: 'recent', limit: 20 })
35
+ * => http://127.0.0.1:7001/index.htm?by=recent&limit=20
36
+ * ```
37
+ * @return {String} full url(with host)
38
+ */
39
+ urlFor(name: string, params: Record<string, any>): string;
37
40
  }
41
+ //#endregion
42
+ export { Helper as default };