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

Make this project compatible with expressjs outside of nestjs #867

Open
samislam opened this issue Feb 8, 2024 · 2 comments
Open

Make this project compatible with expressjs outside of nestjs #867

samislam opened this issue Feb 8, 2024 · 2 comments

Comments

@samislam
Copy link

samislam commented Feb 8, 2024

I am using this package outside of nestjs in a pure expressjs application. it works well but there are some issues.

However, I recently discovered that this file in the project is the only thing that can't be applied in express because we can't use decorators outside of classes. If we can modify this file and split it into two functions, one exported as a general function, the other is what makes up a decorator from it, we can import the regular function, then wrap it as an express middleware and use it right in our projects.

src/decorator.ts

click here to view the file.

Let me know what are your opinions.

For example, I was able to get it to work by doing this

@ppetzold
Copy link
Owner

Great point - might just build 2 packages from this repo. You are welcome to restructure the code 2 support both versions. I will take care of the release process.

@Helveg
Copy link
Collaborator

Helveg commented Mar 3, 2024

Even in NestJS I've run into a use case for this. It requires a pretty nasty workaround at the moment to apply the inner function of the Paginate decorator:

// To assemble the PaginateQuery, we fish out `nestjs-paginate`'s paginate query
// factory from the NestJS parameter decorator

// First, we decorate our dec dummy with the Paginate decorator
const dec = class {};
Paginate()(dec, 'fishy fishy', 0);
// And we declare the types of what we're going to fish out
return Object.values<{
  factory: (_data: unknown, ctx: ExecutionContext) => PaginateQuery;
}>(
  // Then we fish out the route args metadata factory for what we just decorated.
  Reflect.getMetadata(
    ROUTE_ARGS_METADATA,
    dec.constructor,
    'fishy fishy',
  ),
  // Then we use the fished out factory to produce the PaginateQuery from the
  // spoofed request context
)[0].factory(null, reqCtx);

It'd be nicer if we split out a getPaginateQuery(req) function.

PS: I must have amnesia, I had appearantly already opened an issue here: #825

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants