Replies: 1 comment
-
You meant // <root>/server/plugins/service.ts
import { service } from 'path_service'
declare module 'h3' {
interface H3Context {
service: typeof service
}
}
export default defineNitroPlugin((nitroApp) => {
nitro.hooks.hook("request", (event) => {
event.context.service = service
});
})
// <root>/server/api/test.ts
export default defineEventHandler((event) => {
const service = event.context.service
// all code use service
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello. I wonder how to create a singleton object at initial runtime for used in route handlers later, i.e a service object.
For example, to achieve what I mean on a simple web server using Express it would look as follows.
I can achieve this approach by initialize the object in package file, like so:
But with this approach, it's not testable. Because when
Service
class imported in test file, it should also import all service dependencies, which initializes those dependencies as well. Whereas in the test those dependencies will be mocked.How to achieve this with nitro?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions