@ethlete/query 0.1.0-next.10 → 0.1.0-next.11

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.
@@ -2,35 +2,34 @@ import { __awaiter } from 'tslib';
2
2
  import { Subject, timer, filter, takeWhile, switchMap, of, BehaviorSubject, interval, takeUntil, tap, fromEvent, startWith, concat, take, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
3
3
  import * as i0 from '@angular/core';
4
4
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
5
- import * as i1 from '@ethlete/core';
6
- import { DestroyDirective } from '@ethlete/core';
5
+ import { DestroyService } from '@ethlete/core';
7
6
  import { FormGroup } from '@angular/forms';
8
7
  import { Router, ActivatedRoute } from '@angular/router';
9
8
 
10
9
  class CustomHeaderAuthProvider {
10
+ get header() {
11
+ return { [this._config.name]: this._config.value };
12
+ }
11
13
  constructor(_config) {
12
14
  this._config = _config;
13
15
  this.queryClient = null;
14
16
  }
15
- get header() {
16
- return { [this._config.name]: this._config.value };
17
- }
18
17
  cleanUp() {
19
18
  // noop
20
19
  }
21
20
  }
22
21
 
23
22
  class BasicAuthProvider {
24
- constructor(_config) {
25
- this._config = _config;
26
- this.queryClient = null;
27
- }
28
23
  get header() {
29
24
  return { Authorization: `Basic ${this._basicAuthString}` };
30
25
  }
31
26
  get _basicAuthString() {
32
27
  return btoa(`${this._config.username}:${this._config.password}`);
33
28
  }
29
+ constructor(_config) {
30
+ this._config = _config;
31
+ this.queryClient = null;
32
+ }
34
33
  cleanUp() {
35
34
  // noop
36
35
  }
@@ -317,6 +316,9 @@ const decryptBearer = (token) => {
317
316
  };
318
317
 
319
318
  class BearerAuthProvider {
319
+ get header() {
320
+ return { Authorization: `Bearer ${this._token}` };
321
+ }
320
322
  constructor(_config) {
321
323
  var _a, _b;
322
324
  this._config = _config;
@@ -332,16 +334,13 @@ class BearerAuthProvider {
332
334
  this._setupRefresh(this._config.refreshConfig);
333
335
  }
334
336
  }
335
- get header() {
336
- return { Authorization: `Bearer ${this._token}` };
337
- }
338
337
  cleanUp() {
339
338
  var _a;
340
339
  (_a = this._refreshTimerSubscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
341
340
  this._refreshTimerSubscription = null;
342
341
  }
343
342
  _setupRefresh(config) {
344
- var _a;
343
+ var _a, _b;
345
344
  const bearer = decryptBearer(this._token);
346
345
  if (!bearer) {
347
346
  return;
@@ -349,11 +348,12 @@ class BearerAuthProvider {
349
348
  const expiresInPropertyName = config.expiresInPropertyName || 'exp';
350
349
  const expiresIn = bearer[expiresInPropertyName];
351
350
  const fiveMinutes = 5 * 60 * 1000;
351
+ const refreshBuffer = (_a = config.refreshBuffer) !== null && _a !== void 0 ? _a : fiveMinutes;
352
352
  if (expiresIn === undefined) {
353
353
  throw new Error(`Bearer token does not contain an '${expiresInPropertyName}' property`);
354
354
  }
355
- const remainingTime = new Date(expiresIn * 1000).getTime() - fiveMinutes - new Date().getTime();
356
- const strategy = (_a = config.strategy) !== null && _a !== void 0 ? _a : "beforeExpiration" /* AuthBearerRefreshStrategy.BeforeExpiration */;
355
+ const remainingTime = new Date(expiresIn * 1000).getTime() - refreshBuffer - new Date().getTime();
356
+ const strategy = (_b = config.strategy) !== null && _b !== void 0 ? _b : "beforeExpiration" /* AuthBearerRefreshStrategy.BeforeExpiration */;
357
357
  switch (strategy) {
358
358
  case "beforeExpiration" /* AuthBearerRefreshStrategy.BeforeExpiration */:
359
359
  this._refreshTimerSubscription = timer(remainingTime).subscribe(() => this._refresh(config));
@@ -361,6 +361,7 @@ class BearerAuthProvider {
361
361
  }
362
362
  }
363
363
  _refresh(config) {
364
+ var _a;
364
365
  return __awaiter(this, void 0, void 0, function* () {
365
366
  if (!this._refreshToken) {
366
367
  throw new Error('No refresh token found');
@@ -368,13 +369,14 @@ class BearerAuthProvider {
368
369
  if (!this.queryClient) {
369
370
  throw new Error('Query client instance is null');
370
371
  }
371
- const { method, route, bodyAdapter, responseAdapter } = config;
372
+ const { method, route, requestAdapter, responseAdapter, paramLocation } = config;
373
+ const data = (_a = requestAdapter === null || requestAdapter === void 0 ? void 0 : requestAdapter(this._refreshToken)) !== null && _a !== void 0 ? _a : { refreshToken: this._refreshToken };
372
374
  const fullRoute = buildRoute({
373
375
  base: this.queryClient.config.baseRoute,
374
376
  route,
377
+ queryParams: paramLocation === 'query' ? data : undefined,
375
378
  });
376
- const body = bodyAdapter ? bodyAdapter(this._refreshToken) : { refreshToken: this._refreshToken };
377
- const requestInit = { body: buildBody(body), method };
379
+ const requestInit = { body: paramLocation === 'body' ? buildBody(data) : undefined, method };
378
380
  this.onRefreshInitiation$.next();
379
381
  try {
380
382
  const result = yield request({
@@ -427,31 +429,6 @@ const gql = (strings, ...values) => {
427
429
  return str;
428
430
  };
429
431
 
430
- const deepFreeze = (object) => {
431
- // check if object is actually an object or frozen
432
- if (!object || typeof object !== 'object' || Object.isFrozen(object)) {
433
- return object;
434
- }
435
- // Retrieve the property names defined on object
436
- const propNames = Object.getOwnPropertyNames(object);
437
- // Freeze properties before freezing self
438
- for (const name of propNames) {
439
- const value = object[name];
440
- if (value && typeof value === 'object') {
441
- deepFreeze(value);
442
- }
443
- }
444
- return Object.freeze(object);
445
- };
446
-
447
- /**
448
- * Dummy function for providing types while not breaking inference.
449
- * **This function always returns null.**
450
- * @see https://github.com/microsoft/TypeScript/issues/26242
451
- * @returns null
452
- */
453
- const def = () => null;
454
-
455
432
  function filterSuccess() {
456
433
  return function (source) {
457
434
  return source.pipe(filter((value) => isQueryStateSuccess(value)));
@@ -506,21 +483,8 @@ const isGqlQueryConfig = (config) => {
506
483
  };
507
484
 
508
485
  class Query {
509
- constructor(_client, _queryConfig, _route, _args) {
510
- this._client = _client;
511
- this._queryConfig = _queryConfig;
512
- this._route = _route;
513
- this._args = _args;
514
- this._currentId = 0;
515
- this._abortController = new AbortController();
516
- this._pollingSubscription = null;
517
- this._state$ = new BehaviorSubject({
518
- type: "PREPARED" /* QueryStateType.Prepared */,
519
- meta: { id: this._currentId },
520
- });
521
- }
522
486
  get _nextId() {
523
- return this._currentId++;
487
+ return ++this._currentId;
524
488
  }
525
489
  get state$() {
526
490
  return this._state$.asObservable();
@@ -538,6 +502,20 @@ class Query {
538
502
  }
539
503
  return ts < Date.now();
540
504
  }
505
+ constructor(_client, _queryConfig, _route, _args) {
506
+ this._client = _client;
507
+ this._queryConfig = _queryConfig;
508
+ this._route = _route;
509
+ this._args = _args;
510
+ this._currentId = 0;
511
+ this._abortController = new AbortController();
512
+ this._pollingSubscription = null;
513
+ this._onAbort$ = new Subject();
514
+ this._state$ = new BehaviorSubject({
515
+ type: "PREPARED" /* QueryStateType.Prepared */,
516
+ meta: { id: this._currentId },
517
+ });
518
+ }
541
519
  clone() {
542
520
  return this._client.fetch(this._queryConfig);
543
521
  }
@@ -553,11 +531,12 @@ class Query {
553
531
  if (isQueryStateLoading(this._state$.value)) {
554
532
  this.abort();
555
533
  }
556
- const meta = deepFreeze({ id });
534
+ const meta = { id };
557
535
  this._state$.next({
558
536
  type: "LOADING" /* QueryStateType.Loading */,
559
537
  meta,
560
538
  });
539
+ this._updateUseResultInDependencies();
561
540
  let body = null;
562
541
  let contentType = null;
563
542
  if (isGqlQueryConfig(this._queryConfig)) {
@@ -622,9 +601,9 @@ class Query {
622
601
  : responseData;
623
602
  this._state$.next({
624
603
  type: "SUCCESS" /* QueryStateType.Success */,
625
- response: deepFreeze(transformedResponse),
626
- rawResponse: deepFreeze(responseData),
627
- meta: deepFreeze(Object.assign(Object.assign({}, meta), { expiresAt: response.expiresInTimestamp })),
604
+ response: transformedResponse,
605
+ rawResponse: responseData,
606
+ meta: Object.assign(Object.assign({}, meta), { expiresAt: response.expiresInTimestamp }),
628
607
  });
629
608
  })
630
609
  .catch((error) => this._handleExecuteError(error, meta));
@@ -633,6 +612,7 @@ class Query {
633
612
  abort() {
634
613
  this._abortController.abort();
635
614
  this._abortController = new AbortController();
615
+ this._onAbort$.next();
636
616
  return this;
637
617
  }
638
618
  poll(config) {
@@ -650,6 +630,28 @@ class Query {
650
630
  this._pollingSubscription = null;
651
631
  return this;
652
632
  }
633
+ _updateUseResultInDependencies() {
634
+ var _a, _b;
635
+ if (!((_b = (_a = this._args) === null || _a === void 0 ? void 0 : _a.useResultIn) === null || _b === void 0 ? void 0 : _b.length)) {
636
+ return;
637
+ }
638
+ this.state$
639
+ .pipe(filterSuccess(), takeUntil(this._onAbort$), takeUntilResponse(), tap((state) => {
640
+ var _a;
641
+ if (!((_a = this._args) === null || _a === void 0 ? void 0 : _a.useResultIn)) {
642
+ return;
643
+ }
644
+ for (const query of this._args.useResultIn) {
645
+ query._state$.next({
646
+ type: "SUCCESS" /* QueryStateType.Success */,
647
+ meta: Object.assign(Object.assign({}, state.meta), { id: query._nextId }),
648
+ rawResponse: state.rawResponse,
649
+ response: state.response,
650
+ });
651
+ }
652
+ }))
653
+ .subscribe();
654
+ }
653
655
  _handleExecuteError(error, meta) {
654
656
  if (isAbortRequestError(error)) {
655
657
  this._state$.next({
@@ -671,6 +673,19 @@ class Query {
671
673
  }
672
674
 
673
675
  class QueryDirective {
676
+ get query() {
677
+ return this._query;
678
+ }
679
+ set query(v) {
680
+ this._query = v;
681
+ this._subscribeToQuery();
682
+ }
683
+ get cache() {
684
+ return this._cache;
685
+ }
686
+ set cache(v) {
687
+ this._cache = v;
688
+ }
674
689
  constructor(_mainTemplateRef, _viewContainerRef, _errorHandler, _cdr) {
675
690
  this._mainTemplateRef = _mainTemplateRef;
676
691
  this._viewContainerRef = _viewContainerRef;
@@ -687,19 +702,6 @@ class QueryDirective {
687
702
  };
688
703
  this._cache = false;
689
704
  }
690
- get query() {
691
- return this._query;
692
- }
693
- set query(v) {
694
- this._query = v;
695
- this._subscribeToQuery();
696
- }
697
- get cache() {
698
- return this._cache;
699
- }
700
- set cache(v) {
701
- this._cache = v;
702
- }
703
705
  static ngTemplateContextGuard(dir, ctx) {
704
706
  return true;
705
707
  }
@@ -753,9 +755,9 @@ class QueryDirective {
753
755
  }
754
756
  }
755
757
  }
756
- QueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: QueryDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ErrorHandler }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
757
- QueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.0.2", type: QueryDirective, isStandalone: true, selector: "[query]", inputs: { query: "query", cache: ["queryCache", "cache"] }, ngImport: i0 });
758
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: QueryDirective, decorators: [{
758
+ QueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: QueryDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ErrorHandler }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
759
+ QueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.0", type: QueryDirective, isStandalone: true, selector: "[query]", inputs: { query: "query", cache: ["queryCache", "cache"] }, ngImport: i0 });
760
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: QueryDirective, decorators: [{
759
761
  type: Directive,
760
762
  args: [{
761
763
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -771,17 +773,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImpor
771
773
 
772
774
  /* eslint-disable @typescript-eslint/no-explicit-any */
773
775
  class InfinityQuery {
774
- constructor(_config) {
775
- var _a, _b;
776
- this._config = _config;
777
- this._currentPage$ = new BehaviorSubject(null);
778
- this._currentCalculatedPage$ = new BehaviorSubject(null);
779
- this._totalPages$ = new BehaviorSubject(null);
780
- this._itemsPerPage$ = new BehaviorSubject((_b = (_a = this._config.limitParam) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 10);
781
- this._currentQuery$ = new BehaviorSubject(null);
782
- this._data$ = new BehaviorSubject([]);
783
- this._subscriptions = [];
784
- }
785
776
  get currentPage$() {
786
777
  return this._currentPage$.asObservable();
787
778
  }
@@ -818,6 +809,17 @@ class InfinityQuery {
818
809
  get data() {
819
810
  return this._data$.getValue();
820
811
  }
812
+ constructor(_config) {
813
+ var _a, _b;
814
+ this._config = _config;
815
+ this._currentPage$ = new BehaviorSubject(null);
816
+ this._currentCalculatedPage$ = new BehaviorSubject(null);
817
+ this._totalPages$ = new BehaviorSubject(null);
818
+ this._itemsPerPage$ = new BehaviorSubject((_b = (_a = this._config.limitParam) === null || _a === void 0 ? void 0 : _a.value) !== null && _b !== void 0 ? _b : 10);
819
+ this._currentQuery$ = new BehaviorSubject(null);
820
+ this._data$ = new BehaviorSubject([]);
821
+ this._subscriptions = [];
822
+ }
821
823
  nextPage() {
822
824
  var _a, _b, _c, _d, _e;
823
825
  const newPage = ((_a = this._currentPage$.value) !== null && _a !== void 0 ? _a : 0) + 1;
@@ -934,7 +936,7 @@ class InfinityQueryDirective {
934
936
  itemsPerPage: null,
935
937
  };
936
938
  this._infinityQueryInstance = null;
937
- this._destroy$ = inject(DestroyDirective).destroy$;
939
+ this._destroy$ = inject(DestroyService).destroy$;
938
940
  this._cdr = inject(ChangeDetectorRef);
939
941
  this._viewContainerRef = inject(ViewContainerRef);
940
942
  this._mainTemplateRef = inject((TemplateRef));
@@ -1051,17 +1053,16 @@ class InfinityQueryDirective {
1051
1053
  this._data$.next([]);
1052
1054
  }
1053
1055
  }
1054
- InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1055
- InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.0.2", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }], exportAs: ["infinityQuery"], hostDirectives: [{ directive: i1.DestroyDirective }], ngImport: i0 });
1056
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1056
+ InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1057
+ InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.0", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], ngImport: i0 });
1058
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1057
1059
  type: Directive,
1058
1060
  args: [{
1059
1061
  // eslint-disable-next-line @angular-eslint/directive-selector
1060
1062
  selector: '[infinityQuery]',
1061
1063
  exportAs: 'infinityQuery',
1062
1064
  standalone: true,
1063
- providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }],
1064
- hostDirectives: [DestroyDirective],
1065
+ providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService],
1065
1066
  }]
1066
1067
  }], propDecorators: { infinityQuery: [{
1067
1068
  type: Input
@@ -1105,9 +1106,9 @@ class InfinityQueryTriggerDirective {
1105
1106
  this._observer.observe(this._elementRef.nativeElement);
1106
1107
  }
1107
1108
  }
1108
- InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1109
- InfinityQueryTriggerDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.0.2", type: InfinityQueryTriggerDirective, isStandalone: true, selector: "[infinityQueryTrigger], infinity-query-trigger", inputs: { scrollContainerSelector: "scrollContainerSelector" }, ngImport: i0 });
1110
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryTriggerDirective, decorators: [{
1109
+ InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1110
+ InfinityQueryTriggerDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.1.0", type: InfinityQueryTriggerDirective, isStandalone: true, selector: "[infinityQueryTrigger], infinity-query-trigger", inputs: { scrollContainerSelector: "scrollContainerSelector" }, ngImport: i0 });
1111
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: InfinityQueryTriggerDirective, decorators: [{
1111
1112
  type: Directive,
1112
1113
  args: [{
1113
1114
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1202,6 +1203,12 @@ config, args) => {
1202
1203
  };
1203
1204
 
1204
1205
  class QueryClient {
1206
+ get config() {
1207
+ return this._clientConfig;
1208
+ }
1209
+ get authProvider() {
1210
+ return this._authProvider;
1211
+ }
1205
1212
  constructor(_clientConfig) {
1206
1213
  var _a, _b;
1207
1214
  this._clientConfig = _clientConfig;
@@ -1257,23 +1264,17 @@ class QueryClient {
1257
1264
  enableGarbageCollectorLogging: (_b = _clientConfig.logging) === null || _b === void 0 ? void 0 : _b.queryStateGarbageCollector,
1258
1265
  });
1259
1266
  }
1260
- get config() {
1261
- return this._clientConfig;
1262
- }
1263
- get authProvider() {
1264
- return this._authProvider;
1265
- }
1266
1267
  }
1267
1268
 
1268
1269
  class QueryField {
1270
+ get control() {
1271
+ return this.data.control;
1272
+ }
1269
1273
  constructor(data) {
1270
1274
  this.data = data;
1271
1275
  this.changes$ = this._setupChangesObservable();
1272
1276
  this._currentDebounceTimeout = null;
1273
1277
  }
1274
- get control() {
1275
- return this.data.control;
1276
- }
1277
1278
  setValue(value) {
1278
1279
  if (this._currentDebounceTimeout !== null) {
1279
1280
  clearTimeout(this._currentDebounceTimeout);
@@ -1482,6 +1483,31 @@ const transformToSortQueryParam = (value) => {
1482
1483
  return null;
1483
1484
  };
1484
1485
 
1486
+ const deepFreeze = (object) => {
1487
+ // check if object is actually an object or frozen
1488
+ if (!object || typeof object !== 'object' || Object.isFrozen(object)) {
1489
+ return object;
1490
+ }
1491
+ // Retrieve the property names defined on object
1492
+ const propNames = Object.getOwnPropertyNames(object);
1493
+ // Freeze properties before freezing self
1494
+ for (const name of propNames) {
1495
+ const value = object[name];
1496
+ if (value && typeof value === 'object') {
1497
+ deepFreeze(value);
1498
+ }
1499
+ }
1500
+ return Object.freeze(object);
1501
+ };
1502
+
1503
+ /**
1504
+ * Dummy function for providing types while not breaking inference.
1505
+ * **This function always returns null.**
1506
+ * @see https://github.com/microsoft/TypeScript/issues/26242
1507
+ * @returns null
1508
+ */
1509
+ const def = () => null;
1510
+
1485
1511
  /**
1486
1512
  * Generated bundle index. Do not edit.
1487
1513
  */