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

return object instead of string #51

Merged
merged 7 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vippsno/vipps-sdk",
"version": "0.9.14",
"version": "0.9.15",
tomas-zijdemans-vipps marked this conversation as resolved.
Show resolved Hide resolved
"homepage": "https://developer.vippsmobilepay.com/docs/SDKs/node-sdk",
"repository": {
"type": "git",
Expand Down
13 changes: 8 additions & 5 deletions src/utils/http-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ function makeRequest<TR>(
try {
const body = Buffer.concat(chunks).toString();
if (!res.statusCode || res.statusCode < 200 || res.statusCode > 299) {
const error = new Error(`path=${req.path} ,statusCode=${res.statusCode}, contents=${body}`);
reject(error);
throw new Error(body);
} else if (res.headers['content-type']?.includes('application/json')) {
resolve(JSON.parse(body));
} else if (res.headers['content-type']?.includes('text/plain')) {
resolve(body as TR);
}
resolve(null as TR);
} catch (e) {
reject(e);
if (e instanceof Error) {
const message = JSON.parse(e.message);
const result = { ...message, ok: false };
resolve(result as TR);
}
}
});
})
Expand All @@ -63,9 +66,9 @@ function makeRequest<TR>(
}

export const get = <TR>(hostname: string, path: string, headers: OutgoingHttpHeaders): Promise<TR> =>
retry(() => makeRequest<TR>(hostname, 'GET', path, headers), { retries: 4 });
retry(() => makeRequest<TR>(hostname, 'GET', path, headers), { retries: 3 });

export const post = <TI, TR>(hostname: string, path: string, headers: OutgoingHttpHeaders, requestData?: TI) =>
retry(() => makeRequest<TR>(hostname, 'POST', path, headers, requestData), {
retries: 4,
retries: 3,
});