@data-client/core 0.11.5 → 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.
@@ -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
  ]): {