rest-client-sdk 7.1.1 → 7.2.0

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.
@@ -0,0 +1 @@
1
+ export declare function findClosestWord(word: string, list: string[], maxDistance?: number): string | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rest-client-sdk",
3
- "version": "7.1.1",
3
+ "version": "7.2.0",
4
4
  "description": "Rest Client SDK for API",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -9,6 +9,7 @@ import UnitOfWork from './UnitOfWork';
9
9
  import AbstractClient from './client/AbstractClient';
10
10
  import JsSerializer from './serializer/JsSerializer';
11
11
  import SerializerInterface from './serializer/SerializerInterface';
12
+ import { findClosestWord } from './utils/levenshtein';
12
13
  import { Logger } from './utils/logging';
13
14
  import { generateRepository } from './utils/repositoryGenerator';
14
15
 
@@ -80,7 +81,13 @@ class RestClientSdk<M extends SdkMetadata>
80
81
  const metadata = this.mapping.getClassMetadataByKey(key);
81
82
 
82
83
  if (!metadata) {
83
- throw new Error(`Unable to get metadata for repository ${key}`);
84
+ const closeKey = findClosestWord(key, this.mapping.getMappingKeys(), 5);
85
+
86
+ const suggestions = closeKey ? ` Did you mean "${closeKey}"?` : '';
87
+
88
+ throw new Error(
89
+ `Unable to get metadata for repository "${key}".${suggestions}`
90
+ );
84
91
  }
85
92
 
86
93
  // eslint-disable-next-line new-cap
@@ -0,0 +1,53 @@
1
+ function levenshteinDistance(a: string, b: string): number {
2
+ const matrix: number[][] = [];
3
+
4
+ // Increment along the first column of each row
5
+ for (let i = 0; i <= b.length; i += 1) {
6
+ matrix[i] = [i];
7
+ }
8
+
9
+ // Increment each column in the first row
10
+ for (let j = 0; j <= a.length; j += 1) {
11
+ matrix[0][j] = j;
12
+ }
13
+
14
+ // Fill in the rest of the matrix
15
+ for (let i = 1; i <= b.length; i += 1) {
16
+ for (let j = 1; j <= a.length; j += 1) {
17
+ if (b.charAt(i - 1) === a.charAt(j - 1)) {
18
+ matrix[i][j] = matrix[i - 1][j - 1];
19
+ } else {
20
+ matrix[i][j] = Math.min(
21
+ matrix[i - 1][j - 1] + 1, // substitution
22
+ matrix[i][j - 1] + 1, // insertion
23
+ matrix[i - 1][j] + 1 // deletion
24
+ );
25
+ }
26
+ }
27
+ }
28
+
29
+ return matrix[b.length][a.length];
30
+ }
31
+
32
+ // eslint-disable-next-line import/prefer-default-export
33
+ export function findClosestWord(
34
+ word: string,
35
+ list: string[],
36
+ maxDistance = Infinity
37
+ ): string | null {
38
+ let closestWord: string | null = null;
39
+ let minDistance = Infinity;
40
+
41
+ // eslint-disable-next-line no-plusplus
42
+ for (let i = 0; i < list.length; i += 1) {
43
+ const candidate = list[i];
44
+ const distance = levenshteinDistance(word, candidate);
45
+
46
+ if (distance < minDistance && distance <= maxDistance) {
47
+ minDistance = distance;
48
+ closestWord = candidate;
49
+ }
50
+ }
51
+
52
+ return closestWord;
53
+ }