Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dry-axios

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dry-axios

Axios wrapper with typescript decorators

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

DRY Axios

Utilize typescript decorators to create declarative http service with axios

Installation

Add to project with your package manager

npm install axios dry-axios
yarn add axios dry-axios
pnpm install axios dry-axios

Set experimental decorators options in your tsconfig.json

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Example Usage

import Axios from 'axios';
import {
  Asserted,
  Post,
  Get,
  Sample,
  Jwt,
  Http,
} from 'dry-axios;

// runtime is a config passed to certain resolvers when methods are invoked
// ext: jwtResolver, sample validate & apply. See below
type Runtime = {
  mode: 'production' | 'development';
};

@Http<Runtime>(Axios, {
  // config passed to axios.create
  axios: {
    baseURL: 'http://localhost:3005',
  },
  runtime: {
    mode: 'development',
  }
})
class AccountHttpService {
  @Get('/identity', {
    // by default, return response.data. Set preserveAxiosResponse to keep original response object
    preserveAxiosResponse: false;
    // other available options passed to axios.request
  })
  @Jwt<Runtime>((runtime) => someJwtGetter(runtime))
  async getIdentity(): Promise<IdentityResponse> {
    // method body will be ignored completely.
    // use Asserted for better typing
    return Asserted<IdentityResponse>();
  }

  @Post('/sign-up')
  @Sample<Runtime>({
    resolver: async () => {
      // resolve to a mocked response for validate or testing purposes
      // dynamic import json might help reduce bundle size during production
      // (need to set "resolveJsonModule" to true in tsconfig.json)
      const module = await import('./sign-up.sample.json');
      return module.default;
    },
    // apply the mocked response returned in resolver
    // instead of actually calling the api
    // (helpful for testing purposes)
    apply: (runtime) => runtime.mode === 'development',
    // this will validate the 'real' response against the mocked response returned in resolver
    // ex: if a field is missing or typeof is different
    validate: (runtime) => runtime.mode === 'development',
  })
  async signUp(@Body _body: SignUpRequestBody): Promise<SignUpResponse> {
    return Asserted<SignUpResponse>();
  }
}

const accountHttp = new AccountHttpService();

// later
const response = await accountHttp.signUp({ ... });

Todos

  • unit tests, please and thank you (like Ron Swanson needs some bacon)!
  • project inspired by axiosfit

Keywords

FAQs

Package last updated on 09 Sep 2021

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc