Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxied object has function type instead of object type #82

Closed
Patton-L opened this issue Aug 15, 2023 · 4 comments · Fixed by #95
Closed

Proxied object has function type instead of object type #82

Patton-L opened this issue Aug 15, 2023 · 4 comments · Fixed by #95
Labels
bug Something isn't working
Milestone

Comments

@Patton-L
Copy link

Patton-L commented Aug 15, 2023

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 Papooch added the enhancement New feature or request label Aug 16, 2023
@Papooch
Copy link
Owner

Papooch commented Aug 16, 2023

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.

@Papooch Papooch added bug Something isn't working investigation Why is this happening? and removed enhancement New feature or request labels Aug 16, 2023
@aashir-khan
Copy link

@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
Copy link
Owner

Papooch commented Aug 16, 2023

I see, hmm. Your options are currently:

  • either wrapping the data source inside another proxy provider, rather than making a proxy provider out of itself,
  • or using "raw" cls.get(...).

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

@Papooch
Copy link
Owner

Papooch commented Jan 19, 2024

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

@Papooch Papooch added this to the 4.0 milestone Jan 22, 2024
@Papooch Papooch closed this as completed Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants