hey-api / openapi-ts

✨ Turn your OpenAPI specification into a beautiful TypeScript client
https://heyapi.vercel.app
Other
1.01k stars 83 forks source link

uni-app client + client APIs #799

Open normal-coder opened 1 month ago

normal-coder commented 1 month ago

Description

Due to some specific reasons (such as constraints of the execution environment), I need to use my own encapsulated request library instead of public libraries like fetch or axios. Does hey-api allow self-encapsulation and replacement? How should I proceed with this attempt?

mrlubos commented 1 month ago

Hey @normal-coder, can you describe what your constraints are? Are you able to download any dependencies at all? If yes, you can bundle those clients in your output using client.bundle = true in the next release. If not, that complicates the situation somewhat. Would you be able to provide an example code you image you'd need to write to make this work?

normal-coder commented 1 month ago

Hey @normal-coder, can you describe what your constraints are? Are you able to download any dependencies at all? If yes, you can bundle those clients in your output using client.bundle = true in the next release. If not, that complicates the situation somewhat. Would you be able to provide an example code you image you'd need to write to make this work?

I am currently using uni-app to Cross end development with a technology stack of TypeScript+Vue+uni-app. When the code is compiled into a cross end application (such as an iOS/Android APP), the WebView or base runtime of the APP does not provide DOM operations, nor does it support fetch requests. uni-app provides an encapsulated request method: uni. request() (document address: https://en.uniapp.dcloud.io/api/request/request.html ), which used for compatibility with requests from different ends.

If I am unable to use the encapsulated request library on my own. It means I need to modify the code from codegen project or find other solutions to integrate OpenAPI. This is not conducive to the maintenance of the project.

Therefore, in conclusion:

  1. Perhaps hey-api can consider supporting developers to self-encapsulate the request library, so that the encapsulation of the request library can satisfy all or part of the implementation of hey-api through inheritance or implementation?
  2. Perhaps hey-api can use the request library of uni-app as a type of client? The developer base for this cross end development solution is estimated to be in the millions. I understand that it has great value in helping hey-api achieve better development
mrlubos commented 1 month ago

@normal-coder can you show me how to download uni-app? Is it on npm? I'd like to understand how much usage it has.

Both 1 and 2 are possible solutions, not sure yet which one would be better long-term. If I provided 1, how much effort would it be for you to implement the request?

normal-coder commented 1 month ago

@normal-coder can you show me how to download uni-app? Is it on npm? I'd like to understand how much usage it has.

  • Is the request interface a part of uni-app or is it downloaded separately?
  • Have there been any breaking changes to the uni-app request interface in the past 12 months?

Both 1 and 2 are possible solutions, not sure yet which one would be better long-term. If I provided 1, how much effort would it be for you to implement the request?

  1. Currently, uniapp supports initializing an empty scaffold using the CLI method. They invented uni_modules, a modular way similar to node_modules to distribute their own module packages, so the data of NPM is not their full amount. For NPM downloads, you can try to pay attention to the data of these packages: https://npmtrends.com/@dcloudio/uni-app-vs-@dcloudio/uni-app-plus-vs-@dcloudio/uni-h5-vs-@dcloudio/uni-mp-qq-vs-@dcloudio/uni-mp-weixin

    The part about 'uni_modules' is their own defined module extension, aimed at providing free or paid extension services through the distribution of module packages. This section can refer to: https://ext.dcloud.net.cn/ Their plugin market.

  2. The uni.request() method hasn't had a major change (e.g. BreakChange) in at least 4 years that I know of, and it's also a wrapper that's been made available through the uni-app runtime after it's been wrapped for different platforms. See https://github.com/dcloudio/uni-app/blob/next/packages/uni-api/src/protocols/network/request.ts

To quickly create a UNIAPP project using the CLI method, you can refer to this article: https://en.uniapp.dcloud.io/quickstart-cli.html

Due to the need to compile their APP Runtime using their self-developed IDE HBuilderX (you can understand it as similar to compiling iOS applications requiring XCode), you can refer to this article https://en.uniapp.dcloud.io/quickstart-hx.html , import the above CLI into HBuilderX, and try running it on the simulator (if you are using macOS, you can download XCode and use iOS Simulator, if you are using Windows, perhaps you need an Android emulator like Genymotion. Note: Android debugging does not require an AOSP development environment).

I think both options are cool. 😎

mrlubos commented 1 month ago

Thank you for the context @normal-coder. What are your alternatives at the moment? Are you post-processing the generated code? This feature is not a priority right now so it might take a while to implement, but want to make sure you're not blocked

normal-coder commented 1 month ago

Thank you for the context @normal-coder. What are your alternatives at the moment? Are you post-processing the generated code? This feature is not a priority right now so it might take a while to implement, but want to make sure you're not blocked

Thanks for your response. Currently, I have not made any modifications to the generated code. I maintain a package @gitee/typescript-sdk-v5 at https://gitee.com/sdk/typescript-sdk-v5. Three months ago, I migrated it from openapi-typescript-codegen to hey-api, but it did not work well with uni-app.

In the future, maybe I will try to find more code-generator alternatives compatible with uni-app for OAS, or abandon this approach and choose to encapsulate OpenAPI calls on my own. (BTW, openapi-typescript-codegen and hey-api are the most elegant TypeScript code-generators for OAS that I have seen, and it is highly unlikely that I will find better ones 🤣). I will continue to monitor the support for this feature. If it is implemented, perhaps I can also promote it to the uni-app team and the developer of uni-app at github.com/hey-api/openapi-ts 😉

mrlubos commented 1 month ago

@normal-coder can you provide more details around what works differently now that you migrated from openapi-typescript-codegen? The goal is to allow a seamless migration, so if it worked before, I am hoping it's possible it would work with this package too.

Regarding the uni-app, can you find out if they'd be willing to have a section on codegen in their documentation like this one in FastAPI https://fastapi.tiangolo.com/advanced/generate-clients/? That would be one way to make sure this is shipped waaay faster. Thank you for the kind words btw!

normal-coder commented 1 month ago

@normal-coder can you provide more details around what works differently now that you migrated from openapi-typescript-codegen? The goal is to allow a seamless migration, so if it worked before, I am hoping it's possible it would work with this package too.

Regarding the uni-app, can you find out if they'd be willing to have a section on codegen in their documentation like this one in FastAPI https://fastapi.tiangolo.com/advanced/generate-clients/? That would be one way to make sure this is shipped waaay faster. Thank you for the kind words btw!

Originally, I used the CodeGen project for my H5 environment, but as I had the demand to develop an app, the existing implementation encountered obstacles. Initially, I could generate an SDK based on the CodeGen solution. I could directly call methods in the code without the need to encapsulate requests separately for each API, which was very convenient. However, if I were to abandon this approach, I would need to integrate each API separately (indeed, in terms of coding, this is a repetitive and tedious process of refinement 😪 ).

I found two projects on Github that make axios-adapter for uni-app. Perhaps based on this, hey-api could more easily complete the integration of the uni-app client. If you are interested, we can discuss it together.

In addition, I asked them and they said they could add an article to their community https://ask.dcloud.net.cn/ Provide relevant information. 🍤

rookie-luochao commented 1 month ago

如果是使用uniapp, 不支持 uniapp.request 方法,requestLibPath参数可以满足你的需求,支持自定义request函数,request函数里面实现uniapp.request封装,我们的项目就是这么干的 https://github.com/openapi-ui/openapi-ts-request?tab=readme-ov-file#%E5%8F%82%E6%95%B0

normal-coder commented 1 month ago

如果是使用uniapp, 不支持 uniapp.request 方法,requestLibPath参数可以满足你的需求,支持自定义request函数,request函数里面实现uniapp.request封装,我们的项目就是这么干的 https://github.com/openapi-ui/openapi-ts-request?tab=readme-ov-file#%E5%8F%82%E6%95%B0

有意思的建议,我会尝试一下

rookie-luochao commented 1 month ago

如果是使用uniapp, 不支持 uniapp.request 方法,requestLibPath参数可以满足你的需求,支持自定义request函数,request函数里面实现uniapp.request封装,我们的项目就是这么干的 https://github.com/openapi-ui/openapi-ts-request?tab=readme-ov-file#%E5%8F%82%E6%95%B0

有意思的建议,我会尝试一下

没问题,嘿嘿,🤔