99 NetsuiteResponse ,
1010} from "./types.js" ;
1111import { NetsuiteError } from "./errors.js" ;
12+ import { removeLeadingSlash , removeTrailingSlash } from "./utils.js" ;
1213
1314export default class NetsuiteApiClient {
1415 consumer_key : string ;
@@ -28,15 +29,16 @@ export default class NetsuiteApiClient {
2829 this . version = "1.0" ;
2930 this . algorithm = "HMAC-SHA256" ;
3031 this . realm = options . realm ;
31- this . base_url = options . base_url ;
32+ this . base_url = options . base_url ? removeTrailingSlash ( options . base_url ) : undefined ;
3233 }
3334
3435 /**
3536 * Retrieve the Authorization Header
36- * @param options
3737 * @returns
38+ * @param url
39+ * @param method
3840 */
39- getAuthorizationHeader ( url : string , method : string ) {
41+ getAuthorizationHeader ( url : string , method : string ) : { [ key : string ] : string } {
4042 const oauth = new OAuth ( {
4143 consumer : {
4244 key : this . consumer_key ,
@@ -68,16 +70,20 @@ export default class NetsuiteApiClient {
6870 * @returns
6971 */
7072 public async request ( opts : NetsuiteRequestOptions ) {
71- const { path = "*" , method = "GET" , body = "" , heads = { } } = opts ;
73+ const { path = "*" , method = "GET" , body = "" , heads = { } , restletUrl } = opts ;
74+ const cleanPath = removeLeadingSlash ( path ) ;
75+ // Set up the Request URI
7276
73- // Setup the Request URI
74- let uri ;
75- if ( this . base_url ) uri = `${ this . base_url } /services/rest/${ path } ` ;
76- else {
77- // as suggested by dylbarne in #15: sanitize url to enhance overall usability
78- uri = `https://${ this . realm
77+ // as suggested by dylbarne in #15: sanitize url to enhance overall usability
78+ let uri = `https://${ this . realm
7979 . toLowerCase ( )
80- . replace ( "_" , "-" ) } .suitetalk.api.netsuite.com/services/rest/${ path } `;
80+ . replace ( "_" , "-" ) } .suitetalk.api.netsuite.com/services/rest/${ cleanPath } `;
81+ if ( this . base_url ) {
82+ uri = `${ this . base_url } /services/rest/${ cleanPath } `
83+ }
84+ // if restletUrl is provided, use it instead of the default uri
85+ if ( restletUrl ) {
86+ uri = restletUrl ;
8187 }
8288
8389 const options = {
0 commit comments