rest-client-sdk 6.7.1 → 7.0.0-rc.10
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 +1654 -1527
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +1657 -1529
- package/dist/index.js.map +1 -1
- package/dist/types/RestClientSdk.d.ts +7 -5
- package/dist/types/RestClientSdkInterface.d.ts +5 -2
- package/dist/types/TokenGenerator/AbstractTokenGenerator.d.ts +3 -1
- package/dist/types/TokenStorage.d.ts +3 -2
- package/dist/types/client/AbstractClient.d.ts +1 -1
- package/dist/types/client/headerUtils.d.ts +1 -0
- package/dist/types/index.d.ts +18 -17
- package/dist/types/serializer/JsSerializer.d.ts +1 -1
- package/dist/types/serializer/Serializer.d.ts +1 -1
- package/dist/types/utils/logging.d.ts +31 -0
- package/dist/types/utils/repositoryGenerator.d.ts +1 -1
- package/package.json +1 -1
- package/src/ErrorFactory.ts +1 -1
- package/src/RestClientSdk.ts +18 -7
- package/src/RestClientSdkInterface.ts +6 -2
- package/src/TokenGenerator/AbstractTokenGenerator.ts +7 -1
- package/src/TokenGenerator/AuthorizationCodeFlowTokenGenerator.ts +18 -6
- package/src/TokenGenerator/ClientCredentialsGenerator.ts +12 -6
- package/src/TokenGenerator/PasswordGenerator.ts +18 -6
- package/src/TokenStorage.ts +24 -15
- package/src/UnitOfWork.ts +2 -2
- package/src/client/AbstractClient.ts +29 -24
- package/src/client/headerUtils.ts +12 -0
- package/src/index.ts +21 -20
- package/src/serializer/JsSerializer.ts +1 -1
- package/src/serializer/Serializer.ts +1 -1
- package/src/utils/logging.ts +96 -0
- package/src/utils/repositoryGenerator.ts +1 -1
|
@@ -39,3 +39,15 @@ export function removeUndefinedHeaders(headers: HeadersInit): HeadersInit {
|
|
|
39
39
|
|
|
40
40
|
return Object.fromEntries(filterEntries(Object.entries(headers)));
|
|
41
41
|
}
|
|
42
|
+
|
|
43
|
+
export function convertToRecord(headers: HeadersInit): Record<string, string> {
|
|
44
|
+
if (headers instanceof Headers) {
|
|
45
|
+
return Object.fromEntries(headers.entries());
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (Array.isArray(headers)) {
|
|
49
|
+
return Object.fromEntries(headers);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return headers;
|
|
53
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type AsyncStorageInterface from './AsyncStorageInterface';
|
|
2
2
|
import {
|
|
3
3
|
BadRequestError,
|
|
4
4
|
ConflictError,
|
|
@@ -11,37 +11,35 @@ import {
|
|
|
11
11
|
OauthError,
|
|
12
12
|
InvalidGrantError,
|
|
13
13
|
} from './ErrorFactory';
|
|
14
|
-
import
|
|
15
|
-
import
|
|
14
|
+
import Mapping from './Mapping';
|
|
15
|
+
import Attribute from './Mapping/Attribute';
|
|
16
|
+
import ClassMetadata from './Mapping/ClassMetadata';
|
|
17
|
+
import Relation from './Mapping/Relation';
|
|
18
|
+
import RestClientSdk from './RestClientSdk';
|
|
19
|
+
import type { SdkMetadata, MetadataDefinition } from './RestClientSdk';
|
|
20
|
+
// eslint-disable-next-line import/no-duplicates
|
|
21
|
+
import type RestClientSdkInterface from './RestClientSdkInterface';
|
|
22
|
+
// eslint-disable-next-line import/no-duplicates
|
|
23
|
+
import type { Config } from './RestClientSdkInterface';
|
|
16
24
|
import AbstractTokenGenerator from './TokenGenerator/AbstractTokenGenerator';
|
|
17
25
|
import AuthorizationCodeFlowTokenGenerator from './TokenGenerator/AuthorizationCodeFlowTokenGenerator';
|
|
18
26
|
import ClientCredentialsGenerator from './TokenGenerator/ClientCredentialsGenerator';
|
|
19
27
|
import PasswordGenerator from './TokenGenerator/PasswordGenerator';
|
|
20
28
|
import ProvidedTokenGenerator from './TokenGenerator/ProvidedTokenGenerator';
|
|
21
|
-
import
|
|
22
|
-
import type SerializerInterface from './serializer/SerializerInterface';
|
|
23
|
-
import Mapping from './Mapping';
|
|
24
|
-
import ClassMetadata from './Mapping/ClassMetadata';
|
|
25
|
-
import Attribute from './Mapping/Attribute';
|
|
26
|
-
import Relation from './Mapping/Relation';
|
|
29
|
+
import type TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface';
|
|
27
30
|
import type {
|
|
28
31
|
Token,
|
|
29
32
|
ErrorBody,
|
|
30
33
|
TokenBody,
|
|
31
34
|
TokenResponse,
|
|
32
35
|
} from './TokenGenerator/types';
|
|
33
|
-
|
|
34
|
-
import
|
|
35
|
-
// eslint-disable-next-line import/no-duplicates
|
|
36
|
-
import type RestClientSdkInterface from './RestClientSdkInterface';
|
|
37
|
-
// eslint-disable-next-line import/no-duplicates
|
|
38
|
-
import type { Config } from './RestClientSdkInterface';
|
|
39
|
-
// eslint-disable-next-line import/no-duplicates
|
|
40
|
-
import type TokenStorageInterface from './TokenStorageInterface';
|
|
41
|
-
// eslint-disable-next-line import/no-duplicates
|
|
36
|
+
import TokenStorage from './TokenStorage';
|
|
37
|
+
import TokenStorageInterface from './TokenStorageInterface';
|
|
42
38
|
import type { HasExpiresAt } from './TokenStorageInterface';
|
|
43
|
-
import
|
|
44
|
-
import
|
|
39
|
+
import AbstractClient from './client/AbstractClient';
|
|
40
|
+
import Serializer from './serializer/Serializer';
|
|
41
|
+
import type SerializerInterface from './serializer/SerializerInterface';
|
|
42
|
+
import { Logger, Log, LoggerHistory } from './utils/logging';
|
|
45
43
|
|
|
46
44
|
export default RestClientSdk;
|
|
47
45
|
export {
|
|
@@ -67,6 +65,7 @@ export {
|
|
|
67
65
|
ClassMetadata,
|
|
68
66
|
Attribute,
|
|
69
67
|
Relation,
|
|
68
|
+
Logger,
|
|
70
69
|
};
|
|
71
70
|
export type {
|
|
72
71
|
SerializerInterface,
|
|
@@ -82,4 +81,6 @@ export type {
|
|
|
82
81
|
TokenGeneratorInterface,
|
|
83
82
|
AsyncStorageInterface,
|
|
84
83
|
MetadataDefinition,
|
|
84
|
+
LoggerHistory,
|
|
85
|
+
Log,
|
|
85
86
|
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import ClassMetadata from '../Mapping/ClassMetadata';
|
|
2
3
|
import SerializerInterface, {
|
|
3
4
|
Entity,
|
|
4
5
|
NormalizedObject,
|
|
5
6
|
NormalizedList,
|
|
6
7
|
EntityList,
|
|
7
8
|
} from './SerializerInterface';
|
|
8
|
-
import ClassMetadata from '../Mapping/ClassMetadata';
|
|
9
9
|
|
|
10
10
|
class Serializer implements SerializerInterface {
|
|
11
11
|
/**
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { convertToRecord } from '../client/headerUtils';
|
|
2
|
+
|
|
3
|
+
type SdkRequest = RequestInit & { url: string };
|
|
4
|
+
|
|
5
|
+
// type SdkResponse = Partial<Response>;
|
|
6
|
+
type SdkResponse = {
|
|
7
|
+
status: number;
|
|
8
|
+
headers: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const generateId = () =>
|
|
12
|
+
`rest-client-sdk|${Date.now()}-${
|
|
13
|
+
Math.floor(Math.random() * (9e12 - 1)) + 1e12
|
|
14
|
+
}`;
|
|
15
|
+
|
|
16
|
+
export type SerializableRequest = {
|
|
17
|
+
url: string;
|
|
18
|
+
method?: string;
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
body?: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type Log = {
|
|
24
|
+
id: string;
|
|
25
|
+
request?: SerializableRequest;
|
|
26
|
+
response?: SdkResponse;
|
|
27
|
+
logTimes: {
|
|
28
|
+
request?: number;
|
|
29
|
+
response?: number;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type LoggerHistory = Array<Log>;
|
|
34
|
+
|
|
35
|
+
function serializeRequest(req: SdkRequest): SerializableRequest {
|
|
36
|
+
const request = { ...req }; // Make a clone, useful for doing destructive things
|
|
37
|
+
return {
|
|
38
|
+
method: request.method, // The Request Method, e.g. GET, POST, DELETE
|
|
39
|
+
url: request.url, // The URL
|
|
40
|
+
headers: request.headers ? convertToRecord(request.headers) : undefined,
|
|
41
|
+
body: typeof request.body === 'string' ? request.body : undefined,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// eslint-disable-next-line import/prefer-default-export
|
|
46
|
+
export class Logger {
|
|
47
|
+
#history: LoggerHistory;
|
|
48
|
+
|
|
49
|
+
get history(): LoggerHistory {
|
|
50
|
+
return this.#history;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
constructor() {
|
|
54
|
+
this.#history = [];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
logRequest(params: SdkRequest): string {
|
|
58
|
+
const log: Log = {
|
|
59
|
+
id: generateId(),
|
|
60
|
+
request: serializeRequest(params),
|
|
61
|
+
logTimes: { request: Date.now() },
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
this.#history.push(log);
|
|
65
|
+
|
|
66
|
+
return log.id;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
logResponse(response: Response, requestId?: undefined | string): string {
|
|
70
|
+
const id = requestId || generateId();
|
|
71
|
+
|
|
72
|
+
// console.log('clone');
|
|
73
|
+
// const clonedResponse = response.clone(); // THIS IS THE LINE THAT CAUSES THE PROBLEM
|
|
74
|
+
// console.log('after clone', clonedResponse.status);
|
|
75
|
+
|
|
76
|
+
const logContent: SdkResponse = {
|
|
77
|
+
status: response.status, // clonedResponse.status,
|
|
78
|
+
headers: Object.fromEntries(response.headers),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const foundLogIndex = this.#history.findIndex((log) => log.id === id);
|
|
82
|
+
|
|
83
|
+
if (foundLogIndex > -1) {
|
|
84
|
+
this.#history[foundLogIndex].response = logContent;
|
|
85
|
+
this.#history[foundLogIndex].logTimes.response = Date.now();
|
|
86
|
+
} else {
|
|
87
|
+
this.#history.push({
|
|
88
|
+
id,
|
|
89
|
+
response: logContent,
|
|
90
|
+
logTimes: { response: Date.now() },
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return id;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/* eslint-disable import/prefer-default-export */
|
|
2
|
-
import type AbstractClient from '../client/AbstractClient';
|
|
3
2
|
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
4
3
|
// eslint-disable-next-line import/no-duplicates
|
|
5
4
|
import type RestClientSdk from '../RestClientSdk';
|
|
6
5
|
// eslint-disable-next-line import/no-duplicates
|
|
7
6
|
import type { MetadataDefinition, SdkMetadata } from '../RestClientSdk';
|
|
7
|
+
import type AbstractClient from '../client/AbstractClient';
|
|
8
8
|
|
|
9
9
|
export function generateRepository<D extends MetadataDefinition>(
|
|
10
10
|
sdk: RestClientSdk<SdkMetadata>,
|