我正在编写一个ES6模块,但不知道如何从我的主index.js中省略一个参数,但仍然在我公开的方法中检查它。
// index.js
import {getStatus, openingHours} from './lib/methods.js';
export default ID => {
if(!ID) throw new Error(`An ID needs to be passed`);
return {
getStatus: getStatus(ID),
openingHours: openingHours(ID),
};
};
// lib/methods.js
import find from 'lodash.find';
import composedFetch from '../composedFetch.js';
export const getStatus = id => composedFetch(id)
.then(data => find(data.processOverview.processSteps, { status: `active` }))
.catch(e => console.log(e));
export const openingHours = (id, day) => {
if (!day) throw new Error(`Please specify a day`);
return composedFetch(id)
.then(data => {
const obj = find(
data.deliveryPoint.openingSchedules,
{ dayOfTheWeek: day.toUpperCase() }
);
return obj.openingHours[0];
})
.catch(e => console.error(e));
};
正如您所见,我的方法需要参数日。模块应该工作的方式是首先使用ID实例化它,然后使用方法:
import bpost from 'bpost';
const pkg = bpost('someIDhere');
const status = pkg.getStatus();
const openingHours = pkg.openingHours('monday');
我尝试使用rest运算符和默认参数执行此操作,但还没有运气。我的测试仍然用这个测试代码抛出了一天的错误(一旦解决了它就应该工作):
// methods.test.js
import bpost from '../src/index.js';
describe(`Method: global.bpost`, () => {
it(`should show the available methods for the module`, () => {
expect(() => bpost(`someIDhere`)).not.toThrow();
});
});
提前致谢!