Skip to content

Commit febfb59

Browse files
committed
1.1.1 fix uploading formdata
1 parent 078e7d5 commit febfb59

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rantalainen/fortnox-api-client",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {

src/index.ts

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Api, ApiConfig } from './api';
1+
import { Api, ApiConfig, ContentType, FolderFileRowWrap, RequestParams } from './api';
22
import axios, { AxiosRequestConfig } from 'axios';
33
import { HttpsAgent } from 'agentkeepalive';
44
import { IFortnoxApiClientConfig, IFortnoxApiClientOptions, IAccessTokens, FortnoxScope } from './interfaces';
@@ -7,11 +7,7 @@ import https from 'https';
77
import CacheableLookup from 'cacheable-lookup';
88
import FormData from 'form-data';
99

10-
export {
11-
IFortnoxApiClientConfig,
12-
IFortnoxApiClientOptions,
13-
FortnoxScope,
14-
};
10+
export { IFortnoxApiClientConfig, IFortnoxApiClientOptions, FortnoxScope, FileBuffer };
1511

1612
// DNS cache to prevent ENOTFOUND and other such issues
1713
const dnsCache = new CacheableLookup();
@@ -121,8 +117,13 @@ export class FortnoxApiClient {
121117
}
122118
}
123119

124-
// The API requires these headers to be set
125-
config.headers.set('Content-Type', 'application/json');
120+
// // The API requires these headers to be set
121+
if (config.data instanceof FormData) {
122+
config.headers.set('Content-Type', 'multipart/form-data');
123+
} else {
124+
config.headers.set('Content-Type', 'application/json');
125+
}
126+
126127
config.headers.set('Accept', 'application/json');
127128

128129
return config;
@@ -157,18 +158,21 @@ export class FortnoxApiClient {
157158
refresh_token: this.tokens.refreshToken
158159
});
159160

160-
const accessTokenRequest = await axios.post('https://apps.fortnox.se/oauth-v1/token', params, {
161-
headers: {
162-
'Content-Type': 'application/x-www-form-urlencoded',
163-
Authorization: `Basic ${Buffer.from(`${this.options.clientId}:${this.options.clientSecret}`).toString('base64')}`
164-
}
165-
}).catch((error) => {
166-
if (error?.response) {
167-
error.message = `Fortnox HTTP error ${error.response.status} (${error.response.statusText}): ` + JSON.stringify(error.response.data);
168-
}
161+
const accessTokenRequest = await axios
162+
.post('https://apps.fortnox.se/oauth-v1/token', params, {
163+
headers: {
164+
'Content-Type': 'application/x-www-form-urlencoded',
165+
Authorization: `Basic ${Buffer.from(`${this.options.clientId}:${this.options.clientSecret}`).toString('base64')}`
166+
}
167+
})
168+
.catch((error) => {
169+
if (error?.response) {
170+
error.message =
171+
`Fortnox HTTP error ${error.response.status} (${error.response.statusText}): ` + JSON.stringify(error.response.data);
172+
}
169173

170-
throw error;
171-
});
174+
throw error;
175+
});
172176

173177
this.tokens.accessToken = accessTokenRequest.data.access_token;
174178
this.tokens.refreshToken = accessTokenRequest.data.refresh_token;
@@ -234,18 +238,21 @@ export class FortnoxApiClient {
234238
redirect_uri: redirectUri
235239
});
236240

237-
const accessTokenRequest = await axios.post('https://apps.fortnox.se/oauth-v1/token', params, {
238-
headers: {
239-
'Content-Type': 'application/x-www-form-urlencoded',
240-
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
241-
}
242-
}).catch((error) => {
243-
if (error?.response) {
244-
error.message = `Fortnox HTTP error ${error.response.status} (${error.response.statusText}): ` + JSON.stringify(error.response.data);
245-
}
241+
const accessTokenRequest = await axios
242+
.post('https://apps.fortnox.se/oauth-v1/token', params, {
243+
headers: {
244+
'Content-Type': 'application/x-www-form-urlencoded',
245+
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
246+
}
247+
})
248+
.catch((error) => {
249+
if (error?.response) {
250+
error.message =
251+
`Fortnox HTTP error ${error.response.status} (${error.response.statusText}): ` + JSON.stringify(error.response.data);
252+
}
246253

247-
throw error;
248-
});
254+
throw error;
255+
});
249256

250257
return { accessToken: accessTokenRequest.data.access_token, refreshToken: accessTokenRequest.data.refresh_token };
251258
}

src/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type FortnoxScope =
2222
| 'price'
2323
| 'project'
2424
| 'profile'
25+
| 'supplier'
2526
| 'supplierinvoice';
2627

2728
export interface IAccessTokens {

0 commit comments

Comments
 (0)