ng2-rest 13.0.13 → 13.0.17

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 (41) hide show
  1. package/README.md +18 -16
  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/other/simple-resource.mjs +117 -0
  6. package/browser/esm2020/lib/resource.service.mjs +7 -2
  7. package/browser/esm2020/lib/rest-request.mjs +58 -16
  8. package/browser/fesm2015/ng2-rest.mjs +62 -16
  9. package/browser/fesm2015/ng2-rest.mjs.map +1 -1
  10. package/browser/fesm2020/ng2-rest.mjs +62 -16
  11. package/browser/fesm2020/ng2-rest.mjs.map +1 -1
  12. package/browser/lib/index.d.ts +1 -1
  13. package/{lib → browser/lib/other}/simple-resource.d.ts +1 -1
  14. package/browser/lib/resource.service.d.ts +4 -1
  15. package/client/esm2020/lib/index.mjs +2 -2
  16. package/client/esm2020/lib/other/simple-resource.mjs +117 -0
  17. package/client/esm2020/lib/resource.service.mjs +7 -2
  18. package/client/esm2020/lib/rest-request.mjs +58 -16
  19. package/client/fesm2015/ng2-rest.mjs +62 -16
  20. package/client/fesm2015/ng2-rest.mjs.map +1 -1
  21. package/client/fesm2020/ng2-rest.mjs +62 -16
  22. package/client/fesm2020/ng2-rest.mjs.map +1 -1
  23. package/client/lib/index.d.ts +1 -1
  24. package/{browser/lib → client/lib/other}/simple-resource.d.ts +1 -1
  25. package/client/lib/resource.service.d.ts +4 -1
  26. package/lib/index.d.ts +1 -1
  27. package/lib/index.js +1 -1
  28. package/lib/index.js.map +1 -1
  29. package/{client/lib → lib/other}/simple-resource.d.ts +1 -1
  30. package/lib/{simple-resource.js → other/simple-resource.js} +1 -1
  31. package/lib/other/simple-resource.js.map +1 -0
  32. package/lib/resource.service.d.ts +4 -1
  33. package/lib/resource.service.js +10 -1
  34. package/lib/resource.service.js.map +1 -1
  35. package/lib/rest-request.js +65 -24
  36. package/lib/rest-request.js.map +1 -1
  37. package/package.json +4 -4
  38. package/tmp-environment.json +36 -35
  39. package/browser/esm2020/lib/simple-resource.mjs +0 -117
  40. package/client/esm2020/lib/simple-resource.mjs +0 -117
  41. 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';
@@ -1090,6 +1090,9 @@ var Models;
1090
1090
 
1091
1091
  const log$1 = Log.create('[ng2-rest] rest-request', Level.__NOTHING);
1092
1092
  const jobIDkey = 'jobID';
1093
+ const customObs = 'customObs';
1094
+ const cancelFn = 'cancelFn';
1095
+ const isCanceled = 'isCanceled';
1093
1096
  //#region mock request
1094
1097
  //#endregion
1095
1098
  class RestRequest {
@@ -1105,11 +1108,6 @@ class RestRequest {
1105
1108
  }
1106
1109
  // log.d(`HANDLE RESULT (jobid:${options.jobid}) ${sourceRequest.url}`);
1107
1110
  const { res, jobid, isArray, method } = options;
1108
- // if (this.endedJobs[jobid]) {
1109
- // debugger
1110
- // }
1111
- // this.endedJobs[jobid] = true;
1112
- // log.i(`handle jobid ${jobid}`)
1113
1111
  if (typeof res !== 'object') {
1114
1112
  throw new Error('No resposnse for request. ');
1115
1113
  }
@@ -1123,15 +1121,21 @@ class RestRequest {
1123
1121
  }
1124
1122
  const entity = this.meta[jobid].entity;
1125
1123
  const circular = this.meta[jobid].circular;
1126
- 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);
1127
1128
  this.meta[jobid] = void 0;
1128
- return;
1129
+ this.subjectInuUse[jobid].complete();
1129
1130
  }
1130
1131
  checkCache(sourceRequest, jobid) {
1131
1132
  const existedInCache = RequestCache.findBy(sourceRequest);
1132
1133
  if (existedInCache) {
1133
1134
  log$1.i('cache exists', existedInCache);
1135
+ const success = Resource['_listenSuccess'];
1136
+ success.next(existedInCache.response);
1134
1137
  this.subjectInuUse[jobid].next(existedInCache);
1138
+ this.subjectInuUse[jobid].complete();
1135
1139
  return true;
1136
1140
  }
1137
1141
  // log.i(`cache not exists for jobid ${jobid}`)
@@ -1146,6 +1150,9 @@ class RestRequest {
1146
1150
  }, jobid)) {
1147
1151
  return;
1148
1152
  }
1153
+ const CancelToken = axios.CancelToken;
1154
+ const source = CancelToken.source();
1155
+ this.subjectInuUse[jobid][cancelFn] = source.cancel;
1149
1156
  var response;
1150
1157
  if (mockHttp) {
1151
1158
  if (typeof mockHttp === 'object') {
@@ -1176,10 +1183,14 @@ class RestRequest {
1176
1183
  method,
1177
1184
  data: body,
1178
1185
  responseType: 'text',
1179
- headers: headers.toJSON()
1186
+ headers: headers.toJSON(),
1187
+ cancelToken: source.token,
1180
1188
  });
1181
1189
  // log.d(`after response of jobid: ${jobid}`);
1182
1190
  }
1191
+ if (this.subjectInuUse[jobid][isCanceled]) {
1192
+ return;
1193
+ }
1183
1194
  this.handlerResult({
1184
1195
  res: {
1185
1196
  code: response.status,
@@ -1199,13 +1210,17 @@ class RestRequest {
1199
1210
  });
1200
1211
  }
1201
1212
  catch (catchedError) {
1213
+ if (this.subjectInuUse[jobid][isCanceled]) {
1214
+ return;
1215
+ }
1202
1216
  // console.log('ERROR RESPONESE catchedError typeof ', typeof catchedError)
1203
1217
  // console.log('ERROR RESPONESE catchedError', catchedError)
1204
1218
  if (typeof catchedError === 'object' && catchedError.response && catchedError.response.data) {
1205
1219
  const err = catchedError.response.data;
1206
1220
  const msg = catchedError.response.data.message || '';
1207
1221
  let stack = (err.stack || '').split('\n');
1208
- Resource['_listenErrors'].next({
1222
+ const errObs = Resource['_listenErrors'];
1223
+ errObs.next({
1209
1224
  msg,
1210
1225
  stack,
1211
1226
  data: catchedError.response.data
@@ -1262,12 +1277,39 @@ class RestRequest {
1262
1277
  }
1263
1278
  replay = this.replaySubjects[meta.endpoint][meta.path][method][objectIDToCreateOrLast];
1264
1279
  if (!_.isNumber(replay.id)) {
1280
+ if (RestRequest.jobId === Number.MAX_SAFE_INTEGER) {
1281
+ RestRequest.jobId = 0;
1282
+ }
1265
1283
  const jobid = RestRequest.jobId++;
1266
1284
  replay.id = jobid;
1267
1285
  const subject = replay.subject;
1268
1286
  subject[jobIDkey] = jobid; // modify internal rxjs subject obj
1269
1287
  this.meta[jobid] = meta;
1270
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
+ });
1271
1313
  //#region DISPOSE @UNCOMMENT AFTER TESTS
1272
1314
  // if (objectIDToCreateOrLast > 2) {
1273
1315
  // const oldReq: Models.ReplayData = this.replaySubjects[meta.endpoint][meta.path][method][(objectIDToCreateOrLast - 2)];
@@ -1290,8 +1332,8 @@ class RestRequest {
1290
1332
  // log.d(`for ${purl} jobid ${pid}`);
1291
1333
  setTimeout(() => pthis.req(purl, pmethod, pheaders, pbody, pid, pisArray, pmockHttp));
1292
1334
  })(this, url, method, headers, body, replay.id, isArray, mockHttp);
1293
- const resp = firstValueFrom(replay.subject.asObservable());
1294
- resp.observable = replay.subject.asObservable();
1335
+ const resp = firstValueFrom(replay.subject[customObs]);
1336
+ resp.observable = replay.subject[customObs];
1295
1337
  resp.cache = RequestCache.findBy({
1296
1338
  body,
1297
1339
  isArray,
@@ -1355,9 +1397,9 @@ class RestRequest {
1355
1397
  document.body.appendChild(sc);
1356
1398
  document.body.removeChild(sc);
1357
1399
  });
1358
- // return replay.subject.asObservable();
1359
- const resp = firstValueFrom(replay.subject.asObservable());
1360
- resp.observable = replay.subject.asObservable();
1400
+ const resp = firstValueFrom(replay.subject[customObs]);
1401
+ resp.observable = replay.subject[customObs];
1402
+ console.log('assiging custom observable');
1361
1403
  resp.cache = RequestCache.findBy({
1362
1404
  body,
1363
1405
  isArray,
@@ -1403,6 +1445,9 @@ class Resource {
1403
1445
  static get listenErrors() {
1404
1446
  return this._listenErrors.asObservable();
1405
1447
  }
1448
+ static get listenSuccessOperations() {
1449
+ return this._listenSuccess.asObservable();
1450
+ }
1406
1451
  //#region private mthods and fields
1407
1452
  getZone() {
1408
1453
  const isNode = (typeof window === 'undefined');
@@ -1515,7 +1560,7 @@ Instead use nested approach: /book/:bookid/author/:authorid
1515
1560
  url = url.slice(0, url.length - 1);
1516
1561
  log.i('url after', url);
1517
1562
  if (Resource.endpoints[e] !== void 0) {
1518
- !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 ('
1519
1564
  + Resource.endpoints[e].url + ')');
1520
1565
  return false;
1521
1566
  }
@@ -1620,6 +1665,7 @@ Resource.DEFAULT_HEADERS = RestHeaders.from({
1620
1665
  'Accept': 'application/json'
1621
1666
  });
1622
1667
  Resource._listenErrors = new Subject();
1668
+ Resource._listenSuccess = new Subject();
1623
1669
  Resource.enableWarnings = true;
1624
1670
  Resource.instance = new Resource();
1625
1671
  Resource.endpoints = {};