@data-client/core 0.11.4 → 0.12.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.
@@ -5,6 +5,7 @@ import type {
5
5
  Denormalize,
6
6
  Queryable,
7
7
  SchemaArgs,
8
+ NI,
8
9
  } from '@data-client/normalizr';
9
10
  import {
10
11
  ExpiryStatus,
@@ -154,7 +155,7 @@ export default class Controller<
154
155
  args[0] !== null ?
155
156
  this.dispatch(
156
157
  createInvalidate(endpoint, {
157
- args: args as readonly [...Parameters<E>],
158
+ args: args as Parameters<E>,
158
159
  }),
159
160
  )
160
161
  : Promise.resolve();
@@ -314,17 +315,28 @@ export default class Controller<
314
315
  * Gets the error, if any, for a given endpoint. Returns undefined for no errors.
315
316
  * @see https://dataclient.io/docs/api/Controller#getError
316
317
  */
317
- getError = <
318
- E extends Pick<EndpointInterface, 'key'>,
319
- Args extends readonly [...Parameters<E['key']>] | readonly [null],
320
- >(
318
+ getError<E extends EndpointInterface>(
319
+ endpoint: E,
320
+ ...rest:
321
+ | readonly [null, State<unknown>]
322
+ | readonly [...Parameters<E>, State<unknown>]
323
+ ): ErrorTypes | undefined;
324
+
325
+ getError<E extends Pick<EndpointInterface, 'key'>>(
321
326
  endpoint: E,
322
- ...rest: [...Args, State<unknown>]
323
- ): ErrorTypes | undefined => {
327
+ ...rest:
328
+ | readonly [null, State<unknown>]
329
+ | readonly [...Parameters<E['key']>, State<unknown>]
330
+ ): ErrorTypes | undefined;
331
+
332
+ getError(
333
+ endpoint: EndpointInterface,
334
+ ...rest: readonly [...unknown[], State<unknown>]
335
+ ): ErrorTypes | undefined {
324
336
  if (rest[0] === null) return;
325
337
  const state = rest[rest.length - 1] as State<unknown>;
326
338
  // this is typescript generics breaking
327
- const args: any = rest.slice(0, rest.length - 1) as Parameters<E['key']>;
339
+ const args: any = rest.slice(0, rest.length - 1);
328
340
  const key = endpoint.key(...args);
329
341
 
330
342
  const meta = selectMeta(state, key);
@@ -333,7 +345,7 @@ export default class Controller<
333
345
  if (error !== undefined && meta?.errorPolicy === 'soft') return;
334
346
 
335
347
  return meta?.error as any;
336
- };
348
+ }
337
349
 
338
350
  /**
339
351
  * Gets the (globally referentially stable) response for a given endpoint/args pair from state given.
@@ -341,16 +353,9 @@ export default class Controller<
341
353
  */
342
354
  getResponse<E extends EndpointInterface>(
343
355
  endpoint: E,
344
- ...rest: readonly [null, State<unknown>]
345
- ): {
346
- data: DenormalizeNullable<E['schema']>;
347
- expiryStatus: ExpiryStatus;
348
- expiresAt: number;
349
- };
350
-
351
- getResponse<E extends EndpointInterface>(
352
- endpoint: E,
353
- ...rest: readonly [...Parameters<E>, State<unknown>]
356
+ ...rest:
357
+ | readonly [null, State<unknown>]
358
+ | readonly [...Parameters<E>, State<unknown>]
354
359
  ): {
355
360
  data: DenormalizeNullable<E['schema']>;
356
361
  expiryStatus: ExpiryStatus;
@@ -613,15 +618,22 @@ class Snapshot<T = unknown> implements SnapshotInterface {
613
618
  }
614
619
 
615
620
  /** @see https://dataclient.io/docs/api/Snapshot#getError */
616
- getError = <
617
- E extends Pick<EndpointInterface, 'key'>,
618
- Args extends readonly [...Parameters<E['key']>],
619
- >(
621
+ getError<E extends EndpointInterface>(
622
+ endpoint: E,
623
+ ...args: readonly [...Parameters<E>] | readonly [null]
624
+ ): ErrorTypes | undefined;
625
+
626
+ getError<E extends Pick<EndpointInterface, 'key'>>(
620
627
  endpoint: E,
621
- ...args: Args
622
- ): ErrorTypes | undefined => {
628
+ ...args: readonly [...Parameters<E['key']>] | readonly [null]
629
+ ): ErrorTypes | undefined;
630
+
631
+ getError<E extends Pick<EndpointInterface, 'key'>>(
632
+ endpoint: E,
633
+ ...args: readonly [...Parameters<E['key']>] | readonly [null]
634
+ ): ErrorTypes | undefined {
623
635
  return this.controller.getError(endpoint, ...args, this.state);
624
- };
636
+ }
625
637
 
626
638
  /**
627
639
  * Retrieved memoized value for any Querable schema
@@ -1,4 +1,4 @@
1
- import type { EndpointInterface } from '@data-client/normalizr';
1
+ import type { EndpointInterface, NI } from '@data-client/normalizr';
2
2
 
3
3
  import { EndpointUpdateFunction } from './types.js';
4
4
  import { FETCH_TYPE } from '../actionTypes.js';
@@ -19,7 +19,7 @@ export default function createFetch<
19
19
  const promise = new Promise<any>((a, b) => {
20
20
  [resolve, reject] = [a, b];
21
21
  });
22
- const meta: FetchMeta = {
22
+ const meta: FetchMeta<typeof args> = {
23
23
  args,
24
24
  key,
25
25
  throttle: !endpoint.sideEffect,
@@ -71,7 +71,7 @@ export default class NetworkManager implements Manager {
71
71
  if (error) {
72
72
  this.handleSet(
73
73
  createSet(action.endpoint, {
74
- args: action.meta.args as any,
74
+ args: action.meta.args,
75
75
  response: error,
76
76
  fetchedAt: action.meta.fetchedAt,
77
77
  error: true,
@@ -192,7 +192,7 @@ export default class NetworkManager implements Manager {
192
192
  // don't update state with promises started before last clear
193
193
  if (createdAt >= lastReset) {
194
194
  this.controller.resolve(action.endpoint, {
195
- args: action.meta.args as any,
195
+ args: action.meta.args,
196
196
  response: data,
197
197
  fetchedAt: createdAt,
198
198
  });
@@ -204,7 +204,7 @@ export default class NetworkManager implements Manager {
204
204
  // don't update state with promises started before last clear
205
205
  if (createdAt >= lastReset) {
206
206
  this.controller.resolve(action.endpoint, {
207
- args: action.meta.args as any,
207
+ args: action.meta.args,
208
208
  response: error,
209
209
  fetchedAt: createdAt,
210
210
  error: true,
@@ -54,6 +54,7 @@ describe('NetworkManager', () => {
54
54
  (v: { id: number }) => Promise.resolve({ id: 5, title: 'hi' }),
55
55
  {
56
56
  schema: Article,
57
+ name: 'detailEndpoint',
57
58
  },
58
59
  );
59
60
  const fetchResolveAction = createFetch(detailEndpoint, {
@@ -288,7 +288,9 @@ describe('PollingSubscription', () => {
288
288
  afterEach(() => {
289
289
  warnSpy.mockRestore();
290
290
  });
291
- beforeEach(() => (warnSpy = jest.spyOn(console, 'warn')));
291
+ beforeEach(() =>
292
+ (warnSpy = jest.spyOn(console, 'warn')).mockImplementation(() => {}),
293
+ );
292
294
 
293
295
  it('should stop all timers', () => {
294
296
  dispatch.mockClear();
package/src/state/RIC.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  const RIC: (cb: (...args: any[]) => void, options: any) => void = (
2
2
  typeof requestIdleCallback === 'function' ? requestIdleCallback : (
3
- (cb: any) => setTimeout(cb, 0)
3
+ (cb: any) => cb()
4
4
  )) as any;
5
5
  export default RIC;
@@ -29,8 +29,8 @@ export interface SetActionError<E extends EndpointAndUpdate<E> = EndpointDefault
29
29
  error: true;
30
30
  }
31
31
  export type SetAction<E extends EndpointAndUpdate<E> = EndpointDefault> = SetActionSuccess<E> | SetActionError<E>;
32
- export interface FetchMeta {
33
- args: readonly any[];
32
+ export interface FetchMeta<A extends readonly any[] = readonly any[]> {
33
+ args: A;
34
34
  key: string;
35
35
  throttle: boolean;
36
36
  resolve: (value?: any | PromiseLike<any>) => void;
@@ -42,7 +42,9 @@ export interface FetchMeta {
42
42
  export interface FetchAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
43
43
  type: typeof FETCH_TYPE;
44
44
  endpoint: E;
45
- meta: FetchMeta;
45
+ meta: FetchMeta<readonly [
46
+ ...Parameters<E>
47
+ ]>;
46
48
  payload: () => ReturnType<E>;
47
49
  }
48
50
  export interface OptimisticAction<E extends EndpointAndUpdate<E> = EndpointDefault> {
@@ -147,14 +147,20 @@ export default class Controller<D extends GenericDispatch = DataClientDispatch>
147
147
  * Gets the error, if any, for a given endpoint. Returns undefined for no errors.
148
148
  * @see https://dataclient.io/docs/api/Controller#getError
149
149
  */
150
- getError: <E extends Pick<EndpointInterface<FetchFunction, Schema | undefined, boolean | undefined>, "key">, Args extends readonly [
151
- null
150
+ getError<E extends EndpointInterface>(endpoint: E, ...rest: readonly [
151
+ null,
152
+ State<unknown>
153
+ ] | readonly [
154
+ ...Parameters<E>,
155
+ State<unknown>
156
+ ]): ErrorTypes | undefined;
157
+ getError<E extends Pick<EndpointInterface, 'key'>>(endpoint: E, ...rest: readonly [
158
+ null,
159
+ State<unknown>
152
160
  ] | readonly [
153
- ...Parameters<E["key"]>
154
- ]>(endpoint: E, ...rest: [
155
- ...Args,
161
+ ...Parameters<E['key']>,
156
162
  State<unknown>
157
- ]) => ErrorTypes | undefined;
163
+ ]): ErrorTypes | undefined;
158
164
  /**
159
165
  * Gets the (globally referentially stable) response for a given endpoint/args pair from state given.
160
166
  * @see https://dataclient.io/docs/api/Controller#getResponse
@@ -162,12 +168,7 @@ export default class Controller<D extends GenericDispatch = DataClientDispatch>
162
168
  getResponse<E extends EndpointInterface>(endpoint: E, ...rest: readonly [
163
169
  null,
164
170
  State<unknown>
165
- ]): {
166
- data: DenormalizeNullable<E['schema']>;
167
- expiryStatus: ExpiryStatus;
168
- expiresAt: number;
169
- };
170
- getResponse<E extends EndpointInterface>(endpoint: E, ...rest: readonly [
171
+ ] | readonly [
171
172
  ...Parameters<E>,
172
173
  State<unknown>
173
174
  ]): {