node-https
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

node-https

A simple http request client module based on Node.js https module

Notice

  • Only supports multipart/form-data and application/json content type
  • Does not support http redirects(301, 302) handling

Installation

npm i node-https

Useabe

typescript example

import { Http, HttpRequestOptions, Method } from '@node-https';

// ...

const http = new Http();

app.get('/', async (req: Request, res: Response) => {
  const options: HttpRequestOptions = {
    headers: {
      token: '1-2-3-4',
    },
    params: {
      key: 'hello',
    },
  };

  try {
    const result = await http.get(url, options);
    res.status(result.status).json(result.data ?? { body: result.body });
  } catch (e) {
    res.status(e.status || 500).json(e.data);
  }
});

Tips

  1. If send form type request, the field value which is base64 encoded image will convert to file item automaticlly, for example
// form format data:
[
  {
    field: 'id',
    value: 'avatar',
  },
  {
    field: 'image',
    value: 'data:image/png;base64,xxxxxxxxxxxxx',
  },
];

// will convert to
[
  {
    field: 'id',
    value: 'avatar',
  },
  {
    field: 'image',
    file: FileData,
  },
];
  1. It supports force to submit a json data as form type, for example:
Http.post(
  'url',
  {
    id: 'avatar',
    image: 'data:image/png;base64,xxxxxxxxxxxxx',
  },
  { form: true }
);

Interfaces / Types

type Method = 'POST' | 'GET' | 'PUT' | 'DELETE';
type Params = { [key: string]: any };

type JsonDataType = { [key: string]: any } | [];
type FormDataType = {
  field: string;
  value?: string;
  file?: {
    buffer: Buffer;
    filename: string;
  };
}[];

HttpResponse

interface HttpResponse {
  status: number;

  // when http response body is JSON string format
  data?: { [key: string]: any };

  // when http response body is **NOT** JSON string format
  body?: string;
}

HttpRequestOptions

interface HttpRequestOptions {
  hostname?: string;
  port?: number;
  path?: string;
  method?: Method;
  form?: boolean;
  headers?: { [key: string]: any };
  params?: Params;
  data?: JsonDataType | FormDataType;
}

HttpOptions

interface HttpOptions {
  headers?: { [key: string]: string };
  params?: Params;
}

HttpPostOptions

interface HttpPostOptions extends HttpOptions {
  form?: boolean;
}

Methods

get

get(url: string, options?: HttpOptions): Promise<HttpResponse>

put

put(
  url: string,
  data?: JsonDataType,
  options?: HttpOptions
): Promise<HttpResponse>

post

post(
  url: string,
  data?: JsonDataType | FormDataType,
  options?: HttpPostOptions
): Promise<HttpResponse>

delete

delete(url: string, options?: HttpOptions): Promise<HttpResponse>

porp

put or post

porp(
  method: 'PUT' | 'POST',
  url: string,
  data?: JsonDataType,
  options?: HttpOptions
): Promise<HttpResponse>

Request

request(
  url: string,
  method: Method,
  options?: HttpRequestOptions
): Promise<HttpResponse>

Dependencies (2)

Dev Dependencies (7)

Package Sidebar

Install

npm i node-https

Weekly Downloads

334

Version

0.1.3

License

ISC

Unpacked Size

33.9 kB

Total Files

28

Last publish

Collaborators

  • zhiguang