1.0.10 • Published 3 years ago

@feugene/request-interceptors v1.0.10

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

interceptors for the Axios

List of request interceptor

  • AuthInterceptor

List of response interceptor

  • WrapperInterceptor
  • ActionInterceptor

WrapperInterceptor

Wraps axios-response to ResponseWrapper wrapper. Wraps success and error responses.

ActionInterceptor

Execute some action by attributes from response. Actions:

  • Redirect
  • Download file by link
  • Blob download

Response must have following structure:

For redirect

{
  "status": {
    "type": "redirect",
    "url": "https://google.com"  
  }
}

For download by url

{
  "status": {
    "type": "download",
    "url": "https://google.com/file.pdf",  
    "name": "ExampleFile.pdf"  
  }
}

For download blob-file headers:

{
  "Access-Control-Expose-Headers" : "Content-Disposition",
  "x-filename": "test2.pdf"
}

Sample for Server side (Laravel Controler):

return Storage::disk('public')->download(
    $user->avatar,
    "avater.{$ext}",
    [
        'Access-Control-Expose-Headers' => 'Content-Disposition',
        'x-filename'                    => "test2.{$ext}",
    ]
);

Basic usage

import buildRequest from '@feugene/layer-request'

const apiHost = process.env.VUE_APP_API_HOST || ''

export const createRequest = (store, config) => {
  const request = buildRequest({ extra: { store } })

  request.manager.addLayer((cm) => {
    return cm.new({
      requestConfig: {
        headers: {
          ...(isObject(config.headers) ? config.headers : {}),
          'X-Requested-With': 'XMLHttpRequest',
        },
        baseURL: `${apiHost}/api`,
      },
      interceptors: {
        request: [(config) => (rConfig) => {
          console.info(`\t🌐 ${rConfig.baseURL}/${rConfig.url}`)
          return rConfig
        }],
        response: [
          (config) => (response) => {
            console.info(`\t✅ ${response.request.responseURL}`)
            return response
          },
          Interceptor(),
          ActionInterceptorBuild({
            actionAttributeName: 'status',
          }),
        ],
      },
    })
  }, 'api')

  return request
}
1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.10

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

0.5.2

3 years ago

0.5.0

3 years ago

0.4.3

3 years ago

0.3.5

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.0

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.13

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.9

4 years ago

0.0.6

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1-2

4 years ago

0.0.1

4 years ago