@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.
@@ -1,35 +1,34 @@
1
1
  import { Subject, timer, filter, takeWhile, switchMap, of, BehaviorSubject, interval, takeUntil, tap, fromEvent, startWith, concat, take, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
2
2
  import * as i0 from '@angular/core';
3
3
  import { Directive, Input, InjectionToken, inject, ChangeDetectorRef, ViewContainerRef, TemplateRef, ErrorHandler, ElementRef } from '@angular/core';
4
- import * as i1 from '@ethlete/core';
5
- import { DestroyDirective } from '@ethlete/core';
4
+ import { DestroyService } from '@ethlete/core';
6
5
  import { FormGroup } from '@angular/forms';
7
6
  import { Router, ActivatedRoute } from '@angular/router';
8
7
 
9
8
  class CustomHeaderAuthProvider {
9
+ get header() {
10
+ return { [this._config.name]: this._config.value };
11
+ }
10
12
  constructor(_config) {
11
13
  this._config = _config;
12
14
  this.queryClient = null;
13
15
  }
14
- get header() {
15
- return { [this._config.name]: this._config.value };
16
- }
17
16
  cleanUp() {
18
17
  // noop
19
18
  }
20
19
  }
21
20
 
22
21
  class BasicAuthProvider {
23
- constructor(_config) {
24
- this._config = _config;
25
- this.queryClient = null;
26
- }
27
22
  get header() {
28
23
  return { Authorization: `Basic ${this._basicAuthString}` };
29
24
  }
30
25
  get _basicAuthString() {
31
26
  return btoa(`${this._config.username}:${this._config.password}`);
32
27
  }
28
+ constructor(_config) {
29
+ this._config = _config;
30
+ this.queryClient = null;
31
+ }
33
32
  cleanUp() {
34
33
  // noop
35
34
  }
@@ -314,6 +313,9 @@ const decryptBearer = (token) => {
314
313
  };
315
314
 
316
315
  class BearerAuthProvider {
316
+ get header() {
317
+ return { Authorization: `Bearer ${this._token}` };
318
+ }
317
319
  constructor(_config) {
318
320
  this._config = _config;
319
321
  this._refreshToken = null;
@@ -328,9 +330,6 @@ class BearerAuthProvider {
328
330
  this._setupRefresh(this._config.refreshConfig);
329
331
  }
330
332
  }
331
- get header() {
332
- return { Authorization: `Bearer ${this._token}` };
333
- }
334
333
  cleanUp() {
335
334
  this._refreshTimerSubscription?.unsubscribe();
336
335
  this._refreshTimerSubscription = null;
@@ -343,10 +342,11 @@ class BearerAuthProvider {
343
342
  const expiresInPropertyName = config.expiresInPropertyName || 'exp';
344
343
  const expiresIn = bearer[expiresInPropertyName];
345
344
  const fiveMinutes = 5 * 60 * 1000;
345
+ const refreshBuffer = config.refreshBuffer ?? fiveMinutes;
346
346
  if (expiresIn === undefined) {
347
347
  throw new Error(`Bearer token does not contain an '${expiresInPropertyName}' property`);
348
348
  }
349
- const remainingTime = new Date(expiresIn * 1000).getTime() - fiveMinutes - new Date().getTime();
349
+ const remainingTime = new Date(expiresIn * 1000).getTime() - refreshBuffer - new Date().getTime();
350
350
  const strategy = config.strategy ?? "beforeExpiration" /* AuthBearerRefreshStrategy.BeforeExpiration */;
351
351
  switch (strategy) {
352
352
  case "beforeExpiration" /* AuthBearerRefreshStrategy.BeforeExpiration */:
@@ -361,13 +361,14 @@ class BearerAuthProvider {
361
361
  if (!this.queryClient) {
362
362
  throw new Error('Query client instance is null');
363
363
  }
364
- const { method, route, bodyAdapter, responseAdapter } = config;
364
+ const { method, route, requestAdapter, responseAdapter, paramLocation } = config;
365
+ const data = requestAdapter?.(this._refreshToken) ?? { refreshToken: this._refreshToken };
365
366
  const fullRoute = buildRoute({
366
367
  base: this.queryClient.config.baseRoute,
367
368
  route,
369
+ queryParams: paramLocation === 'query' ? data : undefined,
368
370
  });
369
- const body = bodyAdapter ? bodyAdapter(this._refreshToken) : { refreshToken: this._refreshToken };
370
- const requestInit = { body: buildBody(body), method };
371
+ const requestInit = { body: paramLocation === 'body' ? buildBody(data) : undefined, method };
371
372
  this.onRefreshInitiation$.next();
372
373
  try {
373
374
  const result = await request({
@@ -419,31 +420,6 @@ const gql = (strings, ...values) => {
419
420
  return str;
420
421
  };
421
422
 
422
- const deepFreeze = (object) => {
423
- // check if object is actually an object or frozen
424
- if (!object || typeof object !== 'object' || Object.isFrozen(object)) {
425
- return object;
426
- }
427
- // Retrieve the property names defined on object
428
- const propNames = Object.getOwnPropertyNames(object);
429
- // Freeze properties before freezing self
430
- for (const name of propNames) {
431
- const value = object[name];
432
- if (value && typeof value === 'object') {
433
- deepFreeze(value);
434
- }
435
- }
436
- return Object.freeze(object);
437
- };
438
-
439
- /**
440
- * Dummy function for providing types while not breaking inference.
441
- * **This function always returns null.**
442
- * @see https://github.com/microsoft/TypeScript/issues/26242
443
- * @returns null
444
- */
445
- const def = () => null;
446
-
447
423
  function filterSuccess() {
448
424
  return function (source) {
449
425
  return source.pipe(filter((value) => isQueryStateSuccess(value)));
@@ -501,21 +477,8 @@ const isGqlQueryConfig = (config) => {
501
477
  };
502
478
 
503
479
  class Query {
504
- constructor(_client, _queryConfig, _route, _args) {
505
- this._client = _client;
506
- this._queryConfig = _queryConfig;
507
- this._route = _route;
508
- this._args = _args;
509
- this._currentId = 0;
510
- this._abortController = new AbortController();
511
- this._pollingSubscription = null;
512
- this._state$ = new BehaviorSubject({
513
- type: "PREPARED" /* QueryStateType.Prepared */,
514
- meta: { id: this._currentId },
515
- });
516
- }
517
480
  get _nextId() {
518
- return this._currentId++;
481
+ return ++this._currentId;
519
482
  }
520
483
  get state$() {
521
484
  return this._state$.asObservable();
@@ -533,6 +496,20 @@ class Query {
533
496
  }
534
497
  return ts < Date.now();
535
498
  }
499
+ constructor(_client, _queryConfig, _route, _args) {
500
+ this._client = _client;
501
+ this._queryConfig = _queryConfig;
502
+ this._route = _route;
503
+ this._args = _args;
504
+ this._currentId = 0;
505
+ this._abortController = new AbortController();
506
+ this._pollingSubscription = null;
507
+ this._onAbort$ = new Subject();
508
+ this._state$ = new BehaviorSubject({
509
+ type: "PREPARED" /* QueryStateType.Prepared */,
510
+ meta: { id: this._currentId },
511
+ });
512
+ }
536
513
  clone() {
537
514
  return this._client.fetch(this._queryConfig);
538
515
  }
@@ -547,11 +524,12 @@ class Query {
547
524
  if (isQueryStateLoading(this._state$.value)) {
548
525
  this.abort();
549
526
  }
550
- const meta = deepFreeze({ id });
527
+ const meta = { id };
551
528
  this._state$.next({
552
529
  type: "LOADING" /* QueryStateType.Loading */,
553
530
  meta,
554
531
  });
532
+ this._updateUseResultInDependencies();
555
533
  let body = null;
556
534
  let contentType = null;
557
535
  if (isGqlQueryConfig(this._queryConfig)) {
@@ -616,9 +594,9 @@ class Query {
616
594
  : responseData;
617
595
  this._state$.next({
618
596
  type: "SUCCESS" /* QueryStateType.Success */,
619
- response: deepFreeze(transformedResponse),
620
- rawResponse: deepFreeze(responseData),
621
- meta: deepFreeze({ ...meta, expiresAt: response.expiresInTimestamp }),
597
+ response: transformedResponse,
598
+ rawResponse: responseData,
599
+ meta: { ...meta, expiresAt: response.expiresInTimestamp },
622
600
  });
623
601
  })
624
602
  .catch((error) => this._handleExecuteError(error, meta));
@@ -627,6 +605,7 @@ class Query {
627
605
  abort() {
628
606
  this._abortController.abort();
629
607
  this._abortController = new AbortController();
608
+ this._onAbort$.next();
630
609
  return this;
631
610
  }
632
611
  poll(config) {
@@ -643,6 +622,26 @@ class Query {
643
622
  this._pollingSubscription = null;
644
623
  return this;
645
624
  }
625
+ _updateUseResultInDependencies() {
626
+ if (!this._args?.useResultIn?.length) {
627
+ return;
628
+ }
629
+ this.state$
630
+ .pipe(filterSuccess(), takeUntil(this._onAbort$), takeUntilResponse(), tap((state) => {
631
+ if (!this._args?.useResultIn) {
632
+ return;
633
+ }
634
+ for (const query of this._args.useResultIn) {
635
+ query._state$.next({
636
+ type: "SUCCESS" /* QueryStateType.Success */,
637
+ meta: { ...state.meta, id: query._nextId },
638
+ rawResponse: state.rawResponse,
639
+ response: state.response,
640
+ });
641
+ }
642
+ }))
643
+ .subscribe();
644
+ }
646
645
  _handleExecuteError(error, meta) {
647
646
  if (isAbortRequestError(error)) {
648
647
  this._state$.next({
@@ -664,6 +663,19 @@ class Query {
664
663
  }
665
664
 
666
665
  class QueryDirective {
666
+ get query() {
667
+ return this._query;
668
+ }
669
+ set query(v) {
670
+ this._query = v;
671
+ this._subscribeToQuery();
672
+ }
673
+ get cache() {
674
+ return this._cache;
675
+ }
676
+ set cache(v) {
677
+ this._cache = v;
678
+ }
667
679
  constructor(_mainTemplateRef, _viewContainerRef, _errorHandler, _cdr) {
668
680
  this._mainTemplateRef = _mainTemplateRef;
669
681
  this._viewContainerRef = _viewContainerRef;
@@ -680,19 +692,6 @@ class QueryDirective {
680
692
  };
681
693
  this._cache = false;
682
694
  }
683
- get query() {
684
- return this._query;
685
- }
686
- set query(v) {
687
- this._query = v;
688
- this._subscribeToQuery();
689
- }
690
- get cache() {
691
- return this._cache;
692
- }
693
- set cache(v) {
694
- this._cache = v;
695
- }
696
695
  static ngTemplateContextGuard(dir, ctx) {
697
696
  return true;
698
697
  }
@@ -744,9 +743,9 @@ class QueryDirective {
744
743
  }
745
744
  }
746
745
  }
747
- 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 });
748
- 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 });
749
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: QueryDirective, decorators: [{
746
+ 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 });
747
+ 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 });
748
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: QueryDirective, decorators: [{
750
749
  type: Directive,
751
750
  args: [{
752
751
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -762,16 +761,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImpor
762
761
 
763
762
  /* eslint-disable @typescript-eslint/no-explicit-any */
764
763
  class InfinityQuery {
765
- constructor(_config) {
766
- this._config = _config;
767
- this._currentPage$ = new BehaviorSubject(null);
768
- this._currentCalculatedPage$ = new BehaviorSubject(null);
769
- this._totalPages$ = new BehaviorSubject(null);
770
- this._itemsPerPage$ = new BehaviorSubject(this._config.limitParam?.value ?? 10);
771
- this._currentQuery$ = new BehaviorSubject(null);
772
- this._data$ = new BehaviorSubject([]);
773
- this._subscriptions = [];
774
- }
775
764
  get currentPage$() {
776
765
  return this._currentPage$.asObservable();
777
766
  }
@@ -808,6 +797,16 @@ class InfinityQuery {
808
797
  get data() {
809
798
  return this._data$.getValue();
810
799
  }
800
+ constructor(_config) {
801
+ this._config = _config;
802
+ this._currentPage$ = new BehaviorSubject(null);
803
+ this._currentCalculatedPage$ = new BehaviorSubject(null);
804
+ this._totalPages$ = new BehaviorSubject(null);
805
+ this._itemsPerPage$ = new BehaviorSubject(this._config.limitParam?.value ?? 10);
806
+ this._currentQuery$ = new BehaviorSubject(null);
807
+ this._data$ = new BehaviorSubject([]);
808
+ this._subscriptions = [];
809
+ }
811
810
  nextPage() {
812
811
  const newPage = (this._currentPage$.value ?? 0) + 1;
813
812
  const calculatedPage = this._config?.pageParam?.valueCalculator?.({
@@ -940,7 +939,7 @@ class InfinityQueryDirective {
940
939
  itemsPerPage: null,
941
940
  };
942
941
  this._infinityQueryInstance = null;
943
- this._destroy$ = inject(DestroyDirective).destroy$;
942
+ this._destroy$ = inject(DestroyService).destroy$;
944
943
  this._cdr = inject(ChangeDetectorRef);
945
944
  this._viewContainerRef = inject(ViewContainerRef);
946
945
  this._mainTemplateRef = inject((TemplateRef));
@@ -1055,17 +1054,16 @@ class InfinityQueryDirective {
1055
1054
  this._data$.next([]);
1056
1055
  }
1057
1056
  }
1058
- InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1059
- 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 });
1060
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1057
+ InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1058
+ 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 });
1059
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.1.0", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1061
1060
  type: Directive,
1062
1061
  args: [{
1063
1062
  // eslint-disable-next-line @angular-eslint/directive-selector
1064
1063
  selector: '[infinityQuery]',
1065
1064
  exportAs: 'infinityQuery',
1066
1065
  standalone: true,
1067
- providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }],
1068
- hostDirectives: [DestroyDirective],
1066
+ providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService],
1069
1067
  }]
1070
1068
  }], propDecorators: { infinityQuery: [{
1071
1069
  type: Input
@@ -1108,9 +1106,9 @@ class InfinityQueryTriggerDirective {
1108
1106
  this._observer.observe(this._elementRef.nativeElement);
1109
1107
  }
1110
1108
  }
1111
- InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.0.2", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1112
- 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 });
1113
- 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: [{
1114
1112
  type: Directive,
1115
1113
  args: [{
1116
1114
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1202,6 +1200,12 @@ config, args) => {
1202
1200
  };
1203
1201
 
1204
1202
  class QueryClient {
1203
+ get config() {
1204
+ return this._clientConfig;
1205
+ }
1206
+ get authProvider() {
1207
+ return this._authProvider;
1208
+ }
1205
1209
  constructor(_clientConfig) {
1206
1210
  this._clientConfig = _clientConfig;
1207
1211
  this._authProvider = null;
@@ -1276,23 +1280,17 @@ class QueryClient {
1276
1280
  enableGarbageCollectorLogging: _clientConfig.logging?.queryStateGarbageCollector,
1277
1281
  });
1278
1282
  }
1279
- get config() {
1280
- return this._clientConfig;
1281
- }
1282
- get authProvider() {
1283
- return this._authProvider;
1284
- }
1285
1283
  }
1286
1284
 
1287
1285
  class QueryField {
1286
+ get control() {
1287
+ return this.data.control;
1288
+ }
1288
1289
  constructor(data) {
1289
1290
  this.data = data;
1290
1291
  this.changes$ = this._setupChangesObservable();
1291
1292
  this._currentDebounceTimeout = null;
1292
1293
  }
1293
- get control() {
1294
- return this.data.control;
1295
- }
1296
1294
  setValue(value) {
1297
1295
  if (this._currentDebounceTimeout !== null) {
1298
1296
  clearTimeout(this._currentDebounceTimeout);
@@ -1498,6 +1496,31 @@ const transformToSortQueryParam = (value) => {
1498
1496
  return null;
1499
1497
  };
1500
1498
 
1499
+ const deepFreeze = (object) => {
1500
+ // check if object is actually an object or frozen
1501
+ if (!object || typeof object !== 'object' || Object.isFrozen(object)) {
1502
+ return object;
1503
+ }
1504
+ // Retrieve the property names defined on object
1505
+ const propNames = Object.getOwnPropertyNames(object);
1506
+ // Freeze properties before freezing self
1507
+ for (const name of propNames) {
1508
+ const value = object[name];
1509
+ if (value && typeof value === 'object') {
1510
+ deepFreeze(value);
1511
+ }
1512
+ }
1513
+ return Object.freeze(object);
1514
+ };
1515
+
1516
+ /**
1517
+ * Dummy function for providing types while not breaking inference.
1518
+ * **This function always returns null.**
1519
+ * @see https://github.com/microsoft/TypeScript/issues/26242
1520
+ * @returns null
1521
+ */
1522
+ const def = () => null;
1523
+
1501
1524
  /**
1502
1525
  * Generated bundle index. Do not edit.
1503
1526
  */