-
Notifications
You must be signed in to change notification settings - Fork 0
/
closures.js
44 lines (37 loc) · 913 Bytes
/
closures.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// function outerFunction() {
// let count = 0;
// function plusOne() {
// count++;
// return count;
// }
// function minusOne() {
// count--;
// return count;
// }
// return {
// plusOne: plusOne(),
// minusOne: minusOne()
// };
// }
// const innerFuncs = outerFunction();
// console.log(innerFuncs.plusOne);
// console.log(innerFuncs.minusOne);
function apiConnect(apiKey) {
function get(route) {
return fetch(`${route}?key=${apiKey}`);
}
function post(route, params) {
return fetch(route, {
method: "POST",
body: JSON.stringify(params),
headers: {
Authorization: `Bearer ${apiKey}`
}
});
}
return { get, post };
}
const api = apiConnect("my-secret-key");
// No need to include the apiKey anymore
api.get("http://www.example.com/get-endpoint");
api.post("http://www.example.com/post-endpoint", { name: "Joe" });