Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
Typescript open-api cilent generation and usage of `fetch`

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?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda