Skip to content

Commit

Permalink
feat: make options optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 15, 2022
1 parent 15f6084 commit e28c626
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ let homeWizardEnergyApi: HomeWizardEnergyApi;

describe('HomeWizardEnergyApi', () => {
beforeEach(() => {
homeWizardEnergyApi = new HomeWizardEnergyApi(mockApiUrl, {
logger: vi.fn(),
});
homeWizardEnergyApi = new HomeWizardEnergyApi(mockApiUrl);

mockApiAgent = new MockAgent({
bodyTimeout: 10,
Expand Down
2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class HomeWizardEnergyApi extends Base {
public p1Meter: P1MeterApi;
public waterMeter: WaterMeterApi;

constructor(baseUrl: string, options: HomeWizardEnergyApiOptions) {
constructor(baseUrl: string, options?: HomeWizardEnergyApiOptions) {
super(baseUrl, options);

this.energySocket = new EnergySocketApi(baseUrl, options);
Expand Down
12 changes: 7 additions & 5 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export class HomeWizardEnergyApiResponseError extends HomeWizardEnergyApiError {
}

export interface HomeWizardEnergyApiOptions {
/** The API version to be used. Defaults to `v1` */
apiVersion?: 'v1' | undefined;
requestOptions?: Omit<RequestParameters[1], 'method'>;
/** Request options to be used in the HTTP request to the API. */
requestOptions?: RequestParameters[1];
logger?: (...args: unknown[]) => void;
}

Expand All @@ -35,17 +37,17 @@ export class Base {
protected requestOptions: HomeWizardEnergyApiOptions['requestOptions'];
protected logger: HomeWizardEnergyApiOptions['logger'];

constructor(baseUrl: string, options: HomeWizardEnergyApiOptions) {
constructor(baseUrl: string, options?: HomeWizardEnergyApiOptions) {
this.baseUrl = baseUrl;
this.apiVersion = options.apiVersion || 'v1';
this.apiVersion = options?.apiVersion || 'v1';
this.requestOptions = {
bodyTimeout: 1000, // 1 seconds, we are on a local network, so all request should be fast
headersTimeout: 1000,
// Allow user to overwrite the defaults
...options.requestOptions,
...options?.requestOptions,
};

this.logger = options.logger || console.log;
this.logger = options?.logger;
}

protected get endpoints() {
Expand Down
2 changes: 1 addition & 1 deletion src/energy-socket-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class EnergySocketApi extends Base {
public getBasicInformation: () => Promise<BasicInformationResponse>;
public getData: <T extends EnergySocketDataResponse>() => Promise<T>;

constructor(baseUrl: string, options: HomeWizardEnergyApiOptions) {
constructor(baseUrl: string, options?: HomeWizardEnergyApiOptions) {
super(baseUrl, options);

this.getBasicInformation = super.getBasicInformation;
Expand Down
2 changes: 1 addition & 1 deletion src/p1-meter-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class P1MeterApi extends Base {
public getBasicInformation: () => Promise<BasicInformationResponse>;
public getData: <T extends P1MeterDataResponse>() => Promise<T>;

constructor(baseUrl: string, options: HomeWizardEnergyApiOptions) {
constructor(baseUrl: string, options?: HomeWizardEnergyApiOptions) {
super(baseUrl, options);

this.getBasicInformation = super.getBasicInformation;
Expand Down
2 changes: 1 addition & 1 deletion src/water-meter-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class WaterMeterApi extends Base {
public getBasicInformation: () => Promise<BasicInformationResponse>;
public getData: <T extends WaterMeterDataResponse>() => Promise<T>;

constructor(baseUrl: string, options: HomeWizardEnergyApiOptions) {
constructor(baseUrl: string, options?: HomeWizardEnergyApiOptions) {
super(baseUrl, options);

this.getBasicInformation = super.getBasicInformation;
Expand Down

0 comments on commit e28c626

Please sign in to comment.