Comparing version 2.0.0 to 3.0.0
@@ -1,144 +0,10 @@ | ||
"use strict"; | ||
const $rqt_aqt = require('@rqt/aqt'); | ||
const $_lib = require('./lib'); | ||
const $_lib_Session = require('./lib/Session'); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = rqt; | ||
Object.defineProperty(exports, "Session", { | ||
enumerable: true, | ||
get: function () { | ||
return _session.default; | ||
} | ||
}); | ||
var _http = require("http"); | ||
var _https = require("https"); | ||
var _catchment = _interopRequireDefault(require("catchment")); | ||
var _url = require("url"); | ||
var _erotic = _interopRequireDefault(require("erotic")); | ||
var _lib = require("./lib"); | ||
var _package = require("../package.json"); | ||
var _session = _interopRequireDefault(require("./session")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Request an HTTP page. If `returnHeaders` is set to true, an object will be returned. | ||
* @param {string} address Url such as http://example.com/api | ||
* @param {Config} [config] Configuration object | ||
* @param {object} [config.data] Data to send to the server using a post request. | ||
* @param {object} [config.headers] A map of headers to use in the request. | ||
* @param {boolean} [config.binary] Whether to return a buffer. Default false. | ||
* @param {boolean} [config.returnHeaders] Return an object with `body` and `headers` properties instead of just the response. | ||
* @param {'form'|'json'} [config.type=json] How to send data: `form` for url-encoded transmission and `json` to serialise JSON data. `json` mode by default. | ||
* @param {string} [config.method] What method to use to send data (only works when `data` is set). Default `POST`. | ||
* @returns {Promise.<string|Buffer|{ body: string|Buffer, headers: Object.<string, string> }>} A string or buffer as a response. If `config.headers` was set, an object is returned. | ||
*/ | ||
async function rqt(address, config = {}) { | ||
const { | ||
data, | ||
type = 'json', | ||
headers = { | ||
'User-Agent': `Mozilla/5.0 (Node.js) rqt/${_package.version}` | ||
}, | ||
binary = false, | ||
returnHeaders = false, | ||
method = 'POST' | ||
} = config; | ||
const er = (0, _erotic.default)(true); | ||
const { | ||
hostname, | ||
protocol, | ||
port, | ||
path | ||
} = (0, _url.parse)(address); | ||
const isHttps = protocol === 'https:'; | ||
const request = isHttps ? _https.request : _http.request; | ||
const options = { | ||
hostname, | ||
port, | ||
path, | ||
headers | ||
}; | ||
let d = data; | ||
if (d) { | ||
let contentType; | ||
switch (type) { | ||
case 'json': | ||
d = JSON.stringify(data); | ||
contentType = 'application/json'; | ||
break; | ||
case 'form': | ||
d = (0, _lib.getFormData)(data); | ||
contentType = 'application/x-www-form-urlencoded'; | ||
break; | ||
} | ||
options.method = method; | ||
options.headers['Content-Type'] = contentType; | ||
options.headers['Content-Length'] = Buffer.byteLength(d); | ||
} | ||
let h; | ||
const body = await new Promise((resolve, reject) => { | ||
const req = request(options, async rs => { | ||
({ | ||
headers: h | ||
} = rs); | ||
const { | ||
promise | ||
} = new _catchment.default({ | ||
rs, | ||
binary | ||
}); | ||
const response = await promise; | ||
if (h['content-type'].startsWith('application/json')) { | ||
try { | ||
const parsed = JSON.parse(response); | ||
resolve(parsed); | ||
} catch (e) { | ||
const err = er(e); | ||
err.response = response; | ||
reject(err); | ||
} | ||
} else { | ||
resolve(response); | ||
} | ||
}).on('error', error => { | ||
const err = er(error); | ||
reject(err); | ||
}); | ||
if (d) { | ||
req.write(d); | ||
} | ||
req.end(); | ||
}); | ||
if (returnHeaders) return { | ||
body, | ||
headers: h | ||
}; | ||
return body; | ||
} | ||
/** | ||
* @typedef {Object} Config | ||
* @property {object} [data] Data to send to the server. | ||
* @property {object} [headers] A map of headers. | ||
* @property {boolean} [binary] Whether to return a buffer. | ||
* @property {boolean} [returnHeaders] Return an object with `body` and `headers` properties instead of just the response. | ||
* @property {'form'|'json'} [type] How to send data: `form` for url-encoded transmission and `json` to serialise JSON data. | ||
* @param {string} [method=POST] What method to use to send data (only works when `data` is set). Default `POST`. | ||
*/ | ||
module.exports = $_lib.rqt | ||
module.exports.aqt = $rqt_aqt | ||
module.exports.bqt = $_lib.bqt | ||
module.exports.jqt = $_lib.jqt | ||
module.exports.Session = $_lib_Session | ||
//# sourceMappingURL=index.js.map |
## 20 June 2018 | ||
### 3.0.0 | ||
- [breaking] Compression by default. | ||
- [feature] Split into `rqt`, `jqt`, `bqt` and `aqt`. | ||
- [feature] Session methods. | ||
- [doc] Improve documentation, add server examples. | ||
## 20 June 2018 | ||
### 2.0.0 | ||
@@ -4,0 +13,0 @@ |
{ | ||
"name": "rqt", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "request library", | ||
"main": "build", | ||
"scripts": { | ||
"t": "zoroaster -b", | ||
"test": "zoroaster test/spec -b", | ||
"test-watch": "zoroaster test/spec -b -w", | ||
"test-build": "BABEL_ENV=test-build yarn t test/spec", | ||
"doc": "doc README-source.md -o README.md", | ||
"build": "babel src --out-dir build --source-maps" | ||
"t": "zoroaster -a", | ||
"test": "yarn t test/spec", | ||
"test-build": "ALAMODE_ENV=test-build yarn test", | ||
"doc": "NODE_DEBUG=doc doc documentary -o README.md", | ||
"d1": "NODE_DEBUG=doc doc src/index.js -g", | ||
"d2": "NODE_DEBUG=doc doc src/lib/index.js -g", | ||
"d3": "NODE_DEBUG=doc doc src/lib/Session.js -g", | ||
"d": "yarn-s d1 d2 d3", | ||
"lint": "eslint .", | ||
"b": "alamode src -o build", | ||
"build": "yarn-s d b doc", | ||
"example/rqt.js": "node example example/rqt.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/artdecocode/rqt.git" | ||
"url": "git://github.com/rqt/rqt.git" | ||
}, | ||
@@ -23,2 +29,5 @@ "files": [ | ||
"request", | ||
"network", | ||
"session", | ||
"cookies", | ||
"http", | ||
@@ -30,22 +39,18 @@ "https" | ||
"bugs": { | ||
"url": "https://github.com/artdecocode/rqt/issues" | ||
"url": "https://github.com/rqt/rqt/issues" | ||
}, | ||
"homepage": "https://github.com/artdecocode/rqt#readme", | ||
"homepage": "https://github.com/rqt/rqt#readme", | ||
"devDependencies": { | ||
"@babel/cli": "7.0.0-beta.51", | ||
"@babel/core": "7.0.0-beta.51", | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.51", | ||
"@babel/plugin-transform-modules-commonjs": "7.0.0-beta.51", | ||
"@babel/register": "7.0.0-beta.51", | ||
"babel-plugin-transform-rename-import": "2.2.0", | ||
"documentary": "1.3.1", | ||
"eslint": "4.19.1", | ||
"eslint-config-artdeco": "1.0.0", | ||
"server-destroy": "1.0.1", | ||
"zoroaster": "2.1.0" | ||
"@idio/core": "1.0.2", | ||
"@wrote/read": "1.0.0", | ||
"alamode": "1.5.1", | ||
"documentary": "1.20.1", | ||
"eslint-config-artdeco": "1.0.1", | ||
"https-context": "1.0.3", | ||
"yarn-s": "1.1.0", | ||
"zoroaster": "3.5.1" | ||
}, | ||
"dependencies": { | ||
"catchment": "2.0.1", | ||
"erotic": "1.2.1" | ||
"@rqt/aqt": "1.1.2" | ||
} | ||
} |
390
README.md
@@ -5,3 +5,3 @@ # rqt | ||
`rqt` is a Node.js request library. Send `GET` and `POST` requests at ease. | ||
`rqt` is a Node.js request library. It allows to send requests with or without data, parse a JSON response automatically and enables `gzip` compression by default. | ||
@@ -12,110 +12,364 @@ ## Table Of Contents | ||
- [API](#api) | ||
* [`async rqt(url: string, options?: object): string`](#async-rqturl-stringoptions-headers-objectbinary-boolean-string) | ||
* [`async rqtWithData(url: string, options: object): string`](#async-rqtwithdataurl-stringoptions-data-stringobjecttype-string--jsonmethod-string--post-string) | ||
* [`Class Session`](#class-session) | ||
* [`constructor(headers?: object): Session`](#constructorheaders-object-session) | ||
* [`async request(location: string, options?: object): any`](#async-requestlocation-stringoptions-object-any) | ||
- [`Options` Type](#options-type) | ||
- [`async rqt(url: string, options?: Options): string`](#async-rqturl-stringoptions-options-string) | ||
- [`async jqt(url: string, options?: Options): Object`](#async-jqturl-stringoptions-options-object) | ||
- [`async bqt(url: string, options?: Options): Buffer`](#async-bqturl-stringoptions-options-buffer) | ||
- [`async aqt(url: string, options?: AqtOptions): AqtReturn`](#async-aqturl-stringoptions-aqtoptions-aqtreturn) | ||
* [`AqtOptions`](#type-aqtoptions) | ||
* [`AqtReturn`](#type-aqtreturn) | ||
- [`Session` Class](#session-class) | ||
* [`constructor(options?: SessionOptions): Session`](#constructoroptions-sessionoptions-session) | ||
* [`SessionOptions`](#type-sessionoptions) | ||
* [`async rqt(location: string, options?: Options): String`](#async-rqtlocation-stringoptions-options-string) | ||
* [`async jqt(location: string, options?: Options): String`](#async-jqtlocation-stringoptions-options-string) | ||
* [`async bqt(location: string, options?: Options): String`](#async-bqtlocation-stringoptions-options-string) | ||
* [`async aqt(location: string, options?: AqtOptions): AqtReturn`](#async-aqtlocation-stringoptions-aqtoptions-aqtreturn) | ||
- [Copyright](#copyright) | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/0.svg?sanitize=true"></a></p> | ||
## API | ||
The package can be used from Node.js. | ||
The package can be used from Node.js as multiple functions for different kinds of requests to make. The main purpose of splitting the package into multiple functions is to be able to get the correct type inference, e.g., a string for `rqt`, buffer for `bqt` and an object for `jqt`, and make it visually perceivable what kind of data is expected from the server. A _Session_ instance can be used to persist cookies across requests. | ||
### `async rqt(`<br/> `url: string,`<br/> `options?: {`<br/> `headers?: object,`<br/> `binary?: boolean,`<br/> `},`<br/>`): string` | ||
```js | ||
import rqt, { jqt, bqt, aqt, Session } from 'rqt' | ||
``` | ||
Call this function to request a web page. | ||
| Function | Meaning | Return type | | ||
| --------------------------------------------------------- | -------------------- | --------------------------------------------------------------------------------------------- | | ||
| [`rqt`](#async-rqturl-stringoptions-options-string) | String Request | Request a web page and return the result as a string. | | ||
| [`jqt`]((#async-jqturl-stringoptions-options-string)) | JSON Request | Parse the result as a `JSON` object. | | ||
| [`bqt`](#async-bqturl-stringoptions-options-string) | Binary Request | The result will be returned as a buffer. | | ||
| [`aqt`](#async-aqturl-stringoptions-aqtoptions-aqtreturn) | Advanced Request | In addition to the body, the result will contain headers and status, an alias for `@rqt/aqt`. | | ||
| [`Session`](#session-class) | Session With Cookies | Proxies all other methods from this package, but remembers cookies. | | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/1.svg?sanitize=true"></a></p> | ||
## `Options` Type | ||
Each request function accept options to set headers and send data as the second argument after the URL. | ||
`import('http').OutgoingHttpHeaders` __<a name="type-outgoinghttpheaders">`OutgoingHttpHeaders`</a>__ | ||
__<a name="type-options">`Options`</a>__: Options for requests. | ||
| Name | Type | Description | Default | | ||
| -------- | -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | -------- | | ||
| data | _*_ | 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. | `'json'` | | ||
| headers | _[OutgoingHttpHeaders](#type-outgoinghttpheaders)_ | Headers to use for the request. | - | | ||
| compress | _boolean_ | Add the `Accept-Encoding: gzip, deflate` header automatically to indicate to the server that it can send a compressed response. | `true` | | ||
| method | _string_ | What HTTP method to use to send data. | `POST` | | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/2.svg?sanitize=true"></a></p> | ||
## `async rqt(`<br/> `url: string,`<br/> `options?: Options,`<br/>`): string` | ||
Request a web page, and return the result as a string. | ||
```js | ||
import rqt from 'rqt' | ||
(async () => { | ||
const res = await rqt('http://rqt.adc.sh/') | ||
// web page contents returned | ||
})() | ||
const Request = async (url) => { | ||
const res = await rqt(url) | ||
console.log(res) | ||
} | ||
``` | ||
``` | ||
Hello World | ||
``` | ||
It is possible to pass some options as the second argument. | ||
<details> | ||
<summary>Show Server</summary> | ||
```js | ||
import rqt from 'rqt' | ||
import idioCore from '@idio/core' | ||
(async () => { | ||
const bufferRes = await rqt('http://rqt.adc.sh/', { | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Node.js) rqt', | ||
const Server = async () => { | ||
const { url } = await idioCore({ | ||
/** @type {import('koa').Middleware} */ | ||
async hello(ctx, next) { | ||
ctx.body = 'Hello World' | ||
await next() | ||
}, | ||
binary: true, | ||
returnHeaders: false, | ||
}) | ||
})() | ||
return url | ||
} | ||
export default Server | ||
``` | ||
</details> | ||
| Option | Type | Description | | ||
| ------ | ---- | ----------- | | ||
| `headers` | object | An object to be assigned as request headers. | | ||
| `binary` | boolean | If set to true, a `Buffer` will be returned instead of a string. | | ||
| `returnHeaders` | boolean | Return an object with `body` and `headers` properties instead of just the response. | | ||
--- | ||
### `async rqtWithData(`<br/> `url: string,`<br/> `options: {`<br/> `data: string|object,`<br/> `type?: string = json,`<br/> `method?: string = POST,`<br/> `},`<br/>`): string` | ||
To send data to the server, add some [options](#options-type). | ||
Send a request with data. The default type is `json` into which data will be serialised. `form` type is also supported for sending form data. All options from the blank request are also supported. | ||
```js | ||
import rqt from 'rqt' | ||
(async () => { | ||
const res = await rqt('http://rqt.adc.sh/post', { | ||
const Request = async (url) => { | ||
const res = await rqt(url, { | ||
headers: { | ||
'User-Agent': '@rqt/rqt (Node.js)', | ||
}, | ||
data: { | ||
login: 'user', | ||
password: 123456, | ||
} | ||
username: 'new-user', | ||
password: 'pass123', | ||
}, | ||
type: 'form', | ||
method: 'PUT', | ||
headers: { | ||
'x-token': 'token123', | ||
}, | ||
}) | ||
})() | ||
console.log(res) | ||
} | ||
``` | ||
```json5 | ||
You have requested with PUT: | ||
Body: { | ||
"username": "new-user", | ||
"password": "pass123" | ||
} | ||
Headers: { | ||
"user-agent": "@rqt/rqt (Node.js)", | ||
"content-type": "application/x-www-form-urlencoded", | ||
"content-length": "34", | ||
"accept-encoding": "gzip, deflate", | ||
"host": "localhost:5001", | ||
"connection": "close" | ||
} | ||
``` | ||
| Option | Type | Description | | ||
| ------ | ---- | ----------- | | ||
| `data` | string|object | Raw data or an object with data to send. | | ||
| `type` | string | How to encode data. The following are supported: set `form` for `application/x-www-form-urlencoded` and `json` for `application/json`. | | ||
| `method` | string | An HTTP method to use for sending data. | | ||
| `...` | | All other options from the request function. | | ||
<details> | ||
<summary>Show Server</summary> | ||
### `Class Session` | ||
```js | ||
import idioCore from '@idio/core' | ||
The `Session` class allows to remember the cookies during subsequent requests. It will maintain an internal state and update cookies when necessary. | ||
const Server = async () => { | ||
const { url } = await idioCore({ | ||
bodyparser: { use: true }, | ||
/** @type {import('koa').Middleware} */ | ||
async test(ctx, next) { | ||
ctx.body = `You have requested with ${ctx.method}: | ||
Body: ${JSON.stringify(ctx.request.body, null, 2)} | ||
Headers: ${JSON.stringify(ctx.request.headers, null, 2)} | ||
` | ||
await next() | ||
}, | ||
}, { port: 5001 }) | ||
return url | ||
} | ||
export default Server | ||
``` | ||
</details> | ||
#### `constructor(`<br/> `headers?: object,`<br/>`): Session` | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/3.svg?sanitize=true"></a></p> | ||
Create an instance of a `Session` class. All headers specified here will be present for each request (unless overridden by the `request` method). | ||
## `async jqt(`<br/> `url: string,`<br/> `options?: Options,`<br/>`): Object` | ||
#### `async request(`<br/> `location: string,`<br/> `options?: object,`<br/>`): any` | ||
Request a web page, and return the result as an object. | ||
Request a page. All options are the same as accepted by the `rqt` functions. | ||
```js | ||
import { jqt } from 'rqt' | ||
const Request = async (url) => { | ||
const res = await jqt(url) | ||
console.log(JSON.stringify(res, null, 2)) | ||
} | ||
``` | ||
``` | ||
{ | ||
"Hello": "World" | ||
} | ||
``` | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/4.svg?sanitize=true"></a></p> | ||
## `async bqt(`<br/> `url: string,`<br/> `options?: Options,`<br/>`): Buffer` | ||
Request a web page, and return the result as a buffer. | ||
```js | ||
const session = new Session({ | ||
headers: { | ||
'User-Agent': USER_AGENT, | ||
}, | ||
}) | ||
const { SessionKey } = await session.request('http://127.0.0.1/Session.ashx') | ||
import { bqt } from 'rqt' | ||
const { body, headers } = await session.request('http://127.0.0.1/Login.aspx', { | ||
data: { | ||
LoginUserName: 'test', | ||
LoginPassword: 'test', | ||
sessionEncryptValue: SessionKey, | ||
}, | ||
type: 'form', | ||
returnHeaders: true, | ||
}) | ||
const Request = async (url) => { | ||
const res = await bqt(url) | ||
console.log(res) | ||
} | ||
``` | ||
``` | ||
<Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64> | ||
``` | ||
--- | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/5.svg?sanitize=true"></a></p> | ||
(c) [Art Deco Code](https://artdeco.bz) 2018 | ||
## `async aqt(`<br/> `url: string,`<br/> `options?: AqtOptions,`<br/>`): AqtReturn` | ||
Request a web page and return additional information about the request. This method is also available as a standalone package: [`@rqt/aqt`](https://github.com/rqt/aqt). | ||
`import('http').OutgoingHttpHeaders` __<a name="type-outgoinghttpheaders">`OutgoingHttpHeaders`</a>__ | ||
__<a name="type-aqtoptions">`AqtOptions`</a>__: Configuration for requests. | ||
| Name | Type | Description | Default | | ||
| ----------- | -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- | -------- | | ||
| __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. | `'json'` | | ||
| headers | _[OutgoingHttpHeaders](#type-outgoinghttpheaders)_ | Headers to use for the request. | - | | ||
| compress | _boolean_ | Add the `Accept-Encoding: gzip, deflate` header automatically to indicate to the server that it can send a compressed response. | `true` | | ||
| headers | _string_ | What HTTP method to use to send data. | `POST` | | ||
| binary | _boolean_ | Whether to return a buffer instead of a string. | `false` | | ||
| justHeaders | _boolean_ | Whether to stop the request after response headers were received, without waiting for the data. | `false` | | ||
`import('http').IncomingHttpHeaders` __<a name="type-incominghttpheaders">`IncomingHttpHeaders`</a>__ | ||
__<a name="type-aqtreturn">`AqtReturn`</a>__ | ||
| Name | Type | Description | | ||
| ------------------ | -------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| __body*__ | _string\|object\|Buffer_ | The return from the server. In case the `json` content-type was set by the server, the response will be parsed into an object. If `binary` option was used for the request, a `Buffer` will be returned. Otherwise, a string response is returned. | | ||
| __headers*__ | _[IncomingHttpHeaders](#type-incominghttpheaders)_ | Incoming headers returned by the server. | | ||
| __statusCode*__ | _number_ | The status code returned by the server. | | ||
| __statusMessage*__ | _string_ | The status message set by the server. | | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/6.svg?sanitize=true"></a></p> | ||
## `Session` Class | ||
The _Session_ class allows to remember cookies set during all requests. It will maintain an internal state and update cookies when necessary. | ||
#### `constructor(`<br/> `options?: SessionOptions,`<br/>`): Session` | ||
Create an instance of the _Session_ class. All headers specified in the constructor will be present for each request (unless overridden by individual request options). | ||
__<a name="type-sessionoptions">`SessionOptions`</a>__: Options for a session. | ||
| Name | Type | Description | | ||
| ------- | -------------------------------------------------- | ------------------------------------------------------ | | ||
| host | _string_ | The prefix to each request, such as `https://rqt.biz`. | | ||
| headers | _[OutgoingHttpHeaders](#type-outgoinghttpheaders)_ | Headers to use for each request. | | ||
The methods in the _Session_ class are proxied to the respective methods in the API, but the cookies and session's headers will be set automatically. | ||
```js | ||
import { Session } from 'rqt' | ||
const SessionRequest = async (url) => { | ||
// 0. Create a Session. | ||
const session = new Session({ | ||
host: url, | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 Node.js rqt', | ||
}, | ||
}) | ||
// 1. Request A JSON Page with Jqt. | ||
const { SessionKey } = await session.jqt('/StartSession') | ||
console.log('Session key: %s', SessionKey) | ||
// 2. Send Form Data And Get Headers With Aqt. | ||
const { | ||
statusCode, | ||
body, | ||
headers: { location }, | ||
} = await session.aqt('/Login', { | ||
data: { | ||
LoginUserName: 'test', | ||
LoginPassword: 'test', | ||
sessionEncryptValue: SessionKey, | ||
}, | ||
type: 'form', | ||
}) | ||
console.log('%s Redirect: "%s"', statusCode, body) | ||
// 3. Request A Page As A String With Rqt. | ||
const res = await session.rqt(location) | ||
console.log('Page: "%s"', res) | ||
} | ||
``` | ||
``` | ||
Session key: Example-4736gst4yd | ||
302 Redirect: "Redirecting to <a href="/Portal">/Portal</a>." | ||
Page: "Hello, test" | ||
``` | ||
<details> | ||
<summary>Show Server</summary> | ||
```js | ||
import idioCore from '@idio/core' | ||
const Server = async () => { | ||
const { url, app, router } = await idioCore({ | ||
/** @type {import('koa').Middleware} */ | ||
async error(ctx, next) { | ||
try { | ||
await next() | ||
} catch ({ message }) { | ||
ctx.status = 400 | ||
ctx.body = message | ||
} | ||
}, | ||
session: { use: true, keys: ['example'] }, | ||
bodyparser: { use: true }, | ||
}, { port: 5002 }) | ||
router.get('/StartSession', async (ctx, next) => { | ||
ctx.session.SessionKey = 'Example-4736gst4yd' | ||
ctx.body = { | ||
SessionKey: ctx.session.SessionKey, | ||
} | ||
await next() | ||
}) | ||
router.post('/Login', async (ctx, next) => { | ||
const { sessionEncryptValue } = ctx.request.body | ||
if (!sessionEncryptValue) { | ||
throw new Error('Missing session key.') | ||
} | ||
if (sessionEncryptValue != ctx.session.SessionKey) { | ||
throw new Error('Incorrect session key.') | ||
} | ||
ctx.session.user = ctx.request.body.LoginUserName | ||
ctx.redirect('/Portal') | ||
await next() | ||
}) | ||
router.get('/Portal', async (ctx, next) => { | ||
if (!ctx.session.user) { | ||
throw new Error('Not authorized.') | ||
} | ||
ctx.body = `Hello, ${ctx.session.user}` | ||
await next() | ||
}) | ||
app.use(router.routes()) | ||
return url | ||
} | ||
export default Server | ||
``` | ||
</details> | ||
#### `async rqt(`<br/> `location: string,`<br/> `options?: Options,`<br/>`): String` | ||
Request a page as a string. All [options](#options-type) are the same as accepted by the `rqt` functions. | ||
#### `async jqt(`<br/> `location: string,`<br/> `options?: Options,`<br/>`): String` | ||
Request a page as an object. | ||
#### `async bqt(`<br/> `location: string,`<br/> `options?: Options,`<br/>`): String` | ||
Request a page as a buffer. | ||
#### `async aqt(`<br/> `location: string,`<br/> `options?: AqtOptions,`<br/>`): AqtReturn` | ||
Request a page and return parsed body, headers and status. | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/7.svg?sanitize=true"></a></p> | ||
## Copyright | ||
(c) [Art Deco](https://artdeco.bz) 2018 | ||
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/-1.svg?sanitize=true"></a></p> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
55416
1
8
293
373
1
10
+ Added@rqt/aqt@1.1.2
+ Added@rqt/aqt@1.1.2(transitive)
- Removedcatchment@2.0.1
- Removederotic@1.2.1
- Removedcatchment@2.0.1(transitive)
- Removedclean-stack@1.3.0(transitive)
- Removederotic@1.2.1(transitive)