@data-client/core 0.1.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.
Files changed (308) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +71 -0
  3. package/dist/index.js +1643 -0
  4. package/dist/index.umd.min.js +1 -0
  5. package/dist/next.js +461 -0
  6. package/dist/package.json +1 -0
  7. package/legacy/actionTypes.js +12 -0
  8. package/legacy/compatibleActions.js +2 -0
  9. package/legacy/controller/BaseController.js +289 -0
  10. package/legacy/controller/Controller.js +20 -0
  11. package/legacy/controller/createFetch.js +42 -0
  12. package/legacy/controller/createInvalidate.js +12 -0
  13. package/legacy/controller/createInvalidateAll.js +8 -0
  14. package/legacy/controller/createOptimistic.js +33 -0
  15. package/legacy/controller/createReceive.js +36 -0
  16. package/legacy/controller/createReset.js +8 -0
  17. package/legacy/controller/createSubscription.js +30 -0
  18. package/legacy/controller/types.js +2 -0
  19. package/legacy/endpoint/index.js +2 -0
  20. package/legacy/endpoint/shapes.js +2 -0
  21. package/legacy/endpoint/types.js +2 -0
  22. package/legacy/fsa.js +2 -0
  23. package/legacy/index.js +22 -0
  24. package/legacy/internal.js +4 -0
  25. package/legacy/legacyActions.js +2 -0
  26. package/legacy/manager/ConnectionListener.js +2 -0
  27. package/legacy/manager/DefaultConnectionListener.js +40 -0
  28. package/legacy/manager/DevtoolsManager.js +73 -0
  29. package/legacy/manager/LogoutManager.js +34 -0
  30. package/legacy/manager/NetworkManager.js +291 -0
  31. package/legacy/manager/PollingSubscription.js +159 -0
  32. package/legacy/manager/SubscriptionManager.js +117 -0
  33. package/legacy/manager/applyManager.js +23 -0
  34. package/legacy/manager/devtoolsTypes.js +2 -0
  35. package/legacy/manager/index.js +6 -0
  36. package/legacy/middlewareTypes.js +2 -0
  37. package/legacy/newActions.js +2 -0
  38. package/legacy/next/Controller.js +24 -0
  39. package/legacy/next/index.js +3 -0
  40. package/legacy/previousActions.js +2 -0
  41. package/legacy/state/RIC.js +3 -0
  42. package/legacy/state/applyUpdatersToResults.js +4 -0
  43. package/legacy/state/legacy-actions/createFetch.js +62 -0
  44. package/legacy/state/legacy-actions/createReceive.js +37 -0
  45. package/legacy/state/legacy-actions/createReceiveError.js +28 -0
  46. package/legacy/state/legacy-actions/index.js +4 -0
  47. package/legacy/state/reducer/createReducer.js +54 -0
  48. package/legacy/state/reducer/fetchReducer.js +32 -0
  49. package/legacy/state/reducer/invalidateReducer.js +28 -0
  50. package/legacy/state/reducer/setReducer.js +113 -0
  51. package/legacy/state/reducerInstance.js +9 -0
  52. package/legacy/state/selectMeta.js +4 -0
  53. package/legacy/types.js +8 -0
  54. package/lib/actionTypes.d.ts +11 -0
  55. package/lib/actionTypes.d.ts.map +1 -0
  56. package/lib/actionTypes.js +12 -0
  57. package/lib/compatibleActions.d.ts +47 -0
  58. package/lib/compatibleActions.d.ts.map +1 -0
  59. package/lib/compatibleActions.js +2 -0
  60. package/lib/controller/BaseController.d.ts +128 -0
  61. package/lib/controller/BaseController.d.ts.map +1 -0
  62. package/lib/controller/BaseController.js +289 -0
  63. package/lib/controller/Controller.d.ts +14 -0
  64. package/lib/controller/Controller.d.ts.map +1 -0
  65. package/lib/controller/Controller.js +20 -0
  66. package/lib/controller/createFetch.d.ts +12 -0
  67. package/lib/controller/createFetch.d.ts.map +1 -0
  68. package/lib/controller/createFetch.js +42 -0
  69. package/lib/controller/createInvalidate.d.ts +6 -0
  70. package/lib/controller/createInvalidate.d.ts.map +1 -0
  71. package/lib/controller/createInvalidate.js +12 -0
  72. package/lib/controller/createInvalidateAll.d.ts +3 -0
  73. package/lib/controller/createInvalidateAll.d.ts.map +1 -0
  74. package/lib/controller/createInvalidateAll.js +8 -0
  75. package/lib/controller/createOptimistic.d.ts +10 -0
  76. package/lib/controller/createOptimistic.d.ts.map +1 -0
  77. package/lib/controller/createOptimistic.js +33 -0
  78. package/lib/controller/createReceive.d.ts +20 -0
  79. package/lib/controller/createReceive.d.ts.map +1 -0
  80. package/lib/controller/createReceive.js +36 -0
  81. package/lib/controller/createReset.d.ts +3 -0
  82. package/lib/controller/createReset.d.ts.map +1 -0
  83. package/lib/controller/createReset.js +8 -0
  84. package/lib/controller/createSubscription.d.ts +9 -0
  85. package/lib/controller/createSubscription.d.ts.map +1 -0
  86. package/lib/controller/createSubscription.js +30 -0
  87. package/lib/controller/types.d.ts +6 -0
  88. package/lib/controller/types.d.ts.map +1 -0
  89. package/lib/controller/types.js +2 -0
  90. package/lib/endpoint/index.d.ts +3 -0
  91. package/lib/endpoint/index.d.ts.map +1 -0
  92. package/lib/endpoint/index.js +2 -0
  93. package/lib/endpoint/shapes.d.ts +25 -0
  94. package/lib/endpoint/shapes.d.ts.map +1 -0
  95. package/lib/endpoint/shapes.js +2 -0
  96. package/lib/endpoint/types.d.ts +45 -0
  97. package/lib/endpoint/types.d.ts.map +1 -0
  98. package/lib/endpoint/types.js +2 -0
  99. package/lib/fsa.d.ts +41 -0
  100. package/lib/fsa.d.ts.map +1 -0
  101. package/lib/fsa.js +2 -0
  102. package/lib/index.d.ts +19 -0
  103. package/lib/index.d.ts.map +1 -0
  104. package/lib/index.js +22 -0
  105. package/lib/internal.d.ts +4 -0
  106. package/lib/internal.d.ts.map +1 -0
  107. package/lib/internal.js +4 -0
  108. package/lib/legacyActions.d.ts +92 -0
  109. package/lib/legacyActions.d.ts.map +1 -0
  110. package/lib/legacyActions.js +2 -0
  111. package/lib/manager/ConnectionListener.d.ts +8 -0
  112. package/lib/manager/ConnectionListener.d.ts.map +1 -0
  113. package/lib/manager/ConnectionListener.js +2 -0
  114. package/lib/manager/DefaultConnectionListener.d.ts +20 -0
  115. package/lib/manager/DefaultConnectionListener.d.ts.map +1 -0
  116. package/lib/manager/DefaultConnectionListener.js +40 -0
  117. package/lib/manager/DevtoolsManager.d.ts +24 -0
  118. package/lib/manager/DevtoolsManager.d.ts.map +1 -0
  119. package/lib/manager/DevtoolsManager.js +74 -0
  120. package/lib/manager/LogoutManager.d.ts +25 -0
  121. package/lib/manager/LogoutManager.d.ts.map +1 -0
  122. package/lib/manager/LogoutManager.js +34 -0
  123. package/lib/manager/NetworkManager.d.ts +82 -0
  124. package/lib/manager/NetworkManager.d.ts.map +1 -0
  125. package/lib/manager/NetworkManager.js +295 -0
  126. package/lib/manager/PollingSubscription.d.ts +45 -0
  127. package/lib/manager/PollingSubscription.d.ts.map +1 -0
  128. package/lib/manager/PollingSubscription.js +159 -0
  129. package/lib/manager/SubscriptionManager.d.ts +55 -0
  130. package/lib/manager/SubscriptionManager.d.ts.map +1 -0
  131. package/lib/manager/SubscriptionManager.js +117 -0
  132. package/lib/manager/applyManager.d.ts +10 -0
  133. package/lib/manager/applyManager.d.ts.map +1 -0
  134. package/lib/manager/applyManager.js +23 -0
  135. package/lib/manager/devtoolsTypes.d.ts +205 -0
  136. package/lib/manager/devtoolsTypes.d.ts.map +1 -0
  137. package/lib/manager/devtoolsTypes.js +2 -0
  138. package/lib/manager/index.d.ts +8 -0
  139. package/lib/manager/index.d.ts.map +1 -0
  140. package/lib/manager/index.js +6 -0
  141. package/lib/middlewareTypes.d.ts +18 -0
  142. package/lib/middlewareTypes.d.ts.map +1 -0
  143. package/lib/middlewareTypes.js +2 -0
  144. package/lib/newActions.d.ts +85 -0
  145. package/lib/newActions.d.ts.map +1 -0
  146. package/lib/newActions.js +2 -0
  147. package/lib/next/Controller.d.ts +14 -0
  148. package/lib/next/Controller.d.ts.map +1 -0
  149. package/lib/next/Controller.js +24 -0
  150. package/lib/next/index.d.ts +3 -0
  151. package/lib/next/index.d.ts.map +1 -0
  152. package/lib/next/index.js +3 -0
  153. package/lib/previousActions.d.ts +91 -0
  154. package/lib/previousActions.d.ts.map +1 -0
  155. package/lib/previousActions.js +2 -0
  156. package/lib/state/RIC.d.ts +2 -0
  157. package/lib/state/RIC.js +3 -0
  158. package/lib/state/applyUpdatersToResults.d.ts +13 -0
  159. package/lib/state/applyUpdatersToResults.d.ts.map +1 -0
  160. package/lib/state/applyUpdatersToResults.js +7 -0
  161. package/lib/state/legacy-actions/createFetch.d.ts +19 -0
  162. package/lib/state/legacy-actions/createFetch.d.ts.map +1 -0
  163. package/lib/state/legacy-actions/createFetch.js +62 -0
  164. package/lib/state/legacy-actions/createReceive.d.ts +14 -0
  165. package/lib/state/legacy-actions/createReceive.d.ts.map +1 -0
  166. package/lib/state/legacy-actions/createReceive.js +37 -0
  167. package/lib/state/legacy-actions/createReceiveError.d.ts +9 -0
  168. package/lib/state/legacy-actions/createReceiveError.d.ts.map +1 -0
  169. package/lib/state/legacy-actions/createReceiveError.js +28 -0
  170. package/lib/state/legacy-actions/index.d.ts +4 -0
  171. package/lib/state/legacy-actions/index.d.ts.map +1 -0
  172. package/lib/state/legacy-actions/index.js +4 -0
  173. package/lib/state/reducer/createReducer.d.ts +7 -0
  174. package/lib/state/reducer/createReducer.d.ts.map +1 -0
  175. package/lib/state/reducer/createReducer.js +55 -0
  176. package/lib/state/reducer/fetchReducer.d.ts +4 -0
  177. package/lib/state/reducer/fetchReducer.d.ts.map +1 -0
  178. package/lib/state/reducer/fetchReducer.js +34 -0
  179. package/lib/state/reducer/invalidateReducer.d.ts +37 -0
  180. package/lib/state/reducer/invalidateReducer.d.ts.map +1 -0
  181. package/lib/state/reducer/invalidateReducer.js +34 -0
  182. package/lib/state/reducer/setReducer.d.ts +40 -0
  183. package/lib/state/reducer/setReducer.d.ts.map +1 -0
  184. package/lib/state/reducer/setReducer.js +119 -0
  185. package/lib/state/reducerInstance.d.ts +7 -0
  186. package/lib/state/reducerInstance.d.ts.map +1 -0
  187. package/lib/state/reducerInstance.js +9 -0
  188. package/lib/state/selectMeta.d.ts +3 -0
  189. package/lib/state/selectMeta.d.ts.map +1 -0
  190. package/lib/state/selectMeta.js +4 -0
  191. package/lib/types.d.ts +71 -0
  192. package/lib/types.d.ts.map +1 -0
  193. package/lib/types.js +8 -0
  194. package/node.mjs +1 -0
  195. package/package.json +127 -0
  196. package/src/actionTypes.ts +11 -0
  197. package/src/compatibleActions.ts +96 -0
  198. package/src/controller/BaseController.ts +508 -0
  199. package/src/controller/Controller.ts +32 -0
  200. package/src/controller/__tests__/Controller.ts +29 -0
  201. package/src/controller/__tests__/__snapshots__/getResponse.ts.snap +35 -0
  202. package/src/controller/__tests__/getResponse.ts +182 -0
  203. package/src/controller/createFetch.ts +54 -0
  204. package/src/controller/createInvalidate.ts +16 -0
  205. package/src/controller/createInvalidateAll.ts +11 -0
  206. package/src/controller/createOptimistic.ts +47 -0
  207. package/src/controller/createReceive.ts +85 -0
  208. package/src/controller/createReset.ts +9 -0
  209. package/src/controller/createSubscription.ts +39 -0
  210. package/src/controller/types.ts +22 -0
  211. package/src/endpoint/index.ts +14 -0
  212. package/src/endpoint/shapes.ts +53 -0
  213. package/src/endpoint/types.ts +72 -0
  214. package/src/fsa.ts +99 -0
  215. package/src/index.ts +61 -0
  216. package/src/internal.ts +3 -0
  217. package/src/legacyActions.ts +163 -0
  218. package/src/manager/ConnectionListener.ts +7 -0
  219. package/src/manager/DefaultConnectionListener.ts +54 -0
  220. package/src/manager/DevtoolsManager.ts +99 -0
  221. package/src/manager/LogoutManager.ts +57 -0
  222. package/src/manager/NetworkManager.ts +346 -0
  223. package/src/manager/PollingSubscription.ts +190 -0
  224. package/src/manager/SubscriptionManager.ts +156 -0
  225. package/src/manager/__tests__/__snapshots__/pollingSubscription-endpoint.ts.snap +49 -0
  226. package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +43 -0
  227. package/src/manager/__tests__/logoutManager.ts +112 -0
  228. package/src/manager/__tests__/manager.ts +44 -0
  229. package/src/manager/__tests__/networkManager-legacy.ts +394 -0
  230. package/src/manager/__tests__/networkManager.ts +426 -0
  231. package/src/manager/__tests__/pollingSubscription-endpoint.ts +423 -0
  232. package/src/manager/__tests__/pollingSubscription.ts +313 -0
  233. package/src/manager/__tests__/subscriptionManager.ts +208 -0
  234. package/src/manager/applyManager.ts +33 -0
  235. package/src/manager/devtoolsTypes.ts +210 -0
  236. package/src/manager/index.ts +7 -0
  237. package/src/middlewareTypes.ts +49 -0
  238. package/src/newActions.ts +140 -0
  239. package/src/next/Controller.ts +39 -0
  240. package/src/next/index.ts +2 -0
  241. package/src/package.json +1 -0
  242. package/src/previousActions.ts +159 -0
  243. package/src/state/RIC.d.ts +2 -0
  244. package/src/state/RIC.js +5 -0
  245. package/src/state/__tests__/RIC.web.ts +16 -0
  246. package/src/state/__tests__/__snapshots__/reducer.ts.snap +56 -0
  247. package/src/state/__tests__/applyUpdatersToResults.ts +40 -0
  248. package/src/state/__tests__/reducer.ts +868 -0
  249. package/src/state/applyUpdatersToResults.ts +29 -0
  250. package/src/state/legacy-actions/createFetch.ts +95 -0
  251. package/src/state/legacy-actions/createReceive.ts +68 -0
  252. package/src/state/legacy-actions/createReceiveError.ts +43 -0
  253. package/src/state/legacy-actions/index.ts +3 -0
  254. package/src/state/reducer/createReducer.ts +80 -0
  255. package/src/state/reducer/fetchReducer.ts +48 -0
  256. package/src/state/reducer/invalidateReducer.ts +39 -0
  257. package/src/state/reducer/setReducer.ts +157 -0
  258. package/src/state/reducerInstance.ts +14 -0
  259. package/src/state/selectMeta.ts +8 -0
  260. package/src/types.ts +125 -0
  261. package/ts3.4/actionTypes.d.ts +11 -0
  262. package/ts3.4/compatibleActions.d.ts +47 -0
  263. package/ts3.4/controller/BaseController.d.ts +170 -0
  264. package/ts3.4/controller/Controller.d.ts +14 -0
  265. package/ts3.4/controller/createFetch.d.ts +14 -0
  266. package/ts3.4/controller/createInvalidate.d.ts +8 -0
  267. package/ts3.4/controller/createInvalidateAll.d.ts +3 -0
  268. package/ts3.4/controller/createOptimistic.d.ts +12 -0
  269. package/ts3.4/controller/createReceive.d.ts +24 -0
  270. package/ts3.4/controller/createReset.d.ts +3 -0
  271. package/ts3.4/controller/createSubscription.d.ts +13 -0
  272. package/ts3.4/controller/types.d.ts +6 -0
  273. package/ts3.4/endpoint/index.d.ts +3 -0
  274. package/ts3.4/endpoint/shapes.d.ts +25 -0
  275. package/ts3.4/endpoint/types.d.ts +45 -0
  276. package/ts3.4/fsa.d.ts +41 -0
  277. package/ts3.4/index.d.ts +22 -0
  278. package/ts3.4/internal.d.ts +4 -0
  279. package/ts3.4/legacyActions.d.ts +95 -0
  280. package/ts3.4/manager/ConnectionListener.d.ts +8 -0
  281. package/ts3.4/manager/DefaultConnectionListener.d.ts +20 -0
  282. package/ts3.4/manager/DevtoolsManager.d.ts +24 -0
  283. package/ts3.4/manager/LogoutManager.d.ts +25 -0
  284. package/ts3.4/manager/NetworkManager.d.ts +82 -0
  285. package/ts3.4/manager/PollingSubscription.d.ts +45 -0
  286. package/ts3.4/manager/SubscriptionManager.d.ts +55 -0
  287. package/ts3.4/manager/applyManager.d.ts +10 -0
  288. package/ts3.4/manager/devtoolsTypes.d.ts +205 -0
  289. package/ts3.4/manager/index.d.ts +8 -0
  290. package/ts3.4/middlewareTypes.d.ts +18 -0
  291. package/ts3.4/newActions.d.ts +88 -0
  292. package/ts3.4/next/Controller.d.ts +14 -0
  293. package/ts3.4/next/index.d.ts +3 -0
  294. package/ts3.4/previousActions.d.ts +94 -0
  295. package/ts3.4/state/RIC.d.ts +2 -0
  296. package/ts3.4/state/applyUpdatersToResults.d.ts +13 -0
  297. package/ts3.4/state/legacy-actions/createFetch.d.ts +19 -0
  298. package/ts3.4/state/legacy-actions/createReceive.d.ts +14 -0
  299. package/ts3.4/state/legacy-actions/createReceiveError.d.ts +9 -0
  300. package/ts3.4/state/legacy-actions/index.d.ts +4 -0
  301. package/ts3.4/state/reducer/createReducer.d.ts +7 -0
  302. package/ts3.4/state/reducer/fetchReducer.d.ts +4 -0
  303. package/ts3.4/state/reducer/invalidateReducer.d.ts +37 -0
  304. package/ts3.4/state/reducer/setReducer.d.ts +40 -0
  305. package/ts3.4/state/reducerInstance.d.ts +7 -0
  306. package/ts3.4/state/selectMeta.d.ts +3 -0
  307. package/ts3.4/types.d.ts +73 -0
  308. package/typescript.svg +8 -0
@@ -0,0 +1,182 @@
1
+ import { Endpoint, Entity } from '@data-client/endpoint';
2
+
3
+ import { ExpiryStatus } from '../..';
4
+ import { initialState } from '../../state/reducer/createReducer';
5
+ import Contoller from '../Controller';
6
+
7
+ describe('Controller.getResponse()', () => {
8
+ it('denormalizes schema with extra members but not set', () => {
9
+ const controller = new Contoller();
10
+ class Tacos extends Entity {
11
+ type = '';
12
+ id = '';
13
+ pk() {
14
+ return this.id;
15
+ }
16
+ }
17
+ const ep = new Endpoint(() => Promise.resolve(), {
18
+ key() {
19
+ return 'mytest';
20
+ },
21
+ schema: {
22
+ data: [Tacos],
23
+ extra: '',
24
+ page: {
25
+ first: null,
26
+ second: undefined,
27
+ third: 0,
28
+ complex: { complex: true, next: false },
29
+ },
30
+ },
31
+ });
32
+ const entities = {
33
+ Tacos: {
34
+ 1: { id: '1', type: 'foo' },
35
+ 2: { id: '2', type: 'bar' },
36
+ },
37
+ };
38
+
39
+ const state = {
40
+ ...initialState,
41
+ entities,
42
+ results: {
43
+ [ep.key()]: {
44
+ data: ['1', '2'],
45
+ },
46
+ },
47
+ };
48
+ const { data, expiryStatus } = controller.getResponse(ep, state);
49
+ expect(expiryStatus).toBe(ExpiryStatus.Valid);
50
+ expect(data).toMatchSnapshot();
51
+ });
52
+
53
+ it('denormalizes distinct schemas for null arg', () => {
54
+ const controller = new Contoller();
55
+ class Tacos extends Entity {
56
+ type = '';
57
+ id = '';
58
+ pk() {
59
+ return this.id;
60
+ }
61
+ }
62
+ const ep = new Endpoint(() => Promise.resolve(), {
63
+ key() {
64
+ return 'mytest';
65
+ },
66
+ schema: {
67
+ data: [Tacos],
68
+ extra: '',
69
+ page: {
70
+ first: null,
71
+ second: undefined,
72
+ third: 0,
73
+ complex: { complex: true, next: false },
74
+ },
75
+ },
76
+ });
77
+ const entities = {
78
+ Tacos: {
79
+ 1: { id: '1', type: 'foo' },
80
+ 2: { id: '2', type: 'bar' },
81
+ },
82
+ };
83
+
84
+ const state = {
85
+ ...initialState,
86
+ entities,
87
+ results: {
88
+ [ep.key()]: {
89
+ data: ['1', '2'],
90
+ },
91
+ },
92
+ };
93
+ const { data, expiryStatus } = controller.getResponse(ep, null, state);
94
+ expect(expiryStatus).toBe(ExpiryStatus.Valid);
95
+ // null args means don't fill anything in
96
+ expect(data.data).toBeUndefined();
97
+ expect(data).toMatchInlineSnapshot(`
98
+ {
99
+ "data": undefined,
100
+ "extra": "",
101
+ "page": {
102
+ "complex": {
103
+ "complex": true,
104
+ "next": false,
105
+ },
106
+ "first": null,
107
+ "second": undefined,
108
+ "third": 0,
109
+ },
110
+ }
111
+ `);
112
+ expect(controller.getResponse(ep, null, state).data).toStrictEqual(data);
113
+
114
+ const ep2 = ep.extend({ schema: { data: Tacos, nextPage: { five: '5' } } });
115
+ const data2 = controller.getResponse(ep2, null, state).data;
116
+ expect(data2.data).toBeUndefined();
117
+ expect(data2).toMatchInlineSnapshot(`
118
+ {
119
+ "data": undefined,
120
+ "nextPage": {
121
+ "five": "5",
122
+ },
123
+ }
124
+ `);
125
+ });
126
+
127
+ it('infers schema with extra members but not set', () => {
128
+ const controller = new Contoller();
129
+ class Tacos extends Entity {
130
+ type = '';
131
+ id = '';
132
+ pk() {
133
+ return this.id;
134
+ }
135
+ }
136
+ const ep = new Endpoint(({ id }: { id: string }) => Promise.resolve(), {
137
+ key({ id }) {
138
+ return `mytest ${id}`;
139
+ },
140
+ schema: {
141
+ data: Tacos,
142
+ extra: '',
143
+ page: {
144
+ first: null,
145
+ second: '',
146
+ third: 0,
147
+ complex: { complex: true, next: false },
148
+ },
149
+ },
150
+ });
151
+ const entities = {
152
+ Tacos: {
153
+ 1: { id: '1', type: 'foo' },
154
+ 2: { id: '2', type: 'bar' },
155
+ },
156
+ };
157
+
158
+ const state = {
159
+ ...initialState,
160
+ entities,
161
+ entityMeta: {
162
+ Tacos: {
163
+ 1: { date: 1000000, expiresAt: 1100000, fetchedAt: 1000000 },
164
+ 2: { date: 2000000, expiresAt: 2100000, fetchedAt: 2000000 },
165
+ },
166
+ },
167
+ };
168
+ const { data, expiryStatus, expiresAt } = controller.getResponse(
169
+ ep,
170
+ { id: '1' },
171
+ state,
172
+ );
173
+ expect(expiryStatus).toBe(ExpiryStatus.Valid);
174
+ expect(data).toMatchSnapshot();
175
+ expect(expiresAt).toBe(1100000);
176
+ // test caching
177
+ const second = controller.getResponse(ep, { id: '1' }, state);
178
+ expect(second.data.data).toBe(data.data);
179
+ expect(second.expiryStatus).toBe(expiryStatus);
180
+ expect(second.expiresAt).toBe(expiresAt);
181
+ });
182
+ });
@@ -0,0 +1,54 @@
1
+ import type { EndpointInterface } from '@data-client/normalizr';
2
+
3
+ import { EndpointUpdateFunction } from './types.js';
4
+ import { FETCH_TYPE } from '../actionTypes.js';
5
+ import type {
6
+ CompatibleFetchAction,
7
+ CompatibleFetchMeta,
8
+ } from '../compatibleActions.js';
9
+
10
+ /**
11
+ * Requesting a fetch to begin
12
+ */
13
+ export default function createFetch<
14
+ E extends EndpointInterface & { update?: EndpointUpdateFunction<E> },
15
+ >(
16
+ endpoint: E,
17
+ { args }: { args: readonly [...Parameters<E>] },
18
+ ): CompatibleFetchAction<E> {
19
+ const key = endpoint.key(...args);
20
+ let resolve: (value?: any | PromiseLike<any>) => void = 0 as any;
21
+ let reject: (reason?: any) => void = 0 as any;
22
+ const promise = new Promise<any>((a, b) => {
23
+ [resolve, reject] = [a, b];
24
+ });
25
+ const meta: CompatibleFetchMeta = {
26
+ schema: endpoint.schema,
27
+ type: endpoint.sideEffect ? ('mutate' as const) : ('read' as const),
28
+ args,
29
+ key,
30
+ throttle: !endpoint.sideEffect,
31
+ options: endpoint,
32
+ resolve,
33
+ reject,
34
+ promise,
35
+ createdAt: Date.now(),
36
+ };
37
+
38
+ if (endpoint.update) {
39
+ meta.update = endpoint.update;
40
+ }
41
+
42
+ // TODO: Remove once EOL on this deprecated piece
43
+ /* istanbul ignore if */
44
+ if (endpoint.optimisticUpdate) {
45
+ meta.optimisticResponse = endpoint.optimisticUpdate(...args);
46
+ }
47
+
48
+ return {
49
+ type: FETCH_TYPE,
50
+ payload: () => endpoint(...args) as any,
51
+ meta,
52
+ endpoint,
53
+ };
54
+ }
@@ -0,0 +1,16 @@
1
+ import type { EndpointInterface } from '@data-client/normalizr';
2
+
3
+ import { INVALIDATE_TYPE } from '../actionTypes.js';
4
+ import type { InvalidateAction } from '../newActions.js';
5
+
6
+ export default function createInvalidate<E extends EndpointInterface>(
7
+ endpoint: E,
8
+ { args }: { args: readonly [...Parameters<E>] },
9
+ ): InvalidateAction {
10
+ return {
11
+ type: INVALIDATE_TYPE,
12
+ meta: {
13
+ key: endpoint.key(...args),
14
+ },
15
+ };
16
+ }
@@ -0,0 +1,11 @@
1
+ import { INVALIDATEALL_TYPE } from '../actionTypes.js';
2
+ import type { InvalidateAllAction } from '../newActions.js';
3
+
4
+ export default function createInvalidateAll(
5
+ testKey: (key: string) => boolean,
6
+ ): InvalidateAllAction {
7
+ return {
8
+ type: INVALIDATEALL_TYPE,
9
+ testKey,
10
+ };
11
+ }
@@ -0,0 +1,47 @@
1
+ import type { EndpointInterface } from '@data-client/normalizr';
2
+
3
+ import { EndpointUpdateFunction } from './types.js';
4
+ import { OPTIMISTIC_TYPE } from '../actionTypes.js';
5
+ import { OptimisticAction } from '../newActions.js';
6
+
7
+ export default function createOptimistic<
8
+ E extends EndpointInterface & {
9
+ update?: EndpointUpdateFunction<E>;
10
+ },
11
+ >(
12
+ endpoint: E,
13
+ {
14
+ args,
15
+ fetchedAt,
16
+ }: {
17
+ args: readonly [...Parameters<E>];
18
+ fetchedAt: number;
19
+ },
20
+ ): OptimisticAction<E> {
21
+ const expiryLength: number = endpoint.dataExpiryLength ?? 60000;
22
+ /* istanbul ignore next */
23
+ if (process.env.NODE_ENV === 'development' && expiryLength < 0) {
24
+ throw new Error('Negative expiry length are not allowed.');
25
+ }
26
+ const now = Date.now();
27
+ // TODO: Use correct type once we no longer need backcompat
28
+ const meta: any = {
29
+ args,
30
+ fetchedAt,
31
+ date: now,
32
+ expiresAt: now + expiryLength,
33
+ // For legacy support; TODO: remove
34
+ schema: endpoint.schema,
35
+ key: endpoint.key(...args),
36
+ };
37
+ // For legacy support; TODO: remove
38
+ if (endpoint.update) meta.update = endpoint.update;
39
+ if (endpoint.errorPolicy) meta.errorPolicy = endpoint.errorPolicy;
40
+
41
+ const action: OptimisticAction<E> = {
42
+ type: OPTIMISTIC_TYPE,
43
+ endpoint,
44
+ meta,
45
+ };
46
+ return action;
47
+ }
@@ -0,0 +1,85 @@
1
+ import type { EndpointInterface, ResolveType } from '@data-client/normalizr';
2
+
3
+ import { EndpointUpdateFunction } from './types.js';
4
+ import { SET_TYPE } from '../actionTypes.js';
5
+ import type {
6
+ CompatibleReceiveAction,
7
+ CompatibleReceiveMeta,
8
+ } from '../compatibleActions.js';
9
+
10
+ export default function createReceive<
11
+ E extends EndpointInterface & {
12
+ update?: EndpointUpdateFunction<E>;
13
+ },
14
+ >(
15
+ endpoint: E,
16
+ options: {
17
+ args: readonly [...Parameters<E>];
18
+ response: Error;
19
+ fetchedAt?: number;
20
+ error: true;
21
+ },
22
+ ): CompatibleReceiveAction<E>;
23
+
24
+ export default function createReceive<
25
+ E extends EndpointInterface & {
26
+ update?: EndpointUpdateFunction<E>;
27
+ },
28
+ >(
29
+ endpoint: E,
30
+ options: {
31
+ args: readonly [...Parameters<E>];
32
+ response: ResolveType<E>;
33
+ fetchedAt?: number;
34
+ error?: false;
35
+ },
36
+ ): CompatibleReceiveAction<E>;
37
+
38
+ export default function createReceive<
39
+ E extends EndpointInterface & {
40
+ update?: EndpointUpdateFunction<E>;
41
+ },
42
+ >(
43
+ endpoint: E,
44
+ {
45
+ args,
46
+ fetchedAt,
47
+ response,
48
+ error = false,
49
+ }: {
50
+ args: readonly [...Parameters<E>];
51
+ response: any;
52
+ fetchedAt?: number;
53
+ error?: boolean;
54
+ },
55
+ ): CompatibleReceiveAction<E> {
56
+ const expiryLength: number = error
57
+ ? endpoint.errorExpiryLength ?? 1000
58
+ : endpoint.dataExpiryLength ?? 60000;
59
+ /* istanbul ignore next */
60
+ if (process.env.NODE_ENV === 'development' && expiryLength < 0) {
61
+ throw new Error('Negative expiry length are not allowed.');
62
+ }
63
+ const now = Date.now();
64
+ const meta: CompatibleReceiveMeta = {
65
+ args,
66
+ fetchedAt: fetchedAt ?? now,
67
+ date: now,
68
+ expiresAt: now + expiryLength,
69
+ // For legacy support; TODO: remove
70
+ schema: endpoint.schema,
71
+ key: endpoint.key(...args),
72
+ };
73
+ // For legacy support; TODO: remove
74
+ if (endpoint.update) meta.update = endpoint.update;
75
+ if (endpoint.errorPolicy) meta.errorPolicy = endpoint.errorPolicy;
76
+
77
+ const action: CompatibleReceiveAction<E> = {
78
+ type: SET_TYPE,
79
+ payload: response,
80
+ endpoint: endpoint,
81
+ meta,
82
+ };
83
+ if (error) (action as any).error = true;
84
+ return action;
85
+ }
@@ -0,0 +1,9 @@
1
+ import { RESET_TYPE } from '../actionTypes.js';
2
+ import type { ResetAction } from '../newActions.js';
3
+
4
+ export default function createReset(): ResetAction {
5
+ return {
6
+ type: RESET_TYPE,
7
+ date: Date.now(),
8
+ };
9
+ }
@@ -0,0 +1,39 @@
1
+ import type { EndpointInterface } from '@data-client/normalizr';
2
+
3
+ import { SUBSCRIBE_TYPE, UNSUBSCRIBE_TYPE } from '../actionTypes.js';
4
+ import type {
5
+ CompatibleSubscribeAction,
6
+ CompatibleUnsubscribeAction,
7
+ } from '../compatibleActions.js';
8
+
9
+ export function createSubscription<E extends EndpointInterface>(
10
+ endpoint: E,
11
+ { args }: { args: readonly [...Parameters<E>] },
12
+ ): CompatibleSubscribeAction<E> {
13
+ return {
14
+ type: SUBSCRIBE_TYPE,
15
+ endpoint,
16
+ meta: {
17
+ args,
18
+ key: endpoint.key(...args),
19
+ fetch: () => endpoint(...args),
20
+ schema: endpoint.schema,
21
+ options: endpoint,
22
+ },
23
+ };
24
+ }
25
+
26
+ export function createUnsubscription<E extends EndpointInterface>(
27
+ endpoint: E,
28
+ { args }: { args: readonly [...Parameters<E>] },
29
+ ): CompatibleUnsubscribeAction<E> {
30
+ return {
31
+ type: UNSUBSCRIBE_TYPE,
32
+ endpoint,
33
+ meta: {
34
+ args,
35
+ key: endpoint.key(...args),
36
+ options: endpoint,
37
+ },
38
+ };
39
+ }
@@ -0,0 +1,22 @@
1
+ import type {
2
+ Normalize,
3
+ EndpointInterface,
4
+ ResolveType,
5
+ } from '@data-client/normalizr';
6
+
7
+ export type ResultEntry<E extends EndpointInterface> = E['schema'] extends
8
+ | undefined
9
+ | null
10
+ ? ResolveType<E>
11
+ : Normalize<E['schema']>;
12
+
13
+ export type EndpointUpdateFunction<
14
+ Source extends EndpointInterface,
15
+ Updaters extends Record<string, any> = Record<string, any>,
16
+ > = (
17
+ source: ResultEntry<Source>,
18
+ // this is hopeless because typescript doesn't think it should be contravariant
19
+ ...args: any
20
+ ) => {
21
+ [K in keyof Updaters]: (result: Updaters[K]) => Updaters[K];
22
+ };
@@ -0,0 +1,14 @@
1
+ export type {
2
+ FetchShape,
3
+ ReadShape,
4
+ MutateShape,
5
+ DeleteShape,
6
+ } from './shapes.js';
7
+ export type {
8
+ SetShapeParams,
9
+ ParamsFromShape,
10
+ OptimisticUpdateParams,
11
+ SchemaFromShape,
12
+ BodyFromShape,
13
+ ReturnFromShape,
14
+ } from './types.js';
@@ -0,0 +1,53 @@
1
+ import {
2
+ Schema,
3
+ EndpointExtraOptions as FetchOptions,
4
+ } from '@data-client/normalizr';
5
+
6
+ /** Defines the shape of a network request */
7
+ export interface FetchShape<
8
+ S extends Schema | undefined,
9
+ Params extends Readonly<object> = Readonly<object>,
10
+ Body extends Readonly<object | string> | void | unknown =
11
+ | Readonly<object | string>
12
+ | undefined,
13
+ Response = any,
14
+ > {
15
+ readonly type: 'read' | 'mutate' | 'delete';
16
+ fetch(params: Params, body?: Body): Promise<Response>;
17
+ getFetchKey(params: Params): string;
18
+ readonly schema: S;
19
+ readonly options?: FetchOptions;
20
+ }
21
+
22
+ /** To change values on the server */
23
+ export interface MutateShape<
24
+ S extends Schema | undefined,
25
+ Params extends Readonly<object> = Readonly<object>,
26
+ Body extends Readonly<object | string> | void | unknown =
27
+ | Readonly<object | string>
28
+ | undefined,
29
+ Response extends object | string | number | boolean | null = any,
30
+ > extends FetchShape<S, Params, Body, Response> {
31
+ readonly type: 'mutate';
32
+ fetch(params: Params, body: Body): Promise<Response>;
33
+ }
34
+
35
+ /** Removes entities */
36
+ export interface DeleteShape<
37
+ S extends Schema | undefined,
38
+ Params extends Readonly<object> = Readonly<object>,
39
+ Response extends object | string | number | boolean | null = any,
40
+ > extends FetchShape<S, Params, undefined, Response> {
41
+ readonly type: 'mutate';
42
+ fetch(params: Params, ...args: any): Promise<Response>;
43
+ }
44
+
45
+ /** For retrieval requests */
46
+ export interface ReadShape<
47
+ S extends Schema | undefined,
48
+ Params extends Readonly<object> = Readonly<object>,
49
+ Response extends object | string | number | boolean | null = any,
50
+ > extends FetchShape<S, Params, undefined, Response> {
51
+ readonly type: 'read';
52
+ fetch(params: Params): Promise<Response>;
53
+ }
@@ -0,0 +1,72 @@
1
+ import { Schema } from '@data-client/normalizr';
2
+ import type { Denormalize } from '@data-client/normalizr';
3
+
4
+ import { FetchShape } from './shapes.js';
5
+ import { UpdateFunction } from '../types.js';
6
+
7
+ export type ResultShape<RS> = RS extends { schema: infer U } ? U : never;
8
+ export type SelectReturn<RS> = RS extends {
9
+ select: (...args: any[]) => infer U;
10
+ }
11
+ ? U
12
+ : never;
13
+ export type AlwaysSelect<RS> = NonNullable<SelectReturn<RS>>;
14
+ export type ParamArg<RS> = RS extends {
15
+ getFetchKey: (params: infer U) => any;
16
+ }
17
+ ? U
18
+ : never;
19
+ export type BodyArg<RS> = RS extends {
20
+ fetch: (url: any, body: infer U) => any;
21
+ }
22
+ ? U
23
+ : never;
24
+
25
+ /** Sets a FetchShape's Param type.
26
+ * Useful to constrain acceptable params (second arg) in hooks like useResource().
27
+ *
28
+ * @param [Shape] FetchShape to act upon
29
+ * @param [Params] what to set the Params to
30
+ */
31
+ export type SetShapeParams<
32
+ Shape extends FetchShape<any, any, any>,
33
+ Params extends Readonly<object>,
34
+ > = {
35
+ [K in keyof Shape]: Shape[K];
36
+ } & (Shape['fetch'] extends (first: any, ...rest: infer Args) => infer Return
37
+ ? { fetch: (first: Params, ...rest: Args) => Return }
38
+ : never);
39
+
40
+ /** Get the Params type for a given Shape */
41
+ export type ParamsFromShape<S> = S extends {
42
+ fetch: (first: infer A, ...rest: any) => any;
43
+ }
44
+ ? A
45
+ : S extends { getFetchKey: (first: infer A, ...rest: any) => any }
46
+ ? A
47
+ : never;
48
+
49
+ /** Get the Schema type for a given Shape */
50
+ export type SchemaFromShape<
51
+ F extends FetchShape<Schema | undefined, any, any>,
52
+ > = F['schema'];
53
+
54
+ /** Get the Body type for a given Shape */
55
+ export type BodyFromShape<F extends FetchShape<any, any, any>> = Parameters<
56
+ F['fetch']
57
+ >[1];
58
+
59
+ export type OptimisticUpdateParams<
60
+ SourceSchema extends Schema | undefined,
61
+ DestShape extends FetchShape<any, any, any>,
62
+ > = [
63
+ DestShape,
64
+ ParamsFromShape<DestShape>,
65
+ UpdateFunction<SourceSchema, SchemaFromShape<DestShape>>,
66
+ ];
67
+
68
+ export type ReturnFromShape<S extends FetchShape<any, any, any>> = ReturnType<
69
+ S['fetch']
70
+ > extends unknown
71
+ ? Promise<Denormalize<S['schema']>>
72
+ : ReturnType<S['fetch']>;