Skip to content

Commit

Permalink
refactor: toggle usingVineJS and usingEdgeJS flags inside their servi…
Browse files Browse the repository at this point in the history
…ce providers
  • Loading branch information
thetutlage committed Nov 23, 2023
1 parent d6a29fe commit 8988cbb
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 122 deletions.
4 changes: 3 additions & 1 deletion providers/edge_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ declare module '@adonisjs/core/http' {
* an AdonisJS application environment
*/
export default class EdgeServiceProvider {
constructor(protected app: ApplicationService) {}
constructor(protected app: ApplicationService) {
this.app.usingEdgeJS = true
}

/**
* Bridge AdonisJS and Edge
Expand Down
218 changes: 109 additions & 109 deletions providers/repl_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,115 +49,115 @@ export default class ReplServiceProvider {
* Registering REPL bindings during provider boot
*/
async boot() {
const repl = await this.app.container.make('repl')

repl.addMethod(
'importDefault',
(_, modulePath: string) => {
return this.app.importDefault(modulePath)
},
{
description: 'Returns the default export for a module',
}
)

repl.addMethod(
'make',
(_, service: any, runtimeValues?: any[]) => {
return this.app.container.make(service, runtimeValues)
},
{
description: 'Make class instance using "container.make" method',
}
)

repl.addMethod(
'loadApp',
() => {
return resolveBindingForRepl(this.app, repl, 'app')
},
{
description: 'Load "app" service in the REPL context',
}
)

repl.addMethod(
'loadEncryption',
() => {
return resolveBindingForRepl(this.app, repl, 'encryption')
},
{
description: 'Load "encryption" service in the REPL context',
}
)

repl.addMethod(
'loadHash',
() => {
return resolveBindingForRepl(this.app, repl, 'hash')
},
{
description: 'Load "hash" service in the REPL context',
}
)

repl.addMethod(
'loadRouter',
() => {
return resolveBindingForRepl(this.app, repl, 'router')
},
{
description: 'Load "router" service in the REPL context',
}
)

repl.addMethod(
'loadConfig',
() => {
return resolveBindingForRepl(this.app, repl, 'config')
},
{
description: 'Load "config" service in the REPL context',
}
)

repl.addMethod(
'loadTestUtils',
() => {
return resolveBindingForRepl(this.app, repl, 'testUtils')
},
{
description: 'Load "testUtils" service in the REPL context',
}
)

repl.addMethod(
'loadHelpers',
async () => {
const { default: isModule } = await import('../src/helpers/is.js')
const { default: stringModule } = await import('../src/helpers/string.js')
const { base64, cuid, fsReadAll, slash, parseImports } = await import(
'../src/helpers/main.js'
)
repl.server!.context.helpers = {
string: stringModule,
is: isModule,
base64,
cuid,
fsReadAll,
slash,
parseImports,
this.app.container.resolving('repl', (repl) => {
repl.addMethod(
'importDefault',
(_, modulePath: string) => {
return this.app.importDefault(modulePath)
},
{
description: 'Returns the default export for a module',
}

repl.notify(
`Loaded "helpers" module. You can access it using the "${repl.colors.underline(
'helpers'
)}" variable`
)
},
{
description: 'Load "helpers" module in the REPL context',
}
)
)

repl.addMethod(
'make',
(_, service: any, runtimeValues?: any[]) => {
return this.app.container.make(service, runtimeValues)
},
{
description: 'Make class instance using "container.make" method',
}
)

repl.addMethod(
'loadApp',
() => {
return resolveBindingForRepl(this.app, repl, 'app')
},
{
description: 'Load "app" service in the REPL context',
}
)

repl.addMethod(
'loadEncryption',
() => {
return resolveBindingForRepl(this.app, repl, 'encryption')
},
{
description: 'Load "encryption" service in the REPL context',
}
)

repl.addMethod(
'loadHash',
() => {
return resolveBindingForRepl(this.app, repl, 'hash')
},
{
description: 'Load "hash" service in the REPL context',
}
)

repl.addMethod(
'loadRouter',
() => {
return resolveBindingForRepl(this.app, repl, 'router')
},
{
description: 'Load "router" service in the REPL context',
}
)

repl.addMethod(
'loadConfig',
() => {
return resolveBindingForRepl(this.app, repl, 'config')
},
{
description: 'Load "config" service in the REPL context',
}
)

repl.addMethod(
'loadTestUtils',
() => {
return resolveBindingForRepl(this.app, repl, 'testUtils')
},
{
description: 'Load "testUtils" service in the REPL context',
}
)

repl.addMethod(
'loadHelpers',
async () => {
const { default: isModule } = await import('../src/helpers/is.js')
const { default: stringModule } = await import('../src/helpers/string.js')
const { base64, cuid, fsReadAll, slash, parseImports } = await import(
'../src/helpers/main.js'
)
repl.server!.context.helpers = {
string: stringModule,
is: isModule,
base64,
cuid,
fsReadAll,
slash,
parseImports,
}

repl.notify(
`Loaded "helpers" module. You can access it using the "${repl.colors.underline(
'helpers'
)}" variable`
)
},
{
description: 'Load "helpers" module in the REPL context',
}
)
})
}
}
37 changes: 25 additions & 12 deletions providers/vinejs_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import vine, { BaseLiteralType, Vine } from '@vinejs/vine'
import type { Validation, FieldContext, FieldOptions } from '@vinejs/vine/types'
import type { MultipartFile, FileValidationOptions } from '@adonisjs/bodyparser/types'

import type { ApplicationService } from '../src/types.js'
import { Request, RequestValidator } from '../modules/http/main.js'

/**
Expand Down Expand Up @@ -115,17 +116,29 @@ class VineMultipartFile extends BaseLiteralType<MultipartFile, MultipartFile> {
}

/**
* The file method is used to validate a field to be a valid
* multipart file.
* The Edge service provider configures Edge to work within
* an AdonisJS application environment
*/
Vine.macro('file', function (this: Vine, options) {
return new VineMultipartFile(options)
})
export default class VineJSServiceProvider {
constructor(protected app: ApplicationService) {
this.app.usingVineJS = true
}

/**
* The validate method can be used to validate the request
* data for the current request using VineJS validators
*/
Request.macro('validateUsing', function (this: Request, ...args) {
return new RequestValidator(this.ctx!).validateUsing(...args)
})
boot() {
/**
* The file method is used to validate a field to be a valid
* multipart file.
*/
Vine.macro('file', function (this: Vine, options) {
return new VineMultipartFile(options)
})

/**
* The validate method can be used to validate the request
* data for the current request using VineJS validators
*/
Request.macro('validateUsing', function (this: Request, ...args) {
return new RequestValidator(this.ctx!).validateUsing(...args)
})
}
}

0 comments on commit 8988cbb

Please sign in to comment.