8

Has anyone successfully used the Swagger Code Generator to create a TypeScript Fetch client that can be used within the browser? I'm trying to use the generated API client within a React application that uses TypeScript.

Although I've successfully generated a client (i.e. the api.ts file), I'm getting hung up on the fact that it begins with the following imports:

import * as querystring from "querystring";
import * as url from "url";

import * as isomorphicFetch from "isomorphic-fetch";
import * as assign from "core-js/library/fn/object/assign";

interface Dictionary<T> { [index: string]: T; }
export interface FetchAPI { (url: string, init?: any): Promise<any>; }

...

While I can successfully find the TypeScript typings (i.e. @types) for isomorphic-fetch and core-js, I can't find typings for querystring and url. As a result, I'm getting [ts] Cannot find module... for the querystring, url and also the assign imports.

Is this client not actually intended to be used within the browser? Otherwise, can anyone provide any suggestions for what I might be doing wrong here?

Thanks in advance for any help!

William Cheng
  • 10,137
  • 5
  • 54
  • 79
Sean
  • 293
  • 1
  • 4
  • 10
  • Here are some tests for Typescript-Fetch Petstore client: https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/typescript-fetch/tests/default/test. Would these help? – William Cheng Nov 17 '16 at 14:48
  • Just FYI: You can also generate a TypeScript Fetch client from a swagger file with NSwag (http://nswag.org, I'm the author). It imports only the expected definitions, i.e. whatwg-fetch – Rico Suter Apr 21 '17 at 18:19

1 Answers1

2

Thanks for the link, wing328, that ended up taking me down the path that lead to the 'TypeScript Fetch'-specific readme. In particular, this section here: https://github.com/swagger-api/swagger-codegen/tree/master/samples/client/petstore/typescript-fetch/builds/es6-target#installation explains that the Swagger Code Generator does not directly create JavaScript - you must run npm install or `npm publish'. You'll then be able to find the JavaScript-based API and associated typings in the dist folder.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Sean
  • 293
  • 1
  • 4
  • 10