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
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import UnitOfWork from './UnitOfWork';
|
|
2
1
|
import Mapping from './Mapping';
|
|
3
|
-
import TokenStorageInterface from './TokenStorageInterface';
|
|
4
|
-
import SerializerInterface from './serializer/SerializerInterface';
|
|
5
|
-
import AbstractClient from './client/AbstractClient';
|
|
6
|
-
import { Token } from './TokenGenerator/types';
|
|
7
2
|
import type RestClientSdkInterface from './RestClientSdkInterface';
|
|
8
3
|
import type { Config } from './RestClientSdkInterface';
|
|
4
|
+
import { Token } from './TokenGenerator/types';
|
|
5
|
+
import TokenStorageInterface from './TokenStorageInterface';
|
|
6
|
+
import UnitOfWork from './UnitOfWork';
|
|
7
|
+
import AbstractClient from './client/AbstractClient';
|
|
8
|
+
import SerializerInterface from './serializer/SerializerInterface';
|
|
9
|
+
import { Logger } from './utils/logging';
|
|
9
10
|
declare type Entity = any;
|
|
10
11
|
export declare type MetadataDefinition = {
|
|
11
12
|
entity: Entity;
|
|
@@ -19,6 +20,7 @@ declare class RestClientSdk<M extends SdkMetadata> implements RestClientSdkInter
|
|
|
19
20
|
serializer: SerializerInterface;
|
|
20
21
|
mapping: Mapping;
|
|
21
22
|
unitOfWork: UnitOfWork;
|
|
23
|
+
readonly logger?: Logger;
|
|
22
24
|
constructor(tokenStorage: TokenStorageInterface<Token>, config: Config, mapping: Mapping, serializer?: SerializerInterface);
|
|
23
25
|
/**
|
|
24
26
|
* get a repository by it's metadata "key" attribute
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Mapping from './Mapping';
|
|
2
2
|
import { SdkMetadata } from './RestClientSdk';
|
|
3
3
|
import { Token } from './TokenGenerator/types';
|
|
4
4
|
import TokenStorageInterface from './TokenStorageInterface';
|
|
5
|
-
import
|
|
5
|
+
import AbstractClient from './client/AbstractClient';
|
|
6
|
+
import { Logger } from './utils/logging';
|
|
6
7
|
export declare type Config = {
|
|
7
8
|
path: string;
|
|
8
9
|
scheme: string;
|
|
@@ -11,9 +12,11 @@ export declare type Config = {
|
|
|
11
12
|
authorizationType?: string;
|
|
12
13
|
useDefaultParameters?: boolean;
|
|
13
14
|
unitOfWorkEnabled?: boolean;
|
|
15
|
+
loggerEnabled?: boolean;
|
|
14
16
|
};
|
|
15
17
|
export default interface RestClientSdkInterface<M extends SdkMetadata> {
|
|
16
18
|
tokenStorage: TokenStorageInterface<Token>;
|
|
17
19
|
mapping: Mapping;
|
|
20
|
+
readonly logger?: Logger;
|
|
18
21
|
getRepository<K extends keyof M & string>(key: K): AbstractClient<M[K]>;
|
|
19
22
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from '../utils/logging';
|
|
1
2
|
import TokenGeneratorInterface, { TokenBodyReturn } from './TokenGeneratorInterface';
|
|
2
3
|
import { Token, TokenResponse } from './types';
|
|
3
4
|
interface UrlConfig {
|
|
@@ -7,7 +8,8 @@ interface UrlConfig {
|
|
|
7
8
|
}
|
|
8
9
|
declare abstract class AbstractTokenGenerator<T extends Token, C> implements TokenGeneratorInterface<T> {
|
|
9
10
|
readonly tokenGeneratorConfig: C;
|
|
10
|
-
|
|
11
|
+
protected logger?: Logger;
|
|
12
|
+
constructor(tokenGeneratorConfig: C, logger?: Logger);
|
|
11
13
|
abstract generateToken(parameters: unknown): Promise<TokenBodyReturn<T> | TokenResponse<T>>;
|
|
12
14
|
abstract refreshToken(accessToken: null | T): Promise<TokenBodyReturn<T> | TokenResponse<T>>;
|
|
13
15
|
abstract checkTokenGeneratorConfig(config: C): void;
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import AsyncStorageInterface from './AsyncStorageInterface';
|
|
1
2
|
import TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface';
|
|
2
3
|
import { Token } from './TokenGenerator/types';
|
|
3
|
-
import AsyncStorageInterface from './AsyncStorageInterface';
|
|
4
4
|
import type TokenStorageInterface from './TokenStorageInterface';
|
|
5
5
|
import type { HasExpiresAt } from './TokenStorageInterface';
|
|
6
|
+
import { Logger } from './utils/logging';
|
|
6
7
|
declare class TokenStorage<T extends Token> implements TokenStorageInterface<T> {
|
|
7
8
|
#private;
|
|
8
9
|
accessTokenKey: string;
|
|
9
|
-
constructor(tokenGenerator: TokenGeneratorInterface<T>, asyncStorage: AsyncStorageInterface, accessTokenKey?: string);
|
|
10
|
+
constructor(tokenGenerator: TokenGeneratorInterface<T>, asyncStorage: AsyncStorageInterface, accessTokenKey?: string, logger?: Logger);
|
|
10
11
|
setAsyncStorage(asyncStorage: AsyncStorageInterface): void;
|
|
11
12
|
hasAccessToken(): Promise<boolean>;
|
|
12
13
|
getAccessToken(): Promise<null | string>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import URI from 'urijs';
|
|
2
|
+
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
2
3
|
import type RestClientSdk from '../RestClientSdk';
|
|
3
4
|
import type { MetadataDefinition, SdkMetadata } from '../RestClientSdk';
|
|
4
|
-
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
5
5
|
import type SerializerInterface from '../serializer/SerializerInterface';
|
|
6
6
|
declare class AbstractClient<D extends MetadataDefinition> {
|
|
7
7
|
#private;
|
|
@@ -4,3 +4,4 @@ export declare function removeAuthorization(headers: undefined | HeadersInit): u
|
|
|
4
4
|
* Does not apply on "Headers" instance as values are string in there.
|
|
5
5
|
*/
|
|
6
6
|
export declare function removeUndefinedHeaders(headers: HeadersInit): HeadersInit;
|
|
7
|
+
export declare function convertToRecord(headers: HeadersInit): Record<string, string>;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type AsyncStorageInterface from './AsyncStorageInterface';
|
|
2
2
|
import { BadRequestError, ConflictError, ForbiddenError, HttpError, InternalServerError, ResourceNotFoundError, UnauthorizedError, InvalidScopeError, OauthError, InvalidGrantError } from './ErrorFactory';
|
|
3
|
-
import AbstractClient from './client/AbstractClient';
|
|
4
|
-
import TokenStorage from './TokenStorage';
|
|
5
|
-
import AbstractTokenGenerator from './TokenGenerator/AbstractTokenGenerator';
|
|
6
|
-
import AuthorizationCodeFlowTokenGenerator from './TokenGenerator/AuthorizationCodeFlowTokenGenerator';
|
|
7
|
-
import ClientCredentialsGenerator from './TokenGenerator/ClientCredentialsGenerator';
|
|
8
|
-
import PasswordGenerator from './TokenGenerator/PasswordGenerator';
|
|
9
|
-
import ProvidedTokenGenerator from './TokenGenerator/ProvidedTokenGenerator';
|
|
10
|
-
import Serializer from './serializer/Serializer';
|
|
11
|
-
import type SerializerInterface from './serializer/SerializerInterface';
|
|
12
3
|
import Mapping from './Mapping';
|
|
13
|
-
import ClassMetadata from './Mapping/ClassMetadata';
|
|
14
4
|
import Attribute from './Mapping/Attribute';
|
|
5
|
+
import ClassMetadata from './Mapping/ClassMetadata';
|
|
15
6
|
import Relation from './Mapping/Relation';
|
|
16
|
-
import
|
|
7
|
+
import RestClientSdk from './RestClientSdk';
|
|
17
8
|
import type { SdkMetadata, MetadataDefinition } from './RestClientSdk';
|
|
18
9
|
import type RestClientSdkInterface from './RestClientSdkInterface';
|
|
19
10
|
import type { Config } from './RestClientSdkInterface';
|
|
20
|
-
import
|
|
21
|
-
import
|
|
11
|
+
import AbstractTokenGenerator from './TokenGenerator/AbstractTokenGenerator';
|
|
12
|
+
import AuthorizationCodeFlowTokenGenerator from './TokenGenerator/AuthorizationCodeFlowTokenGenerator';
|
|
13
|
+
import ClientCredentialsGenerator from './TokenGenerator/ClientCredentialsGenerator';
|
|
14
|
+
import PasswordGenerator from './TokenGenerator/PasswordGenerator';
|
|
15
|
+
import ProvidedTokenGenerator from './TokenGenerator/ProvidedTokenGenerator';
|
|
22
16
|
import type TokenGeneratorInterface from './TokenGenerator/TokenGeneratorInterface';
|
|
23
|
-
import type
|
|
17
|
+
import type { Token, ErrorBody, TokenBody, TokenResponse } from './TokenGenerator/types';
|
|
18
|
+
import TokenStorage from './TokenStorage';
|
|
19
|
+
import TokenStorageInterface from './TokenStorageInterface';
|
|
20
|
+
import type { HasExpiresAt } from './TokenStorageInterface';
|
|
21
|
+
import AbstractClient from './client/AbstractClient';
|
|
22
|
+
import Serializer from './serializer/Serializer';
|
|
23
|
+
import type SerializerInterface from './serializer/SerializerInterface';
|
|
24
|
+
import { Logger, Log, LoggerHistory } from './utils/logging';
|
|
24
25
|
export default RestClientSdk;
|
|
25
|
-
export { AbstractClient, AbstractTokenGenerator, AuthorizationCodeFlowTokenGenerator, ClientCredentialsGenerator, PasswordGenerator, ProvidedTokenGenerator, Serializer, TokenStorage, UnauthorizedError, BadRequestError, ConflictError, ForbiddenError, OauthError, InvalidGrantError, InvalidScopeError, HttpError, InternalServerError, ResourceNotFoundError, Mapping, ClassMetadata, Attribute, Relation, };
|
|
26
|
-
export type { SerializerInterface, Token, ErrorBody, TokenResponse, TokenBody, HasExpiresAt, Config, SdkMetadata, RestClientSdkInterface, TokenStorageInterface, TokenGeneratorInterface, AsyncStorageInterface, MetadataDefinition, };
|
|
26
|
+
export { AbstractClient, AbstractTokenGenerator, AuthorizationCodeFlowTokenGenerator, ClientCredentialsGenerator, PasswordGenerator, ProvidedTokenGenerator, Serializer, TokenStorage, UnauthorizedError, BadRequestError, ConflictError, ForbiddenError, OauthError, InvalidGrantError, InvalidScopeError, HttpError, InternalServerError, ResourceNotFoundError, Mapping, ClassMetadata, Attribute, Relation, Logger, };
|
|
27
|
+
export type { SerializerInterface, Token, ErrorBody, TokenResponse, TokenBody, HasExpiresAt, Config, SdkMetadata, RestClientSdkInterface, TokenStorageInterface, TokenGeneratorInterface, AsyncStorageInterface, MetadataDefinition, LoggerHistory, Log, };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import Serializer from './Serializer';
|
|
2
1
|
import ClassMetadata from '../Mapping/ClassMetadata';
|
|
2
|
+
import Serializer from './Serializer';
|
|
3
3
|
declare class JsSerializer extends Serializer {
|
|
4
4
|
encodeItem(object: Record<string, unknown>, classMetadata: ClassMetadata): string;
|
|
5
5
|
decodeItem(rawData: string, classMetadata: ClassMetadata, response: Response): Record<string, unknown>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import SerializerInterface, { Entity, NormalizedObject, NormalizedList, EntityList } from './SerializerInterface';
|
|
2
1
|
import ClassMetadata from '../Mapping/ClassMetadata';
|
|
2
|
+
import SerializerInterface, { Entity, NormalizedObject, NormalizedList, EntityList } from './SerializerInterface';
|
|
3
3
|
declare class Serializer implements SerializerInterface {
|
|
4
4
|
/**
|
|
5
5
|
* convert an entity to a plain javascript object
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
declare type SdkRequest = RequestInit & {
|
|
2
|
+
url: string;
|
|
3
|
+
};
|
|
4
|
+
declare type SdkResponse = {
|
|
5
|
+
status: number;
|
|
6
|
+
headers: Record<string, string>;
|
|
7
|
+
};
|
|
8
|
+
export declare type SerializableRequest = {
|
|
9
|
+
url: string;
|
|
10
|
+
method?: string;
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
body?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare type Log = {
|
|
15
|
+
id: string;
|
|
16
|
+
request?: SerializableRequest;
|
|
17
|
+
response?: SdkResponse;
|
|
18
|
+
logTimes: {
|
|
19
|
+
request?: number;
|
|
20
|
+
response?: number;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
export declare type LoggerHistory = Array<Log>;
|
|
24
|
+
export declare class Logger {
|
|
25
|
+
#private;
|
|
26
|
+
get history(): LoggerHistory;
|
|
27
|
+
constructor();
|
|
28
|
+
logRequest(params: SdkRequest): string;
|
|
29
|
+
logResponse(response: Response, requestId?: undefined | string): string;
|
|
30
|
+
}
|
|
31
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type AbstractClient from '../client/AbstractClient';
|
|
2
1
|
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
3
2
|
import type RestClientSdk from '../RestClientSdk';
|
|
4
3
|
import type { MetadataDefinition, SdkMetadata } from '../RestClientSdk';
|
|
4
|
+
import type AbstractClient from '../client/AbstractClient';
|
|
5
5
|
export declare function generateRepository<D extends MetadataDefinition>(sdk: RestClientSdk<SdkMetadata>, metadata: ClassMetadata, isUnitOfWorkEnabled?: boolean): AbstractClient<D>;
|
package/package.json
CHANGED
package/src/ErrorFactory.ts
CHANGED
|
@@ -27,7 +27,7 @@ class HttpError extends Error {
|
|
|
27
27
|
/**
|
|
28
28
|
* Get the JSON of the baseResponse directly, if possible
|
|
29
29
|
*/
|
|
30
|
-
public
|
|
30
|
+
public responseJson(): Promise<Record<string, unknown>> {
|
|
31
31
|
return this.baseResponse.json();
|
|
32
32
|
}
|
|
33
33
|
}
|
package/src/RestClientSdk.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import JsSerializer from './serializer/JsSerializer';
|
|
2
|
-
import UnitOfWork from './UnitOfWork';
|
|
3
1
|
import Mapping from './Mapping';
|
|
4
|
-
import TokenStorageInterface from './TokenStorageInterface';
|
|
5
|
-
import SerializerInterface from './serializer/SerializerInterface';
|
|
6
|
-
import AbstractClient from './client/AbstractClient';
|
|
7
|
-
import { Token } from './TokenGenerator/types';
|
|
8
2
|
// eslint-disable-next-line import/no-duplicates
|
|
9
3
|
import type RestClientSdkInterface from './RestClientSdkInterface';
|
|
10
4
|
// eslint-disable-next-line import/no-duplicates
|
|
11
5
|
import type { Config } from './RestClientSdkInterface';
|
|
6
|
+
import { Token } from './TokenGenerator/types';
|
|
7
|
+
import TokenStorageInterface from './TokenStorageInterface';
|
|
8
|
+
import UnitOfWork from './UnitOfWork';
|
|
9
|
+
import AbstractClient from './client/AbstractClient';
|
|
10
|
+
import JsSerializer from './serializer/JsSerializer';
|
|
11
|
+
import SerializerInterface from './serializer/SerializerInterface';
|
|
12
|
+
import { Logger } from './utils/logging';
|
|
12
13
|
import { generateRepository } from './utils/repositoryGenerator';
|
|
13
14
|
|
|
14
15
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -38,6 +39,8 @@ class RestClientSdk<M extends SdkMetadata>
|
|
|
38
39
|
|
|
39
40
|
#repositoryList: Partial<Record<keyof M, AbstractClient<M[keyof M]>>>;
|
|
40
41
|
|
|
42
|
+
public readonly logger?: Logger;
|
|
43
|
+
|
|
41
44
|
constructor(
|
|
42
45
|
tokenStorage: TokenStorageInterface<Token>,
|
|
43
46
|
config: Config,
|
|
@@ -60,6 +63,10 @@ class RestClientSdk<M extends SdkMetadata>
|
|
|
60
63
|
this.config.unitOfWorkEnabled
|
|
61
64
|
);
|
|
62
65
|
|
|
66
|
+
if (config.loggerEnabled) {
|
|
67
|
+
this.logger = new Logger();
|
|
68
|
+
}
|
|
69
|
+
|
|
63
70
|
this.#repositoryList = {};
|
|
64
71
|
}
|
|
65
72
|
|
|
@@ -77,7 +84,11 @@ class RestClientSdk<M extends SdkMetadata>
|
|
|
77
84
|
}
|
|
78
85
|
|
|
79
86
|
// eslint-disable-next-line new-cap
|
|
80
|
-
this.#repositoryList[key] = generateRepository<M[K]>(
|
|
87
|
+
this.#repositoryList[key] = generateRepository<M[K]>(
|
|
88
|
+
this,
|
|
89
|
+
metadata,
|
|
90
|
+
this.config.unitOfWorkEnabled
|
|
91
|
+
);
|
|
81
92
|
}
|
|
82
93
|
|
|
83
94
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import Mapping from './Mapping';
|
|
2
2
|
import { SdkMetadata } from './RestClientSdk';
|
|
3
3
|
import { Token } from './TokenGenerator/types';
|
|
4
4
|
import TokenStorageInterface from './TokenStorageInterface';
|
|
5
|
-
import
|
|
5
|
+
import AbstractClient from './client/AbstractClient';
|
|
6
|
+
import { Logger } from './utils/logging';
|
|
6
7
|
|
|
7
8
|
export type Config = {
|
|
8
9
|
path: string;
|
|
@@ -12,11 +13,14 @@ export type Config = {
|
|
|
12
13
|
authorizationType?: string; // default to "Bearer", but can be "Basic" or anything
|
|
13
14
|
useDefaultParameters?: boolean;
|
|
14
15
|
unitOfWorkEnabled?: boolean;
|
|
16
|
+
loggerEnabled?: boolean;
|
|
15
17
|
};
|
|
16
18
|
|
|
17
19
|
export default interface RestClientSdkInterface<M extends SdkMetadata> {
|
|
18
20
|
tokenStorage: TokenStorageInterface<Token>;
|
|
19
21
|
mapping: Mapping;
|
|
20
22
|
|
|
23
|
+
readonly logger?: Logger;
|
|
24
|
+
|
|
21
25
|
getRepository<K extends keyof M & string>(key: K): AbstractClient<M[K]>;
|
|
22
26
|
}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
InvalidScopeError,
|
|
7
7
|
OauthError,
|
|
8
8
|
} from '../ErrorFactory';
|
|
9
|
+
import { Logger } from '../utils/logging';
|
|
9
10
|
import TokenGeneratorInterface, {
|
|
10
11
|
TokenBodyReturn,
|
|
11
12
|
} from './TokenGeneratorInterface';
|
|
@@ -20,7 +21,12 @@ abstract class AbstractTokenGenerator<T extends Token, C>
|
|
|
20
21
|
implements TokenGeneratorInterface<T> {
|
|
21
22
|
readonly tokenGeneratorConfig: C;
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
protected logger?: Logger;
|
|
25
|
+
|
|
26
|
+
constructor(tokenGeneratorConfig: C, logger?: Logger) {
|
|
27
|
+
if (logger) {
|
|
28
|
+
this.logger = logger;
|
|
29
|
+
}
|
|
24
30
|
this.tokenGeneratorConfig = tokenGeneratorConfig;
|
|
25
31
|
if (typeof this.checkTokenGeneratorConfig === 'function') {
|
|
26
32
|
this.checkTokenGeneratorConfig(this.tokenGeneratorConfig);
|
|
@@ -46,7 +46,7 @@ class AuthorizationCodeFlowTokenGenerator extends AbstractTokenGenerator<
|
|
|
46
46
|
|
|
47
47
|
const url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
const params = {
|
|
50
50
|
method: 'POST',
|
|
51
51
|
headers: {
|
|
52
52
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -57,8 +57,14 @@ class AuthorizationCodeFlowTokenGenerator extends AbstractTokenGenerator<
|
|
|
57
57
|
client_secret: this.tokenGeneratorConfig.clientSecret, // TODO : secure this ?
|
|
58
58
|
redirect_uri: this.tokenGeneratorConfig.redirectUri,
|
|
59
59
|
code: parameters.code,
|
|
60
|
-
}),
|
|
61
|
-
}
|
|
60
|
+
}).toString(),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (this.logger) {
|
|
64
|
+
this.logger.logRequest({ url, ...params });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return fetch(url, params);
|
|
62
68
|
}
|
|
63
69
|
|
|
64
70
|
/**
|
|
@@ -80,7 +86,7 @@ class AuthorizationCodeFlowTokenGenerator extends AbstractTokenGenerator<
|
|
|
80
86
|
|
|
81
87
|
const url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
82
88
|
|
|
83
|
-
|
|
89
|
+
const params = {
|
|
84
90
|
method: 'POST',
|
|
85
91
|
headers: {
|
|
86
92
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
@@ -90,8 +96,14 @@ class AuthorizationCodeFlowTokenGenerator extends AbstractTokenGenerator<
|
|
|
90
96
|
client_id: this.tokenGeneratorConfig.clientId,
|
|
91
97
|
client_secret: this.tokenGeneratorConfig.clientSecret,
|
|
92
98
|
refresh_token: oldAccessToken.refresh_token,
|
|
93
|
-
}),
|
|
94
|
-
}
|
|
99
|
+
}).toString(),
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (this.logger) {
|
|
103
|
+
this.logger.logRequest({ url, ...params });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return fetch(url, params);
|
|
95
107
|
}
|
|
96
108
|
|
|
97
109
|
checkTokenGeneratorConfig(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable camelcase */
|
|
2
|
-
import AbstractTokenGenerator from './AbstractTokenGenerator';
|
|
3
2
|
import { memoizePromise } from '../decorator';
|
|
3
|
+
import AbstractTokenGenerator from './AbstractTokenGenerator';
|
|
4
4
|
import { Token, TokenResponse } from './types';
|
|
5
5
|
|
|
6
6
|
const ERROR_CONFIG_EMPTY = 'TokenGenerator config must be set';
|
|
@@ -55,15 +55,21 @@ class ClientCredentialsGenerator extends AbstractTokenGenerator<
|
|
|
55
55
|
body.append('scope', this.tokenGeneratorConfig.scope);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
return fetch(url, {
|
|
58
|
+
const params = {
|
|
61
59
|
method: 'POST',
|
|
62
60
|
headers: {
|
|
63
61
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
64
62
|
},
|
|
65
|
-
body,
|
|
66
|
-
}
|
|
63
|
+
body: body.toString(),
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
67
|
+
|
|
68
|
+
if (this.logger) {
|
|
69
|
+
this.logger.logRequest({ url, ...params });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return fetch(url, params);
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
refreshToken(
|
|
@@ -59,13 +59,19 @@ class PasswordGenerator extends AbstractTokenGenerator<PasswordToken, Config> {
|
|
|
59
59
|
|
|
60
60
|
const url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
61
61
|
|
|
62
|
-
|
|
62
|
+
const params = {
|
|
63
63
|
method: 'POST',
|
|
64
64
|
headers: {
|
|
65
65
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
66
66
|
},
|
|
67
|
-
body,
|
|
68
|
-
}
|
|
67
|
+
body: body.toString(),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
if (this.logger) {
|
|
71
|
+
this.logger.logRequest({ url, ...params });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return fetch(url, params);
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
refreshToken(
|
|
@@ -91,13 +97,19 @@ class PasswordGenerator extends AbstractTokenGenerator<PasswordToken, Config> {
|
|
|
91
97
|
|
|
92
98
|
const url = this.generateUrlFromConfig(this.tokenGeneratorConfig);
|
|
93
99
|
|
|
94
|
-
|
|
100
|
+
const params = {
|
|
95
101
|
method: 'POST',
|
|
96
102
|
headers: {
|
|
97
103
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
98
104
|
},
|
|
99
|
-
body,
|
|
100
|
-
}
|
|
105
|
+
body: body.toString(),
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
if (this.logger) {
|
|
109
|
+
this.logger.logRequest({ url, ...params });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return fetch(url, params);
|
|
101
113
|
}
|
|
102
114
|
|
|
103
115
|
checkTokenGeneratorConfig(config: Config): void {
|
package/src/TokenStorage.ts
CHANGED
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
/* eslint-disable camelcase */
|
|
2
|
-
import TokenGeneratorInterface, {
|
|
3
|
-
TokenBodyReturn,
|
|
4
|
-
} from './TokenGenerator/TokenGeneratorInterface';
|
|
5
|
-
import {
|
|
6
|
-
ErrorBody,
|
|
7
|
-
Token,
|
|
8
|
-
TokenBody,
|
|
9
|
-
TokenResponse,
|
|
10
|
-
} from './TokenGenerator/types';
|
|
11
2
|
import AsyncStorageInterface from './AsyncStorageInterface';
|
|
12
|
-
import { memoizePromise } from './decorator';
|
|
13
|
-
// eslint-disable-next-line import/no-duplicates
|
|
14
|
-
import type TokenStorageInterface from './TokenStorageInterface';
|
|
15
|
-
// eslint-disable-next-line import/no-duplicates
|
|
16
|
-
import type { HasExpiresAt } from './TokenStorageInterface';
|
|
17
3
|
import {
|
|
18
4
|
getHttpErrorFromResponse,
|
|
19
5
|
InvalidGrantError,
|
|
@@ -21,6 +7,16 @@ import {
|
|
|
21
7
|
isOauthError,
|
|
22
8
|
OauthError,
|
|
23
9
|
} from './ErrorFactory';
|
|
10
|
+
import TokenGeneratorInterface, {
|
|
11
|
+
TokenBodyReturn,
|
|
12
|
+
} from './TokenGenerator/TokenGeneratorInterface';
|
|
13
|
+
import { ErrorBody, Token, TokenResponse } from './TokenGenerator/types';
|
|
14
|
+
// eslint-disable-next-line import/no-duplicates
|
|
15
|
+
import type TokenStorageInterface from './TokenStorageInterface';
|
|
16
|
+
// eslint-disable-next-line import/no-duplicates
|
|
17
|
+
import type { HasExpiresAt } from './TokenStorageInterface';
|
|
18
|
+
import { memoizePromise } from './decorator';
|
|
19
|
+
import { Logger } from './utils/logging';
|
|
24
20
|
|
|
25
21
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
22
|
function isResponse(response: any): response is Response {
|
|
@@ -52,16 +48,24 @@ class TokenStorage<T extends Token> implements TokenStorageInterface<T> {
|
|
|
52
48
|
|
|
53
49
|
#asyncStorage!: AsyncStorageInterface;
|
|
54
50
|
|
|
51
|
+
#logger?: Logger;
|
|
52
|
+
|
|
55
53
|
constructor(
|
|
56
54
|
tokenGenerator: TokenGeneratorInterface<T>,
|
|
57
55
|
asyncStorage: AsyncStorageInterface,
|
|
58
|
-
accessTokenKey = 'rest_client_sdk.api.access_token'
|
|
56
|
+
accessTokenKey = 'rest_client_sdk.api.access_token',
|
|
57
|
+
logger?: Logger
|
|
59
58
|
) {
|
|
59
|
+
if (logger) {
|
|
60
|
+
this.#logger = logger;
|
|
61
|
+
}
|
|
60
62
|
this.#tokenGenerator = tokenGenerator;
|
|
61
63
|
this.#hasATokenBeenGenerated = false;
|
|
62
64
|
this.setAsyncStorage(asyncStorage);
|
|
63
65
|
this.accessTokenKey = accessTokenKey;
|
|
64
66
|
|
|
67
|
+
this.handleTokenResponse = this.handleTokenResponse.bind(this);
|
|
68
|
+
|
|
65
69
|
this.generateToken = memoizePromise(this.generateToken);
|
|
66
70
|
this.refreshToken = memoizePromise(this.refreshToken);
|
|
67
71
|
}
|
|
@@ -179,6 +183,11 @@ class TokenStorage<T extends Token> implements TokenStorageInterface<T> {
|
|
|
179
183
|
if (isResponse(responseOrBody)) {
|
|
180
184
|
const response = responseOrBody;
|
|
181
185
|
const originalResponse = response.clone();
|
|
186
|
+
|
|
187
|
+
if (this.#logger) {
|
|
188
|
+
this.#logger.logResponse(response);
|
|
189
|
+
}
|
|
190
|
+
|
|
182
191
|
return response
|
|
183
192
|
.json()
|
|
184
193
|
.then((body) => {
|
package/src/UnitOfWork.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
2
|
import { diff } from 'deep-diff';
|
|
3
|
-
import { isImmutable } from './isImmutable';
|
|
4
3
|
import Mapping from './Mapping';
|
|
4
|
+
import Attribute from './Mapping/Attribute';
|
|
5
5
|
import ClassMetadata, {
|
|
6
6
|
DefaultSerializedModelType,
|
|
7
7
|
} from './Mapping/ClassMetadata';
|
|
8
|
-
import
|
|
8
|
+
import { isImmutable } from './isImmutable';
|
|
9
9
|
|
|
10
10
|
type Id = string | number;
|
|
11
11
|
type StringKeyObject = Record<string, any>;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import URI from 'urijs';
|
|
2
2
|
import { OauthError, getHttpErrorFromResponse } from '../ErrorFactory';
|
|
3
|
-
import
|
|
4
|
-
import { removeAuthorization, removeUndefinedHeaders } from './headerUtils';
|
|
3
|
+
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
5
4
|
// eslint-disable-next-line import/no-duplicates
|
|
6
5
|
import type RestClientSdk from '../RestClientSdk';
|
|
7
6
|
// eslint-disable-next-line import/no-duplicates
|
|
8
7
|
import type { MetadataDefinition, SdkMetadata } from '../RestClientSdk';
|
|
9
|
-
import type ClassMetadata from '../Mapping/ClassMetadata';
|
|
10
|
-
import type SerializerInterface from '../serializer/SerializerInterface';
|
|
11
8
|
import { Token } from '../TokenGenerator/types';
|
|
9
|
+
import TokenStorageInterface from '../TokenStorageInterface';
|
|
10
|
+
import type SerializerInterface from '../serializer/SerializerInterface';
|
|
12
11
|
import { generateRepository } from '../utils/repositoryGenerator';
|
|
12
|
+
import { removeAuthorization, removeUndefinedHeaders } from './headerUtils';
|
|
13
13
|
|
|
14
14
|
const EXPIRE_LIMIT_SECONDS = 300; // = 5 minutes
|
|
15
15
|
|
|
@@ -265,17 +265,19 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
265
265
|
response
|
|
266
266
|
) as D['list'];
|
|
267
267
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
268
|
+
if (this.#isUnitOfWorkEnabled) {
|
|
269
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
270
|
+
for (const decodedItem of itemList) {
|
|
271
|
+
const identifier = this._getEntityIdentifier(decodedItem);
|
|
272
|
+
const normalizedItem = this.serializer.normalizeItem(
|
|
273
|
+
decodedItem,
|
|
274
|
+
this.metadata
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
// then we register the re-normalized item
|
|
278
|
+
if (identifier !== null) {
|
|
279
|
+
this.sdk.unitOfWork.registerClean(identifier, normalizedItem);
|
|
280
|
+
}
|
|
279
281
|
}
|
|
280
282
|
}
|
|
281
283
|
|
|
@@ -428,9 +430,9 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
428
430
|
// https://tools.ietf.org/html/rfc2617#section-1.2
|
|
429
431
|
const authorizationHeader = response.headers.get('www-authenticate');
|
|
430
432
|
if (authorizationHeader) {
|
|
431
|
-
const invalidGrant =
|
|
432
|
-
'error
|
|
433
|
-
|
|
433
|
+
const invalidGrant =
|
|
434
|
+
authorizationHeader.indexOf('error="invalid_grant"') > -1;
|
|
435
|
+
|
|
434
436
|
if (invalidGrant && this.#tokenStorage) {
|
|
435
437
|
return this._refreshTokenAndRefetch(input, requestParams);
|
|
436
438
|
}
|
|
@@ -470,12 +472,6 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
470
472
|
baseHeaders.Authorization = `${this.sdk.config.authorizationType} ${accessToken}`;
|
|
471
473
|
}
|
|
472
474
|
|
|
473
|
-
const currentUri =
|
|
474
|
-
typeof window === 'object' && window.location && window.location.href;
|
|
475
|
-
if (currentUri) {
|
|
476
|
-
baseHeaders.Referer = currentUri;
|
|
477
|
-
}
|
|
478
|
-
|
|
479
475
|
if (params) {
|
|
480
476
|
if (!params.headers) {
|
|
481
477
|
params.headers = {};
|
|
@@ -490,8 +486,17 @@ class AbstractClient<D extends MetadataDefinition> {
|
|
|
490
486
|
params.headers = removeUndefinedHeaders(params.headers);
|
|
491
487
|
}
|
|
492
488
|
|
|
489
|
+
let logId: undefined | string;
|
|
490
|
+
if (this.sdk.logger) {
|
|
491
|
+
logId = this.sdk.logger.logRequest({ url: input, ...params });
|
|
492
|
+
}
|
|
493
|
+
|
|
493
494
|
// eslint-disable-next-line consistent-return
|
|
494
495
|
return fetch(input, params).then((response) => {
|
|
496
|
+
if (this.sdk.logger) {
|
|
497
|
+
this.sdk.logger.logResponse(response, logId);
|
|
498
|
+
}
|
|
499
|
+
|
|
495
500
|
if (response.status < 400) {
|
|
496
501
|
return response;
|
|
497
502
|
}
|