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?