mailgun.js 4.1.1 → 4.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,4 +4,4 @@
4
4
 
5
5
  /*! https://mths.be/punycode v1.3.2 by @mathias */
6
6
 
7
- /*! mailgun.js v4.1.0 */
7
+ /*! mailgun.js v4.1.1 */
@@ -1,15 +1,23 @@
1
- interface StatsOptions {
1
+ export interface Stat {
2
+ time: string | Date,
3
+ delivered: {
4
+ smtp: number,
5
+ http: number,
6
+ total: number
7
+ }
8
+ }
9
+
10
+ export interface StatsOptions {
2
11
  start: string | Date;
3
12
  end: string | Date;
4
13
  resolution: string;
5
- stats: {
6
- time: string | Date,
7
- delivered: {
8
- smtp: number,
9
- http: number,
10
- total: number
11
- }
12
- }[];
14
+ stats: Stat[];
13
15
  }
14
16
 
15
- export default StatsOptions;
17
+ export interface StatsQuery {
18
+ event: string | string[];
19
+ start: string | Date;
20
+ end: string | Date;
21
+ resolution: 'hour'| 'day' | 'month';
22
+ duration: string;
23
+ }
package/lib/stats.ts CHANGED
@@ -1,18 +1,18 @@
1
1
  import urljoin from 'url-join';
2
2
  import Request from './request';
3
- import StatsOptions from './interfaces/StatsOptions';
3
+ import { StatsQuery, StatsOptions, Stat } from './interfaces/StatsOptions';
4
4
 
5
5
  class Stats {
6
6
  start: Date;
7
7
  end: Date;
8
8
  resolution: string;
9
- stats: any;
9
+ stats: Stat[];
10
10
 
11
11
  constructor(data: StatsOptions) {
12
12
  this.start = new Date(data.start);
13
13
  this.end = new Date(data.end);
14
14
  this.resolution = data.resolution;
15
- this.stats = data.stats.map(function (stat: { time: string | Date }) {
15
+ this.stats = data.stats.map(function (stat: Stat) {
16
16
  const res = { ...stat };
17
17
  res.time = new Date(stat.time);
18
18
  return res;
@@ -27,17 +27,36 @@ export default class StatsClient {
27
27
  this.request = request;
28
28
  }
29
29
 
30
- _parseStats(response: { body: StatsOptions }) {
30
+ private prepareSearchParams(query: StatsQuery): Array<Array<string>> {
31
+ let searchParams = [];
32
+ if (typeof query === 'object' && Object.keys(query).length) {
33
+ searchParams = Object.entries(query).reduce((arrayWithPairs, currentPair) => {
34
+ const [key, value] = currentPair;
35
+ if (Array.isArray(value) && value.length) {
36
+ const repeatedProperty = value.map((item) => [key, item]);
37
+ return [...arrayWithPairs, ...repeatedProperty];
38
+ }
39
+ arrayWithPairs.push([key, value]);
40
+ return arrayWithPairs;
41
+ }, []);
42
+ }
43
+
44
+ return searchParams;
45
+ }
46
+
47
+ _parseStats(response: { body: StatsOptions }): Stats {
31
48
  return new Stats(response.body);
32
49
  }
33
50
 
34
- getDomain(domain: string, query: any) {
35
- return this.request.get(urljoin('/v3', domain, 'stats/total'), query)
51
+ getDomain(domain: string, query?: StatsQuery): Promise<Stats> {
52
+ const searchParams = this.prepareSearchParams(query);
53
+ return this.request.get(urljoin('/v3', domain, 'stats/total'), searchParams)
36
54
  .then(this._parseStats);
37
55
  }
38
56
 
39
- getAccount(query: any) {
40
- return this.request.get('/v3/stats/total', query)
57
+ getAccount(query?: StatsQuery): Promise<Stats> {
58
+ const searchParams = this.prepareSearchParams(query);
59
+ return this.request.get('/v3/stats/total', searchParams)
41
60
  .then(this._parseStats);
42
61
  }
43
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mailgun.js",
3
- "version": "4.1.1",
3
+ "version": "4.1.2",
4
4
  "main": "dist/mailgun.node.js",
5
5
  "browser": "dist/mailgun.web.js",
6
6
  "types": "dist/index.d.ts",
@@ -4,7 +4,7 @@ import nock from 'nock';
4
4
  import Request from '../lib/request';
5
5
  import StatsClient from '../lib/stats';
6
6
  import RequestOptions from '../lib/interfaces/RequestOptions';
7
- import StatsOptions from '../lib/interfaces/StatsOptions';
7
+ import { StatsOptions } from '../lib/interfaces/StatsOptions';
8
8
  import { InputFormData } from '../lib/interfaces/IFormData';
9
9
 
10
10
  describe('StatsClient', function () {