-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdevless.js
More file actions
191 lines (162 loc) · 4.54 KB
/
devless.js
File metadata and controls
191 lines (162 loc) · 4.54 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
export default function (ctx, inject) {
const hostUrl = (ctx.env.devless.proxy) ? '/api/v1/service/' : `${ctx.env.devless.host}/api/v1/service/`
ctx.$axios.setHeader("content-type", "application/json");
let header = {
'devless-token': ctx.env.devless['devless-token']
}
const nonce = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min
}
const postProcessor = (url, data) => {
ctx.$axios.onRequest(config => {
config.headers.common = header
})
return ctx.$axios.post(url, data).then(res => {
return res.data
}).catch(e => console.log(e))
}
const updateProcessor = (url, data) => {
ctx.$axios.onRequest(config => {
config.headers.common = header
})
return ctx.$axios.patch(url, data).then(res => {
return res.data
}).catch(e => console.log(e))
}
const deleteProcessor = (url, data) => {
ctx.$axios.onRequest(config => {
config.headers.common = header
})
return ctx.$axios.delete(url, data).then(res => {
return res.data
}).catch(e => console.log(e))
}
const getProcessor = (url) => {
ctx.$axios.onRequest(config => {
config.headers.common = header
})
return ctx.$axios.get(url).then(res => {
return res.data
}).catch(e => console.log(e))
}
const Devless = {
queryData: (service, table, params = null) => {
let parameters = ''
if (params !== null) {
const innerParams = function (key, params) {
for (var eachParam in params) {
parameters = '&' + key + '=' + params[eachParam] + parameters
}
}
for (var key in params) {
if (!params.hasOwnProperty(key)) { /**/ }
if (params[key] instanceof Array) {
innerParams(key, params[key])
} else {
parameters = '&' + key + '=' + params[key] + parameters
}
}
}
const url = `${hostUrl}${service}/db?table=${table}${parameters}`
let result = getProcessor(url)
return result
},
addData: (service, table, data) => {
const body = {
resource: [{
name: table,
field: [data]
}]
}
const url = `${hostUrl}${service}/db`
let result = postProcessor(url, body)
return result
},
updateData: (service, table, identifier, value, data) => {
const body = {
resource: [{
name: table,
params: [{
where: `${identifier},${value}`,
data: [data]
}]
}]
}
const url = `${hostUrl}${service}/db`
let res = updateProcessor(url, body)
return res
},
deleteData: (service, table, identifier, value) => {
const body = {
resource: [{
name: table,
params: [{
delete: true,
where: `${identifier},${value}`
}]
}]
}
const url = `${hostUrl}${service}/db`
let res = deleteProcessor(url, body)
return res
},
setToken: (token) => {
header['devless-user-token'] = token
return true
},
call: (service, method, data = []) => {
const body = {
jsonrpc: '2.0',
method: service,
id: nonce(1, 10000000),
params: data
}
const url = `${hostUrl}${service}/rpc?action=${method}`
let res = postProcessor(url, body)
return res
},
login: async (payload) => {
return await Devless.call('devless', 'login', [
payload.username || null,
payload.email || null,
payload.phone_number || null,
payload.password
]).then(res => {
if (res.payload.result) {
return res.payload.result;
}
return res;
})
},
register: async (payload, extras) => {
return await Devless.call('devless', 'signUp', [
payload.email || null,
payload.password,
payload.username || null,
payload.phone_number || null,
payload.first_name || null,
payload.last_name || null,
'',
'',
extras || null,
]).then(res => {
if (res.payload.result) {
Devless.setToken(res.payload.result.token);
return res.payload.result;
}
return res;
});
},
logout: async (token) => {
Devless.setToken(token);
return Devless.call('devless', 'logout').then(res => {
if (res.payload.result) {
return res.payload.result
}
return res;
})
}
}
ctx.$devless = Devless
inject('devless', Devless)
}