I'm writing an ES6 module but ho no idea how to omit an argument from my main index.js but still check it in my exposed method.
// 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));
};
As you can see my method needs a parameter day. The way that the module should work is so that you first instantiate it with an ID and then use the methods:
import bpost from 'bpost';
const pkg = bpost('someIDhere');
const status = pkg.getStatus();
const openingHours = pkg.openingHours('monday');
I tried doing this with rest operator and default parameters but no luck yet. My test give still threw the day error with this test code (which should work once this is solved):
// 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();
});
});
Thanks in advance!