1- import { Api , ApiConfig } from './api' ;
1+ import { Api , ApiConfig , ContentType , FolderFileRowWrap , RequestParams } from './api' ;
22import axios , { AxiosRequestConfig } from 'axios' ;
33import { HttpsAgent } from 'agentkeepalive' ;
44import { IFortnoxApiClientConfig , IFortnoxApiClientOptions , IAccessTokens , FortnoxScope } from './interfaces' ;
@@ -7,11 +7,7 @@ import https from 'https';
77import CacheableLookup from 'cacheable-lookup' ;
88import 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
1713const 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 }
0 commit comments