@ethlete/query 2.3.0 → 2.5.0

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,5 +1,5 @@
1
1
  import { BehaviorSubjectWithSubscriberCount, getCookie, setCookie, deleteCookie, DestroyService, equal, clone } from '@ethlete/core';
2
- import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, interval, startWith, tap, timer, fromEvent, take, concat, debounceTime, combineLatest, map, distinctUntilChanged } from 'rxjs';
2
+ import { Observable, filter, takeWhile, switchMap, of, BehaviorSubject, Subject, takeUntil, interval, startWith, finalize, tap, timer, fromEvent, take, concat, 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
5
  import { FormGroup } from '@angular/forms';
@@ -32,29 +32,6 @@ class BasicAuthProvider {
32
32
  }
33
33
  }
34
34
 
35
- const getOpName = /(query|mutation) ?([\w\d-_]+)? ?\(.*?\)? \{/;
36
- const transformGql = (str) => {
37
- str = Array.isArray(str) ? str.join('') : str;
38
- const name = getOpName.exec(str);
39
- return function (variables) {
40
- const data = { query: str };
41
- if (variables)
42
- data['variables'] = JSON.stringify(variables);
43
- if (name && name.length) {
44
- const operationName = name[2];
45
- if (operationName)
46
- data['operationName'] = name[2];
47
- }
48
- return data;
49
- };
50
- };
51
- const gql = (strings, ...values) => {
52
- const str = strings.reduce((acc, cur, i) => {
53
- return acc + cur + (values[i] || '');
54
- }, '');
55
- return str;
56
- };
57
-
58
35
  const buildErrorMessage = (code, message) => `[@ethlete\\query:${code}] ${message}`;
59
36
  class QueryError extends Error {
60
37
  constructor(code, message, data) {
@@ -100,9 +77,15 @@ const buildRoute = (options) => {
100
77
  const buildQueryString = (params) => {
101
78
  const validParams = filterInvalidParams(params);
102
79
  const queryString = Object.keys(validParams)
103
- .map((key) => Array.isArray(validParams[key])
104
- ? buildQueryArrayString(key, validParams[key])
105
- : `${key}=${encodeURIComponent(validParams[key])}`)
80
+ .map((key) => {
81
+ if (Array.isArray(validParams[key])) {
82
+ return buildQueryArrayString(key, validParams[key]);
83
+ }
84
+ if (typeof validParams[key] === 'object' && validParams[key] !== null) {
85
+ return buildQueryString(validParams[key]);
86
+ }
87
+ return `${key}=${encodeURIComponent(validParams[key])}`;
88
+ })
106
89
  .join('&');
107
90
  return queryString || null;
108
91
  };
@@ -204,11 +187,14 @@ const serializeBody = (body) => {
204
187
  }
205
188
  return body.toString();
206
189
  };
207
- const transformMethod = (method) => {
208
- if (method === 'GQL_QUERY' || method === 'GQL_MUTATE') {
209
- return 'POST';
190
+ const transformMethod = (config) => {
191
+ if (config.method === 'GQL_QUERY' || config.method === 'GQL_MUTATE') {
192
+ if (!config.transferVia) {
193
+ return 'POST';
194
+ }
195
+ return config.transferVia;
210
196
  }
211
- return method;
197
+ return config.method;
212
198
  };
213
199
  const detectContentTypeHeader = (body) => {
214
200
  // An empty body has no content type.
@@ -489,6 +475,29 @@ const request = (config) => {
489
475
  });
490
476
  };
491
477
 
478
+ const getOpName = /(query|mutation) ?([\w\d-_]+)? ?\(.*?\)? \{/;
479
+ const transformGql = (str) => {
480
+ str = Array.isArray(str) ? str.join('') : str;
481
+ const name = getOpName.exec(str);
482
+ return function (variables) {
483
+ const data = { query: str };
484
+ if (variables)
485
+ data['variables'] = JSON.stringify(variables);
486
+ if (name && name.length) {
487
+ const operationName = name[2];
488
+ if (operationName)
489
+ data['operationName'] = name[2];
490
+ }
491
+ return data;
492
+ };
493
+ };
494
+ const gql = (strings, ...values) => {
495
+ const str = strings.reduce((acc, cur, i) => {
496
+ return acc + cur + (values[i] || '');
497
+ }, '');
498
+ return str;
499
+ };
500
+
492
501
  function filterSuccess() {
493
502
  return function (source) {
494
503
  return source.pipe(filter((value) => isQueryStateSuccess(value)));
@@ -589,6 +598,70 @@ const castQueryCreatorTypes = (config) => {
589
598
  }
590
599
  return config.creator;
591
600
  };
601
+ const computeQueryMethod = (config) => {
602
+ let method;
603
+ if (isGqlQueryConfig(config.config)) {
604
+ method = transformMethod({ method: config.config.method, transferVia: config.config.transferVia });
605
+ }
606
+ else {
607
+ method = transformMethod({ method: config.config.method });
608
+ }
609
+ return method;
610
+ };
611
+ const computeQueryBody = (config) => {
612
+ let body;
613
+ if (config.method === 'GET' ||
614
+ config.method === 'HEAD' ||
615
+ config.method === 'OPTIONS' ||
616
+ (isGqlQueryConfig(config.config) &&
617
+ (config.config.transferVia === 'GET' ||
618
+ (config.clientConfig.request?.gql?.transferVia === 'GET' && !config.config.transferVia)))) {
619
+ return undefined;
620
+ }
621
+ if (isGqlQueryConfig(config.config)) {
622
+ const queryTemplate = config.config.query;
623
+ const query = transformGql(queryTemplate);
624
+ body = query(config.args?.variables);
625
+ }
626
+ else {
627
+ body = config.args?.body;
628
+ }
629
+ return body;
630
+ };
631
+ const computeQueryAuthHeader = (config) => {
632
+ let authHeader = null;
633
+ if (config.config.secure) {
634
+ const header = config.client.authProvider?.header;
635
+ if (header) {
636
+ authHeader = header;
637
+ }
638
+ }
639
+ else if (config.client.authProvider?.header) {
640
+ if (config.config.secure === undefined || config.config.secure) {
641
+ authHeader = config.client.authProvider.header;
642
+ }
643
+ }
644
+ return authHeader;
645
+ };
646
+ const computeQueryHeaders = (config) => {
647
+ const authHeader = computeQueryAuthHeader(config);
648
+ const mergedHeaders = mergeHeaders(getDefaultHeaders(config.client.config.request?.headers, config.config.method), authHeader, config.args?.headers) || undefined;
649
+ return mergedHeaders;
650
+ };
651
+ const computeQueryQueryParams = (config) => {
652
+ if (isGqlQueryConfig(config.config) &&
653
+ (config.config.transferVia === 'GET' ||
654
+ (config.clientConfig.request?.gql?.transferVia === 'GET' && !config.config.transferVia))) {
655
+ const queryTemplate = config.config.query;
656
+ const query = transformGql(queryTemplate);
657
+ let params = query(config.args?.variables);
658
+ if (config.args?.queryParams) {
659
+ params = { ...params, ...config.args?.queryParams };
660
+ }
661
+ return params;
662
+ }
663
+ return config.args?.queryParams;
664
+ };
592
665
 
593
666
  class Query {
594
667
  get _nextId() {
@@ -669,33 +742,19 @@ class Query {
669
742
  meta,
670
743
  });
671
744
  this._updateUseResultInDependencies();
672
- let body;
673
- if (isGqlQueryConfig(this._queryConfig)) {
674
- const queryTemplate = this._queryConfig.query;
675
- const query = transformGql(queryTemplate);
676
- body = query(this._args?.variables);
677
- }
678
- else {
679
- body = this._args?.body;
680
- }
681
- let authHeader = null;
682
- if (this._queryConfig.secure) {
683
- const header = this._client.authProvider?.header;
684
- if (header) {
685
- authHeader = header;
686
- }
687
- }
688
- else if (this._client.authProvider?.header) {
689
- if (this._queryConfig.secure === undefined || this._queryConfig.secure) {
690
- authHeader = this._client.authProvider.header;
691
- }
692
- }
693
- const mergedHeaders = mergeHeaders(getDefaultHeaders(this._client.config.request?.headers, this._queryConfig.method), authHeader, this._args?.headers) || undefined;
745
+ const method = computeQueryMethod({ config: this._queryConfig });
746
+ const body = computeQueryBody({
747
+ config: this._queryConfig,
748
+ clientConfig: this._client.config,
749
+ args: this._args,
750
+ method,
751
+ });
752
+ const headers = computeQueryHeaders({ client: this._client, config: this._queryConfig, args: this._args });
694
753
  request({
695
754
  urlWithParams: this._routeWithParams,
696
- method: transformMethod(this._queryConfig.method),
755
+ method,
697
756
  body,
698
- headers: mergedHeaders,
757
+ headers,
699
758
  reportProgress: this._queryConfig.reportProgress,
700
759
  responseType: this._queryConfig.responseType,
701
760
  withCredentials: this._queryConfig.withCredentials,
@@ -786,7 +845,7 @@ class Query {
786
845
  ? interval(config.interval).pipe(startWith(-1))
787
846
  : interval(config.interval);
788
847
  this._pollingSubscription = _interval
789
- .pipe(takeUntil(config.takeUntil))
848
+ .pipe(takeUntil(config.takeUntil), finalize(() => this.stopPolling()))
790
849
  .subscribe(() => this.execute({ skipCache: true, _triggeredVia: 'poll' }));
791
850
  return this;
792
851
  }
@@ -1074,9 +1133,9 @@ class QueryDirective {
1074
1133
  }
1075
1134
  }
1076
1135
  }
1077
- QueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.1", ngImport: i0, type: QueryDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ErrorHandler }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
1078
- QueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.1", type: QueryDirective, isStandalone: true, selector: "[query]", inputs: { query: "query", cache: ["queryCache", "cache"] }, ngImport: i0 });
1079
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.1", ngImport: i0, type: QueryDirective, decorators: [{
1136
+ QueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.2", ngImport: i0, type: QueryDirective, deps: [{ token: i0.TemplateRef }, { token: i0.ViewContainerRef }, { token: i0.ErrorHandler }, { token: i0.ChangeDetectorRef }], target: i0.ɵɵFactoryTarget.Directive });
1137
+ QueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.2", type: QueryDirective, isStandalone: true, selector: "[query]", inputs: { query: "query", cache: ["queryCache", "cache"] }, ngImport: i0 });
1138
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.2", ngImport: i0, type: QueryDirective, decorators: [{
1080
1139
  type: Directive,
1081
1140
  args: [{
1082
1141
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1414,9 +1473,9 @@ class InfinityQueryDirective {
1414
1473
  this._data$.next([]);
1415
1474
  }
1416
1475
  }
1417
- InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.1", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1418
- InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.1", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], ngImport: i0 });
1419
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.1", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1476
+ InfinityQueryDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.2", ngImport: i0, type: InfinityQueryDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1477
+ InfinityQueryDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.2", type: InfinityQueryDirective, isStandalone: true, selector: "[infinityQuery]", inputs: { infinityQuery: "infinityQuery" }, providers: [{ provide: INFINITY_QUERY_TOKEN, useExisting: InfinityQueryDirective }, DestroyService], exportAs: ["infinityQuery"], ngImport: i0 });
1478
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.2", ngImport: i0, type: InfinityQueryDirective, decorators: [{
1420
1479
  type: Directive,
1421
1480
  args: [{
1422
1481
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1466,9 +1525,9 @@ class InfinityQueryTriggerDirective {
1466
1525
  this._observer.observe(this._elementRef.nativeElement);
1467
1526
  }
1468
1527
  }
1469
- InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.1", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1470
- InfinityQueryTriggerDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.1", type: InfinityQueryTriggerDirective, isStandalone: true, selector: "[infinityQueryTrigger], infinity-query-trigger", inputs: { scrollContainerSelector: "scrollContainerSelector" }, ngImport: i0 });
1471
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.1", ngImport: i0, type: InfinityQueryTriggerDirective, decorators: [{
1528
+ InfinityQueryTriggerDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.2", ngImport: i0, type: InfinityQueryTriggerDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive });
1529
+ InfinityQueryTriggerDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "15.2.2", type: InfinityQueryTriggerDirective, isStandalone: true, selector: "[infinityQueryTrigger], infinity-query-trigger", inputs: { scrollContainerSelector: "scrollContainerSelector" }, ngImport: i0 });
1530
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.2", ngImport: i0, type: InfinityQueryTriggerDirective, decorators: [{
1472
1531
  type: Directive,
1473
1532
  args: [{
1474
1533
  // eslint-disable-next-line @angular-eslint/directive-selector
@@ -1670,7 +1729,7 @@ class QueryClient {
1670
1729
  base: this._clientConfig.baseRoute,
1671
1730
  route: queryConfig.route,
1672
1731
  pathParams: args?.pathParams,
1673
- queryParams: args?.queryParams,
1732
+ queryParams: computeQueryQueryParams({ config: queryConfig, clientConfig: this._clientConfig, args }),
1674
1733
  });
1675
1734
  const cacheKey = isGqlQueryConfig(queryConfig) ? buildGqlCacheKey(queryConfig, args) : route;
1676
1735
  if (shouldCacheQuery(queryConfig.method)) {
@@ -1969,5 +2028,5 @@ const def = () => null;
1969
2028
  * Generated bundle index. Do not edit.
1970
2029
  */
1971
2030
 
1972
- export { BasicAuthProvider, BearerAuthProvider, CustomHeaderAuthProvider, INFINITY_QUERY_TOKEN, InfinityQuery, InfinityQueryDirective, InfinityQueryTriggerDirective, Query, QueryClient, QueryDirective, QueryError, QueryField, QueryForm, QueryStore, buildErrorMessage, buildGqlCacheKey, buildQueryArrayString, buildQueryString, buildRoute, buildTimestampFromSeconds, castQueryCreatorTypes, createInfinityQueryConfig, createQueryCollection, decryptBearer, deepFreeze, def, detectContentTypeHeader, extractExpiresInSeconds, extractQuery, filterFailure, filterInvalidParams, filterNull, filterQueryStates, filterSuccess, forEachHeader, getDefaultHeaders, getResponseUrl, gql, hasHeader, ignoreAutoRefresh, invalidBaseRouteError, invalidBodyError, invalidRouteError, isAutoRefresh, isBasicAuthProvider, isBearerAuthProvider, isCustomHeaderAuthProvider, isFetchResponse, isGqlQueryConfig, isParamValid, isQuery, isQueryStateCancelled, isQueryStateFailure, isQueryStateLoading, isQueryStatePrepared, isQueryStateSuccess, isRequestError, mergeHeaders, parseAllXhrResponseHeaders, pathParamsMissingInRouteFunctionError, queryStateAlreadyHasKeyError, queryStateCannotTransformError, queryStateDoesNotContainKeyError, request, serializeBody, shouldCacheQuery, shouldRetryRequest, skipPaginationPageParamCalculator, switchQueryCollectionState, switchQueryState, takeUntilResponse, transformGql, transformMethod, transformToBoolean, transformToBooleanArray, transformToDate, transformToDateArray, transformToNumber, transformToNumberArray, transformToSort, transformToSortQueryParam, transformToString, transformToStringArray };
2031
+ export { BasicAuthProvider, BearerAuthProvider, CustomHeaderAuthProvider, INFINITY_QUERY_TOKEN, InfinityQuery, InfinityQueryDirective, InfinityQueryTriggerDirective, Query, QueryClient, QueryDirective, QueryError, QueryField, QueryForm, QueryStore, buildErrorMessage, buildGqlCacheKey, buildQueryArrayString, buildQueryString, buildRoute, buildTimestampFromSeconds, castQueryCreatorTypes, computeQueryAuthHeader, computeQueryBody, computeQueryHeaders, computeQueryMethod, computeQueryQueryParams, createInfinityQueryConfig, createQueryCollection, decryptBearer, deepFreeze, def, detectContentTypeHeader, extractExpiresInSeconds, extractQuery, filterFailure, filterInvalidParams, filterNull, filterQueryStates, filterSuccess, forEachHeader, getDefaultHeaders, getResponseUrl, gql, hasHeader, ignoreAutoRefresh, invalidBaseRouteError, invalidBodyError, invalidRouteError, isAutoRefresh, isBasicAuthProvider, isBearerAuthProvider, isCustomHeaderAuthProvider, isFetchResponse, isGqlQueryConfig, isParamValid, isQuery, isQueryStateCancelled, isQueryStateFailure, isQueryStateLoading, isQueryStatePrepared, isQueryStateSuccess, isRequestError, mergeHeaders, parseAllXhrResponseHeaders, pathParamsMissingInRouteFunctionError, queryStateAlreadyHasKeyError, queryStateCannotTransformError, queryStateDoesNotContainKeyError, request, serializeBody, shouldCacheQuery, shouldRetryRequest, skipPaginationPageParamCalculator, switchQueryCollectionState, switchQueryState, takeUntilResponse, transformGql, transformMethod, transformToBoolean, transformToBooleanArray, transformToDate, transformToDateArray, transformToNumber, transformToNumberArray, transformToSort, transformToSortQueryParam, transformToString, transformToStringArray };
1973
2032
  //# sourceMappingURL=ethlete-query.mjs.map