npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

idempotent-query-helper

v0.0.2

Published

Idempotent Request Helper

Downloads

3

Readme

idempotent-query-helper

Idempotent Request Helper

Features

  • [x] batch operation
  • [x] order return

Install

npm install idempotent-query-helper

or

yarn add idempotent-query-helper

Install

npm install tool-library-template

Or

yarn add tool-library-template

Usage

parameter

interface IdenpotentQueryFunParams<T, R> {
  /** request function */
  queryFun: (param: any) => any;
  /** array of request parameters */
  queryParams: T[];
  /** configuration item */
  options: {
    /** Parameters requested after grouping */
    queryKey?: string;
    /** concurrent number  */
    concurrency?: number;
    /** The key of the idempotent request parameter */
    traceIdKey?: string;
    /** Get tradeId according to current parameters and index  */
    getTraceId?: GetTraceIdFun<T>;
    /** Waiting time after defeat (milliseconds) */
    sleepTime?: number;
    /** The number of arrays in a single request */
    singleQueryParamsCount?: number;
    /** A number or a function that generates a number based on the current error  */
    retryTime?: number | GetRetryTimeByErrorFun;
    /** Determine whether to continue retrying based on the current error  */
    getIsRunningByError?: GetIsRunningByErrorFun;
    /** Execute function after a single request  */
    singleQueryDone?: QueryDoneFun<R>;
  };
}

| 参数 | 说明 | 类型 | 默认值 | | :---------------- | :----------------------- | :--------------------------------------------- | :-------- | | queryFun | request function | function | - | | queryParams | array of request parameters | param[] | [] | | options.queryKey | Parameters requested after groupingkey | string | 'batchQueryItem' | | options.concurrency | concurrent number | number | 6 | | options.traceIdKey | The key of the idempotent request parameter | string | 'traceId' | | options.sleepTime | Waiting time after defeat (milliseconds) | number | 2000 | | options.singleQueryParamsCount | The number of arrays in a single request | number | 1 | | options.getTraceId | Get tradeId according to current parameters and indextradeId | function | (item, index) => string | | options.retryTime | A number or a function that generates a number based on the current error | number|function | 3 | | options.getIsRunningByError | Determine whether to continue retrying based on the current error | function | - | | options.singleQueryDone | Execute function after a single request | function | - |

return value

export interface QueryFunResult<R> {
  /** success or error */
  status: 'success' | 'error';
  /** Corresponding to the above success or failure, return result or error */
  result: R | unknown;
}

example

const errCode: Record<string, number> = {};
const result = await idenpotentQuery({
  // request function
  queryFun: (params: any) => {
    const currentParamsStr = JSON.stringify(params);
    errCode[currentParamsStr] = (errCode[currentParamsStr] || 0) + 1;
    // 第一次和第二次会报错
    if (errCode[currentParamsStr] < 3) {
      throw new Error('retry');
    }
    return sleep(10).then(() => params.batchQueryItem);
  },
  // current request parameter
  queryParams: [3, 2, 1],
  options: {
    // The generated traceId backend must be unique within a certain period of time
    getTraceId: (item, index) => `${index}`,
    // number of retries
    retryTime: () => 2,
    // Number of concurrent requests 2
    concurrency: 2,
    // The number of arrays in a single request
    singleQueryParamsCount: 2,
  },
});

// return result
[
  {
    status: 'success'
    result: [3,2]
  },
  {
    status: 'success'
     result: [1]
  },
]

// errCode[currentParamsStr] < 3
// changed to
// errCode[currentParamsStr] < 4
// return result
[
  {
    status: 'error'
    result: [Error: retry],
  },
  {
    status: 'error'
    result: [Error: retry],
  },
]

Changelog

  • 0.0.1 basically available