@ethlete/query 2.6.0 → 2.7.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.
@@ -82,7 +82,7 @@ const buildQueryString = (params) => {
82
82
  return buildQueryArrayString(key, validParams[key]);
83
83
  }
84
84
  if (typeof validParams[key] === 'object' && validParams[key] !== null) {
85
- return buildQueryString(validParams[key]);
85
+ return buildQueryObjectString(key, validParams[key]);
86
86
  }
87
87
  return `${key}=${encodeURIComponent(validParams[key])}`;
88
88
  })
@@ -96,6 +96,22 @@ const buildQueryArrayString = (key, array) => {
96
96
  .map((item) => `${key}${uriBrackets}=${encodeURIComponent(item)}`)
97
97
  .join('&');
98
98
  };
99
+ const buildQueryObjectString = (key, object) => {
100
+ const uriBracketStart = encodeURIComponent(`${key}[`);
101
+ const uriBracketEnd = encodeURIComponent(`]`);
102
+ return Object.keys(object)
103
+ .map((k) => {
104
+ const key = `${uriBracketStart}${k}${uriBracketEnd}`;
105
+ if (Array.isArray(object[k])) {
106
+ return buildQueryArrayString(key, object[k]);
107
+ }
108
+ if (typeof object[k] === 'object' && object[k] !== null) {
109
+ return buildQueryObjectString(key, object[k]);
110
+ }
111
+ return `${key}=${encodeURIComponent(object[k])}`;
112
+ })
113
+ .join('&');
114
+ };
99
115
  const isFetchResponse = (response) => typeof response === 'object' &&
100
116
  response !== null &&
101
117
  'status' in response &&
@@ -105,10 +121,13 @@ const isFetchResponse = (response) => typeof response === 'object' &&
105
121
  const filterInvalidParams = (params) => {
106
122
  const filteredParams = Object.entries(params)
107
123
  .map(([key, value]) => {
108
- if (!Array.isArray(value)) {
109
- return [key, value];
124
+ if (Array.isArray(value)) {
125
+ return [key, value.filter((v) => isParamValid(v))];
126
+ }
127
+ else if (typeof value === 'object' && value !== null) {
128
+ return [key, filterInvalidParams(value)];
110
129
  }
111
- return [key, value.filter((v) => isParamValid(v))];
130
+ return [key, value];
112
131
  })
113
132
  .filter(([, value]) => isParamValid(value))
114
133
  .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
@@ -663,6 +682,29 @@ const computeQueryQueryParams = (config) => {
663
682
  }
664
683
  return config.args?.queryParams;
665
684
  };
685
+ const paginatedEntityValueUpdater = (findFn) => ({ response, rawResponse, entity }) => {
686
+ const indexOfEntityRaw = rawResponse.items.findIndex((item) => findFn(item, entity));
687
+ const indexOfEntity = response.items.findIndex((item) => findFn(item, entity));
688
+ if (indexOfEntity === -1 || indexOfEntityRaw === -1) {
689
+ return null;
690
+ }
691
+ const newRawResponse = {
692
+ ...rawResponse,
693
+ items: [
694
+ ...rawResponse.items.slice(0, indexOfEntityRaw),
695
+ entity,
696
+ ...rawResponse.items.slice(indexOfEntityRaw + 1),
697
+ ],
698
+ };
699
+ const newResponse = {
700
+ ...response,
701
+ items: [...response.items.slice(0, indexOfEntity), entity, ...response.items.slice(indexOfEntity + 1)],
702
+ };
703
+ return {
704
+ response: newResponse,
705
+ rawResponse: newRawResponse,
706
+ };
707
+ };
666
708
 
667
709
  class Query {
668
710
  get _nextId() {
@@ -739,46 +781,33 @@ class Query {
739
781
  if (!key) {
740
782
  return;
741
783
  }
742
- const currentValue = this._queryConfig.entity?.valueSelector({
743
- args: this._args,
744
- response,
745
- rawResponse,
746
- });
747
784
  const newData = this._queryConfig.entity?.store.getOne(event.key);
748
- if (Array.isArray(currentValue)) {
749
- const index = currentValue.findIndex((item) => item[key] === event.key);
750
- if (index === -1) {
751
- return;
752
- }
753
- const newValue = [...currentValue];
754
- if (newData) {
755
- newValue[index] = newData;
756
- }
757
- else {
758
- newValue.splice(index, 1);
759
- }
760
- this._state$.next({
761
- ...this.state,
762
- response: newValue,
763
- meta: {
764
- ...this.state.meta,
765
- triggeredVia: 'auto',
766
- },
767
- });
785
+ if (!newData) {
786
+ return;
768
787
  }
769
- else {
770
- if (currentValue?.[key] !== event.key) {
771
- return;
772
- }
773
- this._state$.next({
774
- ...this.state,
788
+ const newResponse = this._queryConfig.entity?.valueUpdater
789
+ ? this._queryConfig.entity?.valueUpdater({
790
+ args: this._args,
791
+ entity: newData,
792
+ rawResponse,
793
+ response,
794
+ })
795
+ : {
775
796
  response: newData,
776
- meta: {
777
- ...this.state.meta,
778
- triggeredVia: 'auto',
779
- },
780
- });
797
+ rawResponse: newData,
798
+ };
799
+ if (!newResponse) {
800
+ return;
781
801
  }
802
+ this._state$.next({
803
+ ...this.state,
804
+ response: newResponse.response,
805
+ rawResponse: newResponse.rawResponse,
806
+ meta: {
807
+ ...this.state.meta,
808
+ triggeredVia: 'auto',
809
+ },
810
+ });
782
811
  });
783
812
  }
784
813
  }
@@ -2259,5 +2288,5 @@ const def = () => null;
2259
2288
  * Generated bundle index. Do not edit.
2260
2289
  */
2261
2290
 
2262
- export { BasicAuthProvider, BearerAuthProvider, CustomHeaderAuthProvider, EntityStore, 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 };
2291
+ export { BasicAuthProvider, BearerAuthProvider, CustomHeaderAuthProvider, EntityStore, INFINITY_QUERY_TOKEN, InfinityQuery, InfinityQueryDirective, InfinityQueryTriggerDirective, Query, QueryClient, QueryDirective, QueryError, QueryField, QueryForm, QueryStore, buildErrorMessage, buildGqlCacheKey, buildQueryArrayString, buildQueryObjectString, 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, paginatedEntityValueUpdater, 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 };
2263
2292
  //# sourceMappingURL=ethlete-query.mjs.map