yorjs
v1.0.0
Published
[![NPM version](https://img.shields.io/npm/v/@yorjs/core)](https://www.npmjs.com/package/@yorjs/core)
Downloads
1
Readme
Yorjs
A DI framework for frontend.
Installation
import package
# use npm
npm install @yorjs/core
# use pnpm
pnpm add @yorjs/core
import module
import { } from '@yorjs/core'
Usage
defineProvider
Define a injectable module.
Basic Usage
import { defineProvider } from '@yorjs/core'
interface Provider {
do: () => void
}
const providerImpl = defineProvider<Provider>(() => ({
do() { return 'done' }
}))
defineController
Define a controller for view.
Basic Usage
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 }
})
defineModule
Define a module for controller dependencies relationship.
Basic Usage
import { defineModule } from '@yorjs/core'
const module = defineModule({
controller: controllerImpl.dependencies(providerImpl),
providers: [providerImpl]
})
useModule
Use a module implement in view.
Basic Usage
import { useModule } from '@yorjs/core'
const { message } = useModule(module)
defineGuard
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)
defineInterceptor
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)