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

vmo-request

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vmo-request

axios request http https

latest
npmnpm
Version
0.0.9
Version published
Maintainers
0
Created
Source

vmo-request

VmoRequest is a request utility class based on Axios, designed to provide features such as caching, state management, and configuration merging. It is particularly suitable for scenarios where the same requests need to be frequently made in an application, as the caching mechanism can effectively reduce server load and improve application performance.

Features

  • Caching Mechanism: By hashing request configurations with MD5, it caches request results to avoid duplicate requests.
  • Retry Mechanism: To handle network or API instability, you can set a retry mechanism to improve request success rates.
  • State Management: It supports three state management options: PENDING, FULFILLED, and REJECTED, ensuring consistency in request states.
  • Configuration Merging: It merges default configurations with user configurations using the mergeDeepRight method to prevent configuration overriding issues.
  • Expiration Time: It supports setting cache expiration times, automatically clearing expired caches.

Installation

npm install axios ramda md5

Usage Example

import VmoRequest from './VmoRequest'
import axios from 'axios'

const vmoRequest = new VmoRequest({
  baseURL: 'https://api.example.com',
  headers: {
    'Content-Type': 'application/json'
  }
})

async function fetchData() {
  try {
    const response = await vmoRequest.request(
      {
        url: '/data',
        method: 'get'
      },
      1000 * 2, // Cache for 2 seconds
      3 // Retry 3 times, total 4 requests
    )
    console.log(response.data)
  } catch (error) {
    console.error(error)
  }
}
fetchData() // Initiate the request
fetchData() // Enter the waiting queue
fetchData() // Enter the waiting queue
fetchData() // Enter the waiting queue
setTimeout(() => {
  fetchData() // Enter the waiting queue or directly get the cached result if the data has been returned
}, 1000 * 1)
// In the end, only one request will be truly initiated. If it fails, it will retry 3 times. When the result is finally returned, it will be distributed to subsequent requests.

setTimeout(() => {
  fetchData() // The cache will expire and a new request will be initiated
}, 1000 * 10)

API Documentation

VmoRequest Class Constructor

const vmoRequest = new VmoRequest({
  baseURL: 'https://api.example.com',
  headers: {
    'Content-Type': 'application/json'
  }
})
console.log(!!vmRequest.instance) // false

// Build mode for generating instances
const vmoRequest = new VmoRequest(
  {
    baseURL: 'https://api.example.com',
    headers: {
      'Content-Type': 'application/json'
    }
  },
  true
)
console.log(!!vmRequest.instance) // true

vmoRequest.instance // The created axios instance
  • new VmoRequest(config, createInstance) Parameters:
    • config: Default configuration.
    • createInstance: Boolean indicating whether to create an accompanying axios instance, default is false.

getDefaultConfig Method

vmoRequest.getDefaultConfig() // Partial<AxiosRequestConfig>
  • Return Value:
    • Returns the current default configuration.。

setDefaultConfig Method

vmoRequest.setDefaultConfig(config: Partial<AxiosRequestConfig>): boolean
  • Parameters:
    • config: The new default configuration.
  • Return Value:
    • Returns true if the configuration update is successful.。

request Method

vmoRequest.request(config: Partial<AxiosRequestConfig>, expiredTime: number = 0, retryTimes:number=0): Promise<any>
  • Parameters:
    • config: User’s Axios configuration.
    • expiredTime: Cache expiration time (in milliseconds), default is 0 (no caching). However, requests initiated in the same event loop will still be merged, unless set to -1, in which case they will be treated as normal requests to handle certain special scenarios.
    • retryTimes: Number of retries, default is 0 (no retries).
  • Return Value:
    • Returns a Promise that resolves to the request response data or rejects with error data.

FAQs

Package last updated on 17 Feb 2025

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