rstify-ask

1.0.19 • Public • Published

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

Section Description
Installation npm i rstify-ask
Classes Descriptions of various classes provided by the Restify library.
- Cancel Represents an object used for canceling an operation. It contains a message describing the cancellation reason.
- CancelToken A class providing a token for canceling requests. It works in conjunction with the Cancel class to handle request cancellation.
- CanceledError An extension of the standard Error class. Represents an error that occurs when an operation is canceled.
- RestifyError An 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.
- HttpStatusCode A class containing static constants representing various HTTP status codes for convenience.
- RestifyHeaders A class containing constants for commonly used HTTP headers, including content types, caching, authentication, CORS, security, cookies, compression, and custom headers.
Utility Functions Descriptions of utility functions provided by the Restify library.
- isCancel A function that checks whether a given value is an instance of the Cancel class.
- isRestifyError A function that checks whether a given value is an instance of the RestifyError class.
- spread A utility function that returns a new function capable of spreading an array as arguments when called.
- toFormData A function that converts an object into a FormData instance, useful for handling form data in HTTP requests.
- formToJSON A function that converts form data (from FormData) into a JSON object.
- getAdapter A function that returns an adapter function based on the environment. In a browser environment, it uses fetch to make HTTP requests.
- mergeConfig A function that merges two configuration objects, with special handling for headers and params.
Restify Class Descriptions of the Restify class and its methods.
- Restify A 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 Methods Descriptions of static methods provided by the Restify class.
- Request Methods Descriptions of methods for making various HTTP request types (GET, POST, DELETE, etc.).
- Form Data Methods Descriptions of methods for handling form data in HTTP requests.
Authorization Information on handling authorization with the Restify class.
Usage Guidelines on how to use the Restify library in your projects.
Exported Components List 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.`

Package Sidebar

Install

npm i rstify-ask

Weekly Downloads

6

Version

1.0.19

License

ISC

Unpacked Size

46.4 kB

Total Files

5

Last publish

Collaborators

  • dev_abhay