Loopback4 - Call REST API with OpenAPI definition

818 Views Asked by At

I'm new to Loopback4 and trying to call REST endpoint using lb4 datasource and have some questions:

  1. Does lb4 datasource and lb4 service bound to datasource generates proxy methods to call REST endpoints based on OpenAPI definition?
  2. If #1 is yes, what is the correct way to do it?

I tried what is in this guide:

  1. Create a LoopBack 4 DataSource with OpenAPI connector using the lb4 datasource command.
  2. Create a service that maps to the operations using the lb4 service command.

But it doesn't generate any method in service nor datasource.

PS: I'm using Swagger Petstore definition to do this sample. What am I missing?

2

There are 2 best solutions below

0
On

Ok, found the right tool to do it:

  1. Just call lb4 openapi --client
  2. Select existing datasource (or create a new one setting up datasource)
  3. Select controller

Stubs will be generated.

To call the service, just inject it in Controller and execute generated methods.

1
On

Hi from the LoopBack team

In the first case where you use lb4 datasource and lb4 service to generate the proxy, you need to add the operation mappings in the datasource and the functions in the service manually. See https://github.com/dhmlau/loopback4-external-apis/blob/master/src/datasources/restds.datasource.json for an example.

But as you mentioned, if you have the OpenAPI/swagger spec, you can simply use the lb4 openapi --client command.