Ajax-mappersmith REST客户端封装方案

Ajax-mappersmith 的 REST 客户端写法挺轻巧的,支持 Node.js 和浏览器端都能跑,比较适合你做前后端通用封装的时候用一套调用逻辑,前后都省事。

Ajax 的异步就不用多说了,常用的 fetchaxios 都绕不开。这个 Ajax-mappersmith 的思路是用配置+调用的方式,把 REST API 变成“方法”。写起来比手撸 XHRfetch 更直观。

比如你可以这样:

const client = forge({
  clientId: 'myapi',
  host: 'https://api.example.com',
  resources: {
    User: {
      byId: { path: '/users/:id' }
    }
  }
});

client.User.byId({ id: 123 }).then(...);

是不是有点像调用本地服务?响应也快,错误也比较友好。

它对 浏览器Node.js 都做了适配,哪边用都行。尤其你如果在搞 SSR,或者写那种能复用逻辑的 SDK,蛮方便的。

文档嘛,稍微得啃一下,不过用起来一旦上手,挺顺的。推荐你配合一些类型定义工具一起上,比如用 TypeScript 来写接口结构,体验更棒。

如果你还在找轻量、好上手的 REST 客户端,不妨试试 Ajax-mappersmith。适合做接口封装、写 SDK 或前后共用逻辑的场景。

zip
Ajax-mappersmith.zip 预估大小:124个文件
file
.gitignore 43B
file
.eslintrc 118B
file
package.json 3KB
file
mappersmith.js 3KB
file
mock-assert.js 218B
file
mock-request.js 3KB
file
mock-utils.js 791B
file
mock-resource.js 6KB
file
csrf.js 821B
file
index.js 79B
file
index.js 3KB
file
index.js 5KB
file
global-error-handler.js 695B
file
duration.js 643B
file
log.js 1KB
file
timeout.js 594B
file
encode-json.js 1KB
file
basic-auth.js 667B
file
request.js 5KB
file
client-builder.js 4KB
file
gateway.js 2KB
file
response.js 3KB
file
manifest.js 3KB
file
index.js 566B
file
test.js 4KB
file
method-descriptor.js 1KB
file
utils.js 4KB
file
csrf.js 45B
file
index.js 97B
file
index.js 86B
file
global-error-handler.js 78B
file
duration.js 49B
file
log.js 138B
file
timeout.js 48B
file
encode-json.js 71B
file
basic-auth.js 51B
file
xhr.js 4KB
file
fetch.js 3KB
file
http.js 4KB
file
timeout-error.js 213B
file
mock.js 620B
file
.travis.yml 282B
file
mappersmith.js 17KB
file
mappersmith.map 56KB
file
LICENSE 1KB
file
CHANGELOG.md 10KB
file
tsconfig.json 157B
file
webpack.conf.js 1KB
file
utils.spec.js 1KB
file
http.spec.js 10KB
file
jasmine.json 245B
file
integration.spec.js 3KB
file
responses.js 963B
file
manifest.js 1KB
file
index.js 412B
file
hello_world.pdf 6KB
file
csrf.js 708B
file
karma.conf.js 840B
file
file-upload.js 1KB
file
integration.spec.js 3KB
file
shared-examples.js 11KB
file
server.js 4KB
file
middleware.spec.ts 1KB
file
csrf.spec.ts 234B
file
basic-auth.spec.ts 308B
file
retry-v2.spec.ts 689B
file
retry.spec.ts 208B
file
duration.spec.ts 217B
file
global-error-handler.spec.ts 379B
file
encode-json.spec.ts 224B
file
timeout.spec.ts 264B
file
log.spec.ts 202B
file
basic.spec.ts 444B
file
config.spec.ts 840B
file
test.spec.ts 1KB
file
deprecated-middleware.spec.ts 732B
file
fetch.spec.ts 188B
file
helper.js 6KB
file
response.spec.js 6KB
file
basic-auth.spec.js 1KB
file
retry.spec.js 2KB
file
shared-examples.js 4KB
file
retry.spec.js 2KB
file
global-error-handler.spec.js 2KB
file
duration.spec.js 1KB
file
timeout.spec.js 1KB
file
csrf.spec.js 1KB
file
encode-json.spec.js 4KB
file
client-builder.spec.js 4KB
file
utils.spec.js 5KB
file
request.spec.js 16KB
file
gateway.spec.js 4KB
file
global-middleware.spec.js 2KB
file
mappersmith.spec.js 2KB
file
middleware.spec.js 12KB
file
fetch.spec.js 6KB
file
xhr.spec.js 7KB
file
mock-request.spec.js 7KB
file
mock-assert.spec.js 2KB
file
match-functions.spec.js 1KB
file
mock-resource.spec.js 15KB
file
ci.js 544B
file
release.sh 153B
file
README.md 38KB
file
example.js 471B
file
.eslintignore 22B
file
appveyor.yml 262B
file
.babelrc.js 302B
file
timeout.d.ts 201B
file
retry.d.ts 756B
file
csrf.d.ts 156B
file
global-error-handler.d.ts 349B
file
basic-auth.d.ts 197B
file
encode-json.d.ts 157B
file
duration.d.ts 150B
file
log.d.ts 135B
file
test.d.ts 2KB
file
fetch.d.ts 142B
file
index.d.ts 5KB
file
yarn.lock 288KB
file
index.js 47B
file
config.web.json 329B
file
config.node.json 292B
file
web.js 27B
...
zip 文件大小:227.97KB