@ethlete/query 5.16.0 → 5.16.2

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 (184) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/esm2022/ethlete-query.mjs +5 -0
  3. package/esm2022/index.mjs +20 -0
  4. package/esm2022/lib/auth/auth-provider.types.mjs +2 -0
  5. package/esm2022/lib/auth/auth-provider.utils.mjs +25 -0
  6. package/esm2022/lib/auth/basic-auth-provider.mjs +15 -0
  7. package/esm2022/lib/auth/bearer-auth-provider.mjs +168 -0
  8. package/esm2022/lib/auth/custom-header-auth-provider.mjs +12 -0
  9. package/esm2022/lib/auth/index.mjs +6 -0
  10. package/esm2022/lib/class-validator/class-validator.types.mjs +2 -0
  11. package/esm2022/lib/class-validator/class-validator.utils.mjs +4 -0
  12. package/esm2022/lib/class-validator/index.mjs +3 -0
  13. package/esm2022/lib/devtools/components/query-devtools/index.mjs +2 -0
  14. package/esm2022/lib/devtools/components/query-devtools/query-devtools.component.mjs +297 -0
  15. package/esm2022/lib/devtools/pipes/index.mjs +2 -0
  16. package/esm2022/lib/devtools/pipes/query-short-name.pipe.mjs +24 -0
  17. package/esm2022/lib/devtools/utils/index.mjs +2 -0
  18. package/esm2022/lib/devtools/utils/query-client.utils.mjs +13 -0
  19. package/esm2022/lib/directives/index.mjs +4 -0
  20. package/esm2022/lib/directives/infinity-query-trigger.directive.mjs +56 -0
  21. package/esm2022/lib/directives/infinity-query.directive.mjs +147 -0
  22. package/esm2022/lib/directives/query.directive.mjs +123 -0
  23. package/esm2022/lib/entity/entity-store.mjs +127 -0
  24. package/esm2022/lib/entity/entity.types.mjs +2 -0
  25. package/esm2022/lib/entity/entity.utils.mjs +29 -0
  26. package/esm2022/lib/entity/index.mjs +4 -0
  27. package/esm2022/lib/gql/gql.mjs +23 -0
  28. package/esm2022/lib/gql/index.mjs +2 -0
  29. package/esm2022/lib/infinite-query/index.mjs +4 -0
  30. package/esm2022/lib/infinite-query/infinity-query.mjs +171 -0
  31. package/esm2022/lib/infinite-query/infinity-query.types.mjs +2 -0
  32. package/esm2022/lib/infinite-query/infinity-query.utils.mjs +3 -0
  33. package/esm2022/lib/logger/index.mjs +2 -0
  34. package/esm2022/lib/logger/logger.mjs +16 -0
  35. package/esm2022/lib/query/index.mjs +4 -0
  36. package/esm2022/lib/query/query.mjs +430 -0
  37. package/esm2022/lib/query/query.types.mjs +2 -0
  38. package/esm2022/lib/query/query.utils.mjs +253 -0
  39. package/esm2022/lib/query-client/index.mjs +4 -0
  40. package/esm2022/lib/query-client/query-client.mjs +80 -0
  41. package/esm2022/lib/query-client/query-client.types.mjs +2 -0
  42. package/esm2022/lib/query-client/query-client.utils.mjs +20 -0
  43. package/esm2022/lib/query-creator/index.mjs +3 -0
  44. package/esm2022/lib/query-creator/query-creator.mjs +55 -0
  45. package/esm2022/lib/query-creator/query-creator.types.mjs +2 -0
  46. package/esm2022/lib/query-form/index.mjs +4 -0
  47. package/esm2022/lib/query-form/query-form.mjs +379 -0
  48. package/esm2022/lib/query-form/query-form.types.mjs +2 -0
  49. package/esm2022/lib/query-form/query-form.utils.mjs +97 -0
  50. package/esm2022/lib/query-store/index.mjs +2 -0
  51. package/esm2022/lib/query-store/query-store.mjs +144 -0
  52. package/esm2022/lib/request/index.mjs +4 -0
  53. package/esm2022/lib/request/request.mjs +196 -0
  54. package/esm2022/lib/request/request.types.mjs +2 -0
  55. package/esm2022/lib/request/request.util.mjs +252 -0
  56. package/esm2022/lib/symfony/index.mjs +3 -0
  57. package/esm2022/lib/symfony/symfony.types.mjs +2 -0
  58. package/esm2022/lib/symfony/symfony.utils.mjs +31 -0
  59. package/esm2022/lib/utils/data.utils.mjs +138 -0
  60. package/esm2022/lib/utils/deep-freeze.util.mjs +17 -0
  61. package/esm2022/lib/utils/def.util.mjs +8 -0
  62. package/esm2022/lib/utils/index.mjs +4 -0
  63. package/fesm2022/ethlete-query.mjs +3289 -0
  64. package/fesm2022/ethlete-query.mjs.map +1 -0
  65. package/{src/index.ts → index.d.ts} +0 -2
  66. package/lib/auth/auth-provider.types.d.ts +133 -0
  67. package/lib/auth/auth-provider.utils.d.ts +9 -0
  68. package/lib/auth/basic-auth-provider.d.ts +10 -0
  69. package/lib/auth/bearer-auth-provider.d.ts +23 -0
  70. package/lib/auth/custom-header-auth-provider.d.ts +9 -0
  71. package/{src/lib/class-validator/class-validator.types.ts → lib/class-validator/class-validator.types.d.ts} +3 -4
  72. package/lib/class-validator/class-validator.utils.d.ts +2 -0
  73. package/lib/devtools/components/query-devtools/query-devtools.component.d.ts +151 -0
  74. package/lib/devtools/pipes/query-short-name.pipe.d.ts +8 -0
  75. package/lib/devtools/utils/query-client.utils.d.ts +12 -0
  76. package/lib/directives/infinity-query-trigger.directive.d.ts +15 -0
  77. package/lib/directives/infinity-query.directive.d.ts +49 -0
  78. package/lib/directives/query.directive.d.ts +63 -0
  79. package/lib/entity/entity-store.d.ts +25 -0
  80. package/lib/entity/entity.types.d.ts +25 -0
  81. package/lib/entity/entity.utils.d.ts +12 -0
  82. package/lib/gql/gql.d.ts +6 -0
  83. package/lib/infinite-query/infinity-query.d.ts +29 -0
  84. package/lib/infinite-query/infinity-query.types.d.ts +104 -0
  85. package/lib/infinite-query/infinity-query.utils.d.ts +4 -0
  86. package/lib/logger/logger.d.ts +12 -0
  87. package/lib/query/query.d.ts +60 -0
  88. package/lib/query/query.types.d.ts +388 -0
  89. package/lib/query/query.utils.d.ts +82 -0
  90. package/lib/query-client/query-client.d.ts +29 -0
  91. package/lib/query-client/query-client.types.d.ts +82 -0
  92. package/lib/query-client/query-client.utils.d.ts +4 -0
  93. package/lib/query-creator/query-creator.d.ts +20 -0
  94. package/lib/query-creator/query-creator.types.d.ts +13 -0
  95. package/lib/query-form/query-form.d.ts +91 -0
  96. package/lib/query-form/query-form.types.d.ts +145 -0
  97. package/lib/query-form/query-form.utils.d.ts +13 -0
  98. package/lib/query-store/query-store.d.ts +31 -0
  99. package/lib/request/request.d.ts +3 -0
  100. package/lib/request/request.types.d.ts +126 -0
  101. package/lib/request/request.util.d.ts +92 -0
  102. package/lib/symfony/symfony.types.d.ts +19 -0
  103. package/lib/symfony/symfony.utils.d.ts +6 -0
  104. package/lib/utils/data.utils.d.ts +59 -0
  105. package/lib/utils/deep-freeze.util.d.ts +1 -0
  106. package/{src/lib/utils/def.util.ts → lib/utils/def.util.d.ts} +1 -1
  107. package/package.json +23 -6
  108. package/.eslintrc.json +0 -40
  109. package/jest.config.ts +0 -23
  110. package/ng-package.json +0 -8
  111. package/project.json +0 -37
  112. package/src/lib/auth/auth-provider.types.ts +0 -160
  113. package/src/lib/auth/auth-provider.utils.ts +0 -39
  114. package/src/lib/auth/basic-auth-provider.ts +0 -17
  115. package/src/lib/auth/bearer-auth-provider.ts +0 -243
  116. package/src/lib/auth/custom-header-auth-provider.ts +0 -13
  117. package/src/lib/class-validator/class-validator.utils.ts +0 -5
  118. package/src/lib/devtools/components/query-devtools/query-devtools.component.html +0 -402
  119. package/src/lib/devtools/components/query-devtools/query-devtools.component.scss +0 -360
  120. package/src/lib/devtools/components/query-devtools/query-devtools.component.ts +0 -427
  121. package/src/lib/devtools/pipes/query-short-name.pipe.ts +0 -24
  122. package/src/lib/devtools/utils/query-client.utils.ts +0 -25
  123. package/src/lib/directives/infinity-query-trigger.directive.ts +0 -58
  124. package/src/lib/directives/infinity-query.directive.ts +0 -208
  125. package/src/lib/directives/query.directive.ts +0 -201
  126. package/src/lib/entity/entity-store.ts +0 -180
  127. package/src/lib/entity/entity.types.ts +0 -34
  128. package/src/lib/entity/entity.utils.ts +0 -51
  129. package/src/lib/gql/gql.ts +0 -22
  130. package/src/lib/infinite-query/infinity-query.ts +0 -245
  131. package/src/lib/infinite-query/infinity-query.types.ts +0 -149
  132. package/src/lib/infinite-query/infinity-query.utils.ts +0 -14
  133. package/src/lib/logger/logger.ts +0 -30
  134. package/src/lib/query/query.ts +0 -631
  135. package/src/lib/query/query.types.ts +0 -532
  136. package/src/lib/query/query.utils.spec.ts +0 -92
  137. package/src/lib/query/query.utils.ts +0 -450
  138. package/src/lib/query-client/query-client.spec.ts +0 -77
  139. package/src/lib/query-client/query-client.ts +0 -195
  140. package/src/lib/query-client/query-client.types.ts +0 -101
  141. package/src/lib/query-client/query-client.utils.spec.ts +0 -61
  142. package/src/lib/query-client/query-client.utils.ts +0 -28
  143. package/src/lib/query-creator/query-creator.ts +0 -103
  144. package/src/lib/query-creator/query-creator.types.ts +0 -74
  145. package/src/lib/query-form/query-form.ts +0 -526
  146. package/src/lib/query-form/query-form.types.ts +0 -169
  147. package/src/lib/query-form/query-form.utils.ts +0 -116
  148. package/src/lib/query-store/query-store.ts +0 -192
  149. package/src/lib/request/request.ts +0 -253
  150. package/src/lib/request/request.types.ts +0 -164
  151. package/src/lib/request/request.util.spec.ts +0 -177
  152. package/src/lib/request/request.util.ts +0 -399
  153. package/src/lib/stories/angular-example-page.mdx +0 -85
  154. package/src/lib/stories/authentication-page.mdx +0 -35
  155. package/src/lib/stories/changelog-page.mdx +0 -9
  156. package/src/lib/stories/client-page.mdx +0 -100
  157. package/src/lib/stories/queries-page.mdx +0 -117
  158. package/src/lib/symfony/symfony.types.ts +0 -21
  159. package/src/lib/symfony/symfony.utils.ts +0 -43
  160. package/src/lib/utils/data.utils.ts +0 -280
  161. package/src/lib/utils/deep-freeze.util.ts +0 -21
  162. package/src/test-setup.ts +0 -1
  163. package/tsconfig.json +0 -29
  164. package/tsconfig.lib.json +0 -12
  165. package/tsconfig.lib.prod.json +0 -10
  166. package/tsconfig.spec.json +0 -11
  167. /package/{src/lib/auth/index.ts → lib/auth/index.d.ts} +0 -0
  168. /package/{src/lib/class-validator/index.ts → lib/class-validator/index.d.ts} +0 -0
  169. /package/{src/lib/devtools/components/query-devtools/index.ts → lib/devtools/components/query-devtools/index.d.ts} +0 -0
  170. /package/{src/lib/devtools/pipes/index.ts → lib/devtools/pipes/index.d.ts} +0 -0
  171. /package/{src/lib/devtools/utils/index.ts → lib/devtools/utils/index.d.ts} +0 -0
  172. /package/{src/lib/directives/index.ts → lib/directives/index.d.ts} +0 -0
  173. /package/{src/lib/entity/index.ts → lib/entity/index.d.ts} +0 -0
  174. /package/{src/lib/gql/index.ts → lib/gql/index.d.ts} +0 -0
  175. /package/{src/lib/infinite-query/index.ts → lib/infinite-query/index.d.ts} +0 -0
  176. /package/{src/lib/logger/index.ts → lib/logger/index.d.ts} +0 -0
  177. /package/{src/lib/query/index.ts → lib/query/index.d.ts} +0 -0
  178. /package/{src/lib/query-client/index.ts → lib/query-client/index.d.ts} +0 -0
  179. /package/{src/lib/query-creator/index.ts → lib/query-creator/index.d.ts} +0 -0
  180. /package/{src/lib/query-form/index.ts → lib/query-form/index.d.ts} +0 -0
  181. /package/{src/lib/query-store/index.ts → lib/query-store/index.d.ts} +0 -0
  182. /package/{src/lib/request/index.ts → lib/request/index.d.ts} +0 -0
  183. /package/{src/lib/symfony/index.ts → lib/symfony/index.d.ts} +0 -0
  184. /package/{src/lib/utils/index.ts → lib/utils/index.d.ts} +0 -0
@@ -1,195 +0,0 @@
1
- import { BehaviorSubject } from 'rxjs';
2
- import { AuthProvider } from '../auth';
3
- import { EntityStore } from '../entity';
4
- import {
5
- BaseArguments,
6
- GqlQueryConfig,
7
- GqlQueryConfigWithoutMethod,
8
- QueryConfigWithoutMethod,
9
- RestQueryConfig,
10
- RouteType,
11
- } from '../query';
12
- import { QueryCreator } from '../query-creator';
13
- import { QueryStore } from '../query-store';
14
- import { RequestHeaders, RequestHeadersMethodMap } from '../request';
15
- import { QueryClientConfig } from './query-client.types';
16
-
17
- export class QueryClient {
18
- /**
19
- * @internal
20
- */
21
- readonly _store: QueryStore;
22
-
23
- get config() {
24
- return this._clientConfig;
25
- }
26
-
27
- get authProvider() {
28
- return this._authProvider$.getValue() ?? this._clientConfig.parent?._authProvider$.getValue() ?? null;
29
- }
30
- get authProvider$() {
31
- return this._authProvider$.asObservable() ?? this._clientConfig.parent?._authProvider$.asObservable() ?? null;
32
- }
33
- private readonly _authProvider$ = new BehaviorSubject<AuthProvider | null>(null);
34
-
35
- constructor(private _clientConfig: QueryClientConfig) {
36
- this._store = new QueryStore({
37
- enableChangeLogging: _clientConfig.logging?.queryStateChanges,
38
- enableGarbageCollectorLogging: _clientConfig.logging?.queryStateGarbageCollector,
39
- autoRefreshQueriesOnWindowFocus: _clientConfig.request?.autoRefreshQueriesOnWindowFocus ?? true,
40
- enableSmartPolling: _clientConfig.request?.enableSmartPolling ?? true,
41
- });
42
- }
43
-
44
- get = <
45
- Route extends RouteType<Arguments>,
46
- Response,
47
- Id,
48
- Data = Response,
49
- Arguments extends BaseArguments | undefined = undefined,
50
- Store extends EntityStore<unknown> = EntityStore<unknown>,
51
- >(
52
- queryConfig: QueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
53
- ) =>
54
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
55
- ...queryConfig,
56
- method: 'GET',
57
- });
58
-
59
- post = <
60
- Route extends RouteType<Arguments>,
61
- Response,
62
- Id,
63
- Data = Response,
64
- Arguments extends BaseArguments | undefined = undefined,
65
- Store extends EntityStore<unknown> = EntityStore<unknown>,
66
- >(
67
- queryConfig: QueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
68
- ) =>
69
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
70
- ...queryConfig,
71
- method: 'POST',
72
- });
73
-
74
- put = <
75
- Route extends RouteType<Arguments>,
76
- Response,
77
- Id,
78
- Data = Response,
79
- Arguments extends BaseArguments | undefined = undefined,
80
- Store extends EntityStore<unknown> = EntityStore<unknown>,
81
- >(
82
- queryConfig: QueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
83
- ) =>
84
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
85
- ...queryConfig,
86
- method: 'PUT',
87
- });
88
-
89
- patch = <
90
- Route extends RouteType<Arguments>,
91
- Response,
92
- Id,
93
- Data = Response,
94
- Arguments extends BaseArguments | undefined = undefined,
95
- Store extends EntityStore<unknown> = EntityStore<unknown>,
96
- >(
97
- queryConfig: QueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
98
- ) =>
99
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
100
- ...queryConfig,
101
- method: 'PATCH',
102
- });
103
-
104
- delete = <
105
- Route extends RouteType<Arguments>,
106
- Response,
107
- Id,
108
- Data = Response,
109
- Arguments extends BaseArguments | undefined = undefined,
110
- Store extends EntityStore<unknown> = EntityStore<unknown>,
111
- >(
112
- queryConfig: QueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
113
- ) =>
114
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
115
- ...queryConfig,
116
- method: 'DELETE',
117
- });
118
-
119
- gqlQuery = <
120
- Route extends RouteType<Arguments>,
121
- Response,
122
- Id,
123
- Data = Response,
124
- Arguments extends BaseArguments | undefined = undefined,
125
- Store extends EntityStore<unknown> = EntityStore<unknown>,
126
- >(
127
- queryConfig: GqlQueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
128
- ) =>
129
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
130
- ...queryConfig,
131
- method: 'GQL_QUERY',
132
- });
133
-
134
- gqlMutate = <
135
- Route extends RouteType<Arguments>,
136
- Response,
137
- Id,
138
- Data = Response,
139
- Arguments extends BaseArguments | undefined = undefined,
140
- Store extends EntityStore<unknown> = EntityStore<unknown>,
141
- >(
142
- queryConfig: GqlQueryConfigWithoutMethod<Route, Response, Arguments, Store, Data, Id>,
143
- ) =>
144
- this.fetch<Route, Response, Arguments, Id, Store, Data>({
145
- ...queryConfig,
146
- method: 'GQL_MUTATE',
147
- });
148
-
149
- fetch = <
150
- Route extends RouteType<Arguments>,
151
- Response,
152
- Arguments extends BaseArguments | undefined,
153
- Id,
154
- Store extends EntityStore<unknown> = EntityStore<unknown>,
155
- Data = Response,
156
- >(
157
- queryConfig:
158
- | RestQueryConfig<Route, Response, Arguments, Store, Data, Id>
159
- | GqlQueryConfig<Route, Response, Arguments, Store, Data, Id>,
160
- ) => new QueryCreator<Arguments, Response, Route, Store, Data, Id>(queryConfig, this, this._store);
161
-
162
- setAuthProvider = (authProvider: AuthProvider) => {
163
- if (this.authProvider) {
164
- this.authProvider?.cleanUp();
165
- }
166
-
167
- this._authProvider$.next(authProvider);
168
- };
169
-
170
- setDefaultHeaders = (config: {
171
- headers: RequestHeaders | RequestHeadersMethodMap | null;
172
- refreshQueriesInUse?: boolean;
173
- }) => {
174
- if (!this._clientConfig.request) {
175
- this._clientConfig.request = {
176
- headers: config.headers ?? undefined,
177
- };
178
- } else {
179
- this._clientConfig.request.headers = config.headers ?? undefined;
180
- }
181
-
182
- if (config.refreshQueriesInUse) {
183
- this._store.refreshQueriesInUse({ purgeUnused: true, ignoreCacheValidity: true });
184
- }
185
- };
186
-
187
- clearAuthProvider = () => {
188
- this.authProvider?.cleanUp();
189
- this._authProvider$.next(null);
190
- };
191
-
192
- _updateBaseRoute = (route: string) => {
193
- this._clientConfig.baseRoute = route;
194
- };
195
- }
@@ -1,101 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { GqlTransferOption } from '../query/query.types';
3
- import {
4
- BuildQueryStringConfig,
5
- CacheAdapterFn,
6
- RequestHeaders,
7
- RequestHeadersMethodMap,
8
- RequestRetryFn,
9
- } from '../request';
10
- import { QueryClient } from './query-client';
11
-
12
- export interface QueryClientConfig {
13
- /**
14
- * The api base route. Should **not** end with a trailing slash.
15
- * @example 'https://api.example.com'
16
- */
17
- baseRoute: string;
18
-
19
- /**
20
- * Logging configuration for debugging.
21
- */
22
- logging?: {
23
- /**
24
- * Log all query state changes.
25
- */
26
- queryStateChanges?: boolean;
27
-
28
- /**
29
- * Log query state garbage collector runs.
30
- */
31
- queryStateGarbageCollector?: boolean;
32
-
33
- /**
34
- * Log if subscriptions are made to queries that are in the `prepared` state.
35
- * This usually indicates that the `.execute()` method was forgotten.
36
- */
37
- preparedQuerySubscriptions?: boolean;
38
- };
39
-
40
- /**
41
- * Request options.
42
- */
43
- request?: {
44
- /**
45
- * Adapter function used for extracting the time until the response is invalid.
46
- * The default uses the `cache-control` (`max-age` & `s-maxage`), `age` and `expires` headers.
47
- * Should return a number in seconds.
48
- */
49
- cacheAdapter?: CacheAdapterFn;
50
-
51
- /**
52
- * Default headers to be sent with every request.
53
- * Will be overridden by headers passed to the query.
54
- * Do not include the `Authorization` header here. Use an `AuthProvider` instead.
55
- */
56
- headers?: RequestHeaders | RequestHeadersMethodMap;
57
-
58
- /**
59
- * Whether to automatically refresh expired queries when the window regains focus.
60
- * @default true
61
- */
62
- autoRefreshQueriesOnWindowFocus?: boolean;
63
-
64
- /**
65
- * Whether to automatically stop polling queries when the window loses focus.
66
- * Polling will resume when the window regains focus.
67
- * @default true
68
- */
69
- enableSmartPolling?: boolean;
70
-
71
- /**
72
- * A retry function to be used for all queries.
73
- * @default shouldRetryRequest()
74
- */
75
- retryFn?: RequestRetryFn;
76
-
77
- /**
78
- * Configuration options for all graphql queries.
79
- */
80
- gql?: {
81
- /**
82
- * Determines if the query should be sent via GET or POST.
83
- * - `GET`: The query will be sent via query parameters.
84
- * - `POST`: The query will be sent via the body.
85
- * @default 'POST'
86
- */
87
- transferVia?: GqlTransferOption;
88
- };
89
-
90
- /**
91
- * Configuration options for all query strings.
92
- */
93
- queryParams?: BuildQueryStringConfig;
94
- };
95
-
96
- /**
97
- * Parent query client.
98
- * Used for sharing the same auth provider.
99
- */
100
- parent?: QueryClient;
101
- }
@@ -1,61 +0,0 @@
1
- import { BaseArguments } from '../query/query.types';
2
- import { buildQueryCacheKey, shouldCacheQuery } from './query-client.utils';
3
-
4
- describe('buildQueryCacheKey', () => {
5
- it('should return a string with a shortened query and variables', () => {
6
- const route = '/posts';
7
- const args: BaseArguments = {
8
- variables: {
9
- id: 123,
10
- },
11
- };
12
-
13
- const expectedCacheKey = '1769813287';
14
-
15
- const cacheKey = buildQueryCacheKey(route, args);
16
-
17
- expect(cacheKey).toBe(expectedCacheKey);
18
- });
19
- });
20
-
21
- describe('shouldCacheQuery', () => {
22
- it('should return true for GET requests', () => {
23
- const method = 'GET';
24
-
25
- const shouldCache = shouldCacheQuery(method);
26
-
27
- expect(shouldCache).toBe(true);
28
- });
29
-
30
- it('should return true for OPTIONS requests', () => {
31
- const method = 'OPTIONS';
32
-
33
- const shouldCache = shouldCacheQuery(method);
34
-
35
- expect(shouldCache).toBe(true);
36
- });
37
-
38
- it('should return true for HEAD requests', () => {
39
- const method = 'HEAD';
40
-
41
- const shouldCache = shouldCacheQuery(method);
42
-
43
- expect(shouldCache).toBe(true);
44
- });
45
-
46
- it('should return true for GQL_QUERY requests', () => {
47
- const method = 'GQL_QUERY';
48
-
49
- const shouldCache = shouldCacheQuery(method);
50
-
51
- expect(shouldCache).toBe(true);
52
- });
53
-
54
- it('should return false for other HTTP methods', () => {
55
- const method = 'POST';
56
-
57
- const shouldCache = shouldCacheQuery(method);
58
-
59
- expect(shouldCache).toBe(false);
60
- });
61
- });
@@ -1,28 +0,0 @@
1
- import { BaseArguments } from '../query/query.types';
2
- import { Method } from '../request';
3
-
4
- export const shouldCacheQuery = (method: Method) => {
5
- return method === 'GET' || method === 'OPTIONS' || method === 'HEAD' || method === 'GQL_QUERY';
6
- };
7
-
8
- export const buildQueryCacheKey = (route: string, args: BaseArguments | undefined) => {
9
- const variables = JSON.stringify(args?.variables || {})
10
- // replace all curly braces with empty string
11
- .replace(/{|}/g, '')
12
- // replace new lines and whitespaces with empty string
13
- .replace(/\s/g, '');
14
-
15
- const seed = `${route}...${variables}`;
16
-
17
- let hash = 0;
18
-
19
- for (const char of seed) {
20
- hash = (Math.imul(31, hash) + char.charCodeAt(0)) << 0;
21
- }
22
-
23
- // Force positive number hash.
24
- // 2147483647 = equivalent of Integer.MAX_VALUE.
25
- hash += 2147483647 + 1;
26
-
27
- return hash.toString();
28
- };
@@ -1,103 +0,0 @@
1
- import { assertInInjectionContext, signal } from '@angular/core';
2
- import { toObservable } from '@angular/core/rxjs-interop';
3
- import { BehaviorSubject } from 'rxjs';
4
- import { EntityStore } from '../entity';
5
- import { Query, computeQueryQueryParams } from '../query';
6
- import { QueryClient, buildQueryCacheKey, shouldCacheQuery } from '../query-client';
7
- import { QueryStore } from '../query-store';
8
- import {
9
- AnyRoute,
10
- BaseArguments,
11
- GqlQueryConfig,
12
- RestQueryConfig,
13
- RouteType,
14
- WithConfig,
15
- WithHeaders,
16
- WithMock,
17
- } from '../query/query.types';
18
- import { buildRoute } from '../request';
19
- import { QueryContainerConfig, addQueryContainerHandling } from '../utils';
20
- import { QueryPrepareFn } from './query-creator.types';
21
-
22
- export class QueryCreator<
23
- Arguments extends BaseArguments | undefined,
24
- Response,
25
- Route extends RouteType<Arguments>,
26
- Store extends EntityStore<unknown>,
27
- Data,
28
- Id,
29
- > {
30
- constructor(
31
- private _queryConfig:
32
- | RestQueryConfig<Route, Response, Arguments, Store, Data, Id>
33
- | GqlQueryConfig<Route, Response, Arguments, Store, Data, Id>,
34
- private _client: QueryClient,
35
- private _store: QueryStore,
36
- ) {}
37
-
38
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
39
- // @ts-ignore
40
- prepare: QueryPrepareFn<Arguments, Response, Route, Store, Data, Id> = (
41
- args?: Arguments & WithHeaders & WithConfig & WithMock<Response>,
42
- ) => {
43
- const route = buildRoute({
44
- base: this._client.config.baseRoute,
45
- route: this._queryConfig.route as AnyRoute,
46
- pathParams: args?.pathParams,
47
- queryParams: computeQueryQueryParams({ config: this._queryConfig, client: this._client, args }),
48
- queryParamConfig: this._client.config.request?.queryParams,
49
- }) as Route;
50
-
51
- const cacheKey = (args?.config?.queryStoreCacheKey ?? '') + buildQueryCacheKey(route as string, args);
52
-
53
- if (shouldCacheQuery(this._queryConfig.method) && !args?.config?.skipQueryStore) {
54
- const existingQuery = this._store.get<Query<Response, Arguments, Route, Store, Data, Id>>(cacheKey);
55
-
56
- if (existingQuery) {
57
- return existingQuery;
58
- }
59
- }
60
-
61
- const query = new Query<Response, Arguments, Route, Store, Data, Id>(
62
- this._client,
63
- this._queryConfig,
64
- route,
65
- args ?? ({} as Arguments),
66
- cacheKey,
67
- );
68
-
69
- if (shouldCacheQuery(this._queryConfig.method) && !args?.config?.skipQueryStore) {
70
- this._store.add(cacheKey, query);
71
- }
72
-
73
- this._store._dispatchQueryCreated(query);
74
-
75
- return query;
76
- };
77
-
78
- createSubject = (initialValue?: ReturnType<typeof this.prepare> | null, config?: QueryContainerConfig) => {
79
- assertInInjectionContext(this.createSubject);
80
-
81
- const subject = new BehaviorSubject<ReturnType<typeof this.prepare> | null>(initialValue ?? null);
82
-
83
- addQueryContainerHandling(subject, () => subject.getValue(), config);
84
-
85
- return subject;
86
- };
87
-
88
- createSignal = (initialValue?: ReturnType<typeof this.prepare> | null, config?: QueryContainerConfig) => {
89
- assertInInjectionContext(this.createSignal);
90
-
91
- const _signal = signal<ReturnType<typeof this.prepare> | null>(initialValue ?? null);
92
-
93
- addQueryContainerHandling(toObservable(_signal), () => _signal(), config);
94
-
95
- return _signal;
96
- };
97
-
98
- /**
99
- * @deprecated Use `myQuery.createSubject()` or `myQuery.createSignal()` instead. Will be removed in v6.
100
- */
101
- behaviorSubject = (initialValue?: ReturnType<typeof this.prepare> | null) =>
102
- new BehaviorSubject<ReturnType<typeof this.prepare> | null>(initialValue ?? null);
103
- }
@@ -1,74 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { EntityStore } from '../entity';
3
- import { Query } from '../query/query';
4
- import {
5
- AnyQuery,
6
- BaseArguments,
7
- EmptyObject,
8
- RouteType,
9
- WithConfig,
10
- WithHeaders,
11
- WithMock,
12
- } from '../query/query.types';
13
- import { QueryCreator } from './query-creator';
14
-
15
- export type QueryPrepareFn<
16
- Arguments extends BaseArguments | undefined,
17
- Response,
18
- Route extends RouteType<Arguments>,
19
- Store extends EntityStore<unknown>,
20
- Data,
21
- Id,
22
- > = Arguments extends BaseArguments
23
- ? (
24
- args: Arguments & WithHeaders & WithConfig & WithMock<Response>,
25
- ) => Query<Response, Arguments, Route, Store, Data, Id>
26
- : (
27
- args?: (Arguments extends EmptyObject ? Arguments : EmptyObject) & WithHeaders & WithConfig & WithMock<Response>,
28
- ) => Query<Response, Arguments, Route, Store, Data, Id>;
29
-
30
- export type AnyQueryCreator = QueryCreator<any, any, any, any, any, any>;
31
-
32
- export type QueryArgsOf<T extends AnyQueryCreator | AnyQuery | null> =
33
- T extends QueryCreator<infer Args, any, any, any, any, any>
34
- ? Args
35
- : T extends Query<any, infer Args, any, any, any, any>
36
- ? Args
37
- : never;
38
-
39
- export type QueryResponseOf<T extends AnyQueryCreator | AnyQuery | null> =
40
- T extends QueryCreator<any, infer Response, any, any, any, any>
41
- ? Response
42
- : T extends Query<infer Response, any, any, any, any, any>
43
- ? Response
44
- : never;
45
-
46
- export type QueryRouteOf<T extends AnyQueryCreator | AnyQuery | null> =
47
- T extends QueryCreator<any, any, infer Route, any, any, any>
48
- ? Route
49
- : T extends Query<any, any, infer Route, any, any, any>
50
- ? Route
51
- : never;
52
-
53
- export type QueryStoreOf<T extends AnyQueryCreator | AnyQuery | null> =
54
- T extends QueryCreator<any, any, any, infer Store, any, any>
55
- ? Store
56
- : T extends Query<any, any, any, infer Store, any, any>
57
- ? Store
58
- : never;
59
-
60
- export type QueryDataOf<T extends AnyQueryCreator | AnyQuery | null> =
61
- T extends QueryCreator<any, any, any, any, infer Data, any>
62
- ? Data
63
- : T extends Query<any, any, any, any, infer Data, any>
64
- ? Data
65
- : never;
66
-
67
- export type QueryStoreIdOf<T extends AnyQueryCreator | AnyQuery | null> =
68
- T extends QueryCreator<any, any, any, any, any, infer StoreId>
69
- ? StoreId
70
- : T extends Query<any, any, any, any, any, infer StoreId>
71
- ? StoreId
72
- : never;
73
-
74
- export type ConstructQuery<T extends AnyQueryCreator> = ReturnType<T['prepare']>;