Skip to content

Commit aae3f1c

Browse files
committed
feat: add HTTP transformers
1 parent 493b77e commit aae3f1c

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

modules/http/request_validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class RequestValidator {
7676
* name: vine.string().minLength(3)
7777
* })
7878
* )
79-
*
79+
*
8080
* const data = await request.validateUsing(createUserValidator, {
8181
* errorReporter: () => vine.errors.SimpleErrorReporter,
8282
* messagesProvider: customMessages

modules/transformers/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export * from '@adonisjs/http-transformers'

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"./http": "./build/modules/http/main.js",
5757
"./logger": "./build/modules/logger.js",
5858
"./repl": "./build/modules/repl.js",
59+
"./transformers": "./build/modules/transformers/main.js",
5960
"./package.json": "./package.json",
6061
"./exceptions": "./build/src/exceptions.js",
6162
"./test_utils": "./build/src/test_utils/main.js",
@@ -130,6 +131,7 @@
130131
"@adonisjs/hash": "^10.0.0-next.1",
131132
"@adonisjs/health": "^3.0.0-next.0",
132133
"@adonisjs/http-server": "^8.0.0-next.6",
134+
"@adonisjs/http-transformers": "^1.1.0",
133135
"@adonisjs/logger": "^7.1.0-next.0",
134136
"@adonisjs/repl": "^5.0.0-next.0",
135137
"@poppinss/colors": "^4.1.5",

providers/app_provider.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,24 @@ import { Config } from '../modules/config.ts'
1414
import { Logger } from '../modules/logger.ts'
1515
import { Application } from '../modules/app.ts'
1616
import { Dumper } from '../modules/dumper/dumper.ts'
17+
import { HttpContext } from '../modules/http/main.ts'
1718
import { Encryption } from '../modules/encryption.ts'
1819
import { Router, Server } from '../modules/http/main.ts'
1920
import { BaseEvent, Emitter } from '../modules/events.ts'
21+
import { transform } from '../modules/transformers/main.ts'
22+
import { type TransformFn } from '../types/transformers.ts'
2023
import type { ApplicationService, LoggerService } from '../src/types.ts'
2124
import BodyParserMiddleware from '../modules/bodyparser/bodyparser_middleware.ts'
2225

26+
/**
27+
* Extend HTTP request class with the transform method
28+
*/
29+
declare module '@adonisjs/core/http' {
30+
export interface HttpContext {
31+
transform: TransformFn
32+
}
33+
}
34+
2335
/**
2436
* The Application Service provider registers all the baseline
2537
* features required to run the framework.
@@ -170,7 +182,7 @@ export default class AppServiceProvider {
170182
*/
171183
protected async generateRoutesTypes(router: Router) {
172184
const types = router.generateTypes(4)
173-
const outputPath = this.app.makePath('.adonisjs/server/routes.d.ts')
185+
const outputPath = this.app.generatedServerPath('routes.d.ts')
174186

175187
await mkdir(dirname(outputPath), { recursive: true })
176188
await writeFile(
@@ -219,6 +231,9 @@ export default class AppServiceProvider {
219231

220232
async boot() {
221233
BaseEvent.useEmitter(await this.app.container.make('emitter'))
234+
HttpContext.macro('transform', function (this: HttpContext, data, transformer, variant) {
235+
return transform(data, transformer, variant, this.containerResolver) as any
236+
})
222237
}
223238

224239
async ready() {

tests/providers.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Logger, LoggerManager } from '../modules/logger.ts'
2323
import { IgnitorFactory } from '../factories/core/ignitor.ts'
2424
import BodyParserMiddleware from '../modules/bodyparser/bodyparser_middleware.ts'
2525
import { defineConfig as defineDumperConfig } from '../modules/dumper/define_config.ts'
26+
import { HttpContext } from '@adonisjs/http-server'
2627

2728
const BASE_URL = new URL('./tmp/', import.meta.url)
2829
const BASE_PATH = fileURLToPath(BASE_URL)
@@ -313,4 +314,21 @@ test.group('Providers', () => {
313314
'posts.index',
314315
])
315316
})
317+
318+
test('add transform method to HTTP context', async ({ assert }) => {
319+
const ignitor = new IgnitorFactory()
320+
.merge({
321+
rcFileContents: defineConfig({
322+
providers: [() => import('../providers/app_provider.js')],
323+
}),
324+
})
325+
.withCoreConfig()
326+
.create(BASE_URL)
327+
328+
const app = ignitor.createApp('repl')
329+
await app.init()
330+
await app.boot()
331+
332+
assert.isFunction(HttpContext.prototype.transform)
333+
})
316334
})

types/transformers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
export * from '@adonisjs/http-transformers/types'

0 commit comments

Comments
 (0)