Image for post
Image for post

Using Optic to generate your API Specs

Mocking APIs using json-server

Installing Optic

Creating the Project

$ api init
/Users/edgar/OneDrive/skalena/projetos/useoptic/json-serverIs this your API's root directory? (yes/no): yesWhat is this API named?: json-server[optic] Added Optic configuration to /Users/edgar/OneDrive/skalena/projetos/useoptic/json-server/optic.ymlFinish setting up your API start task here: http://localhost:34444/apis/1/setup
Image for post
Image for post
name: "json-server"
tasks:
start:
targetUrl: http://localhost:3000
inboundUrl: http://localhost:4000
$ json-server api start
[optic] Review the API Diff at http://localhost:34444/apis/1/review
[optic] Optic is observing requests made to http://localhost:4000

Time to execute the HTTP Requests

curl --location --request GET 'http://localhost:4000/posts'
[   
{ "id": 1,
"title": "json-server",
"author": "typicode"
}
]
curl --location --request POST 'http://localhost:4000/posts' \--header 'Content-Type: application/json' \--data-raw ' {"id": 2,"title": "optics made great stuff","author": "edgar"}'
curl --location --request PATCH 'http://localhost:4000/posts/2' \--header 'Content-Type: application/json' \--data-raw ' {"title": "optics made great stuff - after patch","author": "edgar silva"}'
curl --location --request DELETE 'http://localhost:4000/posts/2'
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Generating the OAS file

Checking the File

Image for post
Image for post
Image for post
Image for post

Conclusion

Just a dad, a husband, and an Old-School Brazilian Jiu-Jitsu Black-belt. Working in the technology field since 1997.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store