@acheetahk/request
v2.10.4
Published
✨ work with request
Downloads
166
Maintainers
Readme
Please use version greater than 2.9.0
Methods Nav
about
request
about
proxy
about
file
Installation
npm install @acheetahk/request
cnpm install @acheetahk/request
yarn add @acheetahk/request
Dependencies
{
"@types/express": "^4.17.8",
"@types/form-data": "^2.5.0",
"axios": "^0.21.0",
"form-data": "^3.0.0"
}
Usage
fastRequest
fastRequest - get
import { fastRequest } from '@acheetahk/request';
const response = await fastRequest({ url: <YOUR URL>, method: 'GET' });
fastRequest - post
import { fastRequest } from '@acheetahk/request';
const response = await fastRequest({ url: <YOUR URL>, method: 'POST', data: { value: 'test post' } });
fastRequest - options
|param|type|explain|require|
|:-:|:-:|:-:|:-:|
|url
|string|url|true|
|method
|GET
DELETE
HEAD
OPTIONS
POST
PUT
PATCH
PURGE
LINK
UNLINK
|method|true|
|headers
|any|header|false|
|query
|any|Concatenate parameters in the URL|false|
|data
|any|data
Apply only to these request methods 'PUT', 'POST', and 'PATCH'|false|
|timeout
|number|timeout Units are seconds|false|
|withCredentials
|boolean|allows cookie information to be carried across domains|false|
|responseType
|arraybuffer
blob
document
json
text
stream
|response-type|false|
|onUploadProgress
|(progressEvent: any) => void|Configure the progress on upload|false|
|onDownloadProgress
|(progressEvent: any) => void|Configure the progress on download|false|
|proxy
|{ host
: string, port
: number, auth
: { username
: string, password
: string } }| Configure proxy |false|
|decompress
|boolean| indicates whether or not the response body should be decompressed |false|
fastFormData
fastFormData - example
import * as fs from 'fs';
import { fastFormData } from '@acheetahk/request';
const response = await fastFormData(
{
url,
data: {
name: 'test formData',
file: fs.createReadStream(resolve(__dirname, './xx.xx')),
},
},
);
fastFormData - options
|param|type|explain|require|
|:-:|:-:|:-:|:-:|
|url
|string|Resources to address|true|
|data
|any|form-data body
|true|
|configs
|RequestOptions
| ⬇️ 👀 |true|
fastFormData - options - configs
|param|type|explain|require|
|:-:|:-:|:-:|:-:|
|headers
|any|header|false|
|timeout
|number|timeout Units are seconds|false|
|withCredentials
|boolean|allows cookie information to be carried across domains|false|
|responseType
|arraybuffer
blob
document
json
text
stream
|response-type|false|
|onUploadProgress
|(progressEvent: any) => void|Configure the progress on upload|false|
|onDownloadProgress
|(progressEvent: any) => void|Configure the progress on download|false|
|decompress
|boolean| indicates whether or not the response body should be decompressed |false|
fastProxy
Delegate to the specified express.js / nest.js service
fastProxy - example for nest.js
import { NestFactory } from '@nestjs/core';
import { Module, All, Controller, HttpCode, HttpStatus, InternalServerErrorException, Req } from '@nestjs/common';
@Controller('demo')
class DemoController {
@All()
@HttpCode(HttpStatus.OK)
async demo(@Req() requset: Request) {
try {
await fastProxy(
requset,
requset.res,
{
host: '<The server host that requires a proxy>',
port: '<The server port that requires a proxy>',
isRewrite: true,
rewrite: '/demo',
body: requset.body,
path: requset.originalUrl,
headers: requset.headers,
},
);
} catch (error) {
throw new InternalServerErrorException('Error');
}
}
}
const app = await NestFactory.create(CoreModule, { cors: true });
await app.listen(8000);
fastProxy - example of express.js
import * as express from 'express';
import { fastProxy } from '@acheetahk/request';
const app = express();
const router = express.Router();
app.use('/express', router);
router.all('*', async (requset: Request, response: Response) => {
await fastProxy(
requset,
response,
{
host: '<The server host that requires a proxy>',
port: '<The server port that requires a proxy>',
isRewrite: true,
rewrite: '/express',
body: requset.body,
path: requset.originalUrl,
headers: requset.headers,
},
);
});
const server = app.listen(8000);
fastProxy - options
|param|type|explain|require|
|:-:|:-:|:-:|:-:|
|req
|express.request
|The bottom layer is HTTP.IncomingMessage|true|
|res
|express.response
|The bottom layer is HTTP.ServerResponse|true|
|options
|ProxyOptions
| ⬇️ 👀 |true|
fastProxy - options - options
|param|type|explain|require|
|:-:|:-:|:-:|:-:|
|host
| string |The server host that requires a proxy|true|
|port
| number |The server port that requires a proxy|true|
|path
|string|The routing address|true|
|headers
|any|The request header|true|
|body
| any |The request body|false|
|timeout
|number|timeout Units are seconds|false|
|isRewrite
|boolean|Whether to override the routing address|false|
|rewrite
|string|example of rewrite rule: '/express' ==> '/'
or '/express/test' ==> '/test'
|false|
fileDownload
fileDownload - example
Download the file and return to the local file path
import { resolve } from 'path';
const path = resolve(__dirname, './fileDownload');
const url = <YOUR FILE URL>;
// result -> The full path of the downloaded file
const result = await fileDownload(url, path);
fileDownload - args
|param|type|require| |:-:|:-:|:-:| |url|string|true| |path|string|true|
fileToStr
fileToStr - example
Convert the network file into a string of the specified encoding
const url = <YOUR FILE URL>;
const result = await fileToStr(url);
fileToStr - args
|param|type|explain|require|
|:-:|:-:|:-:|:-:|
|url|string|url|true|
|type|string|Encoding type, the default is base64
|true|
fileToBuffer
fileToBuffer - example
Convert web files to Buffer
const url = <YOUR FILE URL>;
const result = await fileToBuffer(url, path);
fileToBuffer - args
|param|type|require| |:-:|:-:|:-:| |url|string|true|
fileToDuplexStream
fileToDuplexStream - example
Convert web files to Duplex Stream
const url = <YOUR FILE URL>;
const result = await fileToDuplexStream(url, path);
fileToDuplexStream - args
|param|type|require| |:-:|:-:|:-:| |url|string|true|
bufferToStream
bufferToStream - example
const result = await bufferToStream(buffer);
bufferToStream - args
|param|type|require| |:-:|:-:|:-:| |buffer|Buffer|true|