ng2-rest 13.0.12 → 13.0.16

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.
Files changed (48) hide show
  1. package/README.md +13 -17
  2. package/app.js +97 -0
  3. package/app.js.map +1 -1
  4. package/browser/esm2020/lib/index.mjs +2 -2
  5. package/browser/esm2020/lib/models.mjs +8 -1
  6. package/browser/esm2020/lib/other/simple-resource.mjs +117 -0
  7. package/browser/esm2020/lib/resource.service.mjs +7 -2
  8. package/browser/esm2020/lib/rest-request.mjs +58 -16
  9. package/browser/fesm2015/ng2-rest.mjs +69 -16
  10. package/browser/fesm2015/ng2-rest.mjs.map +1 -1
  11. package/browser/fesm2020/ng2-rest.mjs +69 -16
  12. package/browser/fesm2020/ng2-rest.mjs.map +1 -1
  13. package/browser/lib/index.d.ts +1 -1
  14. package/browser/lib/models.d.ts +1 -0
  15. package/{lib → browser/lib/other}/simple-resource.d.ts +1 -1
  16. package/browser/lib/resource.service.d.ts +4 -1
  17. package/client/esm2020/lib/index.mjs +2 -2
  18. package/client/esm2020/lib/models.mjs +8 -1
  19. package/client/esm2020/lib/other/simple-resource.mjs +117 -0
  20. package/client/esm2020/lib/resource.service.mjs +7 -2
  21. package/client/esm2020/lib/rest-request.mjs +58 -16
  22. package/client/fesm2015/ng2-rest.mjs +69 -16
  23. package/client/fesm2015/ng2-rest.mjs.map +1 -1
  24. package/client/fesm2020/ng2-rest.mjs +69 -16
  25. package/client/fesm2020/ng2-rest.mjs.map +1 -1
  26. package/client/lib/index.d.ts +1 -1
  27. package/client/lib/models.d.ts +1 -0
  28. package/{browser/lib → client/lib/other}/simple-resource.d.ts +1 -1
  29. package/client/lib/resource.service.d.ts +4 -1
  30. package/lib/index.d.ts +1 -1
  31. package/lib/index.js +1 -1
  32. package/lib/index.js.map +1 -1
  33. package/lib/models.d.ts +1 -0
  34. package/lib/models.js +11 -0
  35. package/lib/models.js.map +1 -1
  36. package/{client/lib → lib/other}/simple-resource.d.ts +1 -1
  37. package/lib/{simple-resource.js → other/simple-resource.js} +1 -1
  38. package/lib/other/simple-resource.js.map +1 -0
  39. package/lib/resource.service.d.ts +4 -1
  40. package/lib/resource.service.js +10 -1
  41. package/lib/resource.service.js.map +1 -1
  42. package/lib/rest-request.js +65 -24
  43. package/lib/rest-request.js.map +1 -1
  44. package/package.json +5 -224
  45. package/tmp-environment.json +36 -35
  46. package/browser/esm2020/lib/simple-resource.mjs +0 -117
  47. package/client/esm2020/lib/simple-resource.mjs +0 -117
  48. package/lib/simple-resource.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { Subject, firstValueFrom, Observable } from 'rxjs';
1
+ import { Subject, Observable, firstValueFrom } from 'rxjs';
2
2
  import { Log, Level } from 'ng2-logger/browser';
3
3
  import { diffChars } from 'diff';
4
4
  import { Helpers as Helpers$1, _, CoreHelpers } from 'tnp-core/browser';
@@ -988,6 +988,13 @@ var Models;
988
988
  get booleanValue() {
989
989
  return ['ok', 'true'].includes(this.body.trim());
990
990
  }
991
+ get rawJson() {
992
+ let res = this.toJSON(this.body, this.isArray);
993
+ if (this.circular && Array.isArray(this.circular)) {
994
+ res = JSON10.parse(JSON.stringify(res), this.circular);
995
+ }
996
+ return res;
997
+ }
991
998
  get json() {
992
999
  if (this.entity && typeof this.entity === 'object') {
993
1000
  const json = this.toJSON(this.body, this.isArray);
@@ -1083,6 +1090,9 @@ var Models;
1083
1090
 
1084
1091
  const log$1 = Log.create('[ng2-rest] rest-request', Level.__NOTHING);
1085
1092
  const jobIDkey = 'jobID';
1093
+ const customObs = 'customObs';
1094
+ const cancelFn = 'cancelFn';
1095
+ const isCanceled = 'isCanceled';
1086
1096
  //#region mock request
1087
1097
  //#endregion
1088
1098
  class RestRequest {
@@ -1098,11 +1108,6 @@ class RestRequest {
1098
1108
  }
1099
1109
  // log.d(`HANDLE RESULT (jobid:${options.jobid}) ${sourceRequest.url}`);
1100
1110
  const { res, jobid, isArray, method } = options;
1101
- // if (this.endedJobs[jobid]) {
1102
- // debugger
1103
- // }
1104
- // this.endedJobs[jobid] = true;
1105
- // log.i(`handle jobid ${jobid}`)
1106
1111
  if (typeof res !== 'object') {
1107
1112
  throw new Error('No resposnse for request. ');
1108
1113
  }
@@ -1116,15 +1121,21 @@ class RestRequest {
1116
1121
  }
1117
1122
  const entity = this.meta[jobid].entity;
1118
1123
  const circular = this.meta[jobid].circular;
1119
- this.subjectInuUse[jobid].next(new Models.HttpResponse(sourceRequest, res.data, res.headers, res.code, entity, circular, jobid, isArray));
1124
+ const success = Resource['_listenSuccess'];
1125
+ const reqResp = new Models.HttpResponse(sourceRequest, res.data, res.headers, res.code, entity, circular, jobid, isArray);
1126
+ success.next(reqResp);
1127
+ this.subjectInuUse[jobid].next(reqResp);
1120
1128
  this.meta[jobid] = void 0;
1121
- return;
1129
+ this.subjectInuUse[jobid].complete();
1122
1130
  }
1123
1131
  checkCache(sourceRequest, jobid) {
1124
1132
  const existedInCache = RequestCache.findBy(sourceRequest);
1125
1133
  if (existedInCache) {
1126
1134
  log$1.i('cache exists', existedInCache);
1135
+ const success = Resource['_listenSuccess'];
1136
+ success.next(existedInCache.response);
1127
1137
  this.subjectInuUse[jobid].next(existedInCache);
1138
+ this.subjectInuUse[jobid].complete();
1128
1139
  return true;
1129
1140
  }
1130
1141
  // log.i(`cache not exists for jobid ${jobid}`)
@@ -1139,6 +1150,9 @@ class RestRequest {
1139
1150
  }, jobid)) {
1140
1151
  return;
1141
1152
  }
1153
+ const CancelToken = axios.CancelToken;
1154
+ const source = CancelToken.source();
1155
+ this.subjectInuUse[jobid][cancelFn] = source.cancel;
1142
1156
  var response;
1143
1157
  if (mockHttp) {
1144
1158
  if (typeof mockHttp === 'object') {
@@ -1169,10 +1183,14 @@ class RestRequest {
1169
1183
  method,
1170
1184
  data: body,
1171
1185
  responseType: 'text',
1172
- headers: headers.toJSON()
1186
+ headers: headers.toJSON(),
1187
+ cancelToken: source.token,
1173
1188
  });
1174
1189
  // log.d(`after response of jobid: ${jobid}`);
1175
1190
  }
1191
+ if (this.subjectInuUse[jobid][isCanceled]) {
1192
+ return;
1193
+ }
1176
1194
  this.handlerResult({
1177
1195
  res: {
1178
1196
  code: response.status,
@@ -1192,13 +1210,17 @@ class RestRequest {
1192
1210
  });
1193
1211
  }
1194
1212
  catch (catchedError) {
1213
+ if (this.subjectInuUse[jobid][isCanceled]) {
1214
+ return;
1215
+ }
1195
1216
  // console.log('ERROR RESPONESE catchedError typeof ', typeof catchedError)
1196
1217
  // console.log('ERROR RESPONESE catchedError', catchedError)
1197
1218
  if (typeof catchedError === 'object' && catchedError.response && catchedError.response.data) {
1198
1219
  const err = catchedError.response.data;
1199
1220
  const msg = catchedError.response.data.message || '';
1200
1221
  let stack = (err.stack || '').split('\n');
1201
- Resource['_listenErrors'].next({
1222
+ const errObs = Resource['_listenErrors'];
1223
+ errObs.next({
1202
1224
  msg,
1203
1225
  stack,
1204
1226
  data: catchedError.response.data
@@ -1255,12 +1277,39 @@ class RestRequest {
1255
1277
  }
1256
1278
  replay = this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast];
1257
1279
  if (!_.isNumber(replay.id)) {
1280
+ if (RestRequest.jobId === Number.MAX_SAFE_INTEGER) {
1281
+ RestRequest.jobId = 0;
1282
+ }
1258
1283
  const jobid = RestRequest.jobId++;
1259
1284
  replay.id = jobid;
1260
1285
  const subject = replay.subject;
1261
1286
  subject[jobIDkey] = jobid; // modify internal rxjs subject obj
1262
1287
  this.meta[jobid] = meta;
1263
1288
  this.subjectInuUse[jobid] = subject;
1289
+ this.subjectInuUse[jobid][customObs] = new Observable((observer) => {
1290
+ // observer.remove(() => {
1291
+ // });
1292
+ observer.add(() => {
1293
+ // console.log(`cancel observable job${jobid}`)
1294
+ if (!this.subjectInuUse[jobid][isCanceled]) {
1295
+ this.subjectInuUse[jobid][isCanceled] = true;
1296
+ if (typeof this.subjectInuUse[jobid][cancelFn] === 'function') {
1297
+ this.subjectInuUse[jobid][cancelFn]('[ng2-rest] on purpose canceled http request');
1298
+ }
1299
+ }
1300
+ else {
1301
+ // console.log(`somehow second time cancel ${jobid}`)
1302
+ }
1303
+ });
1304
+ const sub = subject.subscribe({
1305
+ next: a => observer.next(a),
1306
+ error: a => observer.error(a),
1307
+ complete: () => {
1308
+ sub.unsubscribe();
1309
+ observer.complete();
1310
+ },
1311
+ });
1312
+ });
1264
1313
  //#region DISPOSE @UNCOMMENT AFTER TESTS
1265
1314
  // if (objectIDToCreateOrLast > 2) {
1266
1315
  // const oldReq: Models.ReplayData = this.replaySubjects[meta.endpoint][meta.path][method][(objectIDToCreateOrLast - 2)];
@@ -1283,8 +1332,8 @@ class RestRequest {
1283
1332
  // log.d(`for ${purl} jobid ${pid}`);
1284
1333
  setTimeout(() => pthis.req(purl, pmethod, pheaders, pbody, pid, pisArray, pmockHttp));
1285
1334
  })(this, url, method, headers, body, replay.id, isArray, mockHttp);
1286
- const resp = firstValueFrom(replay.subject.asObservable());
1287
- resp.observable = replay.subject.asObservable();
1335
+ const resp = firstValueFrom(replay.subject[customObs]);
1336
+ resp.observable = replay.subject[customObs];
1288
1337
  resp.cache = RequestCache.findBy({
1289
1338
  body,
1290
1339
  isArray,
@@ -1348,9 +1397,9 @@ class RestRequest {
1348
1397
  document.body.appendChild(sc);
1349
1398
  document.body.removeChild(sc);
1350
1399
  });
1351
- // return replay.subject.asObservable();
1352
- const resp = firstValueFrom(replay.subject.asObservable());
1353
- resp.observable = replay.subject.asObservable();
1400
+ const resp = firstValueFrom(replay.subject[customObs]);
1401
+ resp.observable = replay.subject[customObs];
1402
+ console.log('assiging custom observable');
1354
1403
  resp.cache = RequestCache.findBy({
1355
1404
  body,
1356
1405
  isArray,
@@ -1396,6 +1445,9 @@ class Resource {
1396
1445
  static get listenErrors() {
1397
1446
  return this._listenErrors.asObservable();
1398
1447
  }
1448
+ static get listenSuccessOperations() {
1449
+ return this._listenSuccess.asObservable();
1450
+ }
1399
1451
  //#region private mthods and fields
1400
1452
  getZone() {
1401
1453
  const isNode = (typeof window === 'undefined');
@@ -1508,7 +1560,7 @@ Instead use nested approach: /book/:bookid/author/:authorid
1508
1560
  url = url.slice(0, url.length - 1);
1509
1561
  log.i('url after', url);
1510
1562
  if (Resource.endpoints[e] !== void 0) {
1511
- !global.hideLog && console.warn('Cannot use map function at the same API endpoint again ('
1563
+ Helpers$1.log('Cannot use map function at the same API endpoint again ('
1512
1564
  + Resource.endpoints[e].url + ')');
1513
1565
  return false;
1514
1566
  }
@@ -1613,6 +1665,7 @@ Resource.DEFAULT_HEADERS = RestHeaders.from({
1613
1665
  'Accept': 'application/json'
1614
1666
  });
1615
1667
  Resource._listenErrors = new Subject();
1668
+ Resource._listenSuccess = new Subject();
1616
1669
  Resource.enableWarnings = true;
1617
1670
  Resource.instance = new Resource();
1618
1671
  Resource.endpoints = {};