Skip to content

Instantly share code, notes, and snippets.

@dimsmol
Last active August 29, 2015 13:58
Show Gist options
  • Save dimsmol/10009392 to your computer and use it in GitHub Desktop.
Save dimsmol/10009392 to your computer and use it in GitHub Desktop.
JavaScript DSL examples
// contract example
this.addItems([
res('', { // resource at root of this contract (will be connected to some url at upper level)
get: [ // HTTP GET handler
// This array will be replaced with a handler containing number of other handlers
auth().opt, // optional authentication
data({ // input data specification
roomId: vld.Message.roomId, // Message class is defined below
fromDate: opt(dateStr), // date, allowed in string form, optional
toDate: opt(dateStr),
sortDesc: opt(bool),
maxCount: opt(ordNum) // non-negative whole number, optional
}),
ret([vld.Message]), // returns list of Message objects (specified below)
impl(function (ctx) { // implementation
logic.get(ctx.auth, ctx.data, ctx.cb);
})],
create: [
auth(), // non-optional authentication
data(vld.MessageCreate), // MessageCreate object as input
ret(vld.MessageCreated), // MessageCreated object as output
impl(function (ctx) { // implementation
var connection = (ctx.mechanics.isWebSocket ? ctx.req.connection : null);
logic.create(ctx.auth, connection, ctx.data, ctx.cb);
})]
})
]);
// validators for contract above
var Message = {
id: str,
roomId: roomVld.Room.id, // referencing part of other validator
creatorId: str,
created: date,
contentType: opt(str),
content: str
};
var MessageCreate = valid.basedOn(Message, { // "inheriting" validator
id: noWay, // must not contain this field
creatorId: noWay,
created: noWay
});
var MessageCreated = {
id: Message.id,
creatorId: Message.creatorId,
created: Message.created
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment