Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: timeout as parameter #82

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/transbank/common/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ class Options {
/** Environment correspond to the environment to use, it can be Integration or
* Production, each has a unique URL. */
environment: string;
/** Timeout for requests in milliseconds */
timeout: number;

/**
* Create an instance of Options
* @param commerceCode unique commerce identifier provided by Transbank
* @param apiKey the secret used to authenticate against the API, it must be kept safe at all times.
* @param environment Environment correspond to the environment to use, it can be Integration or
* Production, each has a unique URL.
* @param timeout Timeout for requests in milliseconds
*/
constructor(commerceCode: string, apiKey: string, environment: string) {
constructor(commerceCode: string, apiKey: string, environment: string, timeout?: number) {
this.commerceCode = commerceCode;
this.apiKey = apiKey;
this.environment = environment;
this.timeout = timeout ?? 60000;
}
}

Expand Down
10 changes: 6 additions & 4 deletions lib/transbank/common/request_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const RequestService = {
method: request.method,
url: options.environment + request.endpoint,
headers: requestHeaders,
timeout: 10000,
timeout: options.timeout,
data: request.toJson(),
})
.then((response) => {
Expand Down Expand Up @@ -67,12 +67,14 @@ const RequestService = {
return response.data;
})
.catch((error) => {
console.log(error)
let response = error.response;
let msg = (response?.data?.code || response?.data?.description) ? (`${response.data.code} - ${response.data.description}`) : 'Unexpected error';
let msg =
response?.data?.code || response?.data?.description
? `${response.data.code} - ${response.data.description}`
: 'Unexpected error';
throw new TransbankError(error, msg);
});
}
},
};

export default RequestService;
Loading