0.2.1-beta • Published 6 years ago

dash-agent v0.2.1-beta

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

dash-agent

npm

Making requests easier

Simple usage

const dashAgent = require('dash-agent')
dashAgent.get('https://google.com').send().then(async res => {
    console.log('status code:', res.statusCode)
    if (res.ok)
        console.log('Requset was successful')
    const body = await res.text()
    console.log('body:', body)
})

Reference

Main export: function(method: string, url: string): Request

dashAgent('GET', 'https://google.com')
dashAgent('PUT', 'http://my.url')

Exports: function [method](url: string) where method is get, put, post, del, patch, head, options

dashAgent.get('https://google.com') // == dashAgent('GET', 'https://google.com')
dashAgent.del('https://google.com') // == dashAgent('DELETE', 'https://google.com')

Request

method

not-null string

url

not-null string

query(key, value)

Set additional values to the requests query string.

ParameterTypeDescription
keystringQuery key
valuestringQuery value

Returns: Request - The current request

query(queries)

Set additional values to the requests query string.

ParameterTypeDescription
queriesObjectKey value pairs query string

Returns: Request - The current request

set(name, value)

Set headers on the request.

ParameterTypeDescription
namestringHeader name
valuestring | numberHeader value

Returns: Request - The current request

set(headers)

Set headers on the request.

ParameterTypeDescription
headersObjectHeaders

Returns: Request - The current request

form(data)

Send form data with the request.

This sets the Content-Type header to application/x-www-form-urlencoded.

ParameterTypeDescription
dataObjectData from a form

Returns: Request - The current request

attach(field, data, options)

Attach any data to the request. Like files.

This sets the Content-Type header to multipart/form-data.

ParameterTypeDescription
fieldstringData field name
dataBufferData you want to attach to the request
options{ [contentType]: string, [filename]: string }Content type and potencial filename of data

Returns: Request - The current request

attach(field, data, contentType)

Attach any text data to the request.

This sets the Content-Type header to multipart/form-data.

ParameterTypeDescription
fieldstringData field name
datastringString data you want to attach to the request
contentTypestringThe type of the data. Defaults to "text/plain"

Returns: Request - The current request

attach(field, data)

Attach JSON data to the request.

This sets the Content-Type header to multipart/form-data.

ParameterTypeDescription
fieldstringName of json content
dataObjectjson content

Returns: Request - The current request

write(data)

Write data to the request.

ParameterTypeDescription
dataBuffer | stringData to write to the request

Returns: Request - The current request

clone()

Clone the request.

Returns: Request - The cloned request

send(data)

End the request and send it down the pipe.

ParameterTypeDescription
dataBuffer | Object | stringAny last data you want to send down the pipe

Returns: Promise<Response>

pipe(stream, contentType)

ParameterTypeDescription
streamstream.ReadablePipe the stream to the request
contentTypestringContent type

Returns: Promise<Response>

Response

headers

not-null Object

statusCode

not-null number

statusMessage

not-null string

ok

not-null boolean - If the response code between 200 and 299

readerRaw()

Get the response stream.

Returns: Promise<stream.Readable> - The unprocessed stream.

reader()

Tries to determine the encoding and return the appropiete stream.

Returns: Promise<stream.Readable> - The processed stream.

bufferRaw()

Using readerRaw() to create the buffer.

Returnes: Promise<Buffer> - The buffered response.

buffer()

Using reader() to create the buffer.

Returnes: Promise<Buffer> - The buffered response.

text(encoding)

Using buffer() to get the text content.

ParametersTypeDescription
encodingascii | utf8 | utf16le | ucs2 | base64 | latin1 | binary | hexText encoding. Default 'utf8'

Returns: Promise<string> - The text response.

json()

Get a json Object from the response.

Returns: Promise<Object> - A json response object.

1.0.0-p.2

6 years ago

1.0.0-p.1

6 years ago

1.0.0

6 years ago

0.2.1-beta

7 years ago

0.2.0-beta

7 years ago