Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
427 stars 28 forks source link

Proxied object has function type instead of object type #82

Closed Patton-L closed 8 months ago

Patton-L commented 1 year ago

Proxy is defined as ()=>null at https://github.com/Papooch/nestjs-cls/blob/main/packages/core/src/lib/proxy-provider/proxy-provider-manager.ts#L95C20-L95C25

Is there a specific reason for doing that instead of defining it as an empty object? It's causing object retrieved from cls has typeof proxiedObject equal to function instead of object.

Papooch commented 1 year ago

Hm, if I remember correctly, there was some issue with proxying functions instead of objects. If I remember correctly, JavaScript then complained, that the object's signature doesn't implement the [[Call]] method or something, so that's why I used a function as the "target", which has all features of objects in addition to being callable.

The problem is, that we don't know the actual type before creating the proxy. It ts created at bootstrap, but the instance it proxies to is created on each request anew. And the Proxy cannot trap the typeof call :(

Can you tell me what your use-case is? I will try to revisit if there's some way to get around this.

aashir-khan commented 1 year ago

@Papooch the usecase is using nestjs-cls with typeorm 0.3.17 (we used to use 0.2.34). After we upgraded to 0.3.17, then this line of code (where isDataSource is defined here) ended up being false since the proxy was a function instead of object

the usecase is switching on the typeorm DataSource depending on a header value passed in a request (req.headers)

Papooch commented 1 year ago

I see, hmm. Your options are currently:

I'll try to think of some other way in the new version and add this information to the documentation.

Papooch commented 8 months ago

A fix has been merged and will be released in a future version (v4) together with other breaking changes and features.