”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 开源月数:我如何维护下一代请求工具

开源月数:我如何维护下一代请求工具

发布于2024-11-02
浏览:662

Two years ago, I designed and developed a JavaScript request strategy library called alovajs. Since its promotion in April 2023, it has received acclaim from developers around the world and has garnered over 2700 stars, including recognition from experts at major companies.

months of open source: How I maintain the Next-Gen request tool

Currently, [email protected] has been released, positioned as "the next-generation request tool for streamlined workflows."

I have been maintaining it without compensation for 30 months, and the version has reached 3.x. During this period, through continuous reflection, rejection, and rethinking, I aimed to achieve something that other request tools have not done, to become a tool that can truly help front-end development. I believe I have found a reliable direction.

That is, to create a "next-generation request tool for streamlined workflows", maximizing the assistance to front-end development in streamlining the API integration process.

Below is the exploration story of alovajs, which is also the story of an open-source project from its inception.

If you are interested in alovajs, I sincerely invite you to join the community and make progress together.

  • Join the Discord community

alovajs Originated from a Small Project, but Its Mission is to Sail the Ocean

One day in March 2022, due to certain circumstances, I conceived the idea of developing an App called "Con of goal." Inspired by some apps, I hoped that "Con of goal" could achieve delay-free data requests and submissions, that is, an immediate response mode, even under weak or no network conditions. However, after searching online and not finding a suitable solution, and similar optimistic update solutions not meeting the needs, my damn desire to share led me to decide to implement it in the form of a request library, which was the starting point of alovajs, but it didn't have a name at that time.

Designing alovajs

The beginning of a library is not design, not development, but clarifying the needs

pro Tips: It is strongly recommended that you first briefly understand the overview section of alovajs to better understand the following content.

At that time, there was no product positioning, just creating a JavaScript library to meet my own needs. I studied existing request libraries and listed the following needs:

  1. Support for seamless data interaction mode, that is, under network disconnection, it can still be submitted without delay, without the user's perception
  2. Designed according to the popular useHook of the time, making the interface more user-friendly
  3. A set of code supports multiple frameworks such as Vue, React, etc., and JavaScript operating environments such as browsers, React Native, UniApp, and Taro, etc.
  4. Consistent usage in multiple JavaScript environments
  5. Considering the caching function of react-query is very good, this should also be added
  6. Due to the influence and simplicity of axios, make alovajs easy to get started with, and the design should be similar to axios

Then, based on the needs, the library's API was designed.

  • For needs 1, it was the starting point for me to do alovajs, but it was not simple. The design at the time was to add a silent parameter in the useHook configuration, which could immediately respond to the success callback, but it was later proven to be problematic and redesigned, which is now the seamless data interaction strategy

months of open source: How I maintain the Next-Gen request tool

  • For needs 2, three core useHooks were designed: useRequest, useWatcher, and useFetcher. This is very familiar to everyone, such as ahooks' useRequest, vueuse's useAsyncState, react-query, and swr, needless to say, it is indeed very convenient.

  • Since the useHook design is used, different frameworks will have different state management, but I don't want to create a JavaScript library for each framework like react-query. Therefore, for needs 3, the specifications for stateHook adapters, request adapters, and storage adapters were designed, and different adapters can be written according to the specifications, separating the framework environment and runtime environment-related logic into separate modules.

  • For needs 4, the method instance proxy pattern was designed, and the method instance proxy calls the hook functions of different adapters, so that even if you develop any application, you can get started with alovajs without any unfamiliarity, and it can also be seamlessly transplanted.

  • For needs 5, similar JavaScript libraries implement caching in the form of custom keys, but my idea is to focus on request information. The common scenario is that when requesting the same interface with the same request method and parameters, it is necessary to hit the response data of the last time. Why don't we use these request information as caching keys? Therefore, alovajs has designed an automatic caching mechanism, which is enabled by default on GET requests.

  • For needs 6, refer to and learn from axios.

These designs have indeed been proven over time. alovajs has perfectly compatible with Vue, React, Svelte frameworks through the adapter method, and can also run in various JavaScript environments such as browsers, React Native, UniApp, and Taro, while maintaining a consistent usage method, which has given me a glimmer of hope.

In the following months, although alovajs was released, it has not been promoted. On the one hand, it is because I used it in the "Con of goal" project. Although it has been tempered and improved in this project, it is still very incomplete, and the positioning is not clear. The initial version introduction is like this.

months of open source: How I maintain the Next-Gen request tool

Later, the "Con of goal" project was aborted, but alovajs is still persisting.

alovajs' Direction Exploration

With the obsession of once being an Internet product manager, I still hope to make alovajs a differentiated product. I often ask myself, what is the difference between alovajs and other request libraries? Why should users use alovajs? It is indeed different from other libraries in design, which is not a question that can be answered immediately. Later, I tried to position the direction of the request library as a "lightweight request library" and a "multi-end unified request library", but they were all denied by myself, because these cannot bring substantial help to developers, and they cannot be called advantages.

In September 2022, an opportunity made me discover the excellent request library based on Vue, vue-request. Its usePagination and useLoadMore immediately inspired me. This form of pagination implementation made me realize that this is also what I want. At the same time, it made me realize the power of useHook. I can divide the module according to the request scenario, use different useHooks for different scenarios, and the previously implemented seamless data interaction is actually one of the scenarios. If this is the direction, developers can choose different useHooks according to different request scenarios, which not only improves development efficiency and reduces coding complexity, but also prevents junior front-end from writing inefficient code, and can make better use of the core features of alovajs to achieve better performance and less server pressure. request features, so far, the "lightweight request strategy library" was chosen by me.

Then, in order to guide the future design direction of alovajs, I also refined and abstracted the request scenario model (RSM) of alovajs, mainly divided into the following four processes:

Request timing -> Request behavior -> Request event -> Response processing

Let's do it, I started to reconstruct alovajs 2.0 according to this positioning, separated the seamless data interaction function from 1.0, and designed a middleware mechanism to adapt to the development of more request scenario strategy useHooks, studied and developed the pagination strategy and seamless data interaction strategy.

  • The pagination strategy of alovajs is what I think is the most useful usePagination. It uses the caching function of alovajs to achieve pre-loading of front and back page data, pagination data search, request-level debouncing, and provides automated management of new editing and deletion functions, as well as refreshing the data of a specified page code without resetting.

  • The seamless data interaction strategy took me 4 months, constantly encountering dead ends, and constantly redesigning the result. It has achieved a strategy that allows users to interact with data without delay even in weak or disconnected network environments, and it can also more stably ensure the success of the request. I designed a virtual data thing, which can be used as a placeholder for response data before the response, making users feel that it is an immediate response, and then replace the virtual data with real data after the response is successful. According to the current exploration results, its usage scenarios can be editor-like applications, system setting modules, and some simpler lists.

Later, request strategy modules such as useForm, useAutoRequest, and useSSE were also added, but this is far from enough.

The idea of the Next-Generation Request Tool

On May 13, 2023, I received such an issue on github

months of open source: How I maintain the Next-Gen request tool

At first, I didn't pay much attention to this issue, just simply understood the function of automatically generating request code for openAPI, and thought it was very good, but due to limited energy, I didn't think deeply about it, and I hadn't been thinking about the direction of alovajs at that time. But recently, I would occasionally think about the direction of alovajs, and this issue that is still open has always come to my mind, and then quietly changed the label of this issue from "feature-request" to "feature-request:confirmed".

At the same time, this issue made me realize that alovajs can also narrow the collaboration distance between front-end and back-end, and further simplify the front-end development workflow. This is the development direction I set for alovajs 3.0:

alovajs will help developers simplify the API integration workflow to the greatest extent, you only need to specify which API to use (this is also the result of thinking for 3 months)

months of open source: How I maintain the Next-Gen request tool

My specific plan is as follows:

  1. The steps 1, 2, 3, 4, 6 in the above figure are solved by automatically generating API code, but our generation plan will go further compared to tools like openapi-generator. It will automatically generate complete ts type, complete description request functions, whether it is a js project or a ts project, there is no need to introduce, let developers call as convenient as directly calling location.reload, and the request function can directly see the complete description and request parameter type prompts.

  2. Since the automatically generated request functions have complete descriptions and type prompts, the vscode plugin completes the interactive way to quickly retrieve the required APIs, and you no longer need to refer to the API documentation.

  3. Solve the problem of the front-end and back-end collaboration gap, and any changes in the interface can be automatically notified to the front-end. If there are changes found during the project construction, an error will be thrown to stop the construction. If it is a ts project, an error will also be thrown during compilation, and the change records can also be viewed through the vscode plugin.

Here is the demo video of vscode extension.

How to solve step 6 "write complex request logic"? Of course, it is to use the request strategy module, which has the characteristics of high performance and scene-based, and users can use a very small amount of code to implement various scene request functions.

In March 2024, the development plan for [email protected] was formulated, and it took 4 months to restructure almost the entire project with core member MeetinaXD, and there are many optimizations:

  1. The underlying architecture was redesigned, and a set of hooks can be used simultaneously in Vue, React, Svelte, and even in the Vue options style.

  2. The available range has been increased to the server side. You can use alova in server-side frameworks such as express, koa, and nestjs, and new server-side request strategy server hooks have been added.

  3. 10 configurations in [email protected] were deprecated, and 9 designs were optimized.

In addition, the most important part of 3.0, the vscode plugin, which is in charge of our core member czlin, is also available, and it has basically achieved the goals we set at the beginning.

So far, [email protected] has passed the beta period and can be stably used in the production environment.

Only by continuous exploration can we become better

At that time, an article that was criticized, It's time to replace your axios made it to the hot search.

It was indeed not that good when it was just launched, but I know that every product is not achieved overnight, and it needs time to precipitate.

The Apple 1 computer didn't even have a case at the beginning

months of open source: How I maintain the Next-Gen request tool

The development journey of Vue is also a process of continuous exploration and progress

months of open source: How I maintain the Next-Gen request tool

I am just moved by such a product journey, and persisting in doing one thing is the easiest way to succeed. Good products need to be tested for several years, let alone alovajs, which has only been around for about 1.5 years, and has only been contacted by some people for 8 months. During this period, it has also been exploring better solutions and moving forward step by step.

alovajs initially designed useHooks including useRequest, useWatcher, useEffectWatcher, useManual, useController, and then gradually reduced to only three core hooks: useRequest, useWatcher, and useFetcher.

months of open source: How I maintain the Next-Gen request tool

Commit address

In the design of parallel requests, whether to implement a form similar to Promise.all, or the current form of binding to the onSuccess function, I have been entangled in several versions, changed back and forth, and the following is the design of the abandoned responder of that year.

months of open source: How I maintain the Next-Gen request tool

Commit record not found

In order to be compatible with asynchronous caching schemes such as IndexedDB, I initially tried to design the storage adapter as an asynchronous function, but it would bring a series of problems, and then tried to implement the event mechanism through StorageConnector, which is still not perfect enough, and finally to the current custom localCache asynchronous function mechanism.

months of open source: How I maintain the Next-Gen request tool

Commit address

I also thank friends who have supported and contributed to alovajs. The following are a few screenshots, and there are many contributions that are not included.

months of open source: How I maintain the Next-Gen request tool

months of open source: How I maintain the Next-Gen request tool

months of open source: How I maintain the Next-Gen request tool

And continuously supplement the details of the document and improve the best practices, boldly try the cache version plan for the cache recovery mode, and in order to provide complete ts type prompts for alovajs, try to use automatic type inference to reduce the trouble of developers defining types, and indeed spend a lot of effort to optimize and compatible with different UI frameworks, etc.

Among them, the document has been greatly changed two to three times. I am grateful to @Orange Peel for providing the first document modification opinion, and @Well for providing the second document modification opinion, and then our document has such a reputation.

@green tree helped me open up new ideas for alova.

Many things are no longer clear, and the records on npmjs tell me that 146 versions have been released.

months of open source: How I maintain the Next-Gen request tool

There are also many people on github who have raised bugs, and I have also responded and fixed them in the first time. I am really very grateful. If I can't judge the problem immediately, I will also reproduce it on codesandbox, and use this demo as a bridge for communication with users.

The END

Thank you very much for your reading?, no matter how difficult it is, the road still needs to continue.

If you recognize alovajs, please go to Github to give it a star.

If you want to understand alova, you can visit the official website, where you can find more detailed documentation and example code to help you better understand and use this tool.

If you have any questions, you can join the following group chats for consultation, or you can also post Discussions in the github repository. If you encounter problems, please submit them in github issues, and we will solve them in the fastest time.

  • Join the Discord community

We also welcome you to contribute your strength, please go to the Contribution Guide.

版本声明 本文转载于:https://dev.to/coderhu/30-months-of-open-source-how-i-maintain-the-next-gen-request-tool-4l5b?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • 促进软件开发成功:整合跨学科技能以获得更好的结果
    促进软件开发成功:整合跨学科技能以获得更好的结果
    In today's world of software development, success is more than just technical know-how. Teams that work well together and mix different skills in vari...
    编程 发布于2024-11-07
  • WatchYourLAN - 轻量级网络 IP 扫描器
    WatchYourLAN - 轻量级网络 IP 扫描器
    WatchYourLAN的主要特点 找到新主机时发送通知 监控主机在线/离线历史记录 保留网络中所有主机的列表 发送数据到InfluxDB2制作Grafana仪表板 v2 有什么新功能? 基础API 导出到 InfluxDB2 SQLite 和 PostgreSQL...
    编程 发布于2024-11-07
  • 掌握 React Native 中的深度链接和通用链接:OpenGraph Share 和 Node.js 集成
    掌握 React Native 中的深度链接和通用链接:OpenGraph Share 和 Node.js 集成
    设想 假设您有一个名为 ShopEasy 的电子商务应用程序,并且您希望点击电子邮件、消息或社交媒体中的产品链接的用户被直接重定向到应用程序中的相关产品页面,而不是网站。 步骤1:在nodejs服务器中进行Opengraph配置以进行链接预览: Open Graph...
    编程 发布于2024-11-07
  • 如何在不使用“eval”的情况下安全地解析“宽松”JSON?
    如何在不使用“eval”的情况下安全地解析“宽松”JSON?
    在没有风险评估的情况下解析“轻松”的 JSONJSON 是一种广泛使用的数据交换格式,需要带引号的键的严格语法。但是,某些应用程序可能会遇到带有不带引号的键的“宽松”JSON。由于安全风险,不鼓励使用 eval 解析此类数据。避免邪恶的 Evaleval 的一种替代方法是基于正则表达式的方法,该方法...
    编程 发布于2024-11-07
  • 通过sponge+dtm快速轻松实现高性能的电商系统
    通过sponge+dtm快速轻松实现高性能的电商系统
    This article demonstrates how to use the Sponge framework to quickly build a simplified high-performance e-commerce system, implementing flash sale an...
    编程 发布于2024-11-07
  • 什么是 Java 中的关闭钩子以及如何有效地使用它?
    什么是 Java 中的关闭钩子以及如何有效地使用它?
    1. 了解关闭钩子 关闭钩子是 Java 中的一种特殊构造,允许您注册一个线程,该线程将在 Java 虚拟机 (JVM) 关闭时执行。这可以由各种事件触发,例如用户中断 (Ctrl C)、系统关闭或编程终止。 1.1 关闭钩子如何工作 当 JVM 启动时,它会创建...
    编程 发布于2024-11-07
  • 在 Web 工具中使用 JavaScript 进行安全加密
    在 Web 工具中使用 JavaScript 进行安全加密
    这是开发人员用来帮助保护程序中的敏感信息免受潜在攻击者攻击的方法。加密将可读数据转变为编码格式,只有使用正确的密钥才能解码;因此,它对于保护密码、财务详细信息和个人数据等信息的安全至关重要。 在数据泄露和网络攻击非常猖獗的时候,这一点变得非常重要。通过加密数据,开发人员将确保没有未经授权的一方在网络...
    编程 发布于2024-11-07
  • 如何将 java.util.Date 转换为 java.time 类型?
    如何将 java.util.Date 转换为 java.time 类型?
    将 java.util.Date 转换为 java.time 类型遗留的 java.util.Date 和 Calendar 类以其复杂性而闻名和麻烦。虽然建议使用 java.time 框架进行日期时间管理,但在使用现有代码时,必须在这些新旧类型之间进行转换。映射到 java.time 类型将 ja...
    编程 发布于2024-11-07
  • #daysofMiva 编码挑战日:JS 中的值和变量
    #daysofMiva 编码挑战日:JS 中的值和变量
    嗨,大家好。最近太忙了,都没有时间记录一下我的旅程是怎样的?无论如何,我会继续关注❤️。 对于我的第 5 天文档,我只是做了一些非常简单和简短的事情。 正如您从上面看到的,一旦您理解了值和变量,这就是一个非常简单的任务。 那么什么是值和变量? JavaScript 中的值 将值...
    编程 发布于2024-11-07
  • 您需要了解的 Python 4 关键特性
    您需要了解的 Python 4 关键特性
    Spotify、Google、NASA 和摩根大通有什么共同点?他们每天都使用Python。 Python 是一种令人印象深刻且全面的编程语言,在各种技术方法中发挥着至关重要的作用。从搜索引擎、网络应用程序、游戏到动画和其他编程语言,Python 都是发明的核心。 多年来,Python 经历了流行...
    编程 发布于2024-11-07
  • MySQL如何实现乐观锁?
    MySQL如何实现乐观锁?
    MySQL 中的乐观锁:全面解释乐观锁是数据库管理系统中采用的一种技术,用于防止在以下情况下可能出现的数据冲突:多个用户尝试同时更新相同的数据。虽然 MySQL 本身不支持乐观锁定,但可以使用标准 SQL 指令来实现。理解概念乐观锁定的操作假设数据不可能被多个用户频繁修改。该技术不是获取数据锁,而是...
    编程 发布于2024-11-07
  • 如何从 C++ 中的函数安全地返回数组?
    如何从 C++ 中的函数安全地返回数组?
    从 C 中的函数返回数组 尝试从 C 中的函数返回数组可能会导致意外行为,如以下代码所示片段:int* uni(int *a,int *b) { int c[10]; ... return c; }此函数尝试从函数返回本地数组 c。但是,当函数返回时,数组占用的内存将被释放,...
    编程 发布于2024-11-07
  • HTML5中如何将表格属性迁移到CSS?
    HTML5中如何将表格属性迁移到CSS?
    HTML5 表格属性转换Visual Studio 的 HTML5 验证功能将单元格填充、单元格间距、valign 和对齐属性标识为对表格元素无效。为了解决这个问题,HTML5 引入了 CSS 替代方案来控制表格的外观和间距。单元格填充:将 cellpadding 替换为应用于表格单元格的 CSS ...
    编程 发布于2024-11-07
  • 为什么不能在 C++ 中使用非常量变量定义数组大小?
    为什么不能在 C++ 中使用非常量变量定义数组大小?
    了解使用 Const Int 作为数组大小的限制在 C 中,使用 const int 作为数组大小受到以下限制某些限制。让我们深入研究一下为什么存在这些限制。考虑以下场景:允许:const int size = 2; int array[size] = {0}; // Compilation suc...
    编程 发布于2024-11-07
  • 如何在 MySQL WHERE 子句中指定不明确的“user_id”列?
    如何在 MySQL WHERE 子句中指定不明确的“user_id”列?
    解决 MySQL 'user_id' WHERE 子句中的歧义当处理共享列名的多个表时,MySQL 在使用时可能会遇到歧义WHERE 子句中的该列。这在错误消息“user_id”中很明显,其中 where 子句不明确。考虑示例查询:SELECT user.*, user_info.*...
    编程 发布于2024-11-07

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3