3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
7 YUI.add('io-nodejs', function(Y) {
9 /*global Y: false, Buffer: false, clearInterval: false, clearTimeout: false, console: false, exports: false, global: false, module: false, process: false, querystring: false, require: false, setInterval: false, setTimeout: false, __filename: false, __dirname: false */
12 * Passthru to the NodeJS <a href="https://github.com/mikeal/request">request</a> module.
13 * This method is return of `require('request')` so you can use it inside NodeJS without
19 Y.IO.request = require('request');
24 NodeJS IO transport, uses the NodeJS <a href="https://github.com/mikeal/request">request</a>
25 module under the hood to perform all network IO.
26 @method transports.nodejs
28 @returns {Object} This object contains only a `send` method that accepts a
29 `transaction object`, `uri` and the `config object`.
32 Y.io('https://somedomain.com/url', {
35 //Extra request module config options.
41 'content-type': 'application/json',
42 body: JSON.stringify({
48 'content_type': 'text/plain'
54 body: 'I am an attachment'
59 success: function(id, e) {
65 var flatten = function(o) {
67 Object.keys(o).forEach(function(name) {
68 str.push(name + ': ' + o[name]);
70 return str.join('\n');
73 Y.IO.transports.nodejs = function() {
75 send: function (transaction, uri, config) {
77 config.notify('start', transaction, config);
78 config.method = config.method || 'GET';
81 method: config.method,
85 rconf.body = config.data;
88 rconf.headers = config.headers;
91 rconf.timeout = config.timeout;
94 Y.mix(rconf, config.request);
96 Y.IO.request(rconf, function(err, data) {
100 config.notify(((err.code === 'ETIMEDOUT') ? 'timeout' : 'failure'), transaction, config);
105 status: data.statusCode,
106 statusCode: data.statusCode,
107 headers: data.headers,
108 responseText: data.body,
110 getResponseHeader: function(name) {
111 return this.headers[name];
113 getAllResponseHeaders: function() {
114 return flatten(this.headers);
119 config.notify('complete', transaction, config);
120 config.notify(((data && (data.statusCode >= 200 && data.statusCode <= 299)) ? 'success' : 'failure'), transaction, config);
131 Y.IO.defaultTransport('nodejs');
135 }, '3.5.1' ,{requires:['io-base']});