Socket
Socket
Sign inDemoInstall

rstify-ask

Package Overview
Dependencies
176
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    rstify-ask

Restify is a comprehensive npm package that streamlines and enhances the handling of HTTP requests in JavaScript applications.


Version published
Weekly downloads
6
decreased by-75%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Restify is a comprehensive npm package that streamlines and enhances the handling of HTTP requests in JavaScript applications. This package offers a clean and consistent interface for making HTTP requests, managing cancellations, and handling errors effectively. Designed to be versatile, Restify is suitable for use in both browser and non-browser environments, catering to a wide array of applications.

Table of Contents

SectionDescription
Installationnpm i rstify-ask
ClassesDescriptions of various classes provided by the Restify library.
- CancelRepresents an object used for canceling an operation. It contains a message describing the cancellation reason.
- CancelTokenA class providing a token for canceling requests. It works in conjunction with the Cancel class to handle request cancellation.
- CanceledErrorAn extension of the standard Error class. Represents an error that occurs when an operation is canceled.
- RestifyErrorAn extension of the standard Error class. Represents an error specific to the Restify library, providing additional information such as configuration, status code, request, and response.
- HttpStatusCodeA class containing static constants representing various HTTP status codes for convenience.
- RestifyHeadersA class containing constants for commonly used HTTP headers, including content types, caching, authentication, CORS, security, cookies, compression, and custom headers.
Utility FunctionsDescriptions of utility functions provided by the Restify library.
- isCancelA function that checks whether a given value is an instance of the Cancel class.
- isRestifyErrorA function that checks whether a given value is an instance of the RestifyError class.
- spreadA utility function that returns a new function capable of spreading an array as arguments when called.
- toFormDataA function that converts an object into a FormData instance, useful for handling form data in HTTP requests.
- formToJSONA function that converts form data (from FormData) into a JSON object.
- getAdapterA function that returns an adapter function based on the environment. In a browser environment, it uses fetch to make HTTP requests.
- mergeConfigA function that merges two configuration objects, with special handling for headers and params.
Restify ClassDescriptions of the Restify class and its methods.
- RestifyA class providing a set of static methods for making HTTP requests. It includes methods for various HTTP methods such as GET, POST, PUT, etc. Additionally, it has methods for handling form data, dynamic authorization, and generic request handling. The class exports individual components for flexibility.
- Static MethodsDescriptions of static methods provided by the Restify class.
- Request MethodsDescriptions of methods for making various HTTP request types (GET, POST, DELETE, etc.).
- Form Data MethodsDescriptions of methods for handling form data in HTTP requests.
AuthorizationInformation on handling authorization with the Restify class.
UsageGuidelines on how to use the Restify library in your projects.
Exported ComponentsList of individual components exported by the Restify library.

Install Restify via npm:

npm i rstify-ask

import Restify from "rstify-ask";

Cancel

The Cancel class represents a cancellation and includes a message describing the cancellation reason.

CancelToken

The CancelToken class provides a mechanism to cancel asynchronous operations. It includes a Promise that rejects with a Cancel instance when the operation is canceled.

CanceledError

An extension of the standard Error class, the CanceledError represents an error thrown when an operation is canceled.

RestifyError

An extension of the standard Error class, the RestifyError represents an error specific to the Restify package. It includes additional properties like config, code, request, and response.

HttpStatusCode

A class containing constants for common HTTP status codes, facilitating the reference of status codes in your code.

RestifyHeaders

A class containing constants for common HTTP headers, simplifying header usage in HTTP requests.

Utility Functions

a. isCancel A function to check if a given value is an instance of the Cancel class.

isRestifyError

A function to check if a given value is an instance of the RestifyError class.

spread

A function that returns a wrapper around a callback, allowing it to be called with an array of arguments.

toFormData

A function to convert an object into a FormData instance.

formToJSON

A function to convert a FormData instance into a JSON string.

getAdapter

A function that returns the appropriate HTTP request adapter based on the environment (browser or non-browser).

mergeConfig

A function to merge two configuration objects, with special handling for headers and params.

Restify Class

The main class providing a high-level interface for making HTTP requests.

Static Methods

request(config): Make a generic HTTP request.
get(url, config): Make a GET request.
delete(url, config): Make a DELETE request.
head(url, config): Make a HEAD request.
options(url, config): Make an OPTIONS request.
post(url, data, config): Make a POST request with JSON data.
put(url, data, config): Make a PUT request with JSON data.
patch(url, data, config): Make a PATCH request with JSON data.
postForm(url, data, config): Make a POST request with form data.
putForm(url, data, config): Make a PUT request with form data.
putForm(url, data, config): Make a PUT request with form data.
patchForm(url, data, config): Make a PATCH request with form data.
withAuthorization(authorizationToken): Create a new instance with dynamic authorization token.

Request Methods

These methods return promises that resolve with the HTTP response.

Form Data Methods

Methods specifically for handling form data, converting objects to FormData instances.

Authorization

A method to generate an instance with a dynamic authorization token.

Usage

javascript Copy code

import Restify, { CancelToken, HttpStatusCode, RestifyHeaders } from 'rstify-ask';

# Use CancelToken to cancel requests
```javascript
const { token, cancel } = CancelToken.source();
const requestConfig = { method: 'GET', url: 'https://example.com', cancelToken: token };


# Make a GET request
```javascript
   Restify.get('https://api.example.com/data', requestConfig)
  .then(response => console.log(response))
  .catch(error => console.error(error));`

# Merge configurations
```javascript
const config1 = { headers: { 'Content-Type': 'application/json' } };
const config2 = { params: { page: 1 } };
const mergedConfig = Restify.mergeConfig(config1, config2);`

# Use Restify class with dynamic authorization
```javascript 
const authorizedInstance = Restify.withAuthorization('your_token');
authorizedInstance.get('https://api.example.com/data')
  .then(response => console.log(response))
  .catch(error => console.error(error));

# Exported Components
All classes, functions, and constants are exported individually for flexibility in usage. Check the documentation above for detailed information on each exported component.`

Keywords

FAQs

Last updated on 09 Feb 2024

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc