2.10.4 • Published 3 years ago

@acheetahk/request v2.10.4

Weekly downloads
1
License
BSD-2-Clause
Repository
github
Last release
3 years ago

codecov build npm npm.io npm.io

Please use version greater than 2.9.0

Methods Nav

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

paramtypeexplainrequire
urlstringurltrue
methodGET DELETE HEAD OPTIONS POST PUT PATCH PURGE LINK UNLINKmethodtrue
headersanyheaderfalse
queryanyConcatenate parameters in the URLfalse
dataanydata Apply only to these request methods 'PUT', 'POST', and 'PATCH'false
timeoutnumbertimeout Units are secondsfalse
withCredentialsbooleanallows cookie information to be carried across domainsfalse
responseTypearraybuffer blob document json text streamresponse-typefalse
onUploadProgress(progressEvent: any) => voidConfigure the progress on uploadfalse
onDownloadProgress(progressEvent: any) => voidConfigure the progress on downloadfalse
proxy{ host: string, port: number, auth: { username: string, password: string } }Configure proxyfalse
decompressbooleanindicates whether or not the response body should be decompressedfalse

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

paramtypeexplainrequire
urlstringResources to addresstrue
dataanyform-data bodytrue
configsRequestOptions⬇️ 👀true

fastFormData - options - configs

paramtypeexplainrequire
headersanyheaderfalse
timeoutnumbertimeout Units are secondsfalse
withCredentialsbooleanallows cookie information to be carried across domainsfalse
responseTypearraybuffer blob document json text streamresponse-typefalse
onUploadProgress(progressEvent: any) => voidConfigure the progress on uploadfalse
onDownloadProgress(progressEvent: any) => voidConfigure the progress on downloadfalse
decompressbooleanindicates whether or not the response body should be decompressedfalse

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

paramtypeexplainrequire
reqexpress.requestThe bottom layer is HTTP.IncomingMessagetrue
resexpress.responseThe bottom layer is HTTP.ServerResponsetrue
optionsProxyOptions⬇️ 👀true

fastProxy - options - options

paramtypeexplainrequire
hoststringThe server host that requires a proxytrue
portnumberThe server port that requires a proxytrue
pathstringThe routing addresstrue
headersanyThe request headertrue
bodyanyThe request bodyfalse
timeoutnumbertimeout Units are secondsfalse
isRewritebooleanWhether to override the routing addressfalse
rewritestringexample 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

paramtyperequire
urlstringtrue
pathstringtrue

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

paramtypeexplainrequire
urlstringurltrue
typestringEncoding type, the default is base64true

fileToBuffer

fileToBuffer - example

Convert web files to Buffer

const url = <YOUR FILE URL>;
const result = await fileToBuffer(url, path);

fileToBuffer - args

paramtyperequire
urlstringtrue

fileToDuplexStream

fileToDuplexStream - example

Convert web files to Duplex Stream

const url = <YOUR FILE URL>;
const result = await fileToDuplexStream(url, path);

fileToDuplexStream - args

paramtyperequire
urlstringtrue

bufferToStream

bufferToStream - example

const result = await bufferToStream(buffer);

bufferToStream - args

paramtyperequire
bufferBuffertrue

LICENSE

2.10.4

3 years ago

2.10.1

3 years ago

2.10.2

3 years ago

2.10.3

3 years ago

2.10.0

3 years ago

2.9.0

3 years ago

2.8.0

3 years ago

2.6.0

3 years ago

2.7.0

3 years ago

2.5.0

3 years ago

2.4.0

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.2

3 years ago

2.1.0

3 years ago

1.36.0

3 years ago

1.37.0

3 years ago

1.35.0

3 years ago

1.38.0

3 years ago

1.34.0

3 years ago

1.32.0

3 years ago

1.33.0

3 years ago

1.30.0

3 years ago

1.31.0

3 years ago

1.29.0

3 years ago

1.28.0

3 years ago

1.26.0

3 years ago

1.27.0

3 years ago

1.25.1

3 years ago

1.22.0

3 years ago

1.25.0

3 years ago

1.23.2

3 years ago

1.23.3

3 years ago

1.23.0

3 years ago

1.24.0

3 years ago

1.23.1

3 years ago

1.23.4

3 years ago

1.23.5

3 years ago

1.19.0

4 years ago

1.21.0

4 years ago

1.20.0

4 years ago

1.18.0

4 years ago

1.15.0

4 years ago

1.14.0

4 years ago

1.17.0

4 years ago

1.16.0

4 years ago

1.13.0

4 years ago

1.12.0

4 years ago

1.9.0

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.11.0

4 years ago

1.10.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago