@stylusapparel/opv3-merchant-api-nodejs
Version:
This is the official NodeJs wrapper for connecting to the StylusOP API V3
158 lines (152 loc) • 4.36 kB
JavaScript
const stylusWrapper = require('../index.js');
const { MERCHANT_ID, MERCHANT_NAME, MERCHANT_SECRET } = require('./constants.js');
const stylusClient = stylusWrapper.createClient(MERCHANT_ID, MERCHANT_SECRET, {
merchantName: MERCHANT_NAME, // Replace with your merchant name / ID
sandbox: true,
apiVersion: 'v3',
tokenType: 'basic',
});
let shipmentId;
stylusClient
.oauthToken()
.then((accessToken) => {
console.log(`Access token: ${accessToken}`);
})
.catch((error) => {
console.error('Error getting the access token: ', error);
handleError(error);
})
.then(fetchShipments)
.then((shipments) => {
shipmentId = shipments.pop().shipmentId;
console.log('Retrieved shipments : ', shipments);
})
.then(fetchShipmentsByStatus)
.then((shipments) => {
console.log('Retrieved shipments based on single status: ', shipments);
})
.then(fetchShipmentsByMultipleStatus)
.then((shipments) => {
console.log('Retrieved shipments based on multiple status: ', shipments);
})
.then(fetchShipmentsByShipDates)
.then((shipments) => {
console.log('Retrieved shipments based on ship dates: ', shipments);
})
.then(() => fetchShipmentsByOrderId('66310d629545d7a416f7230f'))
.then((shipments) => {
console.log('Retrieved shipments based on orderId: ', shipments);
})
.then(() => fetchShipmentDetails(shipmentId))
.then((shipment) => {
console.log('Retrieved shipments details : ', shipment);
})
.then(() => fetchShipmentStatus(shipmentId))
.then((status) => {
console.log('Retrieved shipments status : ', status);
})
.catch((error) => {
console.error('Error fetching shipments: ', error);
handleError(error);
});
async function fetchShipments(offset = 0) {
try {
console.log('fetching shipments');
const shipments = await stylusClient.shipments.list({
limit: 10,
offset,
});
return shipments;
} catch (error) {
console.error(`Error fetching shipments at offset ${offset}:`, error);
throw error;
}
}
async function fetchShipmentsByStatus(offset = 0) {
try {
console.log('fetching shipments by status and pagination');
const shipments = await stylusClient.shipments.list({
limit: 10,
offset,
status: 'unknown',
});
return shipments;
} catch (error) {
console.error(`Error fetching shipments at offset ${offset}:`, error);
throw error;
}
}
async function fetchShipmentsByMultipleStatus() {
try {
console.log('fetching shipments by array of shipment status');
const shipments = await stylusClient.shipments.list({
status: ['unknown', 'pre_transit', 'in_transit'],
});
return shipments;
} catch (error) {
console.error(`Error fetching shipments:`, error);
throw error;
}
}
async function fetchShipmentsByShipDates() {
try {
console.log('fetching shipments by ship dates');
const shipments = await stylusClient.shipments.list({
shipDate: ['2023-01-29T18:46:44.000Z', '2024-04-29T18:46:44.000Z'],
});
return shipments;
} catch (error) {
console.error(`Error fetching shipments:`, error);
throw error;
}
}
async function fetchShipmentsByOrderId(orderRefId) {
try {
console.log('fetching shipments by orderId');
const shipments = await stylusClient.shipments.list({
orderRefId,
});
return shipments;
} catch (error) {
console.error(`Error fetching shipments:`, error);
throw error;
}
}
async function fetchShipmentDetails(shipmentId) {
try {
if (!shipmentId) {
return;
}
console.log('fetching shipment details by shipmentId');
const shipment = await stylusClient.shipments.get(shipmentId);
return shipment;
} catch (error) {
console.error(`Error fetching shipmentdetails`, error);
throw error;
}
}
async function fetchShipmentStatus(shipmentId) {
try {
if (!shipmentId) {
return;
}
console.log('fetching shipment status by shipmentId');
const shipment = await stylusClient.shipments.status(shipmentId);
return shipment;
} catch (error) {
console.error(`Error fetching shipment status`, error);
throw error;
}
}
// Error handler //
function handleError(err) {
// Handle error //
if (err.status === 403 || err.status === 401 || err.errorCode === 'TOKEN_INVALID') {
// Unauthorized
console.error(`Error!\n(${err.errorCode}) ${err.message} ${JSON.stringify(err.rawError)}`);
} else {
// Other error
console.error(`Error!\n(${err.errorCode}) ${err.message}`);
}
process.exit(1);
}