A DI framework for frontend.
import package
# use npm
npm install @yorjs/core
# use pnpm
pnpm add @yorjs/core
import module
import { } from '@yorjs/core'
Define a injectable module.
import { defineProvider } from '@yorjs/core'
interface Provider {
do: () => void
}
const providerImpl = defineProvider<Provider>(() => ({
do() { return 'done' }
}))
Define a controller for view.
import { defineController } from '@yorjs/core'
interface Controller {
message: string
doSth: () => void
}
const controllerImpl = defineController<Controller>((provider: Provider) => {
const message = ''
const doSth = () => {
message = provider.do()
}
return { message, doSth }
})
Define a module for controller dependencies relationship.
import { defineModule } from '@yorjs/core'
const module = defineModule({
controller: controllerImpl.dependencies(providerImpl),
providers: [providerImpl]
})
Use a module implement in view.
import { useModule } from '@yorjs/core'
const { message } = useModule(module)
Define a guard for a controller or provider.
Basic Usage
import { defineGuard } from '@yorjs/core'
export const randomGuard = defineGuard(() => {
return Math.random() <= 0.5
}).error((context) => {
// ...
})
Binding guards
import { providerImpl } from '../'
providerImpl.useGuards(randomGuard, otherGuard)
Define a interceptor for a controller or provider.
Basic Usage
import { defineInterceptor } from '@yorjs/core'
export const loggingInterceptor = defineInterceptor((context) => {
// before...
return () => {
// after...
}
})
Binding interceptors
import { providerImpl } from '../'
providerImpl.useGuards(loggingInterceptor, otherInterceptor)