1.1.16 • Published 3 years ago

net4j v1.1.16

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

net4j

Pluggable & Promise based HTTP client for the browser and node.js

Features

  • Support Typescript
  • Pluggable, easily unified processing
  • Dependency injection
  • Make XMLHttpRequests from the browser
  • Make http requests from node.js
  • Supports the Promise API
  • Automatic transforms for JSON data

gif

Browser Support

ChromeFirefoxSafariOperaEdgeIE
Latest ✔Latest ✔Latest ✔Latest ✔Latest ✔11 ✔

Browser Matrix

Install

$ npm install net4j

$ yarn add net4j

Example

Normal Request

Performing a GET request

import Net from 'net4j';

const net = new Net();

// Type merge,so that /api/v2/goods will be typed
declare module 'net4j' {
  interface IGetRoute {
    '/api/v2/goods': {
      request: {
        id: number
      }
      response: {
        data: {
          GoodsInfo
        } 
      }
    },
  }
}

// `GET` request
// result will be typed with {data: {GoodsInfo}}
const result = await net.get('/api/v2/goods');

Restful

import Net from 'net4j';

const net = new Net();

// Type merge,so that /api/v2/goods/:id will be typed
declare module 'net4j' {
  interface IGetRoute {
    '/api/v2/goods/:id': {
      response: {
        data: {
          GoodsInfo
        } 
      }
    },
  }
}

// `GET` request
// result will be typed with {data: {GoodsInfo}}
const result = await net.get('/api/v2/goods/:id', {restful: {id: 123}});

With plugins

import Net from 'net4j';
import { message, Modal } from 'antd';
import NetLog from 'net4j-log-plugin';
import NetSuccess, { SuccessConfig } from 'net4j-success-plugin';
import NetLoading, { LoadingConfig } from 'net4j-loading-plugin';
import NetException, { ExceptionConfig } from 'net4j-exception-plugin';


declare module 'net4j' {
  // Merge plugin config to net4j config,then you can use it in every requst in net4j
  interface IConfig extends SuccessConfig, LoadingConfig, ExceptionConfig {};

  // Type merge,so that /api/v2/goods will be typed
  interface IGetRoute {
    '/api/v2/goods': {
      request: {
        id: number
      }
      response: {
        data: {
          GoodsInfo
        } 
      }
    },
  }
}

// Exception code to message
// Exception code comes from http status,response result.code,promise reject error.code
const codeMsgMap = {
  404: 'page not found',
  403: function() {
    // In this time, tipsComponent will not show.
    redirect('/login');
  },
  5400: 'Name repeat'
}

const net = new Net(plugins: [
  // Inject own log,so the exception and other operations will be reported
    new NetLog({
      log: {
        info: myLogger.info,
        error: myLogger.error,
      },
    }),
    // When request is pending, message loading will display and close when request finish
    new NetLoading({
      loading: message.loading,
    }),
    // When request completed successfully, message.success will display
    new NetSuccess({
      tipsComponent: message.success,
    }),
    // When request get exception,error modal will display and error will be auto reported
    new NetException({
      tipsComponent: Modal.error,
      codeMsgMap,
    }),
  ]);

// `GET` request
// result will be typed with {data: {GoodsInfo}}
const result = await net.get('/api/v2/goods', {params: {id: 123}});

Plugins

How to write a plugin

export interface Plugin {
  beforeRequest?(e?: Error, config?: AxiosRequestConfig, lib?: Lib): IConfig | Promise<Config>;
  // Inject libs to whole request, so other plugins use these libs 
  applyLib?(lib: { [key: string]: any}): { [key: string]: any};
  afterRequest?<T = any>(e?: Error, response?: T, lib?: Lib): T | Promise<AxiosResponse<Error>>;
}
1.3.0

3 years ago

1.1.16

3 years ago

1.2.0

3 years ago

1.1.15

3 years ago

1.1.14

4 years ago

1.1.13

5 years ago

1.1.12

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

0.0.28

5 years ago

0.0.27

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.24

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.20

5 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago