https://github.com/romansky/contractor
Stricter messages in Javascript
https://github.com/romansky/contractor
Last synced: 8 months ago
JSON representation
Stricter messages in Javascript
- Host: GitHub
- URL: https://github.com/romansky/contractor
- Owner: romansky
- Created: 2012-09-15T12:31:52.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2012-10-07T00:17:48.000Z (over 13 years ago)
- Last Synced: 2025-01-10T05:36:45.819Z (over 1 year ago)
- Language: CoffeeScript
- Size: 117 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Contractor
====
Stricter messages in Javascript
## Why?
In a dynamic language world, you might need to declare a contract for communicating with your APIs, create contracts with Contractor and use them, with:
* server + client side
* pub / sub implementation
* external API's
* Socket.IO communication
Contractor will generate a wrapper function for this "contract" and make sure the api is used as designed, no more, no less.
## Installation
npm install contractor
## Usage
{Contractor} = require 'contractor'
Create a "contract" with two required parameters
**Create** Contractor.Create(String, ..)
LoginMessageContract = Contractor.Create("LoginMessage", Contractor.Required("user name"), Contractor.Required("password"))
Execute the contract
console.log LoginMessageContract("knock-knock","itsme!")
=> ["LoginMessage", "knock-knock","itsme!"]
Bad contract, bad!
console.log LoginMessageContract("knock-knock")
=> null
Also, you can always check the name of the contract by calling the toString method
LoginMessageContract.toString()
=> "LoginMessage"
## Lawyer
One option to use Contracts is to get yourself a Laweyer
Lawyer = require('contractor').Lawyer
Create an object that will handle the different events
client = {
LoginMessage : (userName, password)-> # do something here...
}
Use the lawyer on the contract and client
Layer.Read LoginMessageContract("its me!","white rabbit"), client