Skip to content

Commit

Permalink
throw original error + v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Samu Toljamo committed Jul 27, 2022
1 parent cf059c2 commit 4f9c4b3
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 98 deletions.
81 changes: 38 additions & 43 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

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

98 changes: 47 additions & 51 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,58 +49,54 @@ export class Fivaldi {
uri?: string,
query?: string
}): Promise<any> => {
try {
const baseUrl = "https://api.fivaldi.net"
const { body, method, uri } = config;
let bodyMD5 = ""
let contentType = ""
const query = config.query || ""
const timestamp: string = Math.floor(new Date().getTime() / 1000).toString();
let headers = [
{ key: "X-Fivaldi-Partner", value: this.fivaldiPartner},
{ key: "X-Fivaldi-Timestamp", value: timestamp },
]

if (body) {
bodyMD5 = md5(body)
contentType = "application/json"
headers.push({ key: "Content-Type", value: contentType })
}

let stringToSign: string = [
method || "GET",
bodyMD5,
contentType,
...headers.sort(function (a, b) {
return a.key.toLowerCase().localeCompare(b.key.toLowerCase())
}).filter(header => header.key.startsWith("X-Fivaldi"))
.map(header => `${header.key.trim().toLowerCase()}:${typeof header.value === "string" ? header.value.trim() : header.value}`),
uri
].join(LF);

if (query) {
stringToSign += LF + query;
}

const mac = hmac(
stringToSign,
this.clientSecret
);

headers.push({ key: "Authorization", value: `Fivaldi ${mac}`})
const axiosResponse = await axios({
method,
url: baseUrl + uri + query,
headers: headers.reduce((result, header) => {
result[header.key] = header.value;
return result;
}, {}),
data: body
});
return axiosResponse.data
} catch (error) {
throw Error(error.response.data.message || error.response.statusText || "An error occured.")
const baseUrl = "https://api.fivaldi.net"
const { body, method, uri } = config;
let bodyMD5 = ""
let contentType = ""
const query = config.query || ""
const timestamp: string = Math.floor(new Date().getTime() / 1000).toString();
let headers = [
{ key: "X-Fivaldi-Partner", value: this.fivaldiPartner},
{ key: "X-Fivaldi-Timestamp", value: timestamp },
]

if (body) {
bodyMD5 = md5(body)
contentType = "application/json"
headers.push({ key: "Content-Type", value: contentType })
}

let stringToSign: string = [
method || "GET",
bodyMD5,
contentType,
...headers.sort(function (a, b) {
return a.key.toLowerCase().localeCompare(b.key.toLowerCase())
}).filter(header => header.key.startsWith("X-Fivaldi"))
.map(header => `${header.key.trim().toLowerCase()}:${typeof header.value === "string" ? header.value.trim() : header.value}`),
uri
].join(LF);

if (query) {
stringToSign += LF + query;
}

const mac = hmac(
stringToSign,
this.clientSecret
);

headers.push({ key: "Authorization", value: `Fivaldi ${mac}`})
const axiosResponse = await axios({
method,
url: baseUrl + uri + query,
headers: headers.reduce((result, header) => {
result[header.key] = header.value;
return result;
}, {}),
data: body
});
return axiosResponse.data
}

/**
Expand Down
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": "fivaldi-api",
"version": "1.1.0",
"version": "1.2.0",
"description": "With this marvellous library you can connect easily to Visma Fivaldi API.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 4f9c4b3

Please sign in to comment.