Skip to content

Commit b52c2c8

Browse files
committed
refactor: remove usage of import.meta.resolve API
1 parent 7903e5e commit b52c2c8

File tree

6 files changed

+62
-59
lines changed

6 files changed

+62
-59
lines changed

factories/router.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export class RouterFactory {
3131
* Returns an instance of the application class
3232
*/
3333
#getApp() {
34-
return (
35-
this.#parameters.app || new AppFactory().create(new URL('./app/', import.meta.url), () => {})
36-
)
34+
return this.#parameters.app || new AppFactory().create(new URL('./app/', import.meta.url))
3735
}
3836

3937
/**

factories/server_factory.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ export class ServerFactory {
5858
* Returns an instance of the application class
5959
*/
6060
#getApp() {
61-
return (
62-
this.#parameters.app || new AppFactory().create(new URL('./app/', import.meta.url), () => {})
63-
)
61+
return this.#parameters.app || new AppFactory().create(new URL('./app/', import.meta.url))
6462
}
6563

6664
/**

src/router/route.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Macroable from '@poppinss/macroable'
1212
import Middleware from '@poppinss/middleware'
1313
import { RuntimeException } from '@poppinss/utils'
1414
import type { Application } from '@adonisjs/application'
15-
import { moduleCaller, moduleExpression, moduleImporter } from '@adonisjs/fold'
15+
import { moduleCaller, moduleImporter } from '@adonisjs/fold'
1616

1717
import { execute } from './executor.js'
1818
import { dropSlash } from '../helpers.js'
@@ -139,10 +139,18 @@ export class Route<Controller extends Constructor<any> = any> extends Macroable
139139
| string
140140
| [LazyImport<Controller> | Controller, GetControllerHandlers<Controller>?]
141141
) {
142+
/**
143+
* Convert magic string to handle method call
144+
*/
142145
if (typeof handler === 'string') {
146+
const parts = handler.split('.')
147+
const method = parts.length === 1 ? 'handle' : parts.pop()!
148+
const moduleRefId = parts.join('.')
149+
143150
return {
144151
reference: handler,
145-
...moduleExpression(handler, this.#app.appRoot).toHandleMethod(),
152+
...moduleImporter(() => this.#app.import(moduleRefId), method).toHandleMethod(),
153+
name: handler,
146154
}
147155
}
148156

tests/define_config.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ test.group('Define config', () => {
1919
httpOnly: true,
2020
path: '/',
2121
maxAge: 7200,
22-
sameSite: false,
23-
secure: false,
22+
sameSite: 'lax',
23+
secure: true,
2424
},
2525
generateRequestId: false,
2626
jsonpCallbackName: 'callback',
@@ -37,8 +37,8 @@ test.group('Define config', () => {
3737
httpOnly: true,
3838
path: '/',
3939
maxAge: 7200,
40-
sameSite: false,
41-
secure: false,
40+
sameSite: 'lax',
41+
secure: true,
4242
},
4343
generateRequestId: false,
4444
jsonpCallbackName: 'callback',

tests/router/execute.spec.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test.group('Route | execute', () => {
2424
assert.plan(2)
2525

2626
const stack: string[] = []
27-
const app = new AppFactory().create(BASE_URL, () => {})
27+
const app = new AppFactory().create(BASE_URL)
2828
await app.init()
2929

3030
const context = new HttpContextFactory().create()
@@ -47,41 +47,40 @@ test.group('Route | execute', () => {
4747
})
4848

4949
test('execute route controller specified as a string', async ({ assert }) => {
50-
assert.plan(3)
51-
5250
const stack: string[] = []
53-
const app = new AppFactory().create(BASE_URL, () => {})
51+
52+
class HomeControllerClass {
53+
async handle() {
54+
stack.push('invoked handle')
55+
}
56+
}
57+
const app = new AppFactory().create(BASE_URL, (filePath: string) => {
58+
if (filePath === '#controllers/home') {
59+
return {
60+
default: HomeControllerClass,
61+
}
62+
}
63+
})
5464
await app.init()
5565

5666
const resolver = app.container.createResolver()
57-
5867
const context = new HttpContextFactory().create()
5968

6069
const route = new Route(app, [], {
6170
pattern: '/',
6271
methods: ['GET'],
63-
handler: () => {},
72+
handler: '#controllers/home',
6473
globalMatchers: {},
6574
})
6675

6776
const routeJSON = route.toJSON()
68-
69-
routeJSON.handler = {
70-
reference: '#controllers/home',
71-
async handle(container, ctx) {
72-
assert.strictEqual(container, resolver)
73-
assert.strictEqual(ctx, context)
74-
stack.push('controller')
75-
},
76-
}
77-
7877
await routeJSON.execute(routeJSON, resolver, context, () => {})
79-
assert.deepEqual(stack, ['controller'])
78+
assert.deepEqual(stack, ['invoked handle'])
8079
})
8180

8281
test('execute route controller specified as lazy import', async ({ assert }) => {
8382
const stack: string[] = []
84-
const app = new AppFactory().create(BASE_URL, () => {})
83+
const app = new AppFactory().create(BASE_URL)
8584
await app.init()
8685

8786
const resolver = app.container.createResolver()
@@ -128,7 +127,7 @@ test.group('Route | execute', () => {
128127

129128
test('execute route controller specified as a class constructor', async ({ assert }) => {
130129
const stack: string[] = []
131-
const app = new AppFactory().create(BASE_URL, () => {})
130+
const app = new AppFactory().create(BASE_URL)
132131
await app.init()
133132

134133
const resolver = app.container.createResolver()
@@ -171,7 +170,7 @@ test.group('Route | execute', () => {
171170
assert.plan(4)
172171

173172
const stack: string[] = []
174-
const app = new AppFactory().create(BASE_URL, () => {})
173+
const app = new AppFactory().create(BASE_URL)
175174
await app.init()
176175

177176
const context = new HttpContextFactory().create()
@@ -210,7 +209,7 @@ test.group('Route | execute', () => {
210209
assert.plan(3)
211210

212211
const stack: string[] = []
213-
const app = new AppFactory().create(BASE_URL, () => {})
212+
const app = new AppFactory().create(BASE_URL)
214213
await app.init()
215214

216215
const context = new HttpContextFactory().create()
@@ -246,7 +245,7 @@ test.group('Route | execute', () => {
246245
assert.plan(6)
247246

248247
const stack: string[] = []
249-
const app = new AppFactory().create(BASE_URL, () => {})
248+
const app = new AppFactory().create(BASE_URL)
250249
await app.init()
251250

252251
class BodyParserMiddleware {
@@ -313,7 +312,7 @@ test.group('Route | execute', () => {
313312
assert.plan(3)
314313

315314
const stack: string[] = []
316-
const app = new AppFactory().create(BASE_URL, () => {})
315+
const app = new AppFactory().create(BASE_URL)
317316
await app.init()
318317

319318
class BodyParserMiddleware {
@@ -375,7 +374,7 @@ test.group('Route | execute', () => {
375374

376375
test('catch global middleware exceptions', async ({ assert }) => {
377376
const stack: string[] = []
378-
const app = new AppFactory().create(BASE_URL, () => {})
377+
const app = new AppFactory().create(BASE_URL)
379378
await app.init()
380379

381380
class BodyParserMiddleware {
@@ -439,7 +438,7 @@ test.group('Route | execute', () => {
439438

440439
test('catch route handler exceptions', async ({ assert }) => {
441440
const stack: string[] = []
442-
const app = new AppFactory().create(BASE_URL, () => {})
441+
const app = new AppFactory().create(BASE_URL)
443442
await app.init()
444443

445444
class BodyParserMiddleware {
@@ -502,7 +501,7 @@ test.group('Route | execute', () => {
502501

503502
test('pass arguments to the named middleware', async ({ assert }) => {
504503
const stack: any[] = []
505-
const app = new AppFactory().create(BASE_URL, () => {})
504+
const app = new AppFactory().create(BASE_URL)
506505
await app.init()
507506

508507
class AclMiddleware {

0 commit comments

Comments
 (0)