Typescript open-api cilent generation and usage of fetch

0 votes

I am trying to generate a typescript client for my rest server. This typescript client is expected to work for both browser and in Node.js environment (server side).

I used

openapi-generator-cli generate \ 
-i openapi-pets.yml \
-o output \
--generator-name typescript-fetch \
--additional-properties=typescriptThreePlus=true

after this I get this genrated

── apis
│   ├── PetsApi.ts
│   └── index.ts
├── index.ts
├── models
│   ├── ModelError.ts
│   ├── Pet.ts
│   └── index.ts
└── runtime.ts

When I run

ts-node index.ts

I get tons of errors like

error TS2304: Cannot find name 'Response'.

In attempt to fix this I added tsconfig.json at the root with this content

{
  "compilerOptions": {
    "module": "commonjs",
    "sourceMap": true,
    "lib": [
      "es2015",
      "dom"
    ]
  }
}

All errors are gone now I try to use the API (modified index.ts with following content)

/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './apis';
export * from './models';

import {
    PetsApi,
} from './apis';

import {
    Configuration,
    ConfigurationParameters,
} from "./runtime";

const configParams: ConfigurationParameters = {
    basePath: 'https://petstore.swagger.io/v2/',
    middleware: [],
};

const apiConfig = new Configuration(configParams);

export const apiClient = {
    petsAPI: new PetsApi(apiConfig),
};

export type ApiClient = typeof apiClient;

apiClient.petsAPI.listPets().then(value => console.log(value));

and I try to run it

ts-node index.ts

and results into

ReferenceError: fetch is not defined

How can I make this project a "Node" project and define the dependency of fetch?

Jul 18, 2022 in TypeSript by Logan
• 2,140 points
1,552 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In TypeSript

0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
570 views
0 votes
0 answers

What is TypeScript and why would I use it in place of JavaScript?

Can you please describe what the TypeScript ...READ MORE

Jul 18, 2022 in TypeSript by Logan
• 2,140 points
375 views
0 votes
1 answer
0 votes
1 answer

Conversion from both keys and values of object

You could just change the declaration of ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
538 views
0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
6,541 views
0 votes
1 answer

What is "not assignable to parameter of type never" error in TypeScript?

All you have to do is define ...READ MORE

answered May 31, 2022 in TypeSript by Nina
• 3,060 points
57,665 views
0 votes
1 answer

Mixing JavaScript and TypeScript in Node.js

Combine the following TypeScript compiler options --allowJs Explicitly supports ...READ MORE

answered Jun 15, 2022 in TypeSript by Nina
• 3,060 points
787 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,947 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP