diff --git a/README.md b/README.md index 7ddc134..50f5590 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ This repository is the JavaScript generator for the [OpenAPI Forge](https://gith https://github.com/ScottLogic/openapi-forge +The client API it generates is suitable for running in the browser (after being bundled appropriately), or via node. The generated code uses the Fetch API, and as a result you'll need to use node v18 or greater. + ## Example You should consult the [OpenAPI Forge](https://github.com/ScottLogic/openapi-forge) repository for a complete user guide. The following is a very brief example that quikcly gets you up-and-running with this generator. @@ -17,19 +19,16 @@ $ openapi-forge forge \ -o api ``` -This will generate various files in the `api` folder. The API depends on the node-fetch module, which provides Fetch API for node applications. To run this client you'll need to add that as a dependency: +This will generate various files in the `api` folder. -``` -% npm init -y --silent -% npm i node-fetch@2.6.6 -``` +### Running with node (>= v18) -Finally, add the following `index.js` in the `api` folder: +Add the following `index.js` in the `api` folder: ```javascript const ApiPet = require("./apiPet"); const Configuration = require("./configuration"); -const transport = require("./nodeFetch"); +const transport = require("./fetch"); // create an API client const config = new Configuration(transport); @@ -56,6 +55,24 @@ To test the API, this example adds a Pet named “Fido” to the Pet Store, then Fido ``` +### Running in the browser + +You'll need to bundle the files into a single script, there are various tools that can be used for this purpose, but browserify is one of the simplest: + +``` +% npx browserify index.js -o bundle.js +``` + +Next create a simple HTML file that loads this script: + +``` + + + +``` + +Load the above page in a browser and you should see `Fido` logged to the console. + ## Development The OpenAPI Forge project [details the process for creating a new generator](https://github.com/ScottLogic/openapi-forge#generator-development). The documentation gives a few generator-specific instructions. diff --git a/template/nodeFetch.js b/template/fetch.js similarity index 86% rename from template/nodeFetch.js rename to template/fetch.js index 16f6fe1..2244cb5 100644 --- a/template/nodeFetch.js +++ b/template/fetch.js @@ -1,5 +1,3 @@ -const fetch = require("node-fetch"); - async function transport(params) { const response = await fetch(params.url, params); if (response.status !== 200) {