Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update stats methods to allow an array of events #216

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions dist/lib/interfaces/StatsOptions.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
interface StatsOptions {
export interface Stat {
time: string | Date;
delivered: {
smtp: number;
http: number;
total: number;
};
}
export interface StatsOptions {
start: string | Date;
end: string | Date;
resolution: string;
stats: {
time: string | Date;
delivered: {
smtp: number;
http: number;
total: number;
};
}[];
stats: Stat[];
}
export interface StatsQuery {
event: string | string[];
start: string | Date;
end: string | Date;
resolution: 'hour' | 'day' | 'month';
duration: string;
}
export default StatsOptions;
9 changes: 5 additions & 4 deletions dist/lib/stats.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import Request from './request';
import StatsOptions from './interfaces/StatsOptions';
import { StatsQuery, StatsOptions, Stat } from './interfaces/StatsOptions';
declare class Stats {
start: Date;
end: Date;
resolution: string;
stats: any;
stats: Stat[];
constructor(data: StatsOptions);
}
export default class StatsClient {
request: Request;
constructor(request: Request);
private prepareSearchParams;
_parseStats(response: {
body: StatsOptions;
}): Stats;
getDomain(domain: string, query: any): Promise<Stats>;
getAccount(query: any): Promise<Stats>;
getDomain(domain: string, query?: StatsQuery): Promise<Stats>;
getAccount(query?: StatsQuery): Promise<Stats>;
}
export {};
9,886 changes: 9,883 additions & 3 deletions dist/mailgun.node.js

Large diffs are not rendered by default.

4,341 changes: 4,338 additions & 3 deletions dist/mailgun.web.js

Large diffs are not rendered by default.

28 changes: 18 additions & 10 deletions lib/interfaces/StatsOptions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
interface StatsOptions {
export interface Stat {
time: string | Date,
delivered: {
smtp: number,
http: number,
total: number
}
}

export interface StatsOptions {
start: string | Date;
end: string | Date;
resolution: string;
stats: {
time: string | Date,
delivered: {
smtp: number,
http: number,
total: number
}
}[];
stats: Stat[];
}

export default StatsOptions;
export interface StatsQuery {
event: string | string[];
start: string | Date;
end: string | Date;
resolution: 'hour'| 'day' | 'month';
duration: string;
}
35 changes: 27 additions & 8 deletions lib/stats.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import urljoin from 'url-join';
import Request from './request';
import StatsOptions from './interfaces/StatsOptions';
import { StatsQuery, StatsOptions, Stat } from './interfaces/StatsOptions';

class Stats {
start: Date;
end: Date;
resolution: string;
stats: any;
stats: Stat[];

constructor(data: StatsOptions) {
this.start = new Date(data.start);
this.end = new Date(data.end);
this.resolution = data.resolution;
this.stats = data.stats.map(function (stat: { time: string | Date }) {
this.stats = data.stats.map(function (stat: Stat) {
const res = { ...stat };
res.time = new Date(stat.time);
return res;
Expand All @@ -27,17 +27,36 @@ export default class StatsClient {
this.request = request;
}

_parseStats(response: { body: StatsOptions }) {
private prepareSearchParams(query: StatsQuery): Array<Array<string>> {
let searchParams = [];
if (typeof query === 'object' && Object.keys(query).length) {
searchParams = Object.entries(query).reduce((arrayWithPairs, currentPair) => {
const [key, value] = currentPair;
if (Array.isArray(value) && value.length) {
const repeatedProperty = value.map((item) => [key, item]);
return [...arrayWithPairs, ...repeatedProperty];
}
arrayWithPairs.push([key, value]);
return arrayWithPairs;
}, []);
}

return searchParams;
}

_parseStats(response: { body: StatsOptions }): Stats {
return new Stats(response.body);
}

getDomain(domain: string, query: any) {
return this.request.get(urljoin('/v3', domain, 'stats/total'), query)
getDomain(domain: string, query?: StatsQuery): Promise<Stats> {
const searchParams = this.prepareSearchParams(query);
return this.request.get(urljoin('/v3', domain, 'stats/total'), searchParams)
.then(this._parseStats);
}

getAccount(query: any) {
return this.request.get('/v3/stats/total', query)
getAccount(query?: StatsQuery): Promise<Stats> {
const searchParams = this.prepareSearchParams(query);
return this.request.get('/v3/stats/total', searchParams)
.then(this._parseStats);
}
}
2 changes: 1 addition & 1 deletion test/stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import nock from 'nock';
import Request from '../lib/request';
import StatsClient from '../lib/stats';
import RequestOptions from '../lib/interfaces/RequestOptions';
import StatsOptions from '../lib/interfaces/StatsOptions';
import { StatsOptions } from '../lib/interfaces/StatsOptions';
import { InputFormData } from '../lib/interfaces/IFormData';

describe('StatsClient', function () {
Expand Down