@data-client/core 0.11.5 → 0.12.3

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();
@@ -266,7 +267,7 @@ export default class Controller<
266
267
  args[0] !== null ?
267
268
  this.dispatch(
268
269
  createSubscription(endpoint, {
269
- args: args as readonly [...Parameters<E>],
270
+ args: args as Parameters<E>,
270
271
  }),
271
272
  )
272
273
  : Promise.resolve();
@@ -288,7 +289,7 @@ export default class Controller<
288
289
  args[0] !== null ?
289
290
  this.dispatch(
290
291
  createUnsubscription(endpoint, {
291
- args: args as readonly [...Parameters<E>],
292
+ args: args as Parameters<E>,
292
293
  }),
293
294
  )
294
295
  : 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;
@@ -557,10 +562,12 @@ function schemaHasEntity(schema: Schema): boolean {
557
562
  export type { ErrorTypes };
558
563
 
559
564
  class Snapshot<T = unknown> implements SnapshotInterface {
565
+ static readonly abort = new AbortOptimistic();
566
+
560
567
  private state: State<T>;
561
568
  private controller: Controller;
562
569
  readonly fetchedAt: number;
563
- readonly abort = new AbortOptimistic();
570
+ readonly abort = Snapshot.abort;
564
571
 
565
572
  constructor(controller: Controller, state: State<T>, fetchedAt = 0) {
566
573
  this.state = state;
@@ -613,15 +620,22 @@ class Snapshot<T = unknown> implements SnapshotInterface {
613
620
  }
614
621
 
615
622
  /** @see https://dataclient.io/docs/api/Snapshot#getError */
616
- getError = <
617
- E extends Pick<EndpointInterface, 'key'>,
618
- Args extends readonly [...Parameters<E['key']>],
619
- >(
623
+ getError<E extends EndpointInterface>(
624
+ endpoint: E,
625
+ ...args: readonly [...Parameters<E>] | readonly [null]
626
+ ): ErrorTypes | undefined;
627
+
628
+ getError<E extends Pick<EndpointInterface, 'key'>>(
620
629
  endpoint: E,
621
- ...args: Args
622
- ): ErrorTypes | undefined => {
630
+ ...args: readonly [...Parameters<E['key']>] | readonly [null]
631
+ ): ErrorTypes | undefined;
632
+
633
+ getError<E extends Pick<EndpointInterface, 'key'>>(
634
+ endpoint: E,
635
+ ...args: readonly [...Parameters<E['key']>] | readonly [null]
636
+ ): ErrorTypes | undefined {
623
637
  return this.controller.getError(endpoint, ...args, this.state);
624
- };
638
+ }
625
639
 
626
640
  /**
627
641
  * 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();
@@ -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
  ]): {