rest-client-sdk 7.1.0 → 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.
- package/dist/index.es.js +69 -8
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +69 -8
- package/dist/index.js.map +1 -1
- package/dist/types/client/AbstractClient.d.ts +1 -0
- package/dist/types/utils/levenshtein.d.ts +1 -0
- package/package.json +1 -1
- package/src/RestClientSdk.ts +8 -1
- package/src/client/AbstractClient.ts +16 -8
- package/src/utils/levenshtein.ts +53 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function findClosestWord(word: string, list: string[], maxDistance?: number): string | null;
|
package/package.json
CHANGED
package/src/RestClientSdk.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
|
@@ -386,7 +386,11 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
386
386
|
.refreshToken()
|
|
387
387
|
.then(
|
|
388
388
|
(refreshedTokenObject) => refreshedTokenObject.access_token
|
|
389
|
-
)
|
|
389
|
+
).catch((e) => {
|
|
390
|
+
this._handleRefreshTokenFailure(e);
|
|
391
|
+
|
|
392
|
+
throw e;
|
|
393
|
+
});
|
|
390
394
|
}
|
|
391
395
|
|
|
392
396
|
return this.#tokenStorage.getAccessToken();
|
|
@@ -415,13 +419,7 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
415
419
|
return this._fetchWithToken(input, updatedRequestParams);
|
|
416
420
|
})
|
|
417
421
|
.catch((e) => {
|
|
418
|
-
|
|
419
|
-
this.#tokenStorage.logout().then(() => {
|
|
420
|
-
if (this.sdk.config.onRefreshTokenFailure) {
|
|
421
|
-
this.sdk.config.onRefreshTokenFailure(e);
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
}
|
|
422
|
+
this._handleRefreshTokenFailure(e);
|
|
425
423
|
|
|
426
424
|
throw e;
|
|
427
425
|
});
|
|
@@ -532,6 +530,16 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
532
530
|
// @ts-ignore
|
|
533
531
|
return object[idKey];
|
|
534
532
|
}
|
|
533
|
+
|
|
534
|
+
private _handleRefreshTokenFailure(error: Error): void {
|
|
535
|
+
if (error instanceof OauthError) {
|
|
536
|
+
this.#tokenStorage.logout().then(() => {
|
|
537
|
+
if (this.sdk.config.onRefreshTokenFailure) {
|
|
538
|
+
this.sdk.config.onRefreshTokenFailure(error);
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
}
|
|
542
|
+
}
|
|
535
543
|
}
|
|
536
544
|
|
|
537
545
|
export default AbstractClient;
|
|
@@ -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
|
+
}
|