@rqt/aqt
aqt
is an advanced request for Node.js which returns body, headers and status after gzip when necessary.
yarn add -E @rqt/aqt
Table Of Contents
API
The package exports a main default asynchronous function to make requests.
import aqt from '@rqt/aqt'
AConfig
Type
The configuration object is the following:
Property | Type | Description | Example |
---|
data | object | Optional data to send to the server with the request. | |
type | 'form'|'json' | How to send data: json to serialise JSON data and form for url-encoded transmission with json mode by default. | |
headers | object | Headers to send along with the request. | |
binary | boolean | Whether to return a buffer instead of a string. Default false . | |
method | string | What HTTP method to use to send data (only works when data is set). Default POST . | |
justHeaders | boolean | Whether to stop the request after response headers were received, without waiting for the data. Default false . | |
aqt(
url: string,
config?: AConfig,
): AResult
The requests are made with the aqt
function, which accepts either a single URL, or a URL with a configuration object of the ]AConfig
type.
import { HTTPContext } from 'https-context'
import aqt from 'aqt'
(async () => {
let c
try {
c = new HTTPContext()
await c._init()
c.setResponse('Hello World')
const res = await aqt(c.url)
const i = JSON.stringify(res, null, 2)
process.stdout.write(`${i}\n`)
} catch (err) {
process.stderr.write(`${err.message}\n`)
} finally {
await c._destroy()
}
})()
{
"body": "Hello World",
"headers": {
"content-type": "text/plain",
"date": "Tue, 10 Jul 2018 15:07:52 GMT",
"connection": "close",
"transfer-encoding": "chunked"
},
"statusCode": 200,
"statusMessage": "OK"
}
AResult
Type
The result of the aqt
function will have the following structure:
Property | Type | Description | Example |
---|
body | string|object|Buffer | The return from the server. In case json content-type was set by the server, the response will be parsed into an object. If binary option was for the request, a Buffer will be returned. Otherwise, a string response is returned. | |
headers | object | Incoming headers returned by the server. |
{
"server": "GitHub.com",
"content-type": "application/json",
"content-length": "2",
"connection": "close",
"status": "200 OK"
}
|
statusCode | number | The status code returned by the server. | 200 |
statusMessage | string | The status message set by the server. | OK |
(c) Art Deco 2018