I'm working on a basic TypeScript project with a really basic router library. It doesn't really matter but this is for Cloudflare Workers.
A few times in the past I've run into problems where I am able to use the DIContainer directly to get dependencies in my code, but the dependency injection annotations e.g. @Inject(TYPES.Example) do not work - specifically they return undefined.
I have read a bit and understand that under the hood the reflect-metadata is intended to be a singleton, one version of this through the entire "app". I've read many posts / questions where it turns out people are including this more than once, and so (in my mind, I'm sure it's more complicated) the object with your dependencies are flushed and you end up not having any reference to objects/constructors/factories/etc you can inject.
What I'm a bit confused by (not being a node, typescript, or webpack expert by any means!) is exactly where in my specific situation I should be including reflect-metadata. I am using a index.ts entry point, and currently it looks like this:
import "reflect-metadata";
import { DIContainer } from '../inversify.config';
import { Router } from "cloudflare-router";
import OrderHandler from "./handlers/order/order-handler";
import { TYPES } from "./di/types";;
const router = new Router();
const apiRouter = new Router();
// Connecting routers
router.use("/api", apiRouter);
// Setting up paths
router.get("/", new OrderHandler().handle);
addEventListener("fetch", event => {
event.respondWith(
router.serve(event.request)
.then(res => res.response) as Promise<Response>
);
});
In my DIContainer, e.g. my container configuration, I do NOT import "reflect-metadata" (but of course I make sure to do so first in my index.ts entry point here).
I ALSO have a tsconfig.json file, which has reflect-metadata as a compiler option in the types object:
Hello!
I'm working on a basic TypeScript project with a really basic router library. It doesn't really matter but this is for Cloudflare Workers.
A few times in the past I've run into problems where I am able to use the DIContainer directly to get dependencies in my code, but the dependency injection annotations e.g.
@Inject(TYPES.Example)
do not work - specifically they returnundefined
.I have read a bit and understand that under the hood the
reflect-metadata
is intended to be a singleton, one version of this through the entire "app". I've read many posts / questions where it turns out people are including this more than once, and so (in my mind, I'm sure it's more complicated) the object with your dependencies are flushed and you end up not having any reference to objects/constructors/factories/etc you can inject.What I'm a bit confused by (not being a node, typescript, or webpack expert by any means!) is exactly where in my specific situation I should be including
reflect-metadata
. I am using a index.ts entry point, and currently it looks like this:In my
DIContainer
, e.g. my container configuration, I do NOT import"reflect-metadata"
(but of course I make sure to do so first in my index.ts entry point here).I ALSO have a
tsconfig.json
file, which has reflect-metadata as a compiler option in thetypes
object:So concretely, I guess my questions are:
Thank you!