mobx-tanstack-query-api 0.25.0 → 0.26.1

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 (269) hide show
  1. package/bin.cjs +53 -0
  2. package/bin.cjs.map +1 -0
  3. package/bin.d.ts +1 -0
  4. package/bin.js +51 -0
  5. package/bin.js.map +1 -0
  6. package/builtin.cjs +27 -0
  7. package/builtin.cjs.map +1 -0
  8. package/builtin.d.ts +6 -0
  9. package/builtin.js +27 -0
  10. package/builtin.js.map +1 -0
  11. package/cli.cjs +1278 -0
  12. package/cli.cjs.map +1 -0
  13. package/{codegen/types/generate-query-api-params.d.cts → cli.d.ts} +54 -10
  14. package/cli.js +1277 -0
  15. package/cli.js.map +1 -0
  16. package/index.cjs +771 -16
  17. package/index.cjs.map +1 -0
  18. package/index.d.ts +322 -2
  19. package/index.js +768 -2
  20. package/index.js.map +1 -0
  21. package/package.json +25 -62
  22. package/builtin/index.cjs +0 -25
  23. package/builtin/index.d.cts +0 -4
  24. package/builtin/index.d.cts.map +0 -1
  25. package/builtin/index.d.ts +0 -4
  26. package/builtin/index.d.ts.map +0 -1
  27. package/builtin/index.js +0 -22
  28. package/cli/bin.cjs +0 -17
  29. package/cli/bin.d.cts +0 -2
  30. package/cli/bin.d.cts.map +0 -1
  31. package/cli/bin.d.ts +0 -2
  32. package/cli/bin.d.ts.map +0 -1
  33. package/cli/bin.js +0 -15
  34. package/cli/index.cjs +0 -18
  35. package/cli/index.d.cts +0 -3
  36. package/cli/index.d.cts.map +0 -1
  37. package/cli/index.d.ts +0 -3
  38. package/cli/index.d.ts.map +0 -1
  39. package/cli/index.js +0 -2
  40. package/cli/utils/define-config.cjs +0 -9
  41. package/cli/utils/define-config.d.cts +0 -4
  42. package/cli/utils/define-config.d.cts.map +0 -1
  43. package/cli/utils/define-config.d.ts +0 -4
  44. package/cli/utils/define-config.d.ts.map +0 -1
  45. package/cli/utils/define-config.js +0 -5
  46. package/cli/utils/exec-config-path.cjs +0 -16
  47. package/cli/utils/exec-config-path.d.cts +0 -2
  48. package/cli/utils/exec-config-path.d.cts.map +0 -1
  49. package/cli/utils/exec-config-path.d.ts +0 -2
  50. package/cli/utils/exec-config-path.d.ts.map +0 -1
  51. package/cli/utils/exec-config-path.js +0 -12
  52. package/cli/utils/resolve-config-path.cjs +0 -65
  53. package/cli/utils/resolve-config-path.d.cts +0 -3
  54. package/cli/utils/resolve-config-path.d.cts.map +0 -1
  55. package/cli/utils/resolve-config-path.d.ts +0 -3
  56. package/cli/utils/resolve-config-path.d.ts.map +0 -1
  57. package/cli/utils/resolve-config-path.js +0 -25
  58. package/codegen/index.cjs +0 -545
  59. package/codegen/index.d.cts +0 -4
  60. package/codegen/index.d.cts.map +0 -1
  61. package/codegen/index.d.ts +0 -4
  62. package/codegen/index.d.ts.map +0 -1
  63. package/codegen/index.js +0 -524
  64. package/codegen/templates/all-endpoints-per-file.tmpl.cjs +0 -94
  65. package/codegen/templates/all-endpoints-per-file.tmpl.d.cts +0 -14
  66. package/codegen/templates/all-endpoints-per-file.tmpl.d.cts.map +0 -1
  67. package/codegen/templates/all-endpoints-per-file.tmpl.d.ts +0 -14
  68. package/codegen/templates/all-endpoints-per-file.tmpl.d.ts.map +0 -1
  69. package/codegen/templates/all-endpoints-per-file.tmpl.js +0 -90
  70. package/codegen/templates/all-exports.tmpl.cjs +0 -14
  71. package/codegen/templates/all-exports.tmpl.d.cts +0 -9
  72. package/codegen/templates/all-exports.tmpl.d.cts.map +0 -1
  73. package/codegen/templates/all-exports.tmpl.d.ts +0 -9
  74. package/codegen/templates/all-exports.tmpl.d.ts.map +0 -1
  75. package/codegen/templates/all-exports.tmpl.js +0 -9
  76. package/codegen/templates/constants.cjs +0 -5
  77. package/codegen/templates/constants.d.cts +0 -2
  78. package/codegen/templates/constants.d.cts.map +0 -1
  79. package/codegen/templates/constants.d.ts +0 -2
  80. package/codegen/templates/constants.d.ts.map +0 -1
  81. package/codegen/templates/constants.js +0 -2
  82. package/codegen/templates/data-contract.tmpl.cjs +0 -49
  83. package/codegen/templates/data-contract.tmpl.d.cts +0 -8
  84. package/codegen/templates/data-contract.tmpl.d.cts.map +0 -1
  85. package/codegen/templates/data-contract.tmpl.d.ts +0 -8
  86. package/codegen/templates/data-contract.tmpl.d.ts.map +0 -1
  87. package/codegen/templates/data-contract.tmpl.js +0 -45
  88. package/codegen/templates/data-contracts-file.tmpl.cjs +0 -27
  89. package/codegen/templates/data-contracts-file.tmpl.d.cts +0 -6
  90. package/codegen/templates/data-contracts-file.tmpl.d.cts.map +0 -1
  91. package/codegen/templates/data-contracts-file.tmpl.d.ts +0 -6
  92. package/codegen/templates/data-contracts-file.tmpl.d.ts.map +0 -1
  93. package/codegen/templates/data-contracts-file.tmpl.js +0 -23
  94. package/codegen/templates/endpoint-jsdoc.tmpl.cjs +0 -111
  95. package/codegen/templates/endpoint-jsdoc.tmpl.d.cts +0 -8
  96. package/codegen/templates/endpoint-jsdoc.tmpl.d.cts.map +0 -1
  97. package/codegen/templates/endpoint-jsdoc.tmpl.d.ts +0 -8
  98. package/codegen/templates/endpoint-jsdoc.tmpl.d.ts.map +0 -1
  99. package/codegen/templates/endpoint-jsdoc.tmpl.js +0 -107
  100. package/codegen/templates/endpoint-per-file.tmpl.cjs +0 -87
  101. package/codegen/templates/endpoint-per-file.tmpl.d.cts +0 -14
  102. package/codegen/templates/endpoint-per-file.tmpl.d.cts.map +0 -1
  103. package/codegen/templates/endpoint-per-file.tmpl.d.ts +0 -14
  104. package/codegen/templates/endpoint-per-file.tmpl.d.ts.map +0 -1
  105. package/codegen/templates/endpoint-per-file.tmpl.js +0 -83
  106. package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.cjs +0 -10
  107. package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.cts +0 -6
  108. package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.cts.map +0 -1
  109. package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.ts +0 -6
  110. package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.d.ts.map +0 -1
  111. package/codegen/templates/index-ts-for-endpoint-per-file.tmpl.js +0 -6
  112. package/codegen/templates/meta-info.tmpl.cjs +0 -48
  113. package/codegen/templates/meta-info.tmpl.d.cts +0 -9
  114. package/codegen/templates/meta-info.tmpl.d.cts.map +0 -1
  115. package/codegen/templates/meta-info.tmpl.d.ts +0 -9
  116. package/codegen/templates/meta-info.tmpl.d.ts.map +0 -1
  117. package/codegen/templates/meta-info.tmpl.js +0 -42
  118. package/codegen/templates/new-endpoint.tmpl.cjs +0 -188
  119. package/codegen/templates/new-endpoint.tmpl.d.cts +0 -15
  120. package/codegen/templates/new-endpoint.tmpl.d.cts.map +0 -1
  121. package/codegen/templates/new-endpoint.tmpl.d.ts +0 -15
  122. package/codegen/templates/new-endpoint.tmpl.d.ts.map +0 -1
  123. package/codegen/templates/new-endpoint.tmpl.js +0 -184
  124. package/codegen/types/all-import-file-params.cjs +0 -2
  125. package/codegen/types/all-import-file-params.d.cts +0 -5
  126. package/codegen/types/all-import-file-params.d.cts.map +0 -1
  127. package/codegen/types/all-import-file-params.d.ts +0 -5
  128. package/codegen/types/all-import-file-params.d.ts.map +0 -1
  129. package/codegen/types/all-import-file-params.js +0 -1
  130. package/codegen/types/base-tmpl-params.cjs +0 -2
  131. package/codegen/types/base-tmpl-params.d.cts +0 -16
  132. package/codegen/types/base-tmpl-params.d.cts.map +0 -1
  133. package/codegen/types/base-tmpl-params.d.ts +0 -16
  134. package/codegen/types/base-tmpl-params.d.ts.map +0 -1
  135. package/codegen/types/base-tmpl-params.js +0 -1
  136. package/codegen/types/codegen-data-utils.cjs +0 -2
  137. package/codegen/types/codegen-data-utils.d.cts +0 -8
  138. package/codegen/types/codegen-data-utils.d.cts.map +0 -1
  139. package/codegen/types/codegen-data-utils.d.ts +0 -8
  140. package/codegen/types/codegen-data-utils.d.ts.map +0 -1
  141. package/codegen/types/codegen-data-utils.js +0 -1
  142. package/codegen/types/endpoint-data.cjs +0 -2
  143. package/codegen/types/endpoint-data.d.cts +0 -3
  144. package/codegen/types/endpoint-data.d.cts.map +0 -1
  145. package/codegen/types/endpoint-data.d.ts +0 -3
  146. package/codegen/types/endpoint-data.d.ts.map +0 -1
  147. package/codegen/types/endpoint-data.js +0 -1
  148. package/codegen/types/generate-query-api-params.cjs +0 -2
  149. package/codegen/types/generate-query-api-params.d.cts.map +0 -1
  150. package/codegen/types/generate-query-api-params.d.ts +0 -162
  151. package/codegen/types/generate-query-api-params.d.ts.map +0 -1
  152. package/codegen/types/generate-query-api-params.js +0 -1
  153. package/codegen/types/import-file-params.cjs +0 -2
  154. package/codegen/types/import-file-params.d.cts +0 -5
  155. package/codegen/types/import-file-params.d.cts.map +0 -1
  156. package/codegen/types/import-file-params.d.ts +0 -5
  157. package/codegen/types/import-file-params.d.ts.map +0 -1
  158. package/codegen/types/import-file-params.js +0 -1
  159. package/codegen/types/index.cjs +0 -23
  160. package/codegen/types/index.d.cts +0 -8
  161. package/codegen/types/index.d.cts.map +0 -1
  162. package/codegen/types/index.d.ts +0 -8
  163. package/codegen/types/index.d.ts.map +0 -1
  164. package/codegen/types/index.js +0 -7
  165. package/codegen/types/meta-info.cjs +0 -2
  166. package/codegen/types/meta-info.d.cts +0 -6
  167. package/codegen/types/meta-info.d.cts.map +0 -1
  168. package/codegen/types/meta-info.d.ts +0 -6
  169. package/codegen/types/meta-info.d.ts.map +0 -1
  170. package/codegen/types/meta-info.js +0 -1
  171. package/codegen/types/type-info.cjs +0 -2
  172. package/codegen/types/type-info.d.cts +0 -4
  173. package/codegen/types/type-info.d.cts.map +0 -1
  174. package/codegen/types/type-info.d.ts +0 -4
  175. package/codegen/types/type-info.d.ts.map +0 -1
  176. package/codegen/types/type-info.js +0 -1
  177. package/codegen/utils/create-short-model-type.cjs +0 -12
  178. package/codegen/utils/create-short-model-type.d.cts +0 -5
  179. package/codegen/utils/create-short-model-type.d.cts.map +0 -1
  180. package/codegen/utils/create-short-model-type.d.ts +0 -5
  181. package/codegen/utils/create-short-model-type.d.ts.map +0 -1
  182. package/codegen/utils/create-short-model-type.js +0 -8
  183. package/codegen/utils/remove-unused-types.cjs +0 -117
  184. package/codegen/utils/remove-unused-types.d.cts +0 -7
  185. package/codegen/utils/remove-unused-types.d.cts.map +0 -1
  186. package/codegen/utils/remove-unused-types.d.ts +0 -7
  187. package/codegen/utils/remove-unused-types.d.ts.map +0 -1
  188. package/codegen/utils/remove-unused-types.js +0 -110
  189. package/codegen/utils/unpack-filter-option.cjs +0 -23
  190. package/codegen/utils/unpack-filter-option.d.cts +0 -7
  191. package/codegen/utils/unpack-filter-option.d.cts.map +0 -1
  192. package/codegen/utils/unpack-filter-option.d.ts +0 -7
  193. package/codegen/utils/unpack-filter-option.d.ts.map +0 -1
  194. package/codegen/utils/unpack-filter-option.js +0 -19
  195. package/index.d.cts +0 -3
  196. package/index.d.cts.map +0 -1
  197. package/index.d.ts.map +0 -1
  198. package/runtime/endpoint-infinite-query.cjs +0 -101
  199. package/runtime/endpoint-infinite-query.d.cts +0 -17
  200. package/runtime/endpoint-infinite-query.d.cts.map +0 -1
  201. package/runtime/endpoint-infinite-query.d.ts +0 -17
  202. package/runtime/endpoint-infinite-query.d.ts.map +0 -1
  203. package/runtime/endpoint-infinite-query.js +0 -97
  204. package/runtime/endpoint-infinite-query.types.cjs +0 -2
  205. package/runtime/endpoint-infinite-query.types.d.cts +0 -30
  206. package/runtime/endpoint-infinite-query.types.d.cts.map +0 -1
  207. package/runtime/endpoint-infinite-query.types.d.ts +0 -30
  208. package/runtime/endpoint-infinite-query.types.d.ts.map +0 -1
  209. package/runtime/endpoint-infinite-query.types.js +0 -1
  210. package/runtime/endpoint-mutation.cjs +0 -74
  211. package/runtime/endpoint-mutation.d.cts +0 -13
  212. package/runtime/endpoint-mutation.d.cts.map +0 -1
  213. package/runtime/endpoint-mutation.d.ts +0 -13
  214. package/runtime/endpoint-mutation.d.ts.map +0 -1
  215. package/runtime/endpoint-mutation.js +0 -70
  216. package/runtime/endpoint-mutation.types.cjs +0 -2
  217. package/runtime/endpoint-mutation.types.d.cts +0 -36
  218. package/runtime/endpoint-mutation.types.d.cts.map +0 -1
  219. package/runtime/endpoint-mutation.types.d.ts +0 -36
  220. package/runtime/endpoint-mutation.types.d.ts.map +0 -1
  221. package/runtime/endpoint-mutation.types.js +0 -1
  222. package/runtime/endpoint-query-client.cjs +0 -79
  223. package/runtime/endpoint-query-client.d.cts +0 -13
  224. package/runtime/endpoint-query-client.d.cts.map +0 -1
  225. package/runtime/endpoint-query-client.d.ts +0 -13
  226. package/runtime/endpoint-query-client.d.ts.map +0 -1
  227. package/runtime/endpoint-query-client.js +0 -75
  228. package/runtime/endpoint-query-client.types.cjs +0 -2
  229. package/runtime/endpoint-query-client.types.d.cts +0 -16
  230. package/runtime/endpoint-query-client.types.d.cts.map +0 -1
  231. package/runtime/endpoint-query-client.types.d.ts +0 -16
  232. package/runtime/endpoint-query-client.types.d.ts.map +0 -1
  233. package/runtime/endpoint-query-client.types.js +0 -1
  234. package/runtime/endpoint-query.cjs +0 -214
  235. package/runtime/endpoint-query.d.cts +0 -29
  236. package/runtime/endpoint-query.d.cts.map +0 -1
  237. package/runtime/endpoint-query.d.ts +0 -29
  238. package/runtime/endpoint-query.d.ts.map +0 -1
  239. package/runtime/endpoint-query.js +0 -208
  240. package/runtime/endpoint-query.types.cjs +0 -2
  241. package/runtime/endpoint-query.types.d.cts +0 -42
  242. package/runtime/endpoint-query.types.d.cts.map +0 -1
  243. package/runtime/endpoint-query.types.d.ts +0 -42
  244. package/runtime/endpoint-query.types.d.ts.map +0 -1
  245. package/runtime/endpoint-query.types.js +0 -1
  246. package/runtime/endpoint.cjs +0 -131
  247. package/runtime/endpoint.d.cts +0 -73
  248. package/runtime/endpoint.d.cts.map +0 -1
  249. package/runtime/endpoint.d.ts +0 -73
  250. package/runtime/endpoint.d.ts.map +0 -1
  251. package/runtime/endpoint.js +0 -127
  252. package/runtime/endpoint.types.cjs +0 -2
  253. package/runtime/endpoint.types.d.cts +0 -24
  254. package/runtime/endpoint.types.d.cts.map +0 -1
  255. package/runtime/endpoint.types.d.ts +0 -24
  256. package/runtime/endpoint.types.d.ts.map +0 -1
  257. package/runtime/endpoint.types.js +0 -1
  258. package/runtime/http-client.cjs +0 -190
  259. package/runtime/http-client.d.cts +0 -85
  260. package/runtime/http-client.d.cts.map +0 -1
  261. package/runtime/http-client.d.ts +0 -85
  262. package/runtime/http-client.d.ts.map +0 -1
  263. package/runtime/http-client.js +0 -184
  264. package/runtime/index.cjs +0 -25
  265. package/runtime/index.d.cts +0 -10
  266. package/runtime/index.d.cts.map +0 -1
  267. package/runtime/index.d.ts +0 -10
  268. package/runtime/index.d.ts.map +0 -1
  269. package/runtime/index.js +0 -9
package/index.js CHANGED
@@ -1,2 +1,768 @@
1
- export * from '@tanstack/query-core';
2
- export * from "./runtime/index.js";
1
+ import { hashKey } from "@tanstack/query-core";
2
+ export * from "@tanstack/query-core";
3
+ import { callFunction } from "yummies/common";
4
+ import { makeObservable, observable, runInAction, comparer, computed, reaction, action } from "mobx";
5
+ import { Query, InfiniteQuery, Mutation, QueryClient } from "mobx-tanstack-query";
6
+ import { getMobxAdministration, lazyObserve } from "yummies/mobx";
7
+ import { stringify } from "qs";
8
+ class EndpointQuery extends Query {
9
+ constructor(endpoint, inputQueryClient, queryOptionsInput) {
10
+ const isQueryOptionsInputFn = typeof queryOptionsInput === "function";
11
+ const unpackedQueryOptionsInput = isQueryOptionsInputFn ? queryOptionsInput() : queryOptionsInput;
12
+ const {
13
+ uniqKey,
14
+ transform: transformResponse,
15
+ params,
16
+ queryClient: overridedQueryClient,
17
+ ...queryOptions
18
+ } = unpackedQueryOptionsInput;
19
+ const queryClient = overridedQueryClient ?? inputQueryClient;
20
+ const _observableData = {
21
+ params: null,
22
+ dynamicOptions: void 0,
23
+ response: null,
24
+ uniqKey: unpackedQueryOptionsInput.uniqKey
25
+ };
26
+ if (!isQueryOptionsInputFn && typeof params !== "function") {
27
+ if ("params" in unpackedQueryOptionsInput) {
28
+ _observableData.params = params;
29
+ } else {
30
+ _observableData.params = {};
31
+ }
32
+ _observableData.initialized = true;
33
+ }
34
+ makeObservable(_observableData, {
35
+ params: observable.ref,
36
+ response: observable.ref,
37
+ dynamicOptions: observable
38
+ });
39
+ super({
40
+ ...queryOptions,
41
+ queryClient,
42
+ meta: endpoint.toQueryMeta(queryOptions.meta),
43
+ options: () => {
44
+ const builtOptions = buildOptionsFromParams(
45
+ endpoint,
46
+ _observableData.params,
47
+ _observableData.uniqKey
48
+ );
49
+ let isEnabled = !!_observableData.initialized && builtOptions.enabled;
50
+ if (typeof queryOptionsInput !== "function" && queryOptionsInput.enabled === false) {
51
+ isEnabled = false;
52
+ }
53
+ return {
54
+ ...builtOptions,
55
+ enabled: isEnabled,
56
+ ..._observableData.dynamicOptions
57
+ };
58
+ },
59
+ queryFn: async (ctx) => {
60
+ const params2 = endpoint.getParamsFromContext(ctx);
61
+ runInAction(() => {
62
+ _observableData.response = null;
63
+ if (!comparer.structural(params2, _observableData.params)) {
64
+ _observableData.params = params2;
65
+ }
66
+ });
67
+ let requestParams = params2.requestParams;
68
+ if (requestParams) {
69
+ if (!requestParams.signal) {
70
+ requestParams.signal = ctx.signal;
71
+ }
72
+ } else {
73
+ requestParams = { signal: ctx.signal };
74
+ }
75
+ const fixedInput = {
76
+ ...params2,
77
+ requestParams
78
+ };
79
+ const response = await endpoint.request(fixedInput);
80
+ runInAction(() => {
81
+ _observableData.response = response;
82
+ });
83
+ return await transformResponse?.(response) ?? response.data;
84
+ }
85
+ });
86
+ this.endpoint = endpoint;
87
+ const parentAtom = getMobxAdministration(this);
88
+ computed.struct(this, "params");
89
+ computed.struct(this, "response");
90
+ makeObservable(this);
91
+ if (isQueryOptionsInputFn || typeof params === "function") {
92
+ const createParamsReaction = () => reaction(
93
+ () => {
94
+ let outDynamicOptions;
95
+ let outParams;
96
+ let uniqKey2;
97
+ if (isQueryOptionsInputFn) {
98
+ const result = queryOptionsInput();
99
+ const {
100
+ params: params2,
101
+ abortSignal,
102
+ select,
103
+ onDone,
104
+ onError,
105
+ onInit,
106
+ enableOnDemand,
107
+ uniqKey: _uniqKey,
108
+ ...dynamicOptions
109
+ } = result;
110
+ uniqKey2 = _uniqKey;
111
+ if ("params" in result) {
112
+ outParams = result.params;
113
+ } else {
114
+ outParams = {};
115
+ }
116
+ outDynamicOptions = Object.keys(dynamicOptions).length > 0 ? dynamicOptions : void 0;
117
+ } else if ("params" in unpackedQueryOptionsInput) {
118
+ outParams = unpackedQueryOptionsInput.params;
119
+ uniqKey2 = unpackedQueryOptionsInput.uniqKey;
120
+ } else {
121
+ outParams = {};
122
+ uniqKey2 = unpackedQueryOptionsInput.uniqKey;
123
+ }
124
+ return {
125
+ params: callFunction(outParams),
126
+ dynamicOptions: outDynamicOptions,
127
+ uniqKey: uniqKey2
128
+ };
129
+ },
130
+ ({ params: params2, dynamicOptions, uniqKey: uniqKey2 }) => {
131
+ runInAction(() => {
132
+ _observableData.initialized = true;
133
+ _observableData.params = params2;
134
+ _observableData.dynamicOptions = dynamicOptions;
135
+ _observableData.uniqKey = uniqKey2;
136
+ });
137
+ },
138
+ {
139
+ fireImmediately: true
140
+ }
141
+ );
142
+ if (this.features.lazy) {
143
+ lazyObserve({
144
+ property: parentAtom.values_.get("_result"),
145
+ onStart: createParamsReaction,
146
+ onEnd: (disposeFn) => disposeFn()
147
+ });
148
+ } else {
149
+ this.abortController.signal.addEventListener(
150
+ "abort",
151
+ createParamsReaction()
152
+ );
153
+ }
154
+ }
155
+ this._observableData = _observableData;
156
+ }
157
+ _observableData;
158
+ get params() {
159
+ return this._observableData.params;
160
+ }
161
+ get response() {
162
+ return this._observableData.response;
163
+ }
164
+ update(updateParams) {
165
+ if ("params" in updateParams) {
166
+ const { params, ...options } = updateParams;
167
+ runInAction(() => {
168
+ this._observableData.params = params;
169
+ });
170
+ return super.update({
171
+ ...buildOptionsFromParams(
172
+ this.endpoint,
173
+ params,
174
+ this._observableData.uniqKey
175
+ ),
176
+ ...options
177
+ });
178
+ } else if (this._observableData) {
179
+ return super.update({
180
+ ...buildOptionsFromParams(
181
+ this.endpoint,
182
+ this._observableData.params,
183
+ this._observableData.uniqKey
184
+ ),
185
+ ...updateParams
186
+ });
187
+ } else {
188
+ return super.update(updateParams);
189
+ }
190
+ }
191
+ refetch(options) {
192
+ if (this.params) {
193
+ return super.refetch(options);
194
+ }
195
+ return Promise.resolve(this.queryObserver.getCurrentResult());
196
+ }
197
+ async start(params) {
198
+ runInAction(() => {
199
+ this._observableData.params = params;
200
+ });
201
+ return await super.start(
202
+ buildOptionsFromParams(
203
+ this.endpoint,
204
+ params,
205
+ this._observableData.uniqKey
206
+ )
207
+ );
208
+ }
209
+ handleDestroy() {
210
+ super.handleDestroy();
211
+ runInAction(() => {
212
+ this._observableData.params = void 0;
213
+ this._observableData.dynamicOptions = void 0;
214
+ });
215
+ }
216
+ }
217
+ const getParamsFromContext = (ctx) => {
218
+ return ctx.queryKey.at(-2) || {};
219
+ };
220
+ const buildOptionsFromParams = (endpoint, params, uniqKey) => {
221
+ const { requiredParams } = endpoint.configuration;
222
+ let hasRequiredParams = false;
223
+ if (requiredParams.length > 0) {
224
+ hasRequiredParams = !!params && requiredParams.every((param) => param in params);
225
+ } else {
226
+ hasRequiredParams = !!params;
227
+ }
228
+ return {
229
+ enabled: hasRequiredParams,
230
+ queryKey: endpoint.toQueryKey(params || {}, uniqKey)
231
+ };
232
+ };
233
+ class EndpointInfiniteQuery extends InfiniteQuery {
234
+ constructor(endpoint, queryClient, queryOptionsInput) {
235
+ const {
236
+ uniqKey,
237
+ transform: transformResponse,
238
+ params: paramsFn,
239
+ ...queryOptions
240
+ } = typeof queryOptionsInput === "function" ? queryOptionsInput() : queryOptionsInput;
241
+ super({
242
+ ...queryOptions,
243
+ queryClient,
244
+ meta: endpoint.toQueryMeta(queryOptions.meta),
245
+ options: (query) => {
246
+ const extraOptions = {};
247
+ let willEnableManually;
248
+ let params;
249
+ const pageParam = query.options.initialPageParam;
250
+ if (typeof queryOptionsInput === "function") {
251
+ Object.assign(extraOptions, queryOptionsInput());
252
+ params = paramsFn(pageParam);
253
+ willEnableManually = false;
254
+ } else {
255
+ willEnableManually = queryOptionsInput.enabled === false;
256
+ params = paramsFn(pageParam);
257
+ }
258
+ const builtOptions = buildOptionsFromParams(endpoint, params, uniqKey);
259
+ let isEnabled = false;
260
+ if (willEnableManually) ;
261
+ else {
262
+ isEnabled = builtOptions.enabled;
263
+ }
264
+ return {
265
+ ...query.options,
266
+ ...builtOptions,
267
+ // ...dynamicOuterOptions,
268
+ enabled: isEnabled,
269
+ ...extraOptions
270
+ };
271
+ },
272
+ queryFn: async (ctx) => {
273
+ const params = paramsFn(
274
+ ctx.pageParam ?? queryOptions.initialPageParam
275
+ );
276
+ runInAction(() => {
277
+ this.response = null;
278
+ this.params = params;
279
+ });
280
+ let requestParams = params.request;
281
+ if (requestParams) {
282
+ if (!requestParams.signal) {
283
+ requestParams.signal = ctx.signal;
284
+ }
285
+ } else {
286
+ requestParams = { signal: ctx.signal };
287
+ }
288
+ const fixedInput = {
289
+ ...params,
290
+ request: requestParams
291
+ };
292
+ const response = await endpoint.request(fixedInput);
293
+ runInAction(() => {
294
+ this.response = response;
295
+ });
296
+ return await transformResponse?.(response) ?? response.data;
297
+ }
298
+ });
299
+ this.endpoint = endpoint;
300
+ this.paramsFn = paramsFn;
301
+ this.uniqKey = uniqKey;
302
+ observable.ref(this, "response");
303
+ observable.ref(this, "params");
304
+ makeObservable(this);
305
+ }
306
+ response = null;
307
+ params = null;
308
+ uniqKey;
309
+ paramsFn;
310
+ update({
311
+ params,
312
+ ...options
313
+ }) {
314
+ return super.update({
315
+ ...buildOptionsFromParams(this.endpoint, params, this.uniqKey),
316
+ ...options
317
+ });
318
+ }
319
+ async start(params) {
320
+ return await super.start(
321
+ buildOptionsFromParams(this.endpoint, params, this.uniqKey)
322
+ );
323
+ }
324
+ }
325
+ class EndpointMutation extends Mutation {
326
+ constructor(endpoint, inputQueryClient, {
327
+ transform: transformResponse,
328
+ invalidateEndpoints,
329
+ queryClient: overridedQueryClient,
330
+ ...mutationOptions
331
+ }) {
332
+ const queryClient = overridedQueryClient ?? inputQueryClient;
333
+ super({
334
+ ...mutationOptions,
335
+ queryClient,
336
+ onSuccess: (data, variables, onMutateResult, context) => {
337
+ mutationOptions.onSuccess?.(data, variables, onMutateResult, context);
338
+ if (invalidateEndpoints) {
339
+ if (typeof invalidateEndpoints === "object") {
340
+ queryClient.invalidateEndpoints(invalidateEndpoints);
341
+ } else {
342
+ let filters;
343
+ switch (`${invalidateEndpoints}`) {
344
+ case "true": {
345
+ filters = endpoint.group ? {
346
+ group: endpoint.group
347
+ } : {
348
+ tag: endpoint.tags
349
+ };
350
+ break;
351
+ }
352
+ case "by-group": {
353
+ filters = {
354
+ group: endpoint.group
355
+ };
356
+ break;
357
+ }
358
+ case "by-tag": {
359
+ filters = {
360
+ tag: endpoint.tags
361
+ };
362
+ break;
363
+ }
364
+ }
365
+ if (filters) {
366
+ queryClient.invalidateEndpoints(filters);
367
+ }
368
+ }
369
+ }
370
+ },
371
+ mutationFn: async (input, context) => {
372
+ let requestParams = input?.requestParams;
373
+ if (requestParams) {
374
+ if (!requestParams.signal) {
375
+ requestParams.signal = context.signal;
376
+ }
377
+ } else {
378
+ requestParams = { signal: context.signal };
379
+ }
380
+ const fixedInput = {
381
+ ...input,
382
+ requestParams
383
+ };
384
+ const response = await endpoint.request(fixedInput);
385
+ return await transformResponse?.(response) ?? response.data;
386
+ }
387
+ });
388
+ this.endpoint = endpoint;
389
+ }
390
+ }
391
+ class Endpoint {
392
+ constructor(configuration, queryClient, httpClient) {
393
+ this.configuration = configuration;
394
+ this.queryClient = queryClient;
395
+ this.httpClient = httpClient;
396
+ this.endpointId = globalThis.crypto.randomUUID();
397
+ this.meta = configuration.meta ?? {};
398
+ const instance = this;
399
+ const callable = function(...args) {
400
+ return instance.request.apply(instance, args);
401
+ };
402
+ Object.setPrototypeOf(callable, new.target.prototype);
403
+ Object.getOwnPropertyNames(instance).concat(Object.getOwnPropertyNames(new.target.prototype)).forEach((key) => {
404
+ if (key === "constructor") return;
405
+ const desc = Object.getOwnPropertyDescriptor(instance, key) || Object.getOwnPropertyDescriptor(new.target.prototype, key);
406
+ if (desc) Object.defineProperty(callable, key, desc);
407
+ });
408
+ return callable;
409
+ }
410
+ endpointId;
411
+ presets = {
412
+ mutations: {}
413
+ };
414
+ __params;
415
+ __response;
416
+ meta;
417
+ getFullUrl(...args) {
418
+ const params = this.configuration.params(args[0] ?? {});
419
+ return this.httpClient.buildUrl(params);
420
+ }
421
+ getPath(...args) {
422
+ const params = this.configuration.params(args[0] ?? {});
423
+ return params.path;
424
+ }
425
+ getParamsFromContext(ctx) {
426
+ return ctx.queryKey.at(-2) || {};
427
+ }
428
+ get tags() {
429
+ return this.configuration.tags;
430
+ }
431
+ get path() {
432
+ return this.configuration.path;
433
+ }
434
+ get pathDeclaration() {
435
+ return this.path.join("/");
436
+ }
437
+ get operationId() {
438
+ return this.configuration.operationId;
439
+ }
440
+ get group() {
441
+ return this.configuration.group;
442
+ }
443
+ get namespace() {
444
+ return this.configuration.namespace;
445
+ }
446
+ request(...args) {
447
+ return this.httpClient.request(
448
+ this.configuration.params(args[0] ?? {}),
449
+ this
450
+ );
451
+ }
452
+ toQueryMeta = (meta) => ({
453
+ ...meta,
454
+ tags: this.tags,
455
+ operationId: this.operationId,
456
+ path: this.path,
457
+ pathDeclaration: this.pathDeclaration,
458
+ endpointId: this.endpointId,
459
+ endpointQuery: true
460
+ });
461
+ toQueryKey(params, uniqKey) {
462
+ return [
463
+ ...this.configuration.path,
464
+ this.configuration.operationId,
465
+ params ?? {},
466
+ callFunction(uniqKey)
467
+ ];
468
+ }
469
+ toInfiniteQueryKey(params, uniqKey) {
470
+ return [
471
+ { infiniteQuery: true },
472
+ ...this.configuration.path,
473
+ this.configuration.operationId,
474
+ params ?? {},
475
+ callFunction(uniqKey)
476
+ ];
477
+ }
478
+ invalidateQuery(...args) {
479
+ this.queryClient.invalidateQueries(
480
+ {
481
+ queryKey: this.toQueryKey(args[0], args[1]?.uniqKey),
482
+ exact: true,
483
+ ...args[1]
484
+ },
485
+ args[2]
486
+ );
487
+ }
488
+ toMutation(options) {
489
+ return new EndpointMutation(
490
+ this,
491
+ this.queryClient,
492
+ {
493
+ ...options,
494
+ invalidateEndpoints: options.invalidateEndpoints ?? this.presets.mutations?.invalidateQueries
495
+ }
496
+ );
497
+ }
498
+ toQuery(options) {
499
+ return new EndpointQuery(
500
+ this,
501
+ this.queryClient,
502
+ options
503
+ );
504
+ }
505
+ toInfiniteQuery(options) {
506
+ return new EndpointInfiniteQuery(
507
+ this,
508
+ this.queryClient,
509
+ options
510
+ );
511
+ }
512
+ }
513
+ class EndpointQueryClient extends QueryClient {
514
+ constructor(config) {
515
+ super({
516
+ ...config,
517
+ defaultOptions: {
518
+ ...config?.defaultOptions,
519
+ queries: {
520
+ ...config?.defaultOptions?.queries,
521
+ queryKeyHashFn: config?.defaultOptions?.queries?.queryKeyHashFn ?? hashKey
522
+ }
523
+ }
524
+ });
525
+ }
526
+ invalidateEndpoints({
527
+ group,
528
+ namespace,
529
+ operationId,
530
+ tag,
531
+ predicate,
532
+ endpoint,
533
+ ...queryFilters
534
+ }, options) {
535
+ let endpointIdsToFilter;
536
+ if (Array.isArray(endpoint)) {
537
+ endpointIdsToFilter = new Set(endpoint.map((it) => it.endpointId));
538
+ } else if (endpoint) {
539
+ endpointIdsToFilter = /* @__PURE__ */ new Set([endpoint.endpointId]);
540
+ }
541
+ return this.invalidateQueries(
542
+ {
543
+ ...queryFilters,
544
+ predicate: (query) => {
545
+ if (!query.meta?.endpointQuery) {
546
+ return false;
547
+ }
548
+ const meta = query.meta;
549
+ if (endpointIdsToFilter && !endpointIdsToFilter.has(meta.endpointId)) {
550
+ return false;
551
+ }
552
+ if (namespace && meta.namespace && !applyStringFilter(namespace, meta.namespace)) {
553
+ return false;
554
+ }
555
+ if (group && meta.group && !applyStringFilter(group, meta.group)) {
556
+ return false;
557
+ }
558
+ if (tag && meta.tags && !applyStringFilter(tag, meta.tags)) {
559
+ return false;
560
+ }
561
+ if (tag && meta.tags && !applyStringFilter(tag, meta.tags)) {
562
+ return false;
563
+ }
564
+ if (operationId && !applyStringFilter(operationId, meta.operationId)) {
565
+ return false;
566
+ }
567
+ if (predicate && !predicate(meta, query)) {
568
+ return false;
569
+ }
570
+ return true;
571
+ }
572
+ },
573
+ options
574
+ );
575
+ }
576
+ }
577
+ const applyStringFilter = (filter, value) => {
578
+ const values = Array.isArray(value) ? value : [value];
579
+ if (filter instanceof RegExp) {
580
+ return values.some((value2) => filter.test(value2));
581
+ }
582
+ if (Array.isArray(filter)) {
583
+ return filter.some((filter2) => values.includes(filter2));
584
+ }
585
+ return values.includes(filter);
586
+ };
587
+ const isHttpResponse = (response, status) => !!response && typeof response === "object" && response instanceof Response && "data" in response && (!status || response.status === status);
588
+ const isHttpBadResponse = (response) => {
589
+ return isHttpResponse(response) && (!response.ok || !!response.error);
590
+ };
591
+ class HttpClient {
592
+ config;
593
+ fetch;
594
+ meta;
595
+ baseApiParams;
596
+ badResponse;
597
+ toQueryString;
598
+ constructor(config) {
599
+ this.config = config ?? {};
600
+ this.badResponse = null;
601
+ this.meta = config?.meta ?? null;
602
+ this.fetch = config?.fetch ?? ((...fetchParams) => globalThis.fetch(...fetchParams));
603
+ this.toQueryString = config?.toQueryString ?? ((query) => stringify(query, config?.queryStringifyOptions));
604
+ this.baseApiParams = {
605
+ credentials: "same-origin",
606
+ headers: {},
607
+ redirect: "follow",
608
+ referrerPolicy: "no-referrer"
609
+ };
610
+ this.updateConfig(this.config);
611
+ observable.ref(this, "badResponse");
612
+ observable.ref(this, "meta");
613
+ action(this, "setMeta");
614
+ action(this, "setBadResponse");
615
+ makeObservable(this);
616
+ }
617
+ get baseUrl() {
618
+ return this.config.baseUrl ?? "";
619
+ }
620
+ updateConfig(update) {
621
+ Object.assign(this.config, update);
622
+ if (update.baseApiParams) {
623
+ Object.assign(this.baseApiParams, update.baseApiParams);
624
+ }
625
+ if (update.contentFormatters) {
626
+ Object.assign(this.contentFormatters, update.contentFormatters);
627
+ }
628
+ if (update.fetch) {
629
+ this.fetch = update.fetch;
630
+ }
631
+ }
632
+ setMeta = (data) => {
633
+ this.meta = data;
634
+ };
635
+ setBadResponse = (response) => {
636
+ this.badResponse = response;
637
+ };
638
+ contentFormatters = {
639
+ "application/json": (input) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
640
+ "text/plain": (input) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
641
+ "multipart/form-data": (input) => Object.keys(input || {}).reduce((formData, key) => {
642
+ const property = input[key];
643
+ if (property instanceof Blob) {
644
+ formData.append(key, property);
645
+ } else if (typeof property === "object" && property !== null) {
646
+ formData.append(key, JSON.stringify(property));
647
+ } else {
648
+ formData.append(key, `${property}`);
649
+ }
650
+ return formData;
651
+ }, new FormData()),
652
+ "application/x-www-form-urlencoded": (input) => this.toQueryString(input),
653
+ "application/octet-stream": (input) => input
654
+ };
655
+ mergeRequestParams(params1, params2) {
656
+ return {
657
+ ...this.baseApiParams,
658
+ ...params1,
659
+ ...params2,
660
+ headers: {
661
+ ...this.baseApiParams.headers,
662
+ ...params1.headers,
663
+ ...params2?.headers
664
+ }
665
+ };
666
+ }
667
+ async createResponse(responseFormat, raw, url, params) {
668
+ const response = raw;
669
+ response.request = {
670
+ url,
671
+ params
672
+ };
673
+ response.data = null;
674
+ response.error = null;
675
+ if (responseFormat) {
676
+ try {
677
+ const formatted = await response[responseFormat]();
678
+ if (response.ok) {
679
+ response.data = formatted;
680
+ } else {
681
+ response.error = formatted;
682
+ }
683
+ } catch (error) {
684
+ if (response.ok) {
685
+ response.error = error;
686
+ } else {
687
+ response.error = null;
688
+ }
689
+ }
690
+ }
691
+ if (!response.ok || response.error) {
692
+ this.setBadResponse(response);
693
+ }
694
+ return response;
695
+ }
696
+ buildUrl = (params) => {
697
+ const baseUrl = params.baseUrl ?? this.baseUrl ?? "";
698
+ const path = params.path;
699
+ const queryString = params.query && this.toQueryString(params.query);
700
+ const query = queryString ? `?${queryString}` : "";
701
+ if (this.config.buildUrl) {
702
+ return this.config.buildUrl(params, { baseUrl, path, query }, this.meta);
703
+ }
704
+ const url = baseUrl + path + query;
705
+ return url;
706
+ };
707
+ async request(fullParams, endpoint) {
708
+ this.setBadResponse(null);
709
+ const {
710
+ body,
711
+ contentType = "application/json",
712
+ format = "json",
713
+ ...params
714
+ } = fullParams;
715
+ let requestParams = this.mergeRequestParams(params);
716
+ if (this.config.interceptor) {
717
+ requestParams = await this.config.interceptor(requestParams, this.meta, endpoint) ?? requestParams;
718
+ }
719
+ const payloadFormatter = this.contentFormatters[contentType];
720
+ const responseFormat = format || requestParams.format;
721
+ const url = this.buildUrl(fullParams);
722
+ let headers;
723
+ if (requestParams.headers instanceof Headers) {
724
+ headers = requestParams.headers;
725
+ } else if (Array.isArray(requestParams.headers)) {
726
+ headers = new Headers(requestParams.headers);
727
+ } else {
728
+ headers = new Headers(requestParams.headers);
729
+ }
730
+ if (!headers.has("Content-Type")) {
731
+ headers.set("Content-Type", contentType);
732
+ }
733
+ const fetchUrl = url;
734
+ const fetchParams = {
735
+ ...requestParams,
736
+ headers,
737
+ body: body == null ? null : payloadFormatter(body)
738
+ };
739
+ let response;
740
+ try {
741
+ response = await this.fetch(fetchUrl, fetchParams);
742
+ } catch (error) {
743
+ response = error;
744
+ }
745
+ const httpResponse = await this.createResponse(
746
+ responseFormat,
747
+ response,
748
+ fetchUrl,
749
+ fetchParams
750
+ );
751
+ if (!httpResponse.ok || httpResponse.error) {
752
+ throw httpResponse;
753
+ }
754
+ return httpResponse;
755
+ }
756
+ }
757
+ export {
758
+ Endpoint,
759
+ EndpointMutation,
760
+ EndpointQuery,
761
+ EndpointQueryClient,
762
+ HttpClient,
763
+ buildOptionsFromParams,
764
+ getParamsFromContext,
765
+ isHttpBadResponse,
766
+ isHttpResponse
767
+ };
768
+ //# sourceMappingURL=index.js.map