-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjo.js
158 lines (141 loc) · 3.87 KB
/
jo.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
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
/*
Copyright 2018 Fabrizio Montesi <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
(function() {
"use strict";
function toJson( response )
{
return response.json();
}
function genError( response )
{
if( response.ok ) {
return Promise.resolve( response );
} else {
const error = new Error( response.statusText );
return response.json().then( json => {
error.isFault = true;
error.fault = json.error;
throw error;
} );
}
}
function jolieFetch( operation, params )
{
if ( typeof params.service === 'undefined' ) {
operation = '/' + operation;
} else {
if ( !params.service.startsWith( '/' ) ) {
params.service = '/' + params.service;
}
operation = '/!' + params.service + '!/' + operation;
}
params.headers = { "Content-Type": "application/json" };
if ( typeof params.method === 'undefined' ) {
params.method = 'POST';
}
if ( typeof params.data !== 'undefined' ) {
if ( params.method === 'GET' || params.method === 'HEAD' ) {
operation += "?" + JSON.stringify( params.data );
} else {
params.body = JSON.stringify( params.data );
}
}
return fetch( operation, params ).then( genError ).then( toJson );
}
function initParams( data, params )
{
if ( typeof params === 'undefined' ) {
params = {};
}
if ( typeof data !== 'undefined' ) {
params.data = data;
}
return params;
}
const proxyBuilder = ( service ) => {
return new Proxy( {}, {
get: ( target, prop, receiver ) => {
return ( data, params ) => {
params = initParams( data, params );
params.service = service;
return jolieFetch( prop, params );
}
}
} );
}
const buildHttpVerbs = ( buildFetch ) => { return {
get: ( data, params ) => {
return buildFetch( data, params, 'GET' );
},
post: ( data, params ) => {
return buildFetch( data, params, 'POST' );
},
delete: ( data, params ) => {
return buildFetch( data, params, 'DELETE' );
},
head: ( data, params ) => {
return buildFetch( data, params, 'HEAD' );
},
patch: ( data, params ) => {
return buildFetch( data, params, 'PATCH' );
},
options: ( data, params ) => {
return buildFetch( data, params, 'OPTIONS' );
},
put: ( data, params ) => {
return buildFetch( data, params, 'PUT' );
}
} };
const resourceProxyBuilder = ( service ) => {
return new Proxy( {}, {
get: ( target, prop, receiver ) => {
const buildFetch = ( data, params, method ) => {
params = initParams( data, params );
params.service = service;
params.method = method;
return jolieFetch( prop, params );
};
return buildHttpVerbs( buildFetch );
}
} )
}
window.Jo = new Proxy( proxyBuilder, {
get: ( target, prop, receiver ) => {
return ( data, params ) => {
params = initParams( data, params );
return jolieFetch( prop, params );
}
}
} );
window.Jor = new Proxy( resourceProxyBuilder, {
get: ( target, prop, receiver ) => {
return ( data, params ) => {
const buildFetch = ( data, params, method ) => {
params = initParams( data, params );
params.method = method;
return jolieFetch( prop, params );
};
return buildHttpVerbs( buildFetch );
}
}
} );
window.JoHelp = {
parseError: error => {
if ( error.isFault ) {
return Promise.reject( JSON.stringify( error.fault ) );
} else {
return Promise.reject( error.message );
}
}
};
}());