From 53c667d1a58aa252e758b16f5c3e327fa8e119db Mon Sep 17 00:00:00 2001 From: James Trinklein Date: Thu, 2 Jun 2016 07:52:47 -0700 Subject: [PATCH] Update restify typings (#9379) * restify: update Request, fix id type change id from string property to function returning string add getId() alias include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L214 * restify: update Request, add getPath() include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L234 * restify: update Request, fix query type define query as function returning a string add getQuery() alias include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L247 * restify: update Request, fix time type change time from number property to function returning number include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L262 * restify: update Request, add getUrl include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L273 * restify: update Request, add getVersion getVersion is a function that returns a string add version() alias include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L288 * restify: update Request, fix header type change header return type from any to string include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L316 * restify: update Request, add trailer trailer is a function that returns a string trailer requires a string parameter called name trailer optionally takes a second string parameter for default value include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L338 * restify: update Request, add JSDoc include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L359 * restify: update Request, add isChunked isChunked is a function that returns a boolean include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L394 * restify: update Request, add isKeepAlive isKeepAlive is a function that returns a boolean include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L405 * restify: update Request, add JSDoc for isSecure include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L426 * restify: update Request, add isUpgradeRequest isUpgradeRequest is a function that returns a boolean include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L442 * restify: update Request, add isUpload isUpload is a function that returns a boolean include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L457 * restify: update Request, add userAgent userAgent is a function that returns a boolean include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L494 * restify: update Request, add start/endHandlerTimer startHandlerTimer is a function that takes a string endHandlerTimer is a function that takes a string include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L505 * restify: update Request, add absoluteUri absoluteUri is a function that takes a string and returns a string include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L52 * restify: update Request, fix accepts accepts can take a parameter of either string or array of strings include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L68 * restify: update Request, add acceptsEncoding acceptsEncoding is a function that returns a boolean acceptsEncoding can take a parameter of either string or array of strings include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L96 * restify: update Request, fix contentLength add getContentLength which is a function which returns a number contentLength is an alias for getContentLength function include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L116 * restify: update Request, fix contentType add getContentType which is a function which returns a string contentType is an alias for getContentType function include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L152 * restify: update Request, add getHref getHref is a function which returns a string href is an alias to getHref include JSDoc from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L202 * restify: update Request, add timers timers is an array of handler timer objects a handler timer object contains a string name and tuple of numbers representing the elapsed time this is the only way to access times from start/endHandlerTimer() from source: https://github.com/restify/node-restify/blob/5.x/lib/request.js\#L557 * restify: update RouteSpec, fix path path can be either a string or RegExp also shown in RouteOptions.path --- restify/restify-tests.ts | 44 +++++-- restify/restify.d.ts | 276 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 293 insertions(+), 27 deletions(-) diff --git a/restify/restify-tests.ts b/restify/restify-tests.ts index c6905878b4..767530d7aa 100644 --- a/restify/restify-tests.ts +++ b/restify/restify-tests.ts @@ -1,6 +1,7 @@ /// import restify = require("restify"); +import url = require("url"); var server = restify.createServer({ formatters: { @@ -40,23 +41,47 @@ server.use((req, res, next)=>{}, (req, res, next)=>{}); function send(req: restify.Request, res: restify.Response, next: restify.Next) { req.header('key', 'val'); req.header('key') === 'val'; - - req.accepts('test') === true; - req.is('test') === true; + req.trailer('key', 'val'); + req.trailer('key') === 'val'; + req.accepts('test') === true; + req.accepts(['test']) === true; + req.acceptsEncoding('test') === true; + req.acceptsEncoding(['test']) === true; + req.is('test') === true; + req.isChunked() === true; + req.isKeepAlive() === true; + req.isSecure() === true; + req.isUpgradeRequest() === true; + req.isUpload() === true; + req.userAgent() === 'test'; + req.startHandlerTimer('test'); + req.endHandlerTimer('test'); + req.absoluteUri('test') === 'test'; + + req.timers.pop() === { name: 'test', time: [0, 1234] }; req.getLogger('test'); var log = req.log; log.debug({params: req.params}, 'Hello there %s', 'foo'); - req.contentLength === 50; - req.contentType === 'test'; + req.getContentLength() === 50; + req.contentLength() === 50; + req.getContentType() === 'test'; + req.contentType() === 'test'; + req.getHref() === 'test'; req.href() === 'test'; - req.id === 'test'; + req.getId() === 'test'; + req.id() === 'test'; + req.getPath() === 'test'; req.path() === 'test'; - req.query === 'test'; + req.getQuery() === 'test'; + req.query() === 'test'; req.secure === true; - req.time === 50; + req.time() === 1463518410080; + req.getUrl() === url.parse('http://test.test.test/test'); + req.getVersion() === 'test'; + req.version() === 'test'; req.params; res.header('test'); @@ -253,6 +278,7 @@ server.on('after', (req: restify.Request, res: restify.Response, route: restify. route.spec.method === 'GET'; route.spec.name === 'routeName'; route.spec.path === '/some/path'; + route.spec.path === /\/some\/path\/.*/; route.spec.versions === ['v1']; restify.auditLogger({ log: ()=>{} })(req, res, route, err); }); @@ -316,7 +342,7 @@ client2.get('/str/mcavage', function(err: any, req: any) { }); }); }); - + client.basicAuth('test', 'password'); client2.basicAuth('test', 'password'); diff --git a/restify/restify.d.ts b/restify/restify.d.ts index f68ea0b2cc..4196022536 100644 --- a/restify/restify.d.ts +++ b/restify/restify.d.ts @@ -9,6 +9,7 @@ declare module "restify" { import http = require('http'); import bunyan = require('bunyan'); + import url = require('url'); interface addressInterface { @@ -16,12 +17,12 @@ declare module "restify" { family: string; address: string; } - + interface requestFileInterface { path: string; type: string; } - + /** * Comes from authorizationParser plugin */ @@ -35,21 +36,247 @@ declare module "restify" { } interface Request extends http.ServerRequest { - header: (key: string, defaultValue?: string) => any; - accepts: (type: string) => boolean; + /** + * builds an absolute URI for the request. + * @private + * @function absoluteUri + * @param {String} path a url path + * @returns {String} + */ + absoluteUri: (path: string) => string; + + /** + * returns any header off the request. also, 'correct' any + * correctly spelled 'referrer' header to the actual spelling used. + * @public + * @function header + * @param {String} name the name of the header + * @param {String} value default value if header isn't found on the req + * @returns {String} + */ + header: (name: string, value?: string) => string; + + /** + * returns any trailer header off the request. also, 'correct' any + * correctly spelled 'referrer' header to the actual spelling used. + * @public + * @function trailer + * @param {String} name the name of the header + * @param {String} value default value if header isn't found on the req + * @returns {String} + */ + trailer: (name: string, value?: string) => string; + + /** + * checks if the accept header is present and has the value requested. + * e.g., req.accepts('html'); + * @public + * @function accepts + * @param {String | Array} types an array of accept type headers + * @returns {Boolean} + */ + accepts: (types: string | string[]) => boolean; + + /** + * checks if the request accepts the encoding types. + * @public + * @function acceptsEncoding + * @param {String | Array} types an array of accept type headers + * @returns {Boolean} + */ + acceptsEncoding: (types: string | string[]) => boolean; + + /** + * Check if the incoming request contains the Content-Type header field, and + * if it contains the given mime type. + * @public + * @function is + * @param {String} type a content-type header value + * @returns {Boolean} + */ is: (type: string) => boolean; + + /** + * Check if the incoming request is chunked. + * @public + * @function isChunked + * @returns {Boolean} + */ + isChunked: () => boolean; + + /** + * Check if the incoming request is kept alive. + * @public + * @function isKeepAlive + * @returns {Boolean} + */ + isKeepAlive: () => boolean; + + /** + * Check if the incoming request has been upgraded. + * @public + * @function isUpgradeRequest + * @returns {Boolean} + */ + isUpgradeRequest: () => boolean; + + /** + * Check if the incoming request is an upload verb. + * @public + * @function isUpload + * @returns {Boolean} + */ + isUpload: () => boolean; + + /** + * retrieves the user-agent header. + * @public + * @function userAgent + * @returns {String} + */ + userAgent: () => string; + + /** + * Start the timer for a request handler function. You must explicitly invoke + * endHandlerTimer() after invoking this function. Otherwise timing information + * will be inaccurate. + * @public + * @function startHandlerTimer + * @param {String} handlerName The name of the handler. + * @returns {undefined} + */ + startHandlerTimer: (handlerName: string) => void; + + /** + * Stop the timer for a request handler function. + * @public + * @function endHandlerTimer + * @param {String} handlerName The name of the handler. + * @returns {undefined} + */ + endHandlerTimer: (handlerName: string) => void; + getLogger: (component: string) => any; - contentLength: number; - contentType: string; + + /** + * gets the content-length header off the request. + * @public + * @function getContentLength + * @returns {Number} + */ + getContentLength: () => number; + + /** + * @see getContentLength + * @function contentLength + */ + contentLength: () => number; + + /** + * gets the content-type header. + * @public + * @function getContentType + * @returns {String} + */ + getContentType: () => string; + + /** + * @see getContentType + */ + contentType: () => string; + + /** + * retrieves the complete URI requested by the client. + * @public + * @function getHref + * @returns {String} + */ + getHref: () => string; + + /** + * @see getHref + */ href: () => string; + log: bunyan.Logger; - id: string; + /** + * retrieves the request uuid. was created when the request was setup. + * @public + * @function getId + * @returns {String} + */ + getId: () => string; + + /** + * @see getId + */ + id: () => string; + + /** + * retrieves the cleaned up url path. + * e.g., /foo?a=1 => /foo + * @public + * @function getPath + * @returns {String} + */ + getPath: () => string; + + /** + * @see getPath + */ path: () => string; - query: any; + + /** + * returns the raw query string + * @public + * @function getQuery + * @returns {String} + */ + getQuery: () => string; + + /** + * @see getQuery + */ + query: () => string; secure: boolean; - time: number; + + /** + * returns ms since epoch when request was setup. + * @public + * @function time + * @returns {Number} + */ + time: () => number; + + /** + * returns a parsed URL object. + * @public + * @function getUrl + * @returns {Object} + */ + getUrl: () => url.Url; + + /** + * returns the accept-version header. + * @public + * @function getVersion + * @returns {String} + */ + getVersion: () => string; + + /** + * @see getVersion + */ + version: () => string; params: any; files?: { [name: string]: requestFileInterface }; + + /** + * Check if the incoming request is encrypted. + * @public + * @function isSecure + * @returns {Boolean} + */ isSecure: () => boolean; /** available when bodyParser plugin is used */ body?: any; @@ -57,6 +284,19 @@ declare module "restify" { username?: string; /** available when authorizationParser plugin is used */ authorization?: requestAuthorization; + + timers: HandlerTiming[]; + } + + /** + * Timer object used to identify how long a specific handler took to run + * + * @property {String} name The name of the handler. + * @property {Array} time A tuple of [seconds, nanoseconds], how long the handler took. + */ + interface HandlerTiming { + name: string; + time: [number, number]; } interface Response extends http.ServerResponse { @@ -72,11 +312,11 @@ declare module "restify" { headers: Object; id: string; } - + interface RouteSpec { method: string; name: string; - path: string; + path: string | RegExp; versions: string[]; } @@ -88,7 +328,7 @@ declare module "restify" { types: string[]; versions: string[]; } - + interface RouteOptions { name: string; method: string; @@ -98,11 +338,11 @@ declare module "restify" { contentType?: string | string[]; versions?: string | string[]; } - + interface RoutePathRegex extends RegExp { restifyParams: string[]; } - + interface Router { name: string; mounts: { [routeName: string]: Route }; @@ -119,7 +359,7 @@ declare module "restify" { }; log?: any; toString: () => string; - + /** * Takes an object of route params and query params, and 'renders' a URL * @param {String} routeName the route name @@ -128,14 +368,14 @@ declare module "restify" { * @returns {String} */ render: (routeName: string, params: Object, query?: Object) => string; - + /** * adds a route. * @param {Object} options an options object * @returns {String} returns the route name if creation is successful. */ mount: (options: Object) => string; - + /** * unmounts a route. * @param {String} name the route name @@ -184,7 +424,7 @@ declare module "restify" { opts(route: any, routeCallBack: RequestHandler[], ...routeCallBacks: RequestHandler[]): Route; opts(route: any, routeCallBack: RequestHandler, ...routeCallBacks: RequestHandler[][]): Route; opts(route: any, routeCallBack: RequestHandler[], ...routeCallBacks: RequestHandler[][]): Route; - + name: string; version: string; log: Object;