-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
70 lines (69 loc) · 3.04 KB
/
index.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
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
// todo : encodeURIComponent usage?
// todo : https://github.com/Swizec/better-fetch implemntation
const castArray=a=>Array.isArray(a)?a:[a];
const prefix=p=>s=>p+s;
const join=(d=',')=>a=>castArray(a).join(d);
const mapN=a=>(...f)=>f.reduce((acc,v)=>acc.map(v),a);
const pca_join=(key,a)=>mapN(castArray(a))(join(),prefix(key+'='));
const push=(a,...v)=>{a.push(...v);return a;};
const query=(conditions) => '?'+Object.keys(conditions)
.reduce((acc,key)=>
push(acc,...dispatch(key,castArray(conditions[key])))
,[]).join('&');
const nonMultipleConditions=['include','exclude','page','size'];
const dispatch=(key, a)=>key==='join'
? pca_join(key,a)
: nonMultipleConditions.indexOf(key)!==-1
? [key+"="+a.join(',')]
: a.map(v=>{ // todo : should list the cases (first need to implement error system)
return key+'='+(Array.isArray(v)?v.join(','):v)
});
var index = (baseUrl, config={})=>{
// todo headers Content-Type? application/json vs multipart/form-data ...
const headers = {};
const _config = (method, body)=>{
let c= {
method: method,
...config,
headers: {
...headers,
...config.headers,
}
};
// todo do manage array of FormData?
if (body) c.body=body instanceof FormData ? body :JSON.stringify(body);
return c;
};
const url=(parts)=>[baseUrl,...parts].join('/');
const _fetch=(method,body, parts)=> {
return fetch(url(parts), _config(method, body))
.then(async response =>{
const data=await response.json();
return (response.status === 200 || response.ok)
? Promise.resolve(data)
: Promise.reject(data);
}).catch (e=>{
return Promise.reject(e.code?e:{code:-1, message:e.message})
});
};
const _readOrList=([part1,conditions])=>_fetch(
'GET',
null,
['records', ...part1].concat(conditions ? [query(conditions)] : [])
);
return {
list:(table,conditions={})=>_readOrList([[table],conditions]),
// todo : a read method without ids will be considered as a list operation by php-crud-api. JS error?
read:(table,ids,conditions={})=>_readOrList(ids?[[table,join()(ids)],conditions]:[[table]]),
create:(table,data)=>_fetch('POST',data,['records',table]),
update:(table,idOrList,data)=>_fetch('PUT',data,['records',table,join()(idOrList)]),
delete:(table,idOrList)=>_fetch('DELETE',null,['records',table,join()(idOrList)]),
register:(username,password)=>_fetch('POST', {username,password},['register']),
login:(username,password)=>_fetch('POST', {username,password},['login']),
logout:()=>_fetch('POST',{},['logout']),
password:(username, password, newPassword)=>_fetch('POST',{username, password, newPassword},['password']),
me:()=>_fetch('GET',null,['me'])
//,openapi:()=>_fetch('GET',null,['openapi'])
};
};
export { index as default };