UNPKG

@stylusapparel/opv3-merchant-api-nodejs

Version:

This is the official NodeJs wrapper for connecting to the StylusOP API V3

57 lines (54 loc) 2.27 kB
'use strict'; const __auth = require('./lib/auth'); const __inventory = require('./inventory'); const __order = require('./order'); const __products = require('./products'); const __shipment = require('./shipment'); /** * Creates a client instance to interact with the Stylus API * @param {string} _merchantId - Merchant ID similar to client_id * @param {string} _token - Authentication token similar to client_secret * @param {Object} _config - Configuration object * @param {boolean} [_config.sandbox=false] - Use sandbox environment * @param {string} [_config.merchantName=''] - Merchant name or ID * @param {string} [_config.apiVersion=__defaults.LATEST_VERSION] - API version to use * @param {string} [_config.tokenType='basic'] - Type of token ('basic' or 'jwt') * @param {number} [_config.timeout=30000] - Request timeout in milliseconds * @param {number} [_config.maxRetries=3] - Maximum number of retries for failed requests * @returns {Object} Client instance with methods to interact with the API */ module.exports.createClient = (_merchantId, _token, _config = {}) => { const _auth = __auth(_merchantId, _token, _config); const buildModule = (module) => { return module(_auth, _token, _config); }; const __this = { /** * Check if the provided token is valid * @returns {Promise<boolean>} Promise resolving to true if the token is valid */ // isTokenValid: () => _auth._verify().then(() => _auth._isTokenValid_OLD()), oauthToken: () => _auth._oauthToken().then((accessToken) => { if (accessToken && _auth._isAuthenticated()) { // Rebuild the modules with the new access token __this.orders = buildModule(__order); __this.products = buildModule(__products); __this.inventory = buildModule(__inventory); __this.shipments = buildModule(__shipment); } return accessToken; }), verifyToken: () => _auth._verify().then(() => _auth._verifyOauthToken()), isTokenValid: () => _auth._verify().then(() => _auth._verifyOauthToken()), isAuthenticated: () => _auth._isAuthenticated(), orders: buildModule(__order), products: buildModule(__products), inventory: buildModule(__inventory), shipments: buildModule(__shipment), _config, _token, _merchantId, }; return __this; };