@andersonalmeidax0/webcomponents 0.1.21 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/RestAPIAdapter.js +170 -169
- package/package.json +1 -1
package/RestAPIAdapter.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-unused-vars */
|
|
2
|
-
import {PlatAPIError,PlatAPIReturnCodeError,PlatAPIAuthError,PlatAPIDBNotFound,PlatAPIDBIntegrityConstraint,PlatAPIForbidden} from "./Errors";
|
|
2
|
+
import { PlatAPIError, PlatAPIReturnCodeError, PlatAPIAuthError, PlatAPIDBNotFound, PlatAPIDBIntegrityConstraint, PlatAPIForbidden } from "./Errors";
|
|
3
3
|
|
|
4
4
|
//ERPNEXT API request: BODYTAG:data, RESPTAG:data,LISTTAG:null (só o array)
|
|
5
5
|
//https://frappeframework.com/docs/user/en/api/rest
|
|
@@ -32,7 +32,7 @@ import {PlatAPIError,PlatAPIReturnCodeError,PlatAPIAuthError,PlatAPIDBNotFound,P
|
|
|
32
32
|
e.set tem insertedID, seta no payload
|
|
33
33
|
f.se tem data, seta (se for collection.. verifica se campo "type", do primeiro item [0] é igual ao typeName)
|
|
34
34
|
g.senao erro
|
|
35
|
-
*/
|
|
35
|
+
*/
|
|
36
36
|
//8-gerar uma versao sem "data" para usar com Json-server
|
|
37
37
|
/*
|
|
38
38
|
==> para tertar com ERPNext: tem que
|
|
@@ -76,185 +76,186 @@ X-Colocar o MetalMarlek em outro server AWS ... com docker... com outra conta de
|
|
|
76
76
|
/** **********************************************
|
|
77
77
|
*
|
|
78
78
|
********************************************** */
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
79
|
+
class RestAPIAdapter {
|
|
80
|
+
tableName;
|
|
81
|
+
urlServer;
|
|
82
|
+
tagData;
|
|
83
|
+
/** ***********************
|
|
84
|
+
* tableName is entity (after server)
|
|
85
|
+
* Server is option (can be null for local API ou not for remote APIs)
|
|
86
|
+
*********************** */
|
|
87
|
+
constructor(tableName, urlServer, tagData, token, pnewVO, debug) {
|
|
88
|
+
this.DEBUG_APIFETCH = debug;
|
|
89
|
+
this.tableName = tableName;
|
|
90
|
+
this.urlServer = urlServer;
|
|
91
|
+
this.tagData = tagData;
|
|
92
|
+
this.token = token;
|
|
93
|
+
if (pnewVO) this.newVO = pnewVO;
|
|
94
|
+
}
|
|
95
|
+
/** ***********************
|
|
96
|
+
*
|
|
97
|
+
*********************** */
|
|
98
|
+
newVO() { return {}; };
|
|
99
|
+
view2model(vo) { return vo; }
|
|
100
|
+
model2view(vo) { return vo; }
|
|
101
|
+
validate(vo) { return vo; }
|
|
102
|
+
/** ***********************
|
|
103
|
+
*
|
|
104
|
+
*********************** */
|
|
105
|
+
getParams(q, vo, entity) {
|
|
106
|
+
if (q === "create") return { method: 'POST', url: `/${entity}/`, body: vo }
|
|
107
|
+
if (q === "findByPK") return { method: 'GET', url: `/${entity}/${vo._id}`, body: null };
|
|
108
|
+
if (q === "findByFK") return { method: 'GET', url: `/${entity}`, body: null };
|
|
109
|
+
if (q === "findAll") return { method: 'GET', url: `/${entity}`, body: null };
|
|
110
|
+
if (q === "update") return { method: 'PUT', url: `/${entity}/${vo._id}`, body: vo }
|
|
111
|
+
if (q === "removeByPK") return { method: 'DELETE', url: `/${entity}/${vo._id}`, body: null };
|
|
112
|
+
|
|
113
|
+
return { method: 'INVALID', url: `/INVALID`, body: null };
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
/** ***********************
|
|
117
|
+
*
|
|
118
|
+
*********************** */
|
|
119
|
+
async getJSONResponse(response) {
|
|
120
|
+
var r = null;
|
|
121
|
+
r = await response.json();
|
|
122
|
+
if (this.DEBUG_APIFETCH) alert(JSON.stringify(r, null, 2));
|
|
123
|
+
if (r.error)
|
|
124
|
+
throw new PlatAPIReturnCodeError(r.error.message);
|
|
125
|
+
return r;
|
|
126
|
+
}
|
|
127
|
+
/** ***********************
|
|
128
|
+
*
|
|
129
|
+
*********************** */
|
|
130
|
+
async callAPI(q, vo, context, filter) {
|
|
131
|
+
var r = null;
|
|
132
|
+
var ptoken = this.token;
|
|
133
|
+
//?this.token:(getStorageWithExpiry('sid'));
|
|
134
|
+
//if(ptoken==null)
|
|
135
|
+
// throw new PlatAPIAuthError( `An error has occured: ${response.status}`);
|
|
136
|
+
|
|
137
|
+
var { method, url, body } = this.getParams(q, vo, this.tableName, filter);
|
|
138
|
+
//server is optional
|
|
139
|
+
url = (this.urlServer == null) ? url : this.urlServer + url;
|
|
140
|
+
|
|
141
|
+
const response = await this.myFetch3(ptoken, url, method, body);
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
if (response.status === 404)
|
|
144
|
+
throw new PlatAPIDBNotFound(`An error has occured: ${response.status}`);
|
|
145
|
+
if (response.status === 403)
|
|
146
|
+
throw new PlatAPIForbidden(`An error has occured: ${response.status}`);
|
|
147
|
+
if (response.status === 401)
|
|
148
|
+
throw new PlatAPIAuthError(`An error has occured: ${response.status}`);
|
|
149
|
+
throw new PlatAPIReturnCodeError();
|
|
94
150
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
/** ***********************
|
|
103
|
-
*
|
|
104
|
-
*********************** */
|
|
105
|
-
getParams(q,vo,entity){
|
|
106
|
-
if(q==="create") return {method:'POST',url:`/${entity}/`,body:vo}
|
|
107
|
-
if(q==="findByPK") return {method:'GET',url:`/${entity}/${vo._id}`,body:null};
|
|
108
|
-
if(q==="findByFK") return {method:'GET',url:`/${entity}`,body:null};
|
|
109
|
-
if(q==="findAll") return {method:'GET',url:`/${entity}`,body:null};
|
|
110
|
-
if(q==="update") return {method:'PUT',url:`/${entity}/${vo._id}`,body:vo}
|
|
111
|
-
if(q==="removeByPK") return {method:'DELETE',url:`/${entity}/${vo._id}`,body:null};
|
|
151
|
+
r = await this.getJSONResponse(response);
|
|
152
|
+
|
|
153
|
+
//tag de dados pode ser nada,result, data
|
|
154
|
+
if (this.tagData == null) {
|
|
155
|
+
if (vo && vo.filedata)
|
|
156
|
+
await this.uploadFile(vo.filedata);
|
|
157
|
+
return r;
|
|
112
158
|
|
|
113
|
-
|
|
159
|
+
}
|
|
114
160
|
|
|
161
|
+
var respBlock = this.tagData;
|
|
162
|
+
if (r[respBlock])
|
|
163
|
+
r[respBlock].insertedId = r.insertedId;
|
|
164
|
+
if (r[respBlock]) {
|
|
165
|
+
if (vo && vo.filedata)
|
|
166
|
+
await this.uploadFile(vo.filedata);
|
|
167
|
+
return r[respBlock];
|
|
115
168
|
}
|
|
116
|
-
|
|
117
|
-
*
|
|
118
|
-
*********************** */
|
|
119
|
-
async getJSONResponse(response) {
|
|
120
|
-
var r=null;
|
|
121
|
-
r=await response.json();
|
|
122
|
-
if(this.DEBUG_APIFETCH)alert(JSON.stringify(r,null,2));
|
|
123
|
-
if(r.error)
|
|
124
|
-
throw new PlatAPIReturnCodeError(r.error.message);
|
|
125
|
-
return r;
|
|
126
|
-
}
|
|
127
|
-
/** ***********************
|
|
128
|
-
*
|
|
129
|
-
*********************** */
|
|
130
|
-
async callAPI(q,vo,context,filter) {
|
|
131
|
-
var r = null;
|
|
132
|
-
var ptoken=this.token;
|
|
133
|
-
//?this.token:(getStorageWithExpiry('sid'));
|
|
134
|
-
//if(ptoken==null)
|
|
135
|
-
// throw new PlatAPIAuthError( `An error has occured: ${response.status}`);
|
|
136
|
-
|
|
137
|
-
var {method,url,body}=this.getParams(q,vo,this.tableName,filter);
|
|
138
|
-
//server is optional
|
|
139
|
-
url = (this.urlServer==null)?url:this.urlServer+url;
|
|
140
|
-
|
|
141
|
-
const response = await this.myFetch3(ptoken,url,method,body);
|
|
142
|
-
if (!response.ok) {
|
|
143
|
-
if(response.status===404)
|
|
144
|
-
throw new PlatAPIDBNotFound( `An error has occured: ${response.status}`);
|
|
145
|
-
if(response.status===403)
|
|
146
|
-
throw new PlatAPIForbidden( `An error has occured: ${response.status}`);
|
|
147
|
-
if(response.status===401)
|
|
148
|
-
throw new PlatAPIAuthError( `An error has occured: ${response.status}`);
|
|
149
|
-
throw new PlatAPIReturnCodeError();
|
|
150
|
-
}
|
|
151
|
-
r=await this.getJSONResponse(response);
|
|
169
|
+
throw new PlatAPIError('Invalid payload response');
|
|
152
170
|
|
|
153
|
-
//tag de dados pode ser nada,result, data
|
|
154
|
-
if(this.tagData==null){
|
|
155
|
-
if(vo&&vo.filedata)
|
|
156
|
-
await this.uploadFile(vo.filedata);
|
|
157
|
-
return r;
|
|
158
171
|
|
|
159
|
-
}
|
|
160
172
|
|
|
161
|
-
|
|
162
|
-
if(r[respBlock])
|
|
163
|
-
r[respBlock].insertedId=r.insertedId;
|
|
164
|
-
if(r[respBlock]) {
|
|
165
|
-
if(vo&&vo.filedata)
|
|
166
|
-
await this.uploadFile(vo.filedata);
|
|
167
|
-
return r[respBlock];
|
|
168
|
-
}
|
|
169
|
-
throw new PlatAPIError('Invalid payload response');
|
|
173
|
+
};
|
|
170
174
|
|
|
175
|
+
/** ***********************
|
|
176
|
+
*
|
|
177
|
+
*********************** */
|
|
178
|
+
async uploadFile(fileData) {
|
|
179
|
+
alert('UPLOAD start:' + this.urlServer + '/upload');
|
|
180
|
+
let formData = new FormData();
|
|
181
|
+
formData.append('file', fileData);
|
|
182
|
+
const response = await fetch(this.urlServer + '/upload', {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
body: formData,
|
|
185
|
+
})
|
|
186
|
+
if (!response.ok) {
|
|
187
|
+
if (response.status === 404)
|
|
188
|
+
throw new PlatAPIDBNotFound(`An error has occured: ${response.status}`);
|
|
189
|
+
if (response.status === 403)
|
|
190
|
+
throw new PlatAPIForbidden(`An error has occured: ${response.status}`);
|
|
191
|
+
if (response.status === 401)
|
|
192
|
+
throw new PlatAPIAuthError(`An error has occured: ${response.status}`);
|
|
193
|
+
throw new PlatAPIReturnCodeError();
|
|
194
|
+
}
|
|
195
|
+
alert("uploadfile vao ver r");
|
|
196
|
+
var r = await response.json();
|
|
197
|
+
alert("upload response:" + JSON.stringify(r));
|
|
198
|
+
return r;
|
|
199
|
+
}
|
|
171
200
|
|
|
172
|
-
|
|
173
|
-
};
|
|
174
201
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
throw new PlatAPIDBNotFound( `An error has occured: ${response.status}`);
|
|
189
|
-
if(response.status===403)
|
|
190
|
-
throw new PlatAPIForbidden( `An error has occured: ${response.status}`);
|
|
191
|
-
if(response.status===401)
|
|
192
|
-
throw new PlatAPIAuthError( `An error has occured: ${response.status}`);
|
|
193
|
-
throw new PlatAPIReturnCodeError();
|
|
194
|
-
}
|
|
195
|
-
r=await response.json();
|
|
196
|
-
alert("upload response:"+JSON.stringify(r));
|
|
197
|
-
return r;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
async download(fname) {
|
|
202
|
-
alert('UPLOAD start:'+this.urlServer+'/download/'+fname);
|
|
203
|
-
var response=await fetch(this.urlServer+'/download/'+fname, {
|
|
204
|
-
method: 'GET'
|
|
205
|
-
})
|
|
206
|
-
if (!response.ok) {
|
|
207
|
-
if(response.status===404)
|
|
208
|
-
throw new PlatAPIDBNotFound( `An error has occured: ${response.status}`);
|
|
209
|
-
if(response.status===403)
|
|
210
|
-
throw new PlatAPIForbidden( `An error has occured: ${response.status}`);
|
|
211
|
-
if(response.status===401)
|
|
212
|
-
throw new PlatAPIAuthError( `An error has occured: ${response.status}`);
|
|
213
|
-
throw new PlatAPIReturnCodeError();
|
|
214
|
-
}
|
|
215
|
-
var blob = await response.blob();
|
|
216
|
-
return blob;
|
|
202
|
+
async download(fname) {
|
|
203
|
+
alert('UPLOAD start:' + this.urlServer + '/download/' + fname);
|
|
204
|
+
var response = await fetch(this.urlServer + '/download/' + fname, {
|
|
205
|
+
method: 'GET'
|
|
206
|
+
})
|
|
207
|
+
if (!response.ok) {
|
|
208
|
+
if (response.status === 404)
|
|
209
|
+
throw new PlatAPIDBNotFound(`An error has occured: ${response.status}`);
|
|
210
|
+
if (response.status === 403)
|
|
211
|
+
throw new PlatAPIForbidden(`An error has occured: ${response.status}`);
|
|
212
|
+
if (response.status === 401)
|
|
213
|
+
throw new PlatAPIAuthError(`An error has occured: ${response.status}`);
|
|
214
|
+
throw new PlatAPIReturnCodeError();
|
|
217
215
|
}
|
|
216
|
+
var blob = await response.blob();
|
|
217
|
+
return blob;
|
|
218
|
+
}
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
220
|
+
/** ***********************
|
|
221
|
+
*
|
|
222
|
+
*********************** */
|
|
223
|
+
async create(vo, context) {
|
|
224
|
+
return await this.callAPI('create', vo, context)
|
|
225
|
+
};
|
|
226
|
+
async findByPK(id, context) {
|
|
227
|
+
//alert(JSON.stringify(vo));
|
|
228
|
+
return await this.callAPI('findByPK', { _id: id }, context)
|
|
229
|
+
};
|
|
230
|
+
async findByFK(fkArr, context) {
|
|
231
|
+
return await this.callAPI('findByFK', fkArr, context)
|
|
232
|
+
};
|
|
233
|
+
async update(vo, context) {
|
|
234
|
+
//alert(JSON.stringify(vo));
|
|
235
|
+
return await this.callAPI('update', vo, context)
|
|
236
|
+
};
|
|
237
|
+
async removeByPK(vo, context) {
|
|
238
|
+
return await this.callAPI('removeByPK', vo, context)
|
|
239
|
+
};
|
|
240
|
+
async findAll(context, filter) {
|
|
241
|
+
return await this.callAPI('findAll', null, context, filter)
|
|
242
|
+
};
|
|
243
|
+
/** ***********************
|
|
244
|
+
*
|
|
245
|
+
*********************** */
|
|
246
|
+
async myFetch3(token, url, method, body) {
|
|
247
|
+
if (this.DEBUG_APIFETCH) alert("url:" + url);
|
|
248
|
+
if (this.DEBUG_APIFETCH) alert("m:" + method);
|
|
249
|
+
if (this.DEBUG_APIFETCH) alert("body:" + body);
|
|
250
|
+
if (this.DEBUG_APIFETCH) alert("token:" + JSON.stringify(token));
|
|
250
251
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
252
|
+
var r = await fetch(url,
|
|
253
|
+
{
|
|
254
|
+
method: method,
|
|
255
|
+
headers: { "content-type": "application/json", "Authorization": token },
|
|
256
|
+
body: body ? JSON.stringify(body) : null
|
|
257
|
+
});
|
|
258
|
+
return r;
|
|
258
259
|
}
|
|
259
260
|
}
|
|
260
|
-
export {RestAPIAdapter};
|
|
261
|
+
export { RestAPIAdapter };
|