-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
169 lines (149 loc) · 4.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict'
const ow = require('ow')
const PuppeteerEmailProvider = require('puppeteer-email-provider')
const PuppeteerEmailSession = require('puppeteer-email-session')
const signup = require('./lib/signup')
const signin = require('./lib/signin')
const signout = require('./lib/signout')
const sendEmail = require('./lib/send-email')
const getEmails = require('./lib/get-emails')
/**
* Puppeteer email provider for [Outlook](https://outlook.live.com).
*
* @extends PuppeteerEmailProvider
*/
class PuppeteerEmailProviderOutlook extends PuppeteerEmailProvider {
/**
* Email provider to automate.
*
* @member {PuppeteerEmailProvider}
*/
get name () {
return 'outlook'
}
/**
* Creates a new email account.
*
* Returns an email session with the authenticated puppeteer browser.
*
* @param {object} user - User info for the account to create
* @param {string} user.username - Username
* @param {string} user.password - Password
* @param {string} user.firstName - User's given name
* @param {string} user.lastName - User's family name
* @param {object} user.birthday - User's birthday
* @param {string} user.birthday.month - User's birthday month
* @param {string} user.birthday.day - User's birthday day
* @param {string} user.birthday.year - User's birthday year
*
* @param {object} opts - Options
* @param {Object} opts.browser - Puppeteer browser instance to use
*
* @return {Promise<PuppeteerEmailSession>}
*/
async signup (user, opts) {
ow(user, ow.object.plain.nonEmpty.label('user'))
ow(user.username, ow.string.nonEmpty.label('user.username'))
ow(user.password, ow.string.nonEmpty.label('user.password'))
ow(opts, ow.object.nonEmpty.label('opts'))
ow(opts.browser, ow.object.nonEmpty.label('browser'))
await signup(user, opts)
return new PuppeteerEmailSession({
user: {
username: user.username,
email: `${user.username}@outlook.com`
},
browser: opts.browser,
provider: this
})
}
/**
* Signs into an existing email account.
*
* You must specify either `user.username` or `user.email`.
*
* Returns an email session with the authenticated puppeteer browser.
*
* @param {object} user - User info for the account to sign into
* @param {string} [user.username] - Username (implies email)
* @param {string} [user.email] - Email (implies username)
* @param {string} user.password - Password
*
* @param {object} opts - Options
* @param {Object} opts.browser - Puppeteer browser instance to use
*
* @return {Promise<PuppeteerEmailSession>}
*/
async signin (user, opts) {
ow(user, ow.object.plain.nonEmpty.label('user'))
ow(user.password, ow.string.nonEmpty.label('user.password'))
if (user.username) {
ow(user.username, ow.string.nonEmpty)
} else if (user.email) {
ow(user.email, ow.string.nonEmpty)
} else {
throw new Error('missing required parameter "username" or "email"')
}
ow(opts, ow.object.nonEmpty)
ow(opts.browser, ow.object.nonEmpty)
user.email = user.email || `${user.username}@outlook.com`
await signin(user, opts)
return new PuppeteerEmailSession({
user: {
username: user.username,
email: user.email
},
browser: opts.browser,
provider: this
})
}
/**
* Signs out of an authenticated session.
*
* @param {PuppeteerEmailSession} session
*
* @return {Promise}
*/
async signout (session) {
ow(session, ow.object.instanceOf(PuppeteerEmailSession))
return signout({
browser: session.browser
})
}
/**
* Sends an email from an authenticated session.
*
* @param {PuppeteerEmailSession} session
* @param {object} email - TODO
* @param {object} [opts] - Options
*
* @return {Promise}
*/
async sendEmail (session, email, opts = { }) {
ow(session, ow.object.instanceOf(PuppeteerEmailSession))
ow(email, ow.object)
ow(opts, ow.object.plain)
return sendEmail(email, {
browser: session.browser,
...opts
})
}
/**
* Fetches emails from the inbox of an authenticated session.
*
* @param {PuppeteerEmailSession} session
* @param {object} [opts] - Options
* @param {object} [opts.query] - Search query to narrow down results
*
* @return {Promise<Array<Object>>}
*/
async getEmails (session, opts = { }) {
ow(session, ow.object.instanceOf(PuppeteerEmailSession))
ow(opts, ow.object.plain)
return getEmails({
browser: session.browser,
...opts
})
}
}
module.exports = PuppeteerEmailProviderOutlook