Skip to content

Commit 5d0ad15

Browse files
committed
Refactor Client to use Client object
1 parent 036e2c2 commit 5d0ad15

File tree

1 file changed

+75
-53
lines changed

1 file changed

+75
-53
lines changed

src/client.ts

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,103 @@
11
import Config from "./config";
2+
import { TERMINAL_API_ENDPOINT_TEST } from "./config";
3+
24
import HttpURLConnectionClient from "./httpClient/httpURLConnectionClient";
35
import ClientInterface from "./httpClient/clientInterface";
46

5-
type ClientParametersOverload =
6-
| { config: Config }
7-
| { config: Config; httpClient: ClientInterface }
8-
| { username: string; password: string; environment: Environment}
9-
| { username: string; password: string; environment: Environment; httpClient: ClientInterface }
10-
| { username: string; password: string; environment: Environment; liveEndpointUrlPrefix: string }
11-
| { username: string; password: string; environment: Environment; liveEndpointUrlPrefix: string; httpClient: ClientInterface }
12-
| { username: string; password: string; environment: Environment; applicationName: string }
13-
| { username: string; password: string; environment: Environment; applicationName: string; httpClient: ClientInterface }
14-
| { username: string; password: string; environment: Environment; applicationName: string; liveEndpointUrlPrefix: string }
15-
| { username: string; password: string; environment: Environment; applicationName: string; liveEndpointUrlPrefix: string; httpClient: ClientInterface }
16-
| { apiKey: string; environment: Environment }
17-
| { apiKey: string; environment: Environment; httpClient: ClientInterface }
18-
| { apiKey: string; environment: Environment; liveEndpointUrlPrefix: string }
19-
| { apiKey: string; environment: Environment; liveEndpointUrlPrefix: string; httpClient: ClientInterface };
20-
21-
interface ClientParameters {
22-
config?: Config;
23-
username?: string;
24-
password?: string;
25-
environment?: Environment;
26-
applicationName?: string;
27-
liveEndpointUrlPrefix?: string;
28-
apiKey?: string;
29-
httpClient?: ClientInterface;
30-
}
317

8+
/**
9+
* Main Adyen API Client class.
10+
* Handles configuration, authentication, and HTTP client setup for API requests.
11+
*/
3212
class Client {
13+
// Static endpoints and API version constants
14+
// @deprecated: use Config.TERMINAL_API_ENDPOINT_TEST instead
15+
public static TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com";
16+
// @deprecated: use Config.TERMINAL_API_ENDPOINT_LIVE instead
17+
public static TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com";
18+
// legacy support for marketPayEndpoint
3319
public static MARKETPAY_ENDPOINT_TEST = "https://cal-test.adyen.com/cal/services";
3420
public static MARKETPAY_ENDPOINT_LIVE = "https://cal-live.adyen.com/cal/services";
3521
public static MARKETPAY_ACCOUNT_API_VERSION = "v6";
3622
public static MARKETPAY_FUND_API_VERSION = "v6";
3723
public static MARKETPAY_HOP_API_VERSION = "v6";
3824
public static MARKETPAY_NOTIFICATION_API_VERSION = "v5";
3925
public static MARKETPAY_NOTIFICATION_CONFIGURATION_API_VERSION = "v6";
40-
public static TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com";
41-
public static TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com";
26+
4227

4328
private _httpClient!: ClientInterface;
4429
public config: Config;
45-
public liveEndpointUrlPrefix: string;
46-
47-
public constructor(clientParameters: ClientParametersOverload);
48-
public constructor(options: ClientParameters) {
49-
if (options.config) {
50-
this.config = options.config;
51-
} else {
52-
this.config = new Config();
30+
31+
/**
32+
* Constructs a new Client instance.
33+
* @param options - Configuration object
34+
*/
35+
public constructor(options: Config, httpClient?: ClientInterface) {
36+
37+
this.config = options;
38+
39+
if (!this.config.environment) {
40+
throw new Error("Environment must be defined");
5341
}
54-
this.liveEndpointUrlPrefix = options.liveEndpointUrlPrefix ?? "";
55-
56-
const environment = options.environment ?? this.config.environment;
57-
if (environment) {
58-
this.setEnvironment(environment, options.liveEndpointUrlPrefix);
59-
if (options.username && options.password) {
60-
this.config.username = options.username;
61-
this.config.password = options.password;
62-
}
6342

64-
if (options.apiKey) {
65-
this.config.apiKey = options.apiKey;
43+
// set Terminal API endpoints
44+
if (this.config.environment === "TEST") {
45+
// one TEST endpoint for all regions
46+
this.config.terminalApiCloudEndpoint = TERMINAL_API_ENDPOINT_TEST;
47+
} else if (this.config.environment === "LIVE") {
48+
// region-based LIVE endpoints
49+
if(this.config.region) {
50+
if (!Config.isRegionValid(this.config.region)) {
51+
throw new Error(`Invalid region provided: ${this.config.region}`);
52+
}
53+
this.config.terminalApiCloudEndpoint = Config.getTerminalApiEndpoint(this.config.region);
54+
} else {
55+
console.info("TerminalAPI Region is not be defined: ignore this if you are not using Terminal API.");
6656
}
6757
}
6858

59+
if(this.config.environment === "LIVE" && !this.config.liveEndpointUrlPrefix) {
60+
throw new Error("Live endpoint URL prefix must be provided for LIVE environment.");
61+
}
62+
63+
// legacy support for marketPayEndpoint
64+
if (this.config.environment === "TEST") {
65+
this.config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_TEST;
66+
} else if (this.config.environment === "LIVE") {
67+
this.config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_LIVE;
68+
}
69+
70+
// Set application name if provided
6971
if(options.applicationName) {
7072
this.config.applicationName = options.applicationName;
7173
}
7274

73-
if (options.httpClient) {
74-
this._httpClient = options.httpClient;
75+
// Set custom HTTP client if provided
76+
if (httpClient) {
77+
this._httpClient = httpClient;
7578
}
79+
7680
}
7781

82+
/**
83+
* Sets the environment and updates endpoints in the config.
84+
* @param environment - The environment ("TEST" or "LIVE").
85+
* @param liveEndpointUrlPrefix - Optional live endpoint prefix.
86+
*/
7887
public setEnvironment(environment: Environment, liveEndpointUrlPrefix?: string): void {
79-
// ensure environment and liveUrlPrefix is set in config
88+
// ensure environment is set in config
8089
this.config.environment = environment;
81-
this.liveEndpointUrlPrefix = liveEndpointUrlPrefix ?? "";
8290

8391
if (environment === "TEST") {
8492
this.config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_TEST;
85-
this.config.terminalApiCloudEndpoint = Client.TERMINAL_API_ENDPOINT_TEST;
8693
} else if (environment === "LIVE") {
8794
this.config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_LIVE;
88-
this.config.terminalApiCloudEndpoint = Client.TERMINAL_API_ENDPOINT_LIVE;
8995
}
9096
}
9197

98+
/**
99+
* Gets the HTTP client instance, creating a default one if not set.
100+
*/
92101
public get httpClient(): ClientInterface {
93102
if (!this._httpClient) {
94103
this._httpClient = new HttpURLConnectionClient();
@@ -97,17 +106,30 @@ class Client {
97106
return this._httpClient;
98107
}
99108

109+
/**
110+
* Sets a custom HTTP client.
111+
* @param httpClient - The HTTP client to use.
112+
*/
100113
public set httpClient(httpClient: ClientInterface) {
101114
this._httpClient = httpClient;
102115
}
103116

117+
/**
118+
* Sets the application name in the config.
119+
* @param applicationName - The application name.
120+
*/
104121
public setApplicationName(applicationName: string): void {
105122
this.config.applicationName = applicationName;
106123
}
107124

125+
/**
126+
* Sets the connection timeout in milliseconds.
127+
* @param connectionTimeoutMillis - Timeout in milliseconds.
128+
*/
108129
public setTimeouts(connectionTimeoutMillis: number): void {
109130
this.config.connectionTimeoutMillis = connectionTimeoutMillis;
110131
}
111132
}
112133

113134
export default Client;
135+

0 commit comments

Comments
 (0)