validate requests with swagger and lambda - node.js

We are using aws gateway api with lambda functions to create our api.
We then decided that it would be a good idea to have it all documented with swagger, and use it to validate the requests in a similar fashion as it happens with express middleware frameworks.
However i cant find a way to pass a request object and validate it against a swagger spec manually without using one of these frameworks.
Now my question is:
How do validate a request against a swagger spec, by pointing the request at a specific path in spec, what frameworks could i use to achieve it?

Related

NestJS : Passport LinkedIn strategy with graphql

I have nestjs application running on typescript, Graphql, Postgres with Jwt strategy defined, now I need to workout LinkedIn strategy with it. I am not really sure where to start with it, there are a couple of packages available but they are missing the Graphql part and they are mostly pointing to API endpoints on local like /auth/linkedin/callback, I would like to know here and how to start.
If you look at LinkedIn's OAuth documentatioin you'll see that LinkedIn needs to know about a callback url for when authorization attempts are successful. You'll also see that the response for the initial authroization call (what calls your callback URL) is a GET request that doesn't conform to GraphQL format, so you have to implement a REST endpoint for this.
This is pretty true for most OAuth2.0 calls. You need to implement them in REST, not GraphQL. If you really wanted to, you could take the REST call and do some transformation to make it a GQL call, then forward it on to your GQL server, but that's still a REST endpoint you've got in your server.

How to generate a Node Rest API Client based on OpenAPI Spec 3.0

I'm a node/JS beginner, so really sorry if I missed something.
My Need is the following, I have a Swagger/OpenAPI3.0 yaml file describing an API that I need to consume. As the client will be an Node API gateway, I want this API Client code for Node.
I use the Swagger Editor to generate client like this :
Screenshot of Swagger Editor
But the generated code seems dedicated to browser client and I need to refactor this to have it works properly with node.
Does it exist an alternative?
Thanks a lot for your help.
One of the libraries which can create a TypeScript API client for Node.js at runtime from an OpenAPI v3 definition is called openapi-client-axios.

Extending existing RESTful API using Node.js

I am currently running a web service on an Apache Tomcat servlet container. The web service has a base URL and exposes my applications data using the following structure:
http://[hostname]:[port]/path/to/root/[db_table_name]/[primary_key]?fields=name,...
An HTTP GET call to a URL like the one above would return a JSON formatted string.
Though the documentation for my application describes this as a RESTful API, I am confused because I was under the impression that true RESTful APIs do not use query strings. Rather, as I understand it, a true restful API provides a uniform structure, in the form of resource endpoints.
My questions relate to how I can create a custom API to leverage the existing API using Node.js. I do not want to rewrite the application logic or database calls; I just need to know how I can create the API calls using Node.js (possibly using Express or some other framework) and let the existing API handle the request.
For example, I could write Node.js code using the Express module that has several routes, these routes would handle client requests that in turn would call the existing API (i.e. /path/to/root/[table_name]/[pk]... and return the response.
If my Apache Tomcat server is listening on port 8080, how would I deploy my Node.js server to listen on another port and then redirect requests to the existing WS URL on port 8080.
Does the Express framework support explicitly specifying a root path (such as http://localhost:3000/path/to/root/[table_name]/[pk]) as the default root path?
Finally, I know REST APIs support CRUD operations. In the case of a POST method, does Express (or Node.js) have built-in logic to handle duplicate POST requests so that duplicate records don't get created in the database.
I'm reading through different article and tutorials on REST but I think I'm missing something. Any information or advice that can take me in the right direction would be much appreciated.
there's a lot to cover here but I'll try to cover your three questions. Since you have mentioned using Express I will answer assuming that Express is the framework you are using.
If you are using Express, you can choose which port to listen to when you start the server, so you can choose any port that you like at that point (see here).
If you need to redirect a request you can do so easily with res.redirect() (see here). However, you could also call the other web service directly, retrieve the data and return it to the client instead of redirecting them if you prefer. That would require some more code to make the http requests in node.js though.
I am not 100% sure if this is the answer to your question, but there are ways to add a "base path" or namespace to all of your routes. I found this example where various namespaces are used but in your case you only need one which applies to all routes.
I don't think there is a built-in way to do this. The best I can think of is potentially creating some kind of ID for the request so that if it is sent twice you could use this to check but it's far from ideal.
I would like to add that I'm not sure where the idea that query parameters not being RESTful comes from? I think query parameters are fine because that is how you query! Otherwise you couldn't ask for the right data from your RESTful API. Let's say you have a /posts endpoint and you want to get the posts of a particular user (user ID = 1). The RESTful way to do this would be to issue a GET request to /posts?user=1.
Hope this helps!

Auto-generate swagger.yaml from hapi-js routes

I'm working with a fairly complex NodeJS API that has been written using hapiJS.
The API uses the hapi-swagger npm module to generate the API documentation.
I'm now investigating how to integrate the Amazon API Gateway with this API, in order to provide the authentication/request throttling etc.
To create an Amazon Gateway API, it appears that I need to provide a Swagger definition.
My question is, how do I generate this swagger definition? I'm assuming there must be some way to do this automatically from hapi-swagger, but I haven't been able to find it.
If you are using hapi-swagger the default endpoint that returns the swagger.json is /swagger.json.
See https://github.com/glennjones/hapi-swagger/blob/c02dd0dc8632766006854d1a4ddcd88a77a411c8/optionsreference.md#json-json-endpoint-needed-to-create-ui --> jsonPath

Lib to check that JSON payload is valid accord to Swagger API definition

I'm using swagger to define my API and API-gateway to host this API. I found the following lib (see here) to import my API definition to AWS and automatically create the API (models, end-points, etc). It's cool. But, it's not able to validate Requests based on models (defined in Swagger definition). It means that you can send a JSON payload without the required fields.
I don't want to write a node.JS code to check the format because it will not be much easy for schema updates. I'm wondering if it's possible to check if a JSON payload is compatible with a specific type of object defined in swagger (objects defined in the "definitions" section).
If it's possible, it will allow me to only update my swagger definition.
Thanks,
Romain.
We are tracking this feature request on our backlog. For clarity, this would be implemented in the API Gateway service, not the Swagger importer. In the meantime you will need to implement validation logic yourself. See here for libraries which may help: http://json-schema.org/implementations.html

Resources