Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda