Last active
August 29, 2015 13:58
-
-
Save dimsmol/10009392 to your computer and use it in GitHub Desktop.
JavaScript DSL examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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