Skip to content

Commit

Permalink
feat: removed dependency on node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinEberhardt committed Jan 12, 2023
1 parent cf6edc0 commit bf0ca91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 [email protected]
```
### 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);
Expand All @@ -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:

```
<html>
<script src="bundle.js"></script>
</html>
```

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.
Expand Down
2 changes: 0 additions & 2 deletions template/nodeFetch.js → template/fetch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const fetch = require("node-fetch");

async function transport(params) {
const response = await fetch(params.url, params);
if (response.status !== 200) {
Expand Down

0 comments on commit bf0ca91

Please sign in to comment.