resourcerer 2.0.5 → 2.0.7

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;
package/build/lib/sync.js CHANGED
@@ -14,7 +14,7 @@ export default function (model, options = {}) {
14
14
  });
15
15
  }
16
16
  export function ajax(options) {
17
- const hasParams = !!Object.keys(options.params).length;
17
+ const hasParams = options.params instanceof FormData || !!Object.keys(options.params).length;
18
18
  const hasBodyContent = !/^(?:GET|HEAD)$/.test(options.method || "") && hasParams;
19
19
  const startTime = Date.now();
20
20
  const onRequestComplete = (json, response) => {
@@ -42,7 +42,8 @@ export function ajax(options) {
42
42
  },
43
43
  ...(hasBodyContent ?
44
44
  {
45
- body: typeof options.params === "string" ? options.params
45
+ body: typeof options.params === "string" || options.params instanceof FormData ?
46
+ options.params
46
47
  : options.contentType === MIME_TYPE_JSON ? JSON.stringify(options.params)
47
48
  : ResourcesConfig.stringify(options.params, options),
48
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resourcerer",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "github.com/noahgrant/resourcerer"
package/test/sync.test.js CHANGED
@@ -83,6 +83,16 @@ describe("sync", () => {
83
83
  await library.at(0).save(null, { params: { a: "a", one: 1 }, contentType: "resourcerer/json" });
84
84
  expect(window.fetch.mock.calls[0][0]).toEqual("/library");
85
85
  expect(window.fetch.mock.calls[0][1].body).toEqual("a=a&one=1");
86
+ window.fetch.mockClear();
87
+
88
+ // with body content: FormData
89
+ const formData = new FormData();
90
+
91
+ formData.append("a", 1);
92
+
93
+ await library.at(0).save(null, { params: formData, contentType: "multipart/form-data" });
94
+ expect(window.fetch.mock.calls[0][0]).toEqual("/library");
95
+ expect(window.fetch.mock.calls[0][1].body).toEqual(formData);
86
96
  });
87
97
 
88
98
  it("passes urlOptions to the model to formulate the url path", async () => {