Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.

Commit 0e99898

Browse files
author
Satya van Heummen
committed
Removed Iron Router dependency
Replaced Iron Router dependency with `meteorhacks:picker`, which means you can now use this package with Flow Router as well.
1 parent 95d6f79 commit 0e99898

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ In `Mailer.init`, you're able to provide a key-value object with *template objec
136136
teamMembers: ->
137137
@team.users.map (user) -> Meteor.users.findOne(user)
138138
139-
# For previewing the email in your browser. Behaves like an ordinary Iron Router route.
139+
# For previewing the email in your browser.
140140
route:
141141
path: '/activity/:user'
142142
data: ->
@@ -234,14 +234,18 @@ The built in helpers are:
234234
baseUrl: (path) ->
235235
Utils.joinUrl(Mailer.settings.baseUrl, path)
236236
237-
# `emailUrlFor` takes an Iron Router route (with optional params) and
237+
# `emailUrlFor` takes an Iron Router or Flow Router route (with optional params) and
238238
# creates an absolute URL.
239239
#
240240
# {{ emailUrlFor 'myRoute' param='foo' }} => http://root-domain.com/my-route/foo
241-
emailUrlFor: (route, params) ->
242-
if Router
243-
Utils.joinUrl Mailer.settings.baseUrl, Router.path.call(Router, route, params.hash)
244-
241+
emailUrlFor: (routeName, params) ->
242+
# if Iron Router
243+
if Router?
244+
Utils.joinUrl Mailer.settings.baseUrl, Router.path.call(Router, routeName, params.hash)
245+
246+
# if Flow Router
247+
if FlowRouter?
248+
baseUrl = Utils.joinUrl Mailer.settings.baseUrl, FlowRouter.path.call(FlowRouter, routeName, params.hash)
245249
```
246250
247251
#### The preview line
@@ -362,9 +366,9 @@ Consult the `html-to-text` [documentation](https://www.npmjs.com/package/html-to
362366

363367
It's also possible to *send* emails to yourself or others for review in a real mail client.
364368

365-
Noticed the `route` property on the template? It uses Iron Router's server side routes under the hood.
369+
Noticed the `route` property on the template? It uses `meteorhacks:picker` server side routes under the hood.
366370

367-
The `route` property expects a `path` property (feel free to use any of Iron Router's fanciness in here) and an optional `data` function providing the data context (an object). The function has access to the same scope as Iron Router's `action` hook, which means you can get a hold of parameters and the whole shebang.
371+
The `route` property expects a `path` property and an optional `data` function providing the data context (an object). The function has access to the parameters of the route.
368372

369373
**Three routes** will be added:
370374

@@ -375,7 +379,7 @@ The `route` property expects a `path` property (feel free to use any of Iron Rou
375379
```
376380
The `/emails` root prefix is configurable in `config` in the `routePrefix` key.
377381

378-
The Iron Router *route names* will be on the format
382+
The *route names* will be on the format
379383

380384
```
381385
[preview|send]Name
@@ -464,6 +468,7 @@ Why not try [`meteor-logger`](https://github.com/lookback/meteor-logger)? :)
464468

465469
## Version history
466470

471+
- `0.7.0` - Replaced Iron Router dependency with `meteorhacks:picker`, which means you can now use this package with Flow Router as well.
467472
- `0.6.2` - Support passing options to `html-to-text` module in `Mailer.config()`.
468473
- `0.6.1`- Fix critical runtime crash when sending emails.
469474
- `0.6.0` - Automatically create and include plain text email version from your HTML templates, using [`html-to-text`](http://npmjs.com/package/html-to-text).

emails.coffee

+30-25
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ Helpers =
5757
# creates an absolute URL.
5858
#
5959
# {{ emailUrlFor 'myRoute' param='foo' }} => http://root-domain.com/my-route/foo
60-
emailUrlFor: (route, params) ->
61-
if Router
62-
Utils.joinUrl Mailer.settings.baseUrl, Router.path.call(Router, route, params.hash)
60+
emailUrlFor: (routeName, params) ->
61+
# if Iron Router
62+
if Router?
63+
Utils.joinUrl Mailer.settings.baseUrl, Router.path.call(Router, routeName, params.hash)
64+
65+
# if Flow Router
66+
if FlowRouter?
67+
baseUrl = Utils.joinUrl Mailer.settings.baseUrl, FlowRouter.path.call(FlowRouter, routeName, params.hash)
68+
6369

6470
# # The mailer
6571
#
@@ -258,18 +264,18 @@ MailerClass = (options) ->
258264
# This function adds the `preview` route from a `template` object.
259265
# It will apply the returned data from a `data` function on the
260266
# provided `route` prop from the template.
261-
previewAction = (opts) ->
267+
previewAction = (opts, template, params) ->
262268
check opts,
263269
type: Match.OneOf('html', 'text')
264270

265-
return (template) ->
271+
return (template, params) ->
266272
try
267-
data = template.route.data and template.route.data.apply(this, arguments)
273+
data = template.route.data and template.route.data.call(this, params)
268274
catch ex
269275
msg = 'Exception in '+template.name+' data function: '+ex.message
270276
Utils.Logger.error msg, TAG
271-
@response.writeHead 500
272-
return @response.end msg
277+
@writeHead 500
278+
return @end msg
273279

274280
# Compile, since we wanna refresh markup and CSS inlining.
275281
compile template
@@ -285,21 +291,21 @@ MailerClass = (options) ->
285291
Utils.Logger.error msg, TAG
286292
content = msg
287293

288-
@response.writeHead 200, 'Content-Type': if opts.type is 'html' then 'text/html' else 'text/plain'
289-
@response.end(content, 'utf8')
294+
@writeHead 200, 'Content-Type': if opts.type is 'html' then 'text/html' else 'text/plain'
295+
@end(content, 'utf8')
290296

291297
# This function adds the `send` route, for easy sending emails from
292298
# the browser.
293-
sendAction = (template) ->
299+
sendAction = (template, params) ->
294300
# Who to send to? It depends: it primarly reads from the `?to`
295301
# query param, and secondly from the `testEmail` prop in settings.
296-
to = @params.query.to or settings.testEmail
302+
to = params.query.to or settings.testEmail
297303

298304
Utils.Logger.info "Sending #{template.name} ...", TAG
299305

300306
if to?
301307
try
302-
data = template.route.data and template.route.data.apply(this, arguments)
308+
data = template.route.data and template.route.data.call(this, params)
303309
catch ex
304310
Utils.Logger.error 'Exception in '+template.name+' data function: '+ex.message, TAG
305311
return
@@ -315,17 +321,17 @@ MailerClass = (options) ->
315321
@response.writeHead 500
316322
msg = 'Did not send test email, something went wrong. Check the logs.'
317323
else
318-
@response.writeHead 200
324+
@writeHead 200
319325
# If there's no `MAIL_URL` environment variable, Meteor cannot send
320326
# the email and echoes it out to `STDOUT` instead.
321327
reallySentEmail = !!process.env.MAIL_URL
322328
msg = if reallySentEmail then "Sent test email to #{to}" else "Sent email to STDOUT"
323329

324-
@response.end(msg)
330+
@end(msg)
325331

326332
else
327-
@response.writeHead 400
328-
@response.end("No testEmail provided.")
333+
@writeHead 400
334+
@end("No testEmail provided.")
329335

330336
# Adds all the routes from a template.
331337
addRoutes = (template) ->
@@ -347,18 +353,17 @@ MailerClass = (options) ->
347353
# Also capitalize the first character in the template name for
348354
# the name of the route, so it will look like `previewSample` for a
349355
# template named `sample`.
350-
path = "#{settings.routePrefix}/#{type}" + template.route.path
356+
path = "/#{settings.routePrefix}/#{type}" + template.route.path
351357
name = Utils.capitalizeFirstChar(template.name)
352358
routeName = "#{type}#{name}"
353359

354-
Utils.Logger.info "Add route: [#{routeName}] at path /" + path, TAG
360+
Utils.Logger.info "Add route: [#{routeName}] at path " + path, TAG
361+
362+
# we use Picker for server side routes
363+
Picker.route(path, (params, req, res) ->
364+
action.call res, template, params
365+
)
355366

356-
Router.route routeName,
357-
path: path
358-
where: 'server'
359-
# An action still need the route context, but call it
360-
# with our `template` as the sole parameter.
361-
action: -> action.call(this, template)
362367

363368
# ## Init
364369
#

package.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var where = 'server';
33
Package.describe({
44
name: 'lookback:emails',
55
summary: 'Send HTML emails with server side Blaze templates. Preview and debug in the browser.',
6-
version: '0.6.2',
6+
version: '0.7.0',
77
git: 'https://github.com/lookback/meteor-emails.git'
88
});
99

@@ -23,8 +23,8 @@ Package.onUse(function(api) {
2323
'coffeescript',
2424
'email',
2525
26-
27-
'meteorhacks:[email protected]'
26+
'meteorhacks:[email protected]',
27+
'meteorhacks:[email protected]'
2828
], where);
2929

3030
api.addFiles([

0 commit comments

Comments
 (0)