最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Generate static Javascript client from Swagger for use in React Native - Stack Overflow

programmeradmin1浏览0评论

I'm building a React Native app that will consume an API with Swagger 2.0 definition. I went to Swagger's repo at and it points to their Javascript generator at .

The problem is that the generator is dynamic, and since I'll be embedding the client in a mobile app, dynamic generator is not an option. They also say that there's a third party project available at , which says that the project is no longer maintained and points back to . (while that 3rd party generator works, I don't want to use a deprecated tool that might break any time since I'll be updating the API client when new endpoints arrive. And that tool also doesn't generate really good code anyway as it says in its own repo.)

At this point I'm stuck. What is the supported way of generating a static Javascript client from Swagger definition for use in React Native?

I'm building a React Native app that will consume an API with Swagger 2.0 definition. I went to Swagger's repo at https://github./swagger-api/swagger-codegen#where-is-javascript and it points to their Javascript generator at https://github./swagger-api/swagger-js.

The problem is that the generator is dynamic, and since I'll be embedding the client in a mobile app, dynamic generator is not an option. They also say that there's a third party project available at https://github./wcandillon/swagger-js-codegen, which says that the project is no longer maintained and points back to https://github./swagger-api/swagger-codegen. (while that 3rd party generator works, I don't want to use a deprecated tool that might break any time since I'll be updating the API client when new endpoints arrive. And that tool also doesn't generate really good code anyway as it says in its own repo.)

At this point I'm stuck. What is the supported way of generating a static Javascript client from Swagger definition for use in React Native?

Share Improve this question edited Apr 4, 2022 at 22:26 Braiam 4,49611 gold badges49 silver badges83 bronze badges asked Apr 2, 2019 at 19:41 Can PoyrazoğluCan Poyrazoğlu 34.9k54 gold badges209 silver badges427 bronze badges 2
  • Possible duplicate Reactjs client for Swagger / OpenAPI API specification but there're no answers there – Helen Commented Apr 3, 2019 at 9:42
  • @Helen not sure as there might (I'm not sure as I'm new) be differences between React and React Native code. – Can Poyrazoğlu Commented Apr 3, 2019 at 13:28
Add a ment  | 

1 Answer 1

Reset to default 3 +50

You can use Swagger Codegen to generate a javascript client sdk. However, the javascript code used in it will not work with React Native's fetch implementation. To overe that, you can simply extend the implementation of ApiClient to use the React Native fetch like:

class CustomApiClient extends ApiClient {


 callApi(path, httpMethod,   pathParams,queryParams,collectionQueryParams, headerParams, formParams, bodyParam,authNames, contentTypes, accepts,returnType, callback) {
    return fetch(`${this.basePath}${path}`,
      {
        method: httpMethod
      });
  }
}

Later using it in your other methods such as

class CustomUsersApi extends UsersApi {

 constructor() {
    super(new CustomApiClient());
  }
}

For a detailed implementation on this, you can refer the blog post https://medium./@lupugabriel/using-swagger-codegen-with-reactnative-4493d98cac15

发布评论

评论列表(0)

  1. 暂无评论