resourcerer 2.0.4 → 2.0.6

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.
@@ -9,22 +9,22 @@ type CSetOptions = {
9
9
  } & SetOptions & ConstructorOptions;
10
10
  type ModelArg<A extends Record<string, any>, O extends Record<string, any>> = Model<A, O> | Record<string, any>;
11
11
  type comparator = string | ((arg: Model) => number | string) | ((arg1: Model, arg2?: Model) => number | undefined);
12
- export default class Collection<T extends Record<string, any> = object, O extends Record<string, any> = object> extends Events {
12
+ export default class Collection<T extends Record<string, any> = object, O extends Record<string, any> = object, M extends typeof Model<T, O> = typeof Model<T, O>> extends Events {
13
13
  lazy?: boolean;
14
14
  refetching?: boolean;
15
15
  measure?: boolean | ((config: ResourceConfigObj) => boolean);
16
16
  isEmptyModel?: boolean;
17
- Model: typeof Model<T, O>;
17
+ Model: M;
18
18
  comparator?: comparator;
19
- models: InstanceType<this["Model"]>[];
19
+ models: InstanceType<M>[];
20
20
  length: number;
21
- _byId: Record<string, Model<T, O>>;
21
+ _byId: Record<string, InstanceType<M>>;
22
22
  constructor(models?: ModelArg<T, O> | ModelArg<T, O>[], options?: O & {
23
- Model?: typeof Model<T, O>;
23
+ Model?: M;
24
24
  comparator?: comparator;
25
25
  } & CSetOptions);
26
26
  urlOptions: Record<string, any>;
27
- static Model: typeof Model;
27
+ static Model: new (...args: any[]) => Model;
28
28
  static idAttribute: string;
29
29
  static dependencies: Array<string | ((attrs: Record<string, any>) => Record<string, any>)>;
30
30
  static cacheTimeout: number;
@@ -40,68 +40,25 @@ export default class Collection<T extends Record<string, any> = object, O extend
40
40
  has(obj: Model<T, O>["id"] | ModelArg<T, O>): boolean;
41
41
  at(index: number): InstanceType<this["Model"]> | undefined;
42
42
  map(predicate: (model: InstanceType<this["Model"]>) => any): any[];
43
- find(predicate: (model: InstanceType<this["Model"]>) => boolean): InstanceType<this["Model"]> | undefined;
44
- filter(predicate: (model: InstanceType<this["Model"]>) => boolean): InstanceType<this["Model"]>[];
43
+ find(predicate: (model: InstanceType<this["Model"]>) => boolean): InstanceType<M> | undefined;
44
+ filter(predicate: (model: InstanceType<this["Model"]>) => boolean): InstanceType<M>[];
45
45
  findWhere(attrs: Partial<T>): InstanceType<this["Model"]> | undefined;
46
46
  where<B extends boolean = false>(attrs: Partial<T>, first?: B): B extends true ? InstanceType<this["Model"]> | undefined : InstanceType<this["Model"]>[];
47
47
  pluck<K extends keyof T>(attr: K): T[K][];
48
- slice(...args: number[]): InstanceType<this["Model"]>[];
48
+ slice(...args: number[]): InstanceType<M>[];
49
49
  sort(): this;
50
50
  fetch(options?: SyncOptions & CSetOptions): Promise<readonly [this, Response]>;
51
51
  create(model: ModelArg<T, O>, options?: {
52
52
  wait?: boolean;
53
53
  } & SyncOptions & CSetOptions): Promise<[InstanceType<this["Model"]>, Response]>;
54
54
  parse(response: any, options: SyncOptions & CSetOptions): T[];
55
- _getModelClass<T extends Record<string, any>, O extends Record<string, any>>(): {
56
- new (attributes?: T_1 | undefined, options?: O_1 & SetOptions & ConstructorOptions): {
57
- cid: string;
58
- id: string | number;
59
- attributes: T_1;
60
- readonly urlOptions: O_1;
61
- collection?: Collection<object, object> | undefined;
62
- lazy?: boolean | undefined;
63
- refetching?: boolean | undefined;
64
- measure?: boolean | ((config: ResourceConfigObj) => boolean) | undefined;
65
- isEmptyModel?: boolean | undefined;
66
- toJSON(): T_1;
67
- sync(model: Model<Record<string, any>, Record<string, any>> | Collection<object, object>, options?: SyncOptions | undefined): Promise<[any, Response]>;
68
- get<K extends keyof T_1>(attr: K): T_1[K];
69
- pick<K_1 extends keyof T_1>(...attrs: K_1[]): Pick<T_1, K_1>;
70
- has(attr: keyof T_1): boolean;
71
- set(attrs?: Partial<T_1>, options?: SetOptions): any;
72
- unset(attr: keyof T_1, options?: SetOptions | undefined): any;
73
- clear(options?: SetOptions | undefined): any;
74
- fetch(options?: SyncOptions & SetOptions): Promise<readonly [any, Response]>;
75
- save(attrs: Partial<T_1>, options?: {
76
- wait?: boolean | undefined;
77
- patch?: boolean | undefined;
78
- } & SyncOptions & SetOptions): Promise<[any, Response]>;
79
- destroy(options?: {
80
- wait?: boolean | undefined;
81
- } & SyncOptions & SetOptions): Promise<[any, Response]>;
82
- url(options?: O_2): string;
83
- parse(response: any, options: SyncOptions & SetOptions): T_1;
84
- isNew(): boolean;
85
- _callbacks: {
86
- callback: (...args: any[]) => void;
87
- context: {};
88
- }[];
89
- triggerUpdate(): void;
90
- onUpdate(callback: (...args: any[]) => void, context: {}): void;
91
- offUpdate(ctx: {}): void;
92
- };
93
- idAttribute: string;
94
- dependencies: (string | ((attrs: Record<string, any>) => Record<string, any>))[];
95
- cacheTimeout: number;
96
- defaults: {};
97
- measure: boolean | ((config: ResourceConfigObj) => boolean);
98
- };
55
+ _getModelClass<T extends Record<string, any>, O extends Record<string, any>>(): typeof Model<T, O>;
99
56
  _reset(): void;
100
57
  _prepareModel(attrs: ModelArg<T, O>, options?: CSetOptions): Model<T, O>;
101
58
  _removeModels(models: (Model<T, O>["id"] | ModelArg<T, O>)[]): InstanceType<this["Model"]>[];
102
59
  _isModel(model: any): boolean;
103
- _addReference(model: Model<T, O>): void;
60
+ _addReference(model: InstanceType<M>): void;
104
61
  _removeReference(model: Model<T, O>): void;
105
- _updateModelReference(id: string | number, prevId: string | number, model: Model<T, O>): void;
62
+ _updateModelReference(id: string | number, prevId: string | number, model: InstanceType<M>): void;
106
63
  }
107
64
  export {};
@@ -3,7 +3,14 @@ import Model from "./model.js";
3
3
  import { ResourceKeys } from "./types.js";
4
4
  type Component = NonNullable<unknown>;
5
5
  declare const ModelCache: {
6
- get(cacheKey: string): Model<Record<string, any>, Record<string, any>> | Collection<object, object> | undefined;
6
+ get(cacheKey: string): Model<Record<string, any>, Record<string, any>> | Collection<object, object, {
7
+ new (attributes?: object | undefined, options?: object & import("./model.js").SetOptions & import("./model.js").ConstructorOptions): Model<object, object>;
8
+ idAttribute: string;
9
+ dependencies: (string | ((attrs: Record<string, any>) => Record<string, any>))[];
10
+ cacheTimeout: number;
11
+ defaults: {};
12
+ measure: boolean | ((config: import("./types.js").ResourceConfigObj) => boolean);
13
+ }> | undefined;
7
14
  put(cacheKey: string, model: Model | Collection, component?: Component): void;
8
15
  register(cacheKey: string, component?: Component): void;
9
16
  unregister(component: Component, ...cacheKeys: string[]): void;
@@ -10,7 +10,14 @@ interface RequestOptions extends Pick<SyncOptions, "params"> {
10
10
  force?: boolean;
11
11
  lazy?: boolean;
12
12
  }
13
- export declare const getFromCache: (key: string) => Model<Record<string, any>, Record<string, any>> | Collection<object, object> | undefined;
13
+ export declare const getFromCache: (key: string) => Model<Record<string, any>, Record<string, any>> | Collection<object, object, {
14
+ new (attributes?: object | undefined, options?: object & import("./model.js").SetOptions & import("./model.js").ConstructorOptions): Model<object, object>;
15
+ idAttribute: string;
16
+ dependencies: (string | ((attrs: Record<string, any>) => Record<string, any>))[];
17
+ cacheTimeout: number;
18
+ defaults: {};
19
+ measure: boolean | ((config: import("./types.js").ResourceConfigObj) => boolean);
20
+ }> | undefined;
14
21
  export declare const existsInCache: (key: string) => boolean;
15
22
  declare const _default: (key: string, Model: {
16
23
  new (data: Record<string, any>, options: RequestOptions["options"]): Model;
@@ -1,5 +1,6 @@
1
1
  export const hasErrored = (loadingStates) => qualifyLoadingStates(loadingStates).some((state) => state === "error");
2
- export const isLoading = (loadingStates) => qualifyLoadingStates(loadingStates).some((state) => state === "loading");
2
+ export const isLoading = (loadingStates) => qualifyLoadingStates(loadingStates).some((state) => state === "loading") &&
3
+ !qualifyLoadingStates(loadingStates).some((state) => state === "error");
3
4
  export const hasLoaded = (loadingStates) => qualifyLoadingStates(loadingStates).every((state) => state === "loaded");
4
5
  export const isPending = (loadingStates) => qualifyLoadingStates(loadingStates).some((state) => state === "pending");
5
6
  export function noOp() { }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "github.com/noahgrant/resourcerer"
@@ -39,9 +39,10 @@ describe("Utils", () => {
39
39
 
40
40
  describe("isLoading method", () => {
41
41
  it("returns true if any loading state is loading", () => {
42
- expect(isLoading(["error", "loading"])).toBe(true);
42
+ expect(isLoading(["pending", "loading", "loading"])).toBe(true);
43
+ expect(isLoading(["loaded", "loading"])).toBe(true);
43
44
  expect(isLoading("loading")).toBe(true);
44
- expect(Utils.isLoading(["error", "loading"])).toBe(true);
45
+ expect(Utils.isLoading(["loaded", "loading"])).toBe(true);
45
46
  expect(Utils.isLoading("loading")).toBe(true);
46
47
  });
47
48
 
@@ -52,6 +53,11 @@ describe("Utils", () => {
52
53
  expect(Utils.isLoading("loaded")).toBe(false);
53
54
  });
54
55
 
56
+ it("returns false if any other loading states have errored", () => {
57
+ expect(isLoading(["error", "loading", "loaded"])).toBe(false);
58
+ expect(Utils.isLoading(["error", "loading", "loaded"])).toBe(false);
59
+ });
60
+
55
61
  it("returns false if an undefined loading state is passed", () => {
56
62
  expect(isLoading(undefined)).toBe(false);
57
63
  expect(Utils.isLoading(undefined)).toBe(false);