@adonisjs/core 7.0.0-next.10 → 7.0.0-next.11

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 (128) hide show
  1. package/build/commands/add.js +40 -110
  2. package/build/commands/build.js +51 -107
  3. package/build/commands/configure.js +82 -177
  4. package/build/commands/eject.js +17 -60
  5. package/build/commands/env/add.js +55 -134
  6. package/build/commands/generate_key.js +22 -66
  7. package/build/commands/inspect_rcfile.js +27 -55
  8. package/build/commands/list/routes.js +246 -116
  9. package/build/commands/make/command.js +15 -57
  10. package/build/commands/make/controller.js +43 -120
  11. package/build/commands/make/event.js +16 -62
  12. package/build/commands/make/exception.js +16 -62
  13. package/build/commands/make/listener.js +36 -93
  14. package/build/commands/make/middleware.js +41 -101
  15. package/build/commands/make/preload.js +47 -113
  16. package/build/commands/make/provider.js +47 -112
  17. package/build/commands/make/service.js +16 -55
  18. package/build/commands/make/test.js +47 -132
  19. package/build/commands/make/transformer.js +17 -64
  20. package/build/commands/make/validator.js +20 -80
  21. package/build/commands/make/view.js +15 -56
  22. package/build/commands/repl.js +17 -50
  23. package/build/commands/serve.js +80 -150
  24. package/build/commands/test.js +92 -202
  25. package/build/factories/app.js +2 -9
  26. package/build/factories/bodyparser.js +2 -9
  27. package/build/factories/core/ace.js +15 -46
  28. package/build/factories/core/ignitor.js +12 -180
  29. package/build/factories/core/main.js +22 -11
  30. package/build/factories/core/test_utils.js +19 -43
  31. package/build/factories/encryption.js +2 -9
  32. package/build/factories/events.js +2 -9
  33. package/build/factories/hash.js +2 -9
  34. package/build/factories/http.js +2 -9
  35. package/build/factories/logger.js +2 -9
  36. package/build/factories/stubs.js +28 -91
  37. package/build/index.js +13 -46
  38. package/build/modules/ace/codemods.js +222 -477
  39. package/build/modules/ace/main.js +2 -41
  40. package/build/modules/app.js +4 -26
  41. package/build/modules/bodyparser/bodyparser_middleware.js +2 -14
  42. package/build/modules/bodyparser/main.js +4 -9
  43. package/build/modules/config.js +4 -26
  44. package/build/modules/container.js +4 -9
  45. package/build/modules/dumper/main.js +6 -32
  46. package/build/modules/dumper/plugins/edge.js +2 -62
  47. package/build/modules/encryption.js +4 -26
  48. package/build/modules/env/editor.js +2 -9
  49. package/build/modules/env/main.js +2 -28
  50. package/build/modules/events.js +4 -9
  51. package/build/modules/hash/drivers/argon.js +2 -9
  52. package/build/modules/hash/drivers/bcrypt.js +2 -20
  53. package/build/modules/hash/drivers/scrypt.js +2 -9
  54. package/build/modules/hash/main.js +6 -28
  55. package/build/modules/hash/phc_formatter.js +2 -9
  56. package/build/modules/health.js +2 -9
  57. package/build/modules/http/main.js +4 -15
  58. package/build/modules/http/url_builder_client.js +2 -9
  59. package/build/modules/logger.d.ts +30 -0
  60. package/build/modules/logger.js +4 -9
  61. package/build/modules/repl.js +4 -9
  62. package/build/modules/transformers/main.js +4 -9
  63. package/build/providers/app_provider.js +147 -359
  64. package/build/providers/edge_provider.js +97 -165
  65. package/build/providers/hash_provider.js +29 -91
  66. package/build/providers/repl_provider.js +61 -152
  67. package/build/providers/vinejs_provider.d.ts +1 -1
  68. package/build/providers/vinejs_provider.js +20 -66
  69. package/build/services/ace.js +2 -17
  70. package/build/services/app.js +2 -21
  71. package/build/services/config.js +2 -13
  72. package/build/services/dumper.js +5 -21
  73. package/build/services/emitter.js +2 -14
  74. package/build/services/encryption.js +2 -14
  75. package/build/services/hash.js +2 -14
  76. package/build/services/logger.js +2 -14
  77. package/build/services/repl.js +2 -14
  78. package/build/services/router.js +2 -14
  79. package/build/services/server.js +2 -14
  80. package/build/services/test_utils.js +2 -17
  81. package/build/services/url_builder.js +5 -17
  82. package/build/src/exceptions.js +2 -49
  83. package/build/src/helpers/assert.js +2 -55
  84. package/build/src/helpers/http.js +2 -28
  85. package/build/src/helpers/is.js +3 -31
  86. package/build/src/helpers/main.js +5 -52
  87. package/build/src/helpers/string.js +2 -78
  88. package/build/src/helpers/types.js +26 -135
  89. package/build/src/helpers/verification_token.js +2 -120
  90. package/build/src/test_utils/main.js +5 -77
  91. package/build/src/types.js +0 -8
  92. package/build/src/vine.js +2 -105
  93. package/build/types/ace.js +2 -9
  94. package/build/types/app.js +2 -9
  95. package/build/types/bodyparser.js +2 -9
  96. package/build/types/common.js +2 -9
  97. package/build/types/container.js +2 -9
  98. package/build/types/encryption.js +2 -9
  99. package/build/types/events.js +2 -9
  100. package/build/types/hash.js +2 -9
  101. package/build/types/health.js +2 -9
  102. package/build/types/helpers.js +0 -8
  103. package/build/types/http.js +2 -9
  104. package/build/types/logger.js +2 -9
  105. package/build/types/repl.js +2 -9
  106. package/build/types/transformers.js +2 -9
  107. package/package.json +76 -19
  108. package/build/modules/ace/commands.js +0 -157
  109. package/build/modules/ace/create_kernel.js +0 -91
  110. package/build/modules/ace/kernel.js +0 -40
  111. package/build/modules/dumper/define_config.js +0 -36
  112. package/build/modules/dumper/dumper.js +0 -266
  113. package/build/modules/dumper/errors.js +0 -119
  114. package/build/modules/hash/define_config.js +0 -125
  115. package/build/modules/http/request_validator.js +0 -100
  116. package/build/src/assembler_hooks/index_entities.js +0 -112
  117. package/build/src/cli_formatters/routes_list.js +0 -397
  118. package/build/src/config_provider.js +0 -71
  119. package/build/src/debug.js +0 -25
  120. package/build/src/ignitor/ace.js +0 -102
  121. package/build/src/ignitor/http.js +0 -159
  122. package/build/src/ignitor/main.js +0 -124
  123. package/build/src/ignitor/test.js +0 -66
  124. package/build/src/test_utils/http.js +0 -82
  125. package/build/src/utils.js +0 -114
  126. package/build/stubs/main.js +0 -9
  127. package/build/toolkit/commands/index_commands.js +0 -30
  128. package/build/toolkit/main.js +0 -27
@@ -1,266 +0,0 @@
1
- /*
2
- * @adonisjs/core
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import useColors from '@poppinss/colors';
10
- import { dump as consoleDump } from '@poppinss/dumper/console';
11
- import { createScript, createStyleSheet, dump } from '@poppinss/dumper/html';
12
- import { E_DUMP_DIE_EXCEPTION } from "./errors.js";
13
- const colors = useColors.ansi();
14
- const DUMP_TITLE_STYLES = `
15
- .adonisjs-dump-header {
16
- font-family: JetBrains Mono, monaspace argon, Menlo, Monaco, Consolas, monospace;
17
- background: #ff1639;
18
- border-radius: 4px;
19
- color: #fff;
20
- border-bottom-left-radius: 0;
21
- border-bottom-right-radius: 0;
22
- padding: 0.4rem 1.2rem;
23
- font-size: 1em;
24
- display: flex;
25
- justify-content: space-between;
26
- }
27
- .adonisjs-dump-header .adonisjs-dump-header-title {
28
- font-weight: bold;
29
- text-transform: uppercase;
30
- }
31
- .adonisjs-dump-header .adonisjs-dump-header-source {
32
- font-weight: bold;
33
- color: inherit;
34
- text-decoration: underline;
35
- }
36
- .dumper-dump pre {
37
- border-radius: 4px;
38
- border-top-left-radius: 0;
39
- border-top-right-radius: 0;
40
- }`;
41
- const IDE = process.env.ADONIS_IDE ?? process.env.EDITOR ?? '';
42
- /**
43
- * Dumper exposes the API to dump or die/dump values in your
44
- * AdonisJS application. An singleton instance of the Dumper
45
- * is shared as a service and may use it follows.
46
- *
47
- * ```ts
48
- * const dumper = container.make('dumper')
49
- *
50
- * dumper.configureHtmlOutput({
51
- * // parser + html formatter config
52
- * })
53
- *
54
- * dumper.configureAnsiOutput({
55
- * // parser + console formatter config
56
- * })
57
- *
58
- * const html = dumper.dumpToHtml(value)
59
- * const ansi = dumper.dumpToAnsi(value)
60
- *
61
- * // Returns style and script tags that must be
62
- * // injeted to the head of the HTML document
63
- *
64
- * const head = dumper.getHeadElements()
65
- * ```
66
- */
67
- export class Dumper {
68
- #app;
69
- /**
70
- * Configuration for the HTML formatter
71
- */
72
- #htmlConfig = {};
73
- /**
74
- * Configuration for the Console formatter
75
- */
76
- #consoleConfig = {
77
- collapse: ['DateTime', 'Date'],
78
- };
79
- /**
80
- * A collections of known editors to create URLs to open
81
- * them
82
- */
83
- #editors = {
84
- textmate: 'txmt://open?url=file://%f&line=%l',
85
- macvim: 'mvim://open?url=file://%f&line=%l',
86
- emacs: 'emacs://open?url=file://%f&line=%l',
87
- sublime: 'subl://open?url=file://%f&line=%l',
88
- phpstorm: 'phpstorm://open?file=%f&line=%l',
89
- atom: 'atom://core/open/file?filename=%f&line=%l',
90
- vscode: 'vscode://file/%f:%l',
91
- };
92
- /**
93
- * Creates a new Dumper instance
94
- *
95
- * @param app - The AdonisJS application instance
96
- */
97
- constructor(app) {
98
- this.#app = app;
99
- }
100
- /**
101
- * Returns the link to open the file using dd inside one
102
- * of the known code editors. Constructs a URL that can be used
103
- * to open the file at a specific line in supported editors.
104
- *
105
- * @param source - Optional source file information
106
- * @param source.location - The file path to open
107
- * @param source.line - The line number to jump to
108
- */
109
- #getEditorLink(source) {
110
- const editorURL = this.#editors[IDE] || IDE;
111
- if (!editorURL || !source) {
112
- return;
113
- }
114
- return {
115
- href: editorURL.replace('%f', source.location).replace('%l', String(source.line)),
116
- text: `${this.#app.relativePath(source.location)}:${source.line}`,
117
- };
118
- }
119
- /**
120
- * Configure the HTML formatter output options
121
- *
122
- * @param config - Configuration options for HTML dump formatting
123
- *
124
- * @example
125
- * ```ts
126
- * dumper.configureHtmlOutput({
127
- * showHidden: true,
128
- * depth: 5,
129
- * colors: false
130
- * })
131
- * ```
132
- */
133
- configureHtmlOutput(config) {
134
- this.#htmlConfig = config;
135
- return this;
136
- }
137
- /**
138
- * Configure the ANSI formatter output options for console display
139
- *
140
- * @param config - Configuration options for ANSI console formatting
141
- *
142
- * @example
143
- * ```ts
144
- * dumper.configureAnsiOutput({
145
- * showHidden: true,
146
- * depth: 3,
147
- * collapse: ['Date', 'DateTime']
148
- * })
149
- * ```
150
- */
151
- configureAnsiOutput(config) {
152
- this.#consoleConfig = config;
153
- return this;
154
- }
155
- /**
156
- * Returns the style and script elements that need to be injected into
157
- * the HTML document head for proper dump visualization
158
- *
159
- * @param cspNonce - Optional Content Security Policy nonce for inline scripts
160
- *
161
- * @example
162
- * ```ts
163
- * const headElements = dumper.getHeadElements('abc123')
164
- * // Insert into your HTML head section
165
- * ```
166
- */
167
- getHeadElements(cspNonce) {
168
- return (`<style id="dumper-styles">` +
169
- createStyleSheet() +
170
- DUMP_TITLE_STYLES +
171
- '</style>' +
172
- `<script id="dumper-script"${cspNonce ? ` nonce="${cspNonce}"` : ''}>` +
173
- createScript() +
174
- '</script>');
175
- }
176
- /**
177
- * Dump a value to formatted HTML output
178
- *
179
- * @param value - The value to dump and inspect
180
- * @param options - Options for HTML output formatting
181
- * @param options.cspNonce - Optional Content Security Policy nonce
182
- * @param options.title - Optional title to display in the dump header
183
- * @param options.source - Optional source file information for editor links
184
- *
185
- * @example
186
- * ```ts
187
- * const htmlOutput = dumper.dumpToHtml(user, {
188
- * title: 'User Object',
189
- * source: { location: '/app/controllers/user.ts', line: 42 }
190
- * })
191
- * ```
192
- */
193
- dumpToHtml(value, options = {}) {
194
- const link = this.#getEditorLink(options.source) ?? null;
195
- const title = options.title || 'DUMP';
196
- return ('<div class="adonisjs-dump-header">' +
197
- `<span class="adonisjs-dump-header-title">${title}</span>` +
198
- (link ? `<a href="${link.href}" class="adonisjs-dump-header-source">${link.text}</a>` : '') +
199
- '</div>' +
200
- dump(value, { cspNonce: options.cspNonce, ...this.#htmlConfig }));
201
- }
202
- /**
203
- * Dump a value to formatted ANSI output for console display
204
- *
205
- * @param value - The value to dump and inspect
206
- * @param options - Options for ANSI output formatting
207
- * @param options.title - Optional title to display in the dump header
208
- * @param options.source - Optional source file information for editor links
209
- *
210
- * @example
211
- * ```ts
212
- * const ansiOutput = dumper.dumpToAnsi(user, {
213
- * title: 'User Debug',
214
- * source: { location: '/app/controllers/user.ts', line: 42 }
215
- * })
216
- * console.log(ansiOutput)
217
- * ```
218
- */
219
- dumpToAnsi(value, options = {}) {
220
- const columns = process.stdout.columns;
221
- /**
222
- * Link to the source file
223
- */
224
- const link = `${this.#getEditorLink(options.source)?.text ?? ''} `;
225
- /**
226
- * Dump title
227
- */
228
- const title = ` ${options.title || 'DUMP'}`;
229
- /**
230
- * Whitespace between the title and the link to align them
231
- * on each side of x axis
232
- */
233
- const whiteSpaceLength = columns ? columns - link.length - title.length - 4 : 2;
234
- const whiteSpace = new Array(whiteSpaceLength <= 0 ? 2 : whiteSpaceLength).join(' ');
235
- /**
236
- * Styled heading with background color and bold text
237
- */
238
- const heading = colors.bgRed().bold(`${title}${whiteSpace}${link}`);
239
- return `${heading}\n${consoleDump(value, this.#consoleConfig)}`;
240
- }
241
- /**
242
- * Dump values and die. This method dumps the provided value and then
243
- * terminates the application. The output format is automatically chosen
244
- * based on the execution context.
245
- *
246
- * - During an HTTP request, HTML output will be sent to the browser
247
- * - Otherwise the value will be logged to the console in ANSI format
248
- *
249
- * @param value - The value to dump before terminating
250
- * @param traceSourceIndex - Stack trace index for source location (default: 1)
251
- *
252
- * @example
253
- * ```ts
254
- * // This will dump the user object and terminate the application
255
- * dumper.dd(user)
256
- *
257
- * // This will never execute
258
- * console.log('This line will not run')
259
- * ```
260
- */
261
- dd(value, traceSourceIndex = 1) {
262
- const error = new E_DUMP_DIE_EXCEPTION(value, this);
263
- error.setTraceSourceIndex(traceSourceIndex);
264
- throw error;
265
- }
266
- }
@@ -1,119 +0,0 @@
1
- /*
2
- * @adonisjs/core
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { inspect } from 'node:util';
10
- import { parse } from 'error-stack-parser-es';
11
- import { Exception } from '@poppinss/utils/exception';
12
- /**
13
- * DumpDie exception raised by the "dd" (dump and die) function.
14
- * This special exception terminates execution while dumping the provided
15
- * value as HTML (during HTTP requests) or ANSI (in console/CLI).
16
- *
17
- * @example
18
- * ```ts
19
- * // This will dump the user object and terminate
20
- * dumper.dd(user)
21
- *
22
- * // In HTTP context: sends HTML dump to browser
23
- * // In CLI context: prints ANSI dump to console
24
- * ```
25
- */
26
- class DumpDieException extends Exception {
27
- static status = 500;
28
- static code = 'E_DUMP_DIE_EXCEPTION';
29
- #dumper;
30
- #traceSourceIndex = 1;
31
- value;
32
- constructor(value, dumper) {
33
- super('Dump and Die exception');
34
- this.#dumper = dumper;
35
- this.value = value;
36
- }
37
- /**
38
- * Returns the source file and line number location for the error
39
- */
40
- #getErrorSource() {
41
- if (this.fileName && this.lineNumber) {
42
- return {
43
- location: this.fileName,
44
- line: this.lineNumber,
45
- };
46
- }
47
- const source = parse(this)[this.#traceSourceIndex];
48
- if (!source.fileName || !source.lineNumber) {
49
- return;
50
- }
51
- return {
52
- location: source.fileName,
53
- line: source.lineNumber,
54
- };
55
- }
56
- /**
57
- * Set the stack trace index for determining the source location.
58
- * This is useful when building nested helpers on top of dump/die functionality.
59
- *
60
- * @param index - Stack trace index (0 = current function, 1 = caller, etc.)
61
- */
62
- setTraceSourceIndex(index) {
63
- this.#traceSourceIndex = index;
64
- return this;
65
- }
66
- /**
67
- * Preventing itself from getting reported by the
68
- * AdonisJS exception reporter
69
- */
70
- report() { }
71
- /**
72
- * HTTP exception handler that renders the dump as HTML output.
73
- * This method is called automatically by AdonisJS when a DumpDieException
74
- * is thrown during an HTTP request.
75
- *
76
- * @param error - The DumpDieException instance
77
- * @param ctx - HTTP context for the current request
78
- */
79
- async handle(error, ctx) {
80
- const source = this.#getErrorSource();
81
- /**
82
- * Comes from the shield package
83
- */
84
- const cspNonce = 'nonce' in ctx.response ? ctx.response.nonce : undefined;
85
- ctx.response
86
- .status(500)
87
- .send('<!DOCTYPE html>' +
88
- '<html>' +
89
- '<head>' +
90
- '<meta charset="utf-8">' +
91
- '<meta name="viewport" content="width=device-width">' +
92
- `${this.#dumper.getHeadElements(cspNonce)}` +
93
- '</head>' +
94
- '<body>' +
95
- `${this.#dumper.dumpToHtml(error.value, { cspNonce, source, title: 'DUMP DIE' })}` +
96
- '</body>' +
97
- '</html>');
98
- }
99
- /**
100
- * Ace command exception handler that renders the dump as ANSI output.
101
- * This method is called automatically by the Ace kernel when a DumpDieException
102
- * is thrown during command execution.
103
- *
104
- * @param error - The DumpDieException instance
105
- * @param kernel - Ace kernel instance
106
- */
107
- async render(error, kernel) {
108
- const source = this.#getErrorSource();
109
- kernel.ui.logger.log(this.#dumper.dumpToAnsi(error.value, { source, title: 'DUMP DIE' }));
110
- }
111
- /**
112
- * Custom output for the Node.js util inspect
113
- */
114
- [inspect.custom]() {
115
- const source = this.#getErrorSource();
116
- return this.#dumper.dumpToAnsi(this.value, { source, title: 'DUMP DIE' });
117
- }
118
- }
119
- export const E_DUMP_DIE_EXCEPTION = DumpDieException;
@@ -1,125 +0,0 @@
1
- /*
2
- * @adonisjs/core
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- import { InvalidArgumentsException } from '@poppinss/utils/exception';
10
- import debug from "../../src/debug.js";
11
- import { configProvider } from "../../src/config_provider.js";
12
- /**
13
- * Define config for the hash service. This function creates a configuration
14
- * provider that lazily imports and resolves hash drivers when needed.
15
- *
16
- * @param config - Configuration object containing default hasher and list of hashers
17
- * @param config.default - Optional default hasher name (must exist in the list)
18
- * @param config.list - Record of hasher configurations or config providers
19
- *
20
- * @example
21
- * ```ts
22
- * const hashConfig = defineConfig({
23
- * default: 'scrypt',
24
- * list: {
25
- * scrypt: drivers.scrypt({
26
- * cost: 16384,
27
- * blockSize: 8,
28
- * parallelization: 1,
29
- * saltSize: 16,
30
- * keyLength: 64,
31
- * }),
32
- * bcrypt: drivers.bcrypt({
33
- * rounds: 10,
34
- * })
35
- * }
36
- * })
37
- * ```
38
- */
39
- export function defineConfig(config) {
40
- /**
41
- * Hashers list should always be provided
42
- */
43
- if (!config.list) {
44
- throw new InvalidArgumentsException('Missing "list" property in hash config');
45
- }
46
- /**
47
- * The default hasher should be mentioned in the list
48
- */
49
- if (config.default && !config.list[config.default]) {
50
- throw new InvalidArgumentsException(`Missing "list.${String(config.default)}" in hash config. It is referenced by the "default" property`);
51
- }
52
- /**
53
- * Config provider to lazily import drivers as they are used inside
54
- * the user application
55
- */
56
- return configProvider.create(async (app) => {
57
- debug('resolving hash config');
58
- const hashersList = Object.keys(config.list);
59
- const hashers = {};
60
- for (let hasherName of hashersList) {
61
- const hasher = config.list[hasherName];
62
- if (typeof hasher === 'function') {
63
- hashers[hasherName] = hasher;
64
- }
65
- else {
66
- hashers[hasherName] = await hasher.resolver(app);
67
- }
68
- }
69
- return {
70
- default: config.default,
71
- list: hashers,
72
- };
73
- });
74
- }
75
- /**
76
- * Helpers to configure drivers inside the config file. These functions create
77
- * configuration providers that lazily import and instantiate hash drivers.
78
- *
79
- * - Import happens when you first use the hash module
80
- * - Construction of drivers happens when you first use a driver
81
- *
82
- * @example
83
- * ```ts
84
- * const hashConfig = defineConfig({
85
- * default: 'bcrypt',
86
- * list: {
87
- * bcrypt: drivers.bcrypt({ rounds: 12 }),
88
- * argon2: drivers.argon2({
89
- * variant: 'id',
90
- * memory: 65536,
91
- * time: 3,
92
- * parallelism: 4
93
- * }),
94
- * scrypt: drivers.scrypt({
95
- * cost: 16384,
96
- * blockSize: 8,
97
- * parallelization: 1
98
- * })
99
- * }
100
- * })
101
- * ```
102
- */
103
- export const drivers = {
104
- argon2: (config) => {
105
- return configProvider.create(async () => {
106
- const { Argon } = await import('./drivers/argon.js');
107
- debug('configuring argon driver');
108
- return () => new Argon(config);
109
- });
110
- },
111
- bcrypt: (config) => {
112
- return configProvider.create(async () => {
113
- const { Bcrypt } = await import('./drivers/bcrypt.js');
114
- debug('configuring bcrypt driver');
115
- return () => new Bcrypt(config);
116
- });
117
- },
118
- scrypt: (config) => {
119
- return configProvider.create(async () => {
120
- const { Scrypt } = await import('./drivers/scrypt.js');
121
- debug('configuring scrypt driver');
122
- return () => new Scrypt(config);
123
- });
124
- },
125
- };
@@ -1,100 +0,0 @@
1
- /*
2
- * @adonisjs/core
3
- *
4
- * (c) AdonisJS
5
- *
6
- * For the full copyright and license information, please view the LICENSE
7
- * file that was distributed with this source code.
8
- */
9
- /**
10
- * Request validator for validating HTTP request data using VineJS validators.
11
- * This class provides a convenient way to validate request body, files, cookies,
12
- * headers, and URL parameters in AdonisJS applications.
13
- *
14
- * @example
15
- * ```ts
16
- * // Inside a controller method
17
- * const data = await request.validateUsing(createUserValidator, {
18
- * messagesProvider: customMessages
19
- * })
20
- * ```
21
- */
22
- export class RequestValidator {
23
- #ctx;
24
- #experimentalFlags;
25
- constructor(ctx, experimentalFlags) {
26
- this.#ctx = ctx;
27
- this.#experimentalFlags = experimentalFlags;
28
- }
29
- /**
30
- * The error reporter method returns the error reporter
31
- * to use for reporting errors.
32
- *
33
- * You can use this function to pick a different error reporter
34
- * for each HTTP request
35
- */
36
- static errorReporter;
37
- /**
38
- * The messages provider method returns the messages provider to use
39
- * finding custom error messages
40
- *
41
- * You can use this function to pick a different messages provider for
42
- * each HTTP request
43
- */
44
- static messagesProvider;
45
- /**
46
- * Validate the current HTTP request data using a VineJS validator.
47
- * This method automatically includes request body, files, URL parameters,
48
- * headers, and cookies in the validation data.
49
- *
50
- * @param validator - VineJS validator instance
51
- * @param options - Optional validation options including custom error reporters and messages
52
- *
53
- * @example
54
- * ```ts
55
- * const createUserValidator = vine.compile(
56
- * vine.object({
57
- * email: vine.string().email(),
58
- * name: vine.string().minLength(3)
59
- * })
60
- * )
61
- *
62
- * const data = await request.validateUsing(createUserValidator, {
63
- * errorReporter: () => vine.errors.SimpleErrorReporter,
64
- * messagesProvider: customMessages
65
- * })
66
- * ```
67
- */
68
- validateUsing(validator, ...[options]) {
69
- const validatorOptions = options || {};
70
- /**
71
- * Assign request specific error reporter
72
- */
73
- if (RequestValidator.errorReporter && !validatorOptions.errorReporter) {
74
- const errorReporter = RequestValidator.errorReporter(this.#ctx);
75
- validatorOptions.errorReporter = () => errorReporter;
76
- }
77
- /**
78
- * Assign request specific messages provider
79
- */
80
- if (RequestValidator.messagesProvider && !validatorOptions.messagesProvider) {
81
- validatorOptions.messagesProvider = RequestValidator.messagesProvider(this.#ctx);
82
- }
83
- const requestBody = this.#experimentalFlags?.enabled('mergeMultipartFieldsAndFiles')
84
- ? this.#ctx.request.all()
85
- : {
86
- ...this.#ctx.request.all(),
87
- ...this.#ctx.request.allFiles(),
88
- };
89
- /**
90
- * Data to validate
91
- */
92
- const data = validatorOptions.data || {
93
- ...requestBody,
94
- params: this.#ctx.request.params(),
95
- headers: this.#ctx.request.headers(),
96
- cookies: this.#ctx.request.cookiesList(),
97
- };
98
- return validator.validate(data, validatorOptions);
99
- }
100
- }