@data-client/rest 0.1.0 → 0.2.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 (65) hide show
  1. package/dist/index.js +10 -14
  2. package/dist/index.umd.min.js +1 -1
  3. package/legacy/OptionsToFunction.js +1 -1
  4. package/legacy/RestEndpoint.js +5 -5
  5. package/legacy/createResource.js +8 -10
  6. package/legacy/index.js +2 -1
  7. package/legacy/next/index.js +4 -4
  8. package/legacy/resourceTypes.js +2 -0
  9. package/lib/OptionsToFunction.d.ts.map +1 -1
  10. package/lib/OptionsToFunction.js +1 -1
  11. package/lib/RestEndpoint.d.ts +29 -35
  12. package/lib/RestEndpoint.js +5 -5
  13. package/lib/createResource.d.ts +51 -31
  14. package/lib/createResource.d.ts.map +1 -1
  15. package/lib/createResource.js +7 -11
  16. package/lib/index.d.ts +3 -1
  17. package/lib/index.d.ts.map +1 -1
  18. package/lib/index.js +2 -1
  19. package/lib/next/index.d.ts +5 -5
  20. package/lib/next/index.d.ts.map +1 -1
  21. package/lib/next/index.js +4 -4
  22. package/lib/resourceTypes.d.ts.map +1 -0
  23. package/lib/resourceTypes.js +2 -0
  24. package/package.json +3 -3
  25. package/src/OptionsToFunction.ts +0 -1
  26. package/src/RestEndpoint.d.ts +29 -35
  27. package/src/RestEndpoint.js +4 -4
  28. package/src/__tests__/RestEndpoint.ts +38 -9
  29. package/src/__tests__/createResource.test.ts +3 -4
  30. package/src/__tests__/hookifyResource.ts +7 -3
  31. package/src/__tests__/types.test.ts +332 -212
  32. package/src/createResource.ts +81 -60
  33. package/src/index.ts +2 -2
  34. package/src/next/index.ts +5 -5
  35. package/ts4.0/RestEndpoint.d.ts +18 -32
  36. package/ts4.0/createResource.d.ts +59 -31
  37. package/ts4.0/index.d.ts +3 -1
  38. package/ts4.0/next/index.d.ts +5 -5
  39. package/legacy/next/OptionsToFunction.js +0 -2
  40. package/legacy/next/RestEndpoint.js +0 -225
  41. package/legacy/next/createResource.js +0 -86
  42. package/legacy/next/resourceTypes.js +0 -2
  43. package/lib/next/OptionsToFunction.d.ts +0 -7
  44. package/lib/next/OptionsToFunction.d.ts.map +0 -1
  45. package/lib/next/OptionsToFunction.js +0 -2
  46. package/lib/next/RestEndpoint.d.ts +0 -542
  47. package/lib/next/RestEndpoint.js +0 -229
  48. package/lib/next/createResource.d.ts +0 -86
  49. package/lib/next/createResource.d.ts.map +0 -1
  50. package/lib/next/createResource.js +0 -100
  51. package/lib/next/resourceTypes.d.ts.map +0 -1
  52. package/lib/next/resourceTypes.js +0 -2
  53. package/src/next/OptionsToFunction.ts +0 -41
  54. package/src/next/RestEndpoint.d.ts +0 -542
  55. package/src/next/RestEndpoint.js +0 -262
  56. package/src/next/__tests__/RestEndpoint.ts +0 -1333
  57. package/src/next/__tests__/__snapshots__/RestEndpoint.ts.snap +0 -18
  58. package/src/next/__tests__/types.test.ts +0 -429
  59. package/src/next/createResource.ts +0 -194
  60. package/ts4.0/next/OptionsToFunction.d.ts +0 -16
  61. package/ts4.0/next/RestEndpoint.d.ts +0 -226
  62. package/ts4.0/next/createResource.d.ts +0 -94
  63. /package/lib/{next/resourceTypes.d.ts → resourceTypes.d.ts} +0 -0
  64. /package/src/{next/resourceTypes.ts → resourceTypes.ts} +0 -0
  65. /package/ts4.0/{next/resourceTypes.d.ts → resourceTypes.d.ts} +0 -0
@@ -18,13 +18,13 @@ export default class RestEndpoint extends Endpoint {
18
18
  constructor(options) {
19
19
  super(
20
20
  options.fetch ??
21
- function (...args) {
21
+ async function (...args) {
22
22
  const urlParams =
23
23
  this.#hasBody && args.length < 2 ? {} : args[0] || {};
24
24
  const body = this.#hasBody ? args[args.length - 1] : undefined;
25
25
  return this.fetchResponse(
26
26
  this.url(urlParams),
27
- this.getRequestInit(body),
27
+ await this.getRequestInit(body),
28
28
  )
29
29
  .then(response => this.parseResponse(response))
30
30
  .then(res => this.process(res, ...args));
@@ -87,7 +87,7 @@ export default class RestEndpoint extends Endpoint {
87
87
  }
88
88
 
89
89
  /** Init options for fetch - run at fetch */
90
- getRequestInit(body) {
90
+ async getRequestInit(body) {
91
91
  const bodyIsPojo = isPojo(body);
92
92
  if (bodyIsPojo) {
93
93
  body = JSON.stringify(body);
@@ -105,7 +105,7 @@ export default class RestEndpoint extends Endpoint {
105
105
  ...init.headers,
106
106
  };
107
107
  }
108
- init.headers = this.getHeaders(init.headers);
108
+ init.headers = await this.getHeaders(init.headers);
109
109
  return init;
110
110
  }
111
111
 
@@ -2,7 +2,6 @@ import { Entity } from '@data-client/endpoint';
2
2
  import { useController } from '@data-client/react';
3
3
  import { useSuspense } from '@data-client/react';
4
4
  import { CacheProvider } from '@data-client/react';
5
- import { act } from '@testing-library/react-hooks';
6
5
  import { Article, CoolerArticle, CoolerArticleResource } from '__tests__/new';
7
6
  import nock from 'nock';
8
7
 
@@ -733,9 +732,12 @@ describe('RestEndpoint', () => {
733
732
  }
734
733
  `);
735
734
 
736
- const newBody = getUser.extend({
737
- body: {} as { title: string },
738
- });
735
+ const newBody = getUser
736
+ .extend({
737
+ body: {} as { title: string },
738
+ dataExpiryLength: 0,
739
+ })
740
+ .extend({ dataExpiryLength: 5 });
739
741
  () => newBody({ group: 'hi', id: 'what' }, { title: 'cool' });
740
742
  // @ts-expect-error
741
743
  () => newBody({ id: 'what' }, { title: 'cool' });
@@ -746,11 +748,12 @@ describe('RestEndpoint', () => {
746
748
  // @ts-expect-error
747
749
  () => newBody({ group: 'hi', id: 'what' }, { sdfsd: 'cool' });
748
750
 
749
- const bodyNoParams = newBody
750
- .extend({
751
- path: '/',
752
- })
753
- .extend({ body: {} as { happy: string } });
751
+ const bodyNoPath = newBody.extend({
752
+ path: '/',
753
+ });
754
+ const bodyNoParams = bodyNoPath.extend({
755
+ body: {} as { happy: string },
756
+ });
754
757
  () => bodyNoParams({ happy: 'cool' });
755
758
  // @ts-expect-error
756
759
  () => bodyNoParams({ group: 'hi', id: 'what' }, { happy: 'cool' });
@@ -1015,6 +1018,28 @@ describe('RestEndpoint', () => {
1015
1018
  params.id;
1016
1019
  },
1017
1020
  });
1021
+
1022
+ const endpoint2 = new RestEndpoint({
1023
+ sideEffect: true,
1024
+ path: 'http\\://test.com/article-cooler/:id',
1025
+ body: 0 as any,
1026
+ schema: CoolerArticle,
1027
+ getOptimisticResponse(snap, params, body) {
1028
+ params.id;
1029
+ // @ts-expect-error
1030
+ params.two;
1031
+
1032
+ body.hi;
1033
+ },
1034
+ }).extend({
1035
+ getOptimisticResponse(snap, params, body) {
1036
+ params.id;
1037
+ // @ts-expect-error
1038
+ params.two;
1039
+
1040
+ body.hi;
1041
+ },
1042
+ });
1018
1043
  });
1019
1044
  });
1020
1045
 
@@ -1292,6 +1317,10 @@ describe('RestEndpoint.fetch()', () => {
1292
1317
  });
1293
1318
  expect(res).toEqual({ id, title: 'hi' });
1294
1319
  });
1320
+
1321
+ it('without Collection in schema - endpoint.push schema should be null', () => {
1322
+ expect(getArticleList2.push.schema).toBeFalsy();
1323
+ });
1295
1324
  });
1296
1325
  const proto = Object.prototype;
1297
1326
  const gpo = Object.getPrototypeOf;
@@ -187,7 +187,7 @@ describe('createResource()', () => {
187
187
  ...body,
188
188
  }));
189
189
 
190
- const { result, waitForNextUpdate } = renderRestHook(() => {
190
+ const { result } = renderRestHook(() => {
191
191
  return [
192
192
  useCache(UserResource.get, { group: 'five', id: '5' }),
193
193
  useController(),
@@ -195,8 +195,8 @@ describe('createResource()', () => {
195
195
  });
196
196
  // eslint-disable-next-line prefer-const
197
197
  let [_, controller] = result.current;
198
- await act(() => {
199
- controller.fetch(
198
+ await act(async () => {
199
+ await controller.fetch(
200
200
  UserResource.create,
201
201
  { group: 'five' },
202
202
  {
@@ -205,7 +205,6 @@ describe('createResource()', () => {
205
205
  },
206
206
  );
207
207
  });
208
- await waitForNextUpdate();
209
208
  const user = result.current[0];
210
209
  expect(user).toBeDefined();
211
210
  expect(user?.username).toBe('createduser');
@@ -150,7 +150,7 @@ describe('hookifyResource()', () => {
150
150
  const res = await result.current.fetch(result.current.endpoint, {
151
151
  id: payload.id,
152
152
  });
153
- expect(res).toEqual({ id: 5 });
153
+ expect(res).toEqual(CoolerArticle.fromJS({ id: 5 }));
154
154
  });
155
155
 
156
156
  it('useUpdate', async () => {
@@ -166,7 +166,9 @@ describe('hookifyResource()', () => {
166
166
  ...CoolerArticle.fromJS(payload),
167
167
  },
168
168
  );
169
- expect(res).toEqual(putResponseBody);
169
+ expect(res).toEqual(
170
+ result.current.endpoint.schema.fromJS(putResponseBody),
171
+ );
170
172
  });
171
173
 
172
174
  it('usePartialUpdate', async () => {
@@ -180,7 +182,9 @@ describe('hookifyResource()', () => {
180
182
  { id },
181
183
  patchPayload,
182
184
  );
183
- expect(res).toEqual(patchResponseBody);
185
+ expect(res).toEqual(
186
+ result.current.endpoint.schema.fromJS(patchResponseBody),
187
+ );
184
188
  });
185
189
 
186
190
  it('should use useFetchInit if defined (in endpoint method)', async () => {