1.2.4 • Published 5 months ago

ajax-maker v1.2.4

Weekly downloads
3
License
MIT
Repository
-
Last release
5 months ago

Ajax Maker

NPM downloads npm version install size license

API

  import Request from 'ajax-maker';
  import type { RawAxiosRequestHeaders, AxiosRequestHeaders, RawAxiosResponseHeaders, AxiosResponseHeaders } from 'axios';

  type AxiosReqHeaders = RawAxiosRequestHeaders | AxiosRequestHeaders;
  type AxiosResHeaders = RawAxiosResponseHeaders | AxiosResponseHeaders;
  type ParsedError<T extends string = string> = {
    name: T;
    message: string;
    stack: string;
  };

  interface InitConfig<T = any> {
    onSuccess?: (res: T, headers: AxiosResHeaders) => any;
    onFailure?: (res: T, headers: AxiosResHeaders) => any;
    onLogin?: (res: T, headers: AxiosResHeaders) => any;
    onError?: (res: ParsedError, headers: AxiosReqHeaders | AxiosResHeaders) => any;
    onTimeout?: (e: ParsedError<'timeout'>, headers: AxiosReqHeaders) => any;
    isSuccess?: (res: T, status: number, headers: AxiosResHeaders) => boolean;
    isLogin?: (res: T, status: number, headers: AxiosResHeaders) => boolean;
    timeout?: number;
    debug?: boolean;
    logLevel?: TlogLevelStr;
  }

  const onSuccess = res => console.log('init success', res);
  const onFailure = res => console.log('init failure', res);
  const onError = res => console.log('init error', res);
  const onLogin = res => console.log('init login', res);
  const onTimeout = res => console.log('init timeout', res);

  // initialize instance
  // axios is AxiosStatic
  const { request, setting, axios } = new Request({
    isSuccess: (res, status, headers) => res.code === 0,
    isLogin: (res, status, headers) => res.code === 50,
    onSuccess,
    onFailure,
    onError,
    onLogin,
    onTimeout
  });

  // method 1 with init callbacks
  request(
    {
      url: `https://api.com/getMessage`,
      method: 'get'
    }
  )
  .then(res => console.log(res))
  .catch(err => console.error(err))

  // method 2 with custom callbacks
  request(
    {
      baseUrl: `https://api.com/getMessage`,
      method: 'get',
      onSuccess: res => console.log(res),
      onFailure(res => console.log(res)),
      onError: res => console.log(res),
      onLogin: err => console.error(err),
      onTimeout: err => console.error(err),
      isLogin: (res, status, headers) => status === 401
    }
  )

  // method 3 with chain callbacks
  request(
    {
      baseUrl: `https://api.com/getMessage`,
      method: 'get'
    }
  )
  .success(res => console.log(res))
  .login(res => console.log(res))
  .failure(res => console.log(res))
  .error(err => console.error(err))
  .timeout(err => console.error(err))

  // method 4 with rest api
  request(
    {
      baseUrl: `https://api.com/getMessage`,
      method: 'get',
      isSuccess: (res, status) => status === 200
    }
  )
  .success(res => console.log('success', res))
  .rest(res => console.log('rest', res));

  // method 5 rest with scope
  request(
    {
      baseUrl: `https://api.com/getMessage`,
      method: 'get',
      isSuccess: (res, status) => status === 200
    }
  )
  .success(res => console.log('success', res))
  .login(res => console.log('login', res))
  .timeout(err => console.error('timeout', err))
  .rest(res => console.log('rest', res), ['failure', 'error']);

  // dynamic setting
  setting({
    debug: false
  })

  // AxiosStatic
  const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
1.2.4

5 months ago

1.2.3

6 months ago

1.2.2

6 months ago

1.2.0

7 months ago

1.1.1

7 months ago

1.2.1

7 months ago

1.1.2

7 months ago

1.1.0

12 months ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

3 years ago

1.0.3

2 years ago

0.1.0

3 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

4 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