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

Add promise support #266

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
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
36 changes: 21 additions & 15 deletions lib/route.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ class share.Route
return ->
self = this

respond = (responseData, {responseInitiated, res}) ->
if responseInitiated
# Ensure the response is properly completed
res.end()
return
else
if res.headersSent
throw new Error "Must call this.done() after handling endpoint response manually: #{method} #{fullPath}"
else if responseData is null or responseData is undefined
throw new Error "Cannot return null or undefined from an endpoint: #{method} #{fullPath}"

# Generate and return the http response, handling the different endpoint response types
if responseData.body and (responseData.statusCode or responseData.headers)
self._respond res, responseData.body, responseData.statusCode, responseData.headers
else
self._respond res, responseData

# Throw an error if a route has already been added at this path
# TODO: Check for collisions with paths that follow same pattern with different parameter names
if _.contains @api._config.paths, @path
Expand Down Expand Up @@ -57,26 +74,15 @@ class share.Route
responseData = null
try
responseData = self._callEndpoint endpointContext, endpoint
if responseData instanceof Promise

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use Promise.resolve to handle with the same syntax the case where the result is a Promise and when it isn't. But of course it works the way you did it ;)

Promise.resolve(responseData).then (result) ->
  respond result, {responseInitiated, res}

I don't really understand what the try catch block is for though, so maybe I'm wrong.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this package about to publish a new version?!!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use lepozepo:restivus, this package doesn't seem to be maintained.

responseData.then (result) ->
respond result, {responseInitiated, res}
catch error
# Do exactly what Iron Router would have done, to avoid changing the API
ironRouterSendErrorToResponse(error, req, res);
return

if responseInitiated
# Ensure the response is properly completed
res.end()
return
else
if res.headersSent
throw new Error "Must call this.done() after handling endpoint response manually: #{method} #{fullPath}"
else if responseData is null or responseData is undefined
throw new Error "Cannot return null or undefined from an endpoint: #{method} #{fullPath}"

# Generate and return the http response, handling the different endpoint response types
if responseData.body and (responseData.statusCode or responseData.headers)
self._respond res, responseData.body, responseData.statusCode, responseData.headers
else
self._respond res, responseData
if not (responseData instanceof Promise) then respond responseData, {responseInitiated, res}

_.each rejectedMethods, (method) ->
JsonRoutes.add method, fullPath, (req, res) ->
Expand Down
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package.describe({
name: 'nimble:restivus',
summary: 'Create authenticated REST APIs in Meteor 0.9+ via HTTP/HTTPS. Setup CRUD endpoints for Collections.',
version: '0.8.12',
version: '0.8.13',
git: 'https://github.com/kahmali/meteor-restivus.git'
});

Expand Down