Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Migration help: Can Interceptors work with mouted lb3 application? #6099

Closed
dzhiwei opened this issue Aug 12, 2020 · 7 comments
Closed

Migration help: Can Interceptors work with mouted lb3 application? #6099

dzhiwei opened this issue Aug 12, 2020 · 7 comments
Assignees

Comments

@dzhiwei
Copy link

dzhiwei commented Aug 12, 2020

LoopBack 3 use case

I have one LB3 project with operation hook function used: .afterRemote and .observe('before save'.

LoopBack 4 new solution

I am referring to this toturial: https://loopback.io/doc/en/lb4/migration-mounting-lb3app.html for migration now.
The good news is that Swagger API is working after the migration.
Unfortunally, I find the operation hook will not work anymore after I mount LB3 project as a component in LB4.
And there's interceptors will take the role of operation hooks, But it seems inteceptors is only working with LB4 Controllers and handler functions for REST routes.

Is that possible LB4 interceptors can work with mouted LB3 application? Or am I misunderstanding the LB4 interceptors use case?
Not sure if we can configure my global interceptor to app in the index.ts. I did't find a good example to explain the

app.interceptor(CachingInterceptorProvider, {
  global: true,
  group: 'caching',
  key: 'caching-interceptor',
});

Thank you so much.

@dhmlau
Copy link
Member

dhmlau commented Aug 13, 2020

@dzhiwei, I believe the closest migration path for the operation hooks is to override the definePersistedModel method in DefaultCrudRepository in the model’s repository. You can find more details in: https://loopback.io/doc/en/lb4/migration-models-operation-hooks.html.

@dhmlau dhmlau self-assigned this Aug 13, 2020
@dzhiwei
Copy link
Author

dzhiwei commented Aug 13, 2020

@dzhiwei, I believe the closest migration path for the operation hooks is to override the definePersistedModel method in DefaultCrudRepository in the model’s repository. You can find more details in: https://loopback.io/doc/en/lb4/migration-models-operation-hooks.html.

@dhmlau Thank you so much for your reply.
Currently I only have loopback 3 models, does that mean I need to rewrite the loopback 4 repository for loopback 3 models to make the operation hooks function work?
The perferred option is that no more code for rewriting the loopback 3 models and only the remoting/operation hooks will be implemente with Loopback 4 manner. Is that feasible? I would not like to rewrite it, because It will take too much effort to rewrite the whole thing if I need to use LB4 repository instead of LB3 models(in lb3app).

@dhmlau
Copy link
Member

dhmlau commented Aug 17, 2020

@hacksparrow @nabdelgadir, do you think you can help with the question since you both were working on the mounting LB3 app guides? Thanks.

@dhmlau dhmlau assigned hacksparrow and nabdelgadir and unassigned dhmlau Aug 17, 2020
@nabdelgadir
Copy link
Contributor

Hi, sorry for the delayed response. If you want to use a global interceptor, try using the lb4 interceptor command e.g.

$ lb4 interceptor
? Interceptor name: global
? Is it a global interceptor? Yes

Global interceptors are sorted by the order of an array of group names bound to ContextBindings.GLOBAL_INTERCEPTOR_ORDERED_GROUPS. See https://loopback.io/doc/en/lb4/Interceptors.html#order-of-invocation-for-interceptors.

? Group name for the global interceptor: ('') 
   create src/interceptors/global.interceptor.ts
   update src/interceptors/index.ts

Interceptor Global was/were created in src/interceptors

In the created src/interceptors/global.interceptor.ts (or whatever you name it) file, you can add your logic there.

Let us know if this works for you. Thanks!

@dhmlau
Copy link
Member

dhmlau commented Sep 11, 2020

@dzhiwei, we believe we've answered your question. Closing this issue. Thanks.

@dhmlau dhmlau closed this as completed Sep 11, 2020
@Quent1Pr
Copy link
Contributor

I try a global interceptor but it only intercepts lb4 queries and not lb3 queries.
I found a workaround with a lb4 middleware : https://loopback.io/doc/en/lb4/Middleware.html#loopback-style-middleware

@nabdelgadir nabdelgadir reopened this Nov 25, 2020
@nabdelgadir
Copy link
Contributor

Thanks @QPierre1! I think we can document the middleware workaround you're using for now and then we'll have to see if we can get lb4 interceptors to also work for lb3 routes. Would you be interested in doing the documentation portion?

Quent1Pr added a commit to Quent1Pr/loopback-next that referenced this issue Nov 26, 2020
Share a workaround to intercept all requests loopbackio#6099
Interceptors are mentioned in the lb3 migration guide but don't intercept lb3 queries.
Quent1Pr added a commit to Quent1Pr/loopback-next that referenced this issue Nov 26, 2020
Share a workaround to intercept all requests loopbackio#6099
Interceptors are mentioned in the lb3 migration guide but don't intercept lb3 queries.
Signed-off-by: Quentin Pierre <31855566+QPierre1@users.noreply.github.com>
Quent1Pr added a commit to Quent1Pr/loopback-next that referenced this issue Nov 26, 2020
Share a workaround to intercept all requests loopbackio#6099
Interceptors are mentioned in the lb3 migration guide but don't intercept lb3 queries.
Signed-off-by: QPierre1 <31855566+QPierre1@users.noreply.github.com>
Quent1Pr added a commit to Quent1Pr/loopback-next that referenced this issue Nov 26, 2020
Share a workaround to intercept all requests

re loopbackio#6099

Signed-off-by: quentin <31855566+QPierre1@users.noreply.github.com>
achrinza added a commit to Quent1Pr/loopback-next that referenced this issue Dec 16, 2020
Share a workaround to intercept all requests

re loopbackio#6099

Signed-off-by: quentin <31855566+QPierre1@users.noreply.github.com>
Co-authored-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com>
achrinza added a commit to Quent1Pr/loopback-next that referenced this issue Dec 16, 2020
Share a workaround to intercept all requests

re loopbackio#6099

Signed-off-by: quentin <31855566+QPierre1@users.noreply.github.com>
Co-authored-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com>
achrinza added a commit that referenced this issue Dec 16, 2020
Share a workaround to intercept all requests

re #6099

Signed-off-by: quentin <31855566+QPierre1@users.noreply.github.com>
Co-authored-by: Rifa Achrinza <25147899+achrinza@users.noreply.github.com>
@bajtos bajtos closed this as completed Mar 11, 2021
@loopbackio loopbackio locked and limited conversation to collaborators Mar 11, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

6 participants