@clairejs/client 3.5.5 → 3.5.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.
package/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Change Log
2
2
 
3
+ #### 3.5.6:
4
+
5
+ - add id field to mergeInstances
6
+
3
7
  #### 3.5.5:
4
8
 
5
9
  - improve RefreshHttpClient refreshToken
@@ -2,7 +2,7 @@ import { AbstractModel, Constructor, CreateManyRequestBody, CreateManyResponseBo
2
2
  import { AbstractHttpClient, RequestOptions } from "./AbstractHttpClient";
3
3
  export declare const stringifyQueries: (queries: Record<string, any>) => string;
4
4
  export declare const removeInstances: <T extends Identifiable>(target: T[], source: T[]) => T[];
5
- export declare const mergeInstances: <T extends Identifiable>(model: Constructor<T>, target: readonly T[], source: readonly DeepPartial<T>[] | undefined, syncTarget?: boolean) => T[];
5
+ export declare const mergeInstances: <T extends Identifiable>(model: Constructor<T>, target: readonly T[], source: readonly DeepPartial<T>[] | undefined, idField?: keyof T, syncTarget?: boolean) => T[];
6
6
  export declare class CrudApi<T extends AbstractModel> {
7
7
  readonly model: Constructor<T>;
8
8
  readonly httpClient: AbstractHttpClient;
@@ -13,11 +13,13 @@ export const removeInstances = (target, source) => {
13
13
  }
14
14
  return result;
15
15
  };
16
- export const mergeInstances = (model, target, source, syncTarget) => {
17
- const result = (syncTarget ? target.filter((i) => (source || []).some((newI) => i.id && newI.id && i.id === newI.id)) : target).map((i) => ({ ...i }));
16
+ export const mergeInstances = (model, target, source, idField = "id", syncTarget) => {
17
+ const result = (syncTarget
18
+ ? target.filter((i) => (source || []).some((newI) => i[idField] && newI[idField] && i[idField] === newI[idField]))
19
+ : target).map((i) => ({ ...i }));
18
20
  const metadata = getObjectMetadata(model);
19
21
  for (const instance of source || []) {
20
- const index = result.findIndex((i) => i.id && instance.id && i.id === instance.id);
22
+ const index = result.findIndex((i) => i[idField] && instance[idField] && i[idField] === instance[idField]);
21
23
  if (index < 0) {
22
24
  result.push(Object.assign(new model(), instance));
23
25
  }
@@ -42,10 +44,10 @@ export const mergeInstances = (model, target, source, syncTarget) => {
42
44
  }
43
45
  else {
44
46
  if (fieldMeta.hasMany.single) {
45
- targetObj[field] = mergeInstances(targetModel, [targetObj[field]], [sourceObject[field]], true);
47
+ targetObj[field] = mergeInstances(targetModel, [targetObj[field]], [sourceObject[field]], idField, true);
46
48
  }
47
49
  else {
48
- targetObj[field] = mergeInstances(targetModel, targetObj[field], sourceObject[field], true);
50
+ targetObj[field] = mergeInstances(targetModel, targetObj[field], sourceObject[field], idField, true);
49
51
  }
50
52
  }
51
53
  }
@@ -91,7 +93,10 @@ export class CrudApi {
91
93
  }
92
94
  async getMany(queries, options) {
93
95
  const url = `${this.getEndpointBaseUrl()}?${stringifyQueries(queries || {})}`;
94
- const result = await this.httpClient.get(url, undefined, { ...options, noCache: options?.noCache || this.dirty.get(url) });
96
+ const result = await this.httpClient.get(url, undefined, {
97
+ ...options,
98
+ noCache: options?.noCache || this.dirty.get(url),
99
+ });
95
100
  this.clearDirty(url);
96
101
  return result;
97
102
  }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@clairejs/client",
3
- "version": "3.5.5",
3
+ "version": "3.5.6",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
7
7
  "scripts": {
8
- "test": "mocha ./test/*.test.ts",
8
+ "test": "TS_NODE_TRANSPILE_ONLY=true mocha ./test/*.test.ts",
9
9
  "build": "rm -rf ./dist && tsc -p tsconfig-build.json",
10
10
  "push": "npm run build && npm run test && npm publish && git push"
11
11
  },
@@ -17,9 +17,9 @@
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/mocha": "^10.0.1",
20
- "@types/node": "^18.16.1",
20
+ "@types/node": "^22.14.0",
21
21
  "mocha": "^10.2.0",
22
- "ts-node": "^10.9.1",
23
- "typescript": "^5.0.4"
22
+ "ts-node": "^10.9.2",
23
+ "typescript": "^5.5.4"
24
24
  }
25
25
  }