feathers-shippo
Version:
A Feathers JS adapter for the Shippo API
25 lines (24 loc) • 1.5 kB
JavaScript
import { BadRequest } from '@feathersjs/errors';
import { ShippoService, axiosOpts, shippoLimiters } from '../service';
export class ShippoRates extends ShippoService {
constructor(options, app) {
options = Object.assign(Object.assign({ limiters: shippoLimiters(options.token) }, options), { path: 'rates', methods: ['get', 'find'] });
super(options, app);
}
_find(params) {
var _a, _b;
if (!((_a = params === null || params === void 0 ? void 0 : params.query) === null || _a === void 0 ? void 0 : _a.shipment) || !((_b = params === null || params === void 0 ? void 0 : params.query) === null || _b === void 0 ? void 0 : _b.rates)) {
throw new BadRequest('params.query.shipment and params.query.rates are required for Shippo rates');
}
const shippoParams = Object.assign(Object.assign({}, params), { query: Object.assign({}, params.query) });
delete shippoParams.query.shipment;
delete shippoParams.query.rates;
return this.schedule('get', () => {
var _a, _b;
return this.shippo
.get(`shipments/${(_a = params === null || params === void 0 ? void 0 : params.query) === null || _a === void 0 ? void 0 : _a.shipment}/rates/${(_b = params === null || params === void 0 ? void 0 : params.query) === null || _b === void 0 ? void 0 : _b.rates}`, axiosOpts(shippoParams))
.then(this.handleResult)
.catch(this.handleError);
});
}
}