Proxies allow you to use functions that haven't yet been defined on an object. This means that you can invoke a function then create that function if it works with your API. This lesson shows you how to create an API around a rest service so that you can name each get request as a function on the Proxy.

 Imaging you need to call API with fetch for multi API endpoints, we can create multi API call functions, with some duplicate code. Proxy can help us to reduce the level of repeation.
const API_ROOT = "https://swapi.co/api";

const createApi = url => {
return new Proxy(
{
headers: {
"Content-Type": "application/json"
}
},
{
get: (target, key) => {
return async function(id) {
const response = await fetch(`${url}/${key}/${id}`, {}, target);
if (response.ok) {
return response.json();
} return Promise.reject("Network error");
};
}
}
);
};
const api = createApi(API_ROOT);
export const peopleApi = api.people;
export const planetsApi = api.planets;
export const starshipsApi = api.starships; async function go() {
const people = await peopleApi();
console.log(people); const planets = await planetsApi();
console.log(planets); const starships = await starshipsApi();
console.log(starships);
} go();

[Javascript] Wrap an API with a Proxy的更多相关文章

  1. javascript 百度地图API - demo

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="ini ...

  2. 在线聊天室的实现(1)--websocket协议和javascript版的api

    前言: 大家刚学socket编程的时候, 往往以聊天室作为学习DEMO, 实现简单且上手容易. 该Demo被不同语言实现和演绎, 网上相关资料亦不胜枚举. 以至于很多技术书籍在讲解网络相关的编程时, ...

  3. 第二篇、JavaScript常用的API

    下面是我整理的一些JavaScript常用的API清单. 目录 元素查找 class操作 节点操作 属性操作 内容操作 css操作 位置大小 事件 DOM加载完毕 绑定上下文 去除空格 Ajax JS ...

  4. [Javascript] JSON.parse API

    JSON (JavaScript Object Notation) is a standard method to serialize JavaScript objects and is common ...

  5. 【转】NativeScript的工作原理:用JavaScript调用原生API实现跨平台

    原文:https://blog.csdn.net/qq_21298703/article/details/44982547 -------------------------------------- ...

  6. Javascript实现百度API

    百度地图JavaScript API是一套由JavaScript语言编写的应用程序接口,可帮助您在网站中构建功能丰富.交互性强的地图应用,支持PC端和移动端基于浏览器的地图应用开发,且支持HTML5特 ...

  7. JavaScript 对象所有API解析【2020版】

    JavaScript 对象所有API解析[2020版] 写于 2019年08月20日,虽然是2019年写的文章,但现在2020年依旧不过时,现在补充了2019年新增的ES10 Object.fromE ...

  8. ES6的新API如Promise,Proxy,Array.form(),Object.assign()等,Babel不能转码, 使用babel-polyfill来解决

    Babel默认只转换新的JavaScript句法(syntax),而不转换新的API,比如Iterator.Generator.Set.Maps.Proxy.Reflect.Symbol.Promis ...

  9. 移动端API架构 统一Proxy还是各自为政?

    今天首先回答上一篇的问题: 为什么APP通过运营商接入网络,连通率会那么差? 1. 域名缓存问题 运营商的localdns会缓存域名的解析结果,不向权威DNS递归查询解析 为什么要这么干呢? 1)运营 ...

随机推荐

  1. android viewStub

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 开发应用的时候,需要根据条件决定显示某个视图, 这个时候可以用ViewStub Stub ...

  2. css选择器(第n个类选择器)的坑

    css选择器选择第n个子元素,共有两种写法: .parent span:nth-child(n) 选择parent下的第n个子元素(不管前边是不是span,都算在内) .parent span:nth ...

  3. chinese hacker-----WriteUp

    原题地址:http://ctf5.shiyanbar.com/web/2/ 提示下载一个数据库 下载下来后发现是加密的  有密码,但发现密码不是4648 这里用到“DbView” 直接破解密码进入 发 ...

  4. voith项目配置服务程序

    项目需求: 1.程序可以最小化到任务栏 2.tpms标签和限速标签同时只能选择一个,并且要通过button确定修改 3.在程序中需要显示SequenceScanner1.0服务的运行状态 4.能够打开 ...

  5. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力

    C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...

  6. SGU 404 Fortune-telling with camomile

    404. Fortune-telling with camomile Time limit per test: 0.25 second(s)Memory limit: 65536 kilobytes ...

  7. oracle开发学习篇之集合函数

    集合函数; declare type list_nested ) not null; v_all list_nested := list_nested('changan','hubei','shang ...

  8. POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)

    /* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...

  9. 深入浅出VC++串口编程之基于Win32 API

    1.API描述 在WIN32 API中,串口使用文件方式进行访问,其操作的API基本上与文件操作的API一致. 打开串口 Win32 中用于打开串口的API 函数为CreateFile,其原型为: H ...

  10. 【微信小程序】微信小程序 文本过长,自动换行的问题

    小程序开发过程出现的问题: 文本过长,以致于在view中显示不全,没有自动换行的问题 解决方法: 在wxss样式文件中添加样式 .font-break { word-break:break-all; ...