diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..2c5ec4a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,8 @@ +{ + "bracketSpacing": true, + "printWidth": 80, + "singleQuote": true, + "tabWidth": 4, + "trailingComma": "none", + "useTabs": false +} diff --git a/README.md b/README.md index 6d71f24..0c4ea93 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Written from scratch, with zero-dependencies. ## Super simple to use -µRequest is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default. +µRequest is designed to be a drop-in replacement for request. It supports HTTPS and follows redirects by default. ```bash npm install --save @root/request @@ -16,10 +16,10 @@ npm install --save @root/request ```js var request = require('@root/request'); -request('http://www.google.com', function (error, response, body) { - console.log('error:', error); // Print the error if one occurred - console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received - console.log('body:', body); // Print the HTML for the Google homepage. +request('http://www.google.com', function(error, response, body) { + console.log('error:', error); // Print the error if one occurred + console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received + console.log('body:', body); // Print the HTML for the Google homepage. }); ``` @@ -30,25 +30,28 @@ var promisify = require('util').promisify; var request = require('@root/request'); request = promisify(request); -request('http://www.google.com').then(function (response) { - console.log('statusCode:', response.statusCode); // Print the response status code if a response was received - console.log('body:', response.body); // Print the HTML for the Google homepage. -}).catch(function (error) { - console.log('error:', error); // Print the error if one occurred -}); +request('http://www.google.com') + .then(function(response) { + console.log('statusCode:', response.statusCode); // Print the response status code if a response was received + console.log('body:', response.body); // Print the HTML for the Google homepage. + }) + .catch(function(error) { + console.log('error:', error); // Print the error if one occurred + }); ``` ## Table of contents -- [Forms](#forms) -- [HTTP Authentication](#http-authentication) -- [Custom HTTP Headers](#custom-http-headers) -- [Unix Domain Sockets](#unix-domain-sockets) -- [**All Available Options**](#requestoptions-callback) +- [Forms](#forms) +- [HTTP Authentication](#http-authentication) +- [Custom HTTP Headers](#custom-http-headers) +- [Unix Domain Sockets](#unix-domain-sockets) +- [**All Available Options**](#requestoptions-callback) ## Forms `urequest` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. + #### application/x-www-form-urlencoded (URL-Encoded Forms) @@ -56,16 +59,21 @@ request('http://www.google.com').then(function (response) { URL-encoded forms are simple. ```js -request.post('http://service.com/upload', {form:{key:'value'}}) +request.post('http://service.com/upload', { form: { key: 'value' } }); // or -request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ }) +request.post( + { url: 'http://service.com/upload', form: { key: 'value' } }, + function(err, httpResponse, body) { + /* ... */ + } +); ``` + - #### multipart/form-data (Multipart Form Uploads) For `multipart/form-data` we use the [form-data](https://github.com/form-data/form-data) library by [@felixge](https://github.com/felixge). For the most cases, you can pass your upload form data via the `formData` option. @@ -78,35 +86,39 @@ npm install --save form-data@2 ```js var formData = { - // Pass a simple key-value pair - my_field: 'my_value', - // Pass data via Buffers - my_buffer: Buffer.from([1, 2, 3]), - // Pass data via Streams - my_file: fs.createReadStream(__dirname + '/unicycle.jpg'), - // Pass multiple values /w an Array - attachments: [ - fs.createReadStream(__dirname + '/attachment1.jpg'), - fs.createReadStream(__dirname + '/attachment2.jpg') - ], - // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS} - // Use case: for some types of streams, you'll need to provide "file"-related information manually. - // See the `form-data` README for more information about options: https://github.com/form-data/form-data - custom_file: { - value: fs.createReadStream('/dev/urandom'), - options: { - filename: 'topsecret.jpg', - contentType: 'image/jpeg' + // Pass a simple key-value pair + my_field: 'my_value', + // Pass data via Buffers + my_buffer: Buffer.from([1, 2, 3]), + // Pass data via Streams + my_file: fs.createReadStream(__dirname + '/unicycle.jpg'), + // Pass multiple values /w an Array + attachments: [ + fs.createReadStream(__dirname + '/attachment1.jpg'), + fs.createReadStream(__dirname + '/attachment2.jpg') + ], + // Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS} + // Use case: for some types of streams, you'll need to provide "file"-related information manually. + // See the `form-data` README for more information about options: https://github.com/form-data/form-data + custom_file: { + value: fs.createReadStream('/dev/urandom'), + options: { + filename: 'topsecret.jpg', + contentType: 'image/jpeg' + } } - } }; -request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) { - if (err) { - return console.error('upload failed:', err); - } - console.log('Upload successful! Server responded with:', body); -}); +request.post( + { url: 'http://service.com/upload', formData: formData }, + function optionalCallback(err, httpResponse, body) { + if (err) { + return console.error('upload failed:', err); + } + console.log('Upload successful! Server responded with:', body); + } +); ``` + + ```js request.get('http://some.server.com/', { - 'auth': { - 'user': 'username', - 'pass': 'password', - 'sendImmediately': false - } + auth: { + user: 'username', + pass: 'password', + sendImmediately: false + } }); // or request.get('http://some.server.com/', { - 'auth': { - 'bearer': 'bearerToken' - } + auth: { + bearer: 'bearerToken' + } }); ``` If passed as an option, `auth` should be a hash containing values: -- `user` || `username` -- `pass` || `password` -- `bearer` (optional) +- `user` || `username` +- `pass` || `password` +- `bearer` (optional)