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 08ff92e commit fd0297a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This repository is the TypeScript 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 quickly gets you up-and-running with this generator.
Expand All @@ -17,20 +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]
% npm i --save-dev @types/node-fetch
```
### Running with node (>= v18)

Finally, add the following `index.ts` in the `api` folder:
Add the following `index.ts` in the `api` folder:

```typescript
import ApiPet from "./api/apiPet";
import Configuration from "./api/configuration";
import { transport } from "./api/nodeFetch";
import ApiPet from "./apiPet";
import Configuration from "./configuration";
import transport from "./fetch";

// create API client
const config = new Configuration(transport);
Expand All @@ -57,6 +55,30 @@ To test the API, this example adds a Pet named “Fido” to the Pet Store, then
Fido
```

### Running in the browser

The first step is to transpile from TypeScript to JavaScript:

```
% tsc
```

Following this, 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: 1 addition & 1 deletion template/README.md.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is an auto-generated client library for the {{info.title}} API, via the `op
```
import Api{{tagName}} from "./api";
import Configuration from "./configuration";
import { transport } from "./nodeFetch";
import transport from "./fetch";
// import any model types you need
import { {{modelObject}} } from "./api/model";

Expand Down
4 changes: 1 addition & 3 deletions template/nodeFetch.ts → template/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import fetch from "node-fetch";

import { RequestParameters } from "./request";

export async function transport(params: RequestParameters) {
export default async function transport(params: RequestParameters) {
const response = await fetch(params.url, params);
if (response.status !== 200) {
throw new Error(`${response.status} ${response.statusText}`);
Expand Down

0 comments on commit fd0297a

Please sign in to comment.