cypress-mailisk 2.2.1 → 3.0.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cypress-mailisk",
3
- "version": "2.2.1",
3
+ "version": "3.0.0",
4
4
  "description": "Mailisk library for Cypress",
5
5
  "keywords": [
6
6
  "mailisk",
@@ -19,7 +19,7 @@
19
19
  "test:coverage": "jest --coverage"
20
20
  },
21
21
  "peerDependencies": {
22
- "cypress": ">= 2.1.0"
22
+ "cypress": ">= 15.10.0"
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",
@@ -14,20 +14,58 @@ class MailiskCommands {
14
14
  }
15
15
 
16
16
  constructor() {
17
- const apiKey = Cypress.env('MAILISK_API_KEY');
18
- this.mailiskSetApiKey(apiKey);
17
+ this.request = null;
18
+ }
19
+
20
+ _getEnv(keys) {
21
+ if (typeof cy !== 'undefined' && typeof cy.env === 'function') {
22
+ return cy.env(keys);
23
+ }
24
+ // fallback to Cypress.env for Cypress < 15.10.0
25
+ if (typeof Cypress !== 'undefined' && typeof Cypress.env === 'function') {
26
+ const values = {};
27
+ for (const key of keys) {
28
+ values[key] = Cypress.env(key);
29
+ }
30
+ if (typeof cy !== 'undefined' && typeof cy.wrap === 'function') {
31
+ return cy.wrap(values, { log: false });
32
+ }
33
+ return Promise.resolve(values);
34
+ }
35
+ const emptyValues = {};
36
+ for (const key of keys) {
37
+ emptyValues[key] = undefined;
38
+ }
39
+ return Promise.resolve(emptyValues);
19
40
  }
20
41
 
21
42
  mailiskSetApiKey(apiKey) {
22
- this.request = new Request({ apiKey, apiUrl: Cypress.env('MAILISK_API_URL') });
43
+ return this._getEnv(['MAILISK_API_URL']).then(({ MAILISK_API_URL }) => {
44
+ this.request = new Request({ apiKey, apiUrl: MAILISK_API_URL });
45
+ return null;
46
+ });
47
+ }
48
+
49
+ _initRequestFromEnv() {
50
+ return this._getEnv(['MAILISK_API_KEY', 'MAILISK_API_URL']).then(({ MAILISK_API_KEY, MAILISK_API_URL }) => {
51
+ this.request = new Request({ apiKey: MAILISK_API_KEY, apiUrl: MAILISK_API_URL });
52
+ return this.request;
53
+ });
54
+ }
55
+
56
+ _withRequest(action) {
57
+ if (this.request) {
58
+ return action(this.request);
59
+ }
60
+ return this._initRequestFromEnv().then((request) => action(request));
23
61
  }
24
62
 
25
63
  mailiskListNamespaces() {
26
- return this.request.get('api/namespaces');
64
+ return this._withRequest((request) => request.get('api/namespaces'));
27
65
  }
28
66
 
29
- _mailiskSearchInboxAction(namespace, _options, urlParams, startTime, nextTimeout) {
30
- return this.request
67
+ _mailiskSearchInboxAction(request, namespace, _options, urlParams, startTime, nextTimeout) {
68
+ return request
31
69
  .get(`api/emails/${namespace}/inbox?${urlParams.toString()}`, { ..._options, timeout: nextTimeout })
32
70
  .then((response) => {
33
71
  if (response.total_count !== 0) {
@@ -35,7 +73,7 @@ class MailiskCommands {
35
73
  }
36
74
  const timeout = Math.max(_options.timeout - (Date.now() - startTime), 1);
37
75
  cy.wait(Math.min(timeout, 9000), { log: false });
38
- return this._mailiskSearchInboxAction(namespace, _options, urlParams, startTime, timeout);
76
+ return this._mailiskSearchInboxAction(request, namespace, _options, urlParams, startTime, timeout);
39
77
  });
40
78
  }
41
79
 
@@ -66,27 +104,30 @@ class MailiskCommands {
66
104
  }
67
105
 
68
106
  // temporary workaround due cypress not supporting overriding maxRedirects
69
- if (_params.wait) {
70
- urlParams.delete('wait');
71
- const startTime = Date.now();
72
- return this._mailiskSearchInboxAction(namespace, _options, urlParams, startTime, _options.timeout);
73
- } else {
74
- return this.request.get(`api/emails/${namespace}/inbox?${urlParams.toString()}`, _options);
75
- }
107
+ return this._withRequest((request) => {
108
+ if (_params.wait) {
109
+ urlParams.delete('wait');
110
+ const startTime = Date.now();
111
+ return this._mailiskSearchInboxAction(request, namespace, _options, urlParams, startTime, _options.timeout);
112
+ }
113
+ return request.get(`api/emails/${namespace}/inbox?${urlParams.toString()}`, _options);
114
+ });
76
115
  }
77
116
 
78
117
  mailiskGetAttachment(attachmentId, options = {}) {
79
- return this.request.get(`api/attachments/${attachmentId}`, options);
118
+ return this._withRequest((request) => request.get(`api/attachments/${attachmentId}`, options));
80
119
  }
81
120
 
82
121
  mailiskDownloadAttachment(attachmentId, options = {}) {
83
- return this.mailiskGetAttachment(attachmentId, options).then((attachment) => {
84
- return this.request.getBinary(attachment.data.download_url, options);
85
- });
122
+ return this._withRequest((request) =>
123
+ request.get(`api/attachments/${attachmentId}`, options).then((attachment) => {
124
+ return request.getBinary(attachment.data.download_url, options);
125
+ }),
126
+ );
86
127
  }
87
128
 
88
- _mailiskSearchSmsAction(phoneNumber, _options, urlParams, startTime, nextTimeout) {
89
- return this.request
129
+ _mailiskSearchSmsAction(request, phoneNumber, _options, urlParams, startTime, nextTimeout) {
130
+ return request
90
131
  .get(`api/sms/${phoneNumber}/messages?${urlParams.toString()}`, { ..._options, timeout: nextTimeout })
91
132
  .then((response) => {
92
133
  if (response.total_count !== 0) {
@@ -94,7 +135,7 @@ class MailiskCommands {
94
135
  }
95
136
  const timeout = Math.max(_options.timeout - (Date.now() - startTime), 1);
96
137
  cy.wait(Math.min(timeout, 9000), { log: false });
97
- return this._mailiskSearchSmsAction(phoneNumber, _options, urlParams, startTime, timeout);
138
+ return this._mailiskSearchSmsAction(request, phoneNumber, _options, urlParams, startTime, timeout);
98
139
  });
99
140
  }
100
141
 
@@ -132,17 +173,18 @@ class MailiskCommands {
132
173
  }
133
174
 
134
175
  // temporary workaround due cypress not supporting overriding maxRedirects
135
- if (_params.wait) {
136
- urlParams.delete('wait');
137
- const startTime = Date.now();
138
- return this._mailiskSearchSmsAction(phoneNumber, _options, urlParams, startTime, _options.timeout);
139
- } else {
140
- return this.request.get(`api/sms/${phoneNumber}/messages?${urlParams.toString()}`, _options);
141
- }
176
+ return this._withRequest((request) => {
177
+ if (_params.wait) {
178
+ urlParams.delete('wait');
179
+ const startTime = Date.now();
180
+ return this._mailiskSearchSmsAction(request, phoneNumber, _options, urlParams, startTime, _options.timeout);
181
+ }
182
+ return request.get(`api/sms/${phoneNumber}/messages?${urlParams.toString()}`, _options);
183
+ });
142
184
  }
143
185
 
144
186
  mailiskListSmsNumbers(options = {}) {
145
- return this.request.get(`api/sms/numbers`, options);
187
+ return this._withRequest((request) => request.get(`api/sms/numbers`, options));
146
188
  }
147
189
  }
148
190