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

beforerequest as method #22

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 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
61 changes: 38 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ function EventResource() {
util.inherits(EventResource, Resource);

EventResource.label = "Event";
EventResource.events = ["get", "post", "put", "delete"];
EventResource.events = ["get", "post", "put", "delete", "head","beforerequest"];

module.exports = EventResource;

EventResource.prototype.clientGeneration = true;

EventResource.prototype.beforeRequest = function(ctx, domain, cb){
if(this.events.beforerequest !== undefined){
this.events.beforerequest.run(ctx, domain, function(err) {
if(err) return ctx.done(err);
cb();
});
} else {
cb();
}
};

EventResource.prototype.handle = function (ctx, next) {
var parts = ctx.url.split('/').filter(function(p) { return p; });

Expand Down Expand Up @@ -47,26 +58,30 @@ EventResource.prototype.handle = function (ctx, next) {
}
}
};

if (ctx.method === "POST" && this.events.post) {
this.events.post.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else if (ctx.method === "GET" && this.events.get) {
this.events.get.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else if (ctx.method === "DELETE" && this.events.delete) {
this.events.delete.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else if (ctx.method === "PUT" && this.events.put) {
this.events.put.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else {
next();
}


};
this.beforeRequest(ctx, domain, function(){
if (ctx.method === "POST" && this.events.post) {
this.events.post.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "GET" && this.events.get) {
this.events.get.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "DELETE" && this.events.delete) {
this.events.delete.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "PUT" && this.events.put) {
this.events.put.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "HEAD" && this.events.head) {
this.events.head.run(ctx, domain, function (err) {
ctx.done();
Copy link
Member

Choose a reason for hiding this comment

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

@emilkoto you need to apply the parameters properly here ctx.done(err, result) or whatever you might need here.

Choose a reason for hiding this comment

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

@emilkoto I think it would be ctx.done(err, '');

});
} else {
next();
}
}.bind(this));
};