@stylusapparel/opv3-merchant-api-nodejs
Version:
This is the official NodeJs wrapper for connecting to the StylusOP API V3
157 lines (143 loc) • 4.93 kB
JavaScript
;
const __error = require('./error');
const __response = require('./response');
const { __defaults } = require('../constants/url');
const http = require('./http');
/**
* Creates an authentication handler
* @param {string} _merchantId - Client ID
* @param {string} _token - Authentication token
* @param {Object} __config - Configuration object
* @returns {Object} Authentication methods
*/
module.exports = (_merchantId, _token, __config) => {
const httpInstance = http(_merchantId, _token, __config);
const {
merchantName,
apiVersion = __defaults.LATEST_VERSION,
tokenType = 'basic'
} = __config;
// Validate token type
if (!['basic', 'jwt', 'bearer'].includes(tokenType)) {
throw new Error('Invalid token type. Must be either "basic", "jwt" or "bearer"');
}
const __this = {
_merchantId,
_token,
// Will hold the authenticated API access token
_accessToken: null,
// // Will hold the httpInstance
// _httpInstance: httpInstance,
_isAuthenticated: () => __this._accessToken ? true : false,
_verify: () => {
return new Promise((resolve, reject) => {
if (!__this._token) {
return reject(__error._tokenMissing());
}
if (!_merchantId || !merchantName) {
return reject(__error._merchantNameMissing());
}
if (!__defaults.SUPPORTED_VERSIONS.includes(apiVersion)) {
return reject(__error._versionUnknownIssue());
}
resolve(true);
});
},
_isTokenValid_OLD: () => {
return new Promise((resolve, reject) => {
const validationUrl = `/merchant/${merchantName}${__defaults.TOKEN_VALIDATION_URI}`;
const validationData = {
merchantName,
apiToken: __this._token,
tokenType
};
httpInstance
.get(validationUrl, validationData)
.then(response => {
if (response.status === 200) {
resolve(__response._tokenValidation());
} else {
reject(__error._tokenError(response.data, response.status));
}
})
.catch(error => {
const errorStatus = error?.response?.status || 500;
const errorDetails = error?.response?.data || {};
reject(__error._tokenError(errorDetails, errorStatus));
});
});
},
/**
* New! Requests an OAuth token from the API.
*
* @returns {Promise} A promise that resolves with a successful token validation response if the API responds with a status of 200.
* Otherwise, it rejects with a token error, including the response data and status.
* In case of a network or other error, it rejects with a token error using the error details and a default status of 500.
*/
_oauthToken: () => {
// console.log('oauthToken');
return new Promise((resolve, reject) => {
const apiUri = `/merchant${__defaults.OAUTH_REQUEST_TOKEN_URI}`;
const oauthPayload = {
merchantId: __this._merchantId,
apiSecret: __this._token,
};
// console.log('url --- ', apiUri);
// console.log('oauthPayload', oauthPayload);
httpInstance
.post(apiUri, oauthPayload)
.then(response => {
// console.log('oauthToken response', response);
if (response.status === 200) {
resolve(__response._tokenValidation(response));
// Cache the access token in the client instance
__this._accessToken = response.data.accessToken;
} else {
__this._accessToken = null;
reject(__error._tokenError(response.data, response.status));
}
})
.catch(error => {
// console.log('oauthToken error', error);
__this._accessToken = null;
const errorStatus = error?.response?.status || 500;
const errorDetails = error?.response?.data || {};
reject(__error._tokenError(errorDetails, errorStatus));
});
});
},
_verifyOauthToken: (accessToken=null) => {
// console.log('verifyOauthToken');
return new Promise((resolve, reject) => {
if (!__this._accessToken && !accessToken) {
return reject(__error._tokenMissing());
}
const validationUrl = `/merchant${__defaults.TOKEN_VALIDATION_URI}`;
const validationData = {
// merchantName,
merchantId: __this._merchantId,
accessToken: accessToken ?? __this._accessToken,
};
httpInstance
.post(validationUrl, validationData)
.then(response => {
if (response.status === 200) {
// console.log('verifyOauthToken response', response.data);
resolve(response.data);
} else {
__this._accessToken = null;
reject(__error._tokenError(response.data, response.status));
}
})
.catch(error => {
// console.log('verifyOauthToken error', error);
__this._accessToken = null;
const errorStatus = error?.response?.status || 500;
const errorDetails = error?.response?.data || {};
reject(__error._tokenError(errorDetails, errorStatus));
});
});
},
};
return __this;
};