New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

node-https

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-https

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

latest
Source
npmnpm
Version
0.1.3
Version published
Weekly downloads
373
-19.26%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • 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,
  },
];
  • 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>

Keywords

nodejs

FAQs

Package last updated on 31 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts