什么是 JavaScript 中 REST API 客户端库的好例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13175161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:59:54  来源:igfitidea点击:

What are good examples of REST API client libraries in JavaScript

javascriptapirest

提问by Andy Hume

I'm looking for best-practise styles for wrapping a REST API in a light-weight JavaScript client.

我正在寻找在轻量级 JavaScript 客户端中包装 REST API 的最佳实践样式。

In the past I've seen libraries which are implemented in a style like:

在过去,我见过以如下风格实现的库:

var request = new SearchRequest(apikey);
request.query = "search term";
request.send(function(results) {
    console.log(results);
});

Or that embrace HTTP more explicitly like:

或者更明确地包含 HTTP,例如:

api.get("search", "search term", function(results) {
    console.log(results);
});
api.post("comment", comment, function(results) {
    console.log(results);
});

Or that wrap at an even higher level:

或者在更高的层次上包装:

api.search("search term", function(results) {
    console.log(results);
});
api.createComment(comment, function(results) {
    console.log(results);
});

What good examples of modern JavaScript client libraries wrapping REST APIs have you seen recently. Not worried about implementation details, just the API design.

您最近看到了哪些现代 JavaScript 客户端库包装 REST API 的好例子。不担心实现细节,只担心 API 设计。

Thanks!

谢谢!

回答by Bruno

I watched a really good video on good API Design. Definitely worth the watch.

我观看了一个关于良好 API 设计的非常好的视频。绝对值得一看。

RESTful API Design - Second Edition

RESTful API 设计 - 第二版

You can also get a free book Web API Design - Crafting Interfaces that Developers Lovein addition to the above video by going to http://bit.ly/M28lOu

除了上述视频外,您还可以通过访问http://bit.ly/M28lOu获得一本免费书籍Web API 设计 - 开发人员喜爱的制作接口

Regarding wrappers it may be worth thinking about the following:

关于包装器,可能值得考虑以下几点:

  1. Make your wrapper consistent.
  1. 使您的包装保持一致。

Adopt the standards, naming conventions, etc of the programming language or community you are working with to make the wrapper feel as natural as possible to those using it.

采用您正在使用的编程语言或社区的标准、命名约定等,使包装器对使用它的人来说尽可能自然。

  1. To wrap or to abstract? That is the question.
  1. 包装还是抽象?就是那个问题。

By Wrappingyou mimic the REST API methods and structure as much as possible but simply make some of the hard things easier. One of the biggest advantages of this approach is that it makes it easier to upgrade when the target REST API upgrades.

通过Wrapping,您可以尽可能多地模仿 REST API 方法和结构,但只是让一些困难的事情变得更容易。这种方法的最大优点之一是在目标 REST API 升级时更容易升级。

Abstractingis useful when the target REST API is complex or behaves and looks in non-standard ways. In this case your wrapper methods and calls may not resemble your target REST API methods at all but will ultimately simplify the life ( hopefully ) of those using your wrapper.

当目标 REST API 很复杂或以非标准方式运行和查看时,抽象很有用。在这种情况下,您的包装器方法和调用可能根本不像您的目标 REST API 方法,但最终会简化那些使用您的包装器的人的生活(希望如此)。

回答by Fran?ois Zaninotto

Check these ones:

检查这些:

There are other similar discussions on StackOverflow, like JavaScript REST client Library

StackOverflow 上还有其他类似的讨论,比如 JavaScript REST client Library