Merge pull request #279 from blittle/master

Restify TypeScript Defintion
This commit is contained in:
Diullei Gomes
2013-02-14 14:44:54 -08:00
2 changed files with 370 additions and 0 deletions

218
restify/restify-test.ts Normal file
View File

@@ -0,0 +1,218 @@
/// <reference path="restify.d.ts" />
import restify = module("restify");
var server = restify.createServer({
formatters: {
'application/foo': function formatFoo(req, res, body) {
if (body instanceof Error)
return body.stack;
if (body)
return body.toString('base64');
return body;
}
}
});
server = restify.createServer({
certificate: "test",
key: "test",
formatters: {},
log: {},
name: "test",
spdy: {},
version: "",
responseTimeHeader: "",
responseTimeFormatter : (durationInMilliseconds: number) => {}
});
server.on('someEvent', ()=>{});
server.use((req, res, next)=>{});
server.use([(req, res, next)=>{}]);
server.use((req, res, next)=>{}, (req, res, next)=>{});
function send(req, res, next) {
req.header('key', 'val');
req.header('key') === 'val';
req.accepts('test') === true;
req.is('test') === true;
req.getLogger('test');
var log = req.log;
log.debug({params: req.params}, 'Hello there %s', 'foo');
req.contentLength === 50;
req.contentType === 'test';
req.href === 'test';
req.id === 'test';
req.path === 'test';
req.query === 'test';
req.secure === true;
req.time === 50;
req.params;
res.header('test');
res.header('test', {});
res.header('test', new Date());
res.cache();
res.cache('testst', {});
res.status(344);
res.send({hello: 'world'});
res.send(201, {hello: 'world'});
res.send(new restify.BadRequestError('meh'));
res.json(201, {hello: 'world'});
res.json({hello: 'world'});
res.code === 50;
res.contentLength === 50;
res.charSet === 'test';
res.contentType === 'test';
res.headers;
res.id === 'test';
res.send('hello ' + req.params.name);
return next();
}
server.post('/hello', send);
server.put( '/hello', send);
server.del( '/hello', send);
server.get( '/hello', send);
server.head('/hello', send);
server.post(/(.*)/, send);
server.put( /(.*)/, send);
server.del( /(.*)/, send);
server.get( /(.*)/, send);
server.head(/(.*)/, send);
new restify.ConflictError("test");
new restify.InvalidArguementError("message");
new restify.RestError("message");
new restify.BadDigestError("message");
new restify.BadMethodError("message");
new restify.BadRequestError('test');
new restify.InternalError("message");
new restify.InvalidContentError("message");
new restify.InvalidCredentialsError("message");
new restify.InvalidHeaderError("message");
new restify.InvalidVersionError("message");
new restify.MissingParameterError("message");
new restify.NotAuthorizedError("message");
new restify.RequestExpiredError("jjmessage");
new restify.RequestThrottledError("message");
new restify.ResourceNotFoundError("message");
new restify.WrongAcceptError("message");
server.name = "";
server.version = "";
server.log = {};
server.acceptable = ["test"];
server.url = "";
server.address().port;
server.address().family;
server.address().address;
server.listen("somePath", send);
server.close();
server.use(restify.acceptParser(server.acceptable));
server.use(restify.authorizationParser());
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());
server.use(restify.throttle({
burst: 100,
rate: 50,
ip: true,
overrides: {
'192.168.1.1': {
rate: 0,
burst: 0
}
}
}));
server.on('after', restify.auditLogger({
log: ()=>{}
}));
restify.defaultResponseHeaders = function(data) {
this.header('Server', 'helloworld');
};
restify.defaultResponseHeaders = false;
//RESTIFY Client Tests
var client = restify.createJsonClient({
url: 'https://api.us-west-1.joyentcloud.com',
version: '*'
});
client = restify.createStringClient({
accept: "test",
connectTimeout: 30,
dtrace: {},
gzip: {},
headers: {},
log: {},
retry: {},
signRequest: ()=>{},
url: "",
userAgent: "",
version: ""
});
client.get("test", send);
client.head('test', send);
client.post('path', {}, send);
client.put('path', {}, send);
client.del('path', send);
client.post('/foo', { hello: 'world' }, function(err, req, res, obj) {
console.log('%d -> %j', res.statusCode, res.headers);
console.log('%j', obj);
});
client.get('/foo/bar', function(err, req, res, data) {
console.log('%s', data);
});
var client2 = restify.createClient({
url: 'http://127.0.0.1'
});
client2.get('/str/mcavage', function(err, req) {
req.on('result', function(err, res) {
res.body = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
res.body += chunk;
});
res.on('end', function() {
console.log(res.body);
});
});
});
client.basicAuth('test', 'password');
client2.basicAuth('test', 'password');

152
restify/restify.d.ts vendored Normal file
View File

@@ -0,0 +1,152 @@
interface addressInterface {
port: number;
family: string;
address: string;
}
interface Request {
header: (key: string, defaultValue?: string) => any;
accepts: (type: string) => bool;
is: (type: string) => bool;
getLogger: (component: string) => any;
contentLength: number;
contentType: string;
href: string;
log: Object;
id: string;
path: string;
query: string;
secure: bool;
time: number;
params: any;
}
interface Response {
header: (key: string, value ?: any) => any;
cache: (type?: any, options?: Object) => any;
status: (code: number) => any;
send: (status?: any, body?: any) => any;
json: (status?: any, body?: any) => any;
code: number;
contentLength: number;
charSet: string;
contentType: string;
headers: Object;
statusCode: number;
id: string;
}
interface Server {
use: (... handler: any[]) => any;
post: (route: any, routeCallBack: (req: Request, res: Response, next: Function) => any) => any;
put: (route: any, routeCallBack: (req: Request, res: Response, next: Function) => any) => any;
del: (route: any, routeCallBack: (req: Request, res: Response, next: Function) => any) => any;
get: (route: any, routeCallBack: (req: Request, res: Response, next: Function ) => any) => any;
head: (route: any, routeCallBack: (req: Request, res: Response, next: Function) => any) => any;
on: (event: string, callback: Function) => any;
name: string;
version: string;
log: Object;
acceptable: string[];
url: string;
address: () => addressInterface;
listen: (... args: any[]) => any;
close: (... args: any[]) => any;
pre: (routeCallBack: (req: Request, res: Response, next: Function) => any) => any;
}
interface ServerOptions {
certificate ?: string;
key ?: string;
formatters ?: Object;
log ?: Object;
name ?: string;
spdy ?: Object;
version ?: string;
responseTimeHeader ?: string;
responseTimeFormatter ?: (durationInMilliseconds: number) => any;
}
interface ClientOptions {
accept?: string;
connectTimeout?: number;
dtrace?: Object;
gzip?: Object;
headers?: Object;
log?: Object;
retry?: Object;
signRequest?: Function;
url?: string;
userAgent?: string;
version?: string;
}
interface Client {
get: (path: string, callback?: (err: any, req: Request, res: Response, obj: any) => any) => any;
head: (path: string, callback?: (err: any, req: Request, res: Response) => any) => any;
post: (path: string, object: any, callback?: (err: any, req: Request, res: Response, obj: any) => any) => any;
put: (path: string, object: any, callback?: (err: any, req: Request, res: Response, obj: any) => any) => any;
del: (path: string, callback?: (err: any, req: Request, res: Response) => any) => any;
basicAuth: (username: string, password: string) => any;
}
interface HttpClient extends Client {
get: (path?: any, callback?: Function) => any;
head: (path?:any, callback?: Function) => any;
post: (opts?: any, callback?: Function) => any;
put: (opts?: any, callback?: Function) => any;
del: (opts?: any, callback?: Function) => any;
}
interface ThrottleOptions {
burst?: number;
rate?: number;
ip?: bool;
xff?: bool;
username?: bool;
tokensTable?: Object;
maxKeys?: number;
overrides?: Object;
}
declare module "restify" {
export function createServer(options?: ServerOptions): Server;
export function createJsonClient(options?: ClientOptions): Client;
export function createStringClient(options?: ClientOptions): Client;
export function createClient(options?: ClientOptions): HttpClient;
export class ConflictError { constructor(message?: any); };
export class InvalidArguementError { constructor(message?: any); };
export class RestError { constructor(message?: any); };
export class BadDigestError { constructor(message: any); };
export class BadMethodError { constructor(message: any); };
export class BadRequestError { constructor(message: any); };
export class InternalError { constructor(message: any); };
export class InvalidContentError { constructor(message: any); };
export class InvalidCredentialsError { constructor(message: any); };
export class InvalidHeaderError { constructor(message: any); };
export class InvalidVersionError { constructor(message: any); };
export class MissingParameterError { constructor(message: any); };
export class NotAuthorizedError { constructor(message: any); };
export class RequestExpiredError { constructor(message: any); };
export class RequestThrottledError { constructor(message: any); };
export class ResourceNotFoundError { constructor(message: any); };
export class WrongAcceptError { constructor(message: any); };
export function acceptParser(parser: any);
export function authorizationParser();
export function dateParser(skew?: number);
export function queryParser(options?: Object);
export function urlEncodedBodyParser(options?: Object);
export function jsonp(options?: Object);
export function gzipResponse(options?: Object);
export function bodyParser(options?: Object);
export function requestLogger(options?: Object);
export function serveStatic(options?: Object);
export function throttle(options?: ThrottleOptions);
export function conditionalRequest(options?: Object);
export function auditLogger(options?: Object);
export var defaultResponseHeaders : any;
}