/jin-axios-curlize

AxiosRequestConfig to curl command

Primary LanguageTypeScriptMIT LicenseMIT

jin-axios-curlize

ts Download Status Github Star Github Issues NPM version License ci codecov code style: prettier

jin-axios-curlize create curl command from AxiosRequestConfig.

Why?

  1. automatic create curl command from AxiosRequestConfig
  2. Quickly repeat error request
  3. Support querystring, header, body replacer
  4. work without intercepter, no need to request execute for curl command creation

Table of Contents

installation

npm install jin-axios-curlize --save-dev

How to works?

jin-axios-curlize create curl command from AxiosRequestConfig. For example,

  • AxiosRequestConfig.headers to --header option
  • AxiosRequestConfig.url and AxiosRequestConfig.params to querystring and href
flowchart LR
    IMH[AxiosRequestConfig] --> JC[jin-axios-curlize]
    JC --> C[curl command]
Loading

Usage

import axios, { AxiosRequestConfig } from 'axios';
import { createFromAxios } from 'jin-axios-curlize';

const req: AxiosRequestConfig = {
  url: 'http://localhost:3000/v1/superhero/i-am-unique-id'
  method: 'put',
  data: {
    name: 'ironman'
  }
}

const reply = await axios.request(req);

console.log('curl command: ', createFromAxios(req));
// curl -X PUT 'http://localhost:3000/v1/superhero/i-am-unique-id' -d $'{"name":"ironman"}'

Options

Name Requirement Description
prettify require Apply prettifing. Add newline and backslash add on line-ending
indent optional Only work on prettify set true, make space size
disableFollowRedirect optional If set true, remove --location option from command
replacer.querystring optional replacer for querystring
replacer.body optional replacer for body
replacer.header optional replacer for header

How do I add transaction id on querystring?

import { createV3, encodeQuerystring } from 'jin-curlize';

createV3(req, {
  prettify: false,
  replacer: {
    querystring: (qs) => {
      const next = new URLSearchParams(qs);
      // add your transaction id on querystring, `uuidgen` is linux or macosx uuid generator command
      next.set('tid', `'"$(uuidgen)"'`);
      return encodeQuerystring(next);
    },
  },
});

jin-curlize

If you want that curl command generate from FastifyRequest, use jin-curlize package.