-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
100 lines (89 loc) · 2.99 KB
/
agent.js
File metadata and controls
100 lines (89 loc) · 2.99 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import superagentPromise from 'superagent-promise';
import _superagent from 'superagent';
import { DEFAULT_PAGE_SIZE } from './constants/commonConstants'
const superagent = superagentPromise(_superagent, global.Promise);
const API_ROOT = 'http://localhost:8080';
const encode = encodeURIComponent;
const responseBody = res => res.body;
let token = null;
const tokenPlugin = req => {
if (token) {
req.set('authorization', `Bearer ${token}`);
}
}
let basicAuth = "YWNtZTphY21lc2VjcmV0";
const basicPlugin = req => {
req.set('authorization', `Basic ${basicAuth}`)
}
const requests = {
del: url =>
superagent.del(`${API_ROOT}${url}`).use(tokenPlugin).then(responseBody),
get: url =>
superagent.get(`${API_ROOT}${url}`).use(tokenPlugin).then(responseBody),
put: (url, body) =>
superagent.put(`${API_ROOT}${url}`, body).use(tokenPlugin).then(responseBody),
post: (url, body) =>
superagent.post(`${API_ROOT}${url}`, body).use(tokenPlugin).then(responseBody),
postWithBasic: (url, body) =>
superagent.post(`${API_ROOT}${url}?grant_type=password&username=${body.username}&password=${body.password}`).use(basicPlugin).then(responseBody)
};
const directRequest = {
del: url =>
superagent.del(`${url}`).use(tokenPlugin).then(responseBody),
get: url =>
superagent.get(`${url}`).use(tokenPlugin).then(responseBody),
put: (url, body) =>
superagent.put(`${url}`, body).use(tokenPlugin).then(responseBody),
post: (url, body) =>
superagent.post(`${url}`, body).use(tokenPlugin).then(responseBody)
};
const Auth = {
current: () =>
requests.get('/users/user'),
login: (email, password) =>
requests.postWithBasic('/oauth/token', { username: email, password: password }),
register: (username, email, password) =>
requests.post('/users', { username, email, password }),
save: user =>
requests.put('/user', { user })
};
const Tags = {
getAll: () => requests.get('/tags')
};
const limit = (size, page) => `size=${size}&page=${page ? page : 0}`;
const Books = {
all: page =>
requests.get(`/books?${limit(localStorage.getItem("page_size") ? localStorage.getItem("page_size") : DEFAULT_PAGE_SIZE, page)}`),
del: slug =>
requests.del(`/books/${slug}`),
get: slug =>
requests.get(`/books/${slug}`),
update: (id, book) =>
requests.put(`/books/${id}`, { ...book }),
create: book =>
requests.post('/books', { ...book })
};
const Articles = {
del: (bookId, articleId) =>
requests.del(`/books/${bookId}/articles/${articleId}`),
get: (bookId, articleId) =>
requests.get(`/books/${bookId}/articles/${articleId}`),
update: (bookId, articleId, article) =>
requests.put(`/books/${bookId}/articles/${articleId}`, { ...article }),
create: (bookId, article) =>
requests.post(`/books/${bookId}/articles`, { ...article })
};
const Profile = {
get: id =>
requests.get(`/users/${id}`),
me: () =>
requests.get(`/users/me`)
};
export default {
Books,
Auth,
Profile,
directRequest,
Articles,
setToken: _token => { token = _token; }
};