3.2.14 • Published 29 days ago

dk-request v3.2.14

Weekly downloads
-
License
MIT
Repository
github
Last release
29 days ago

Request utility with validations based on Axios & ts-interface-checker

coverage npm license

This lib is maintained for my needs, open-source for your inspiration

Features

  • validates request and response by ts-interface-checker. Checkers could be created using dk-file-generator
  • omits extraneous params from response using dk-checker-remove-extraneous
  • supports mocks
  • supports file downloads if response type is blob with customizable file name
  • supports FormData requests
  • supports url as function
  • supports custom headers
  • works in Node.js
  • request params starting with omit_ are used in in url creation but omitted in body

Installation

Add dk-request to package.json and install.

Usage

import { AxiosError } from 'axios';
import { request, errors } from 'dk-request';

type TypeRequest = {
  id: string;
}

type TypeResponse = {
  data: string;
}

// possibly auto-generated
const validators = t.createCheckers({
  TypeRequest: t.iface([], { id: 'string' }),
  TypeResponse: t.iface([], { data: 'string' }),
});

request({
  url: 'https://google.com/api/get-items',
  // url: (requestParams) => `https://google.com/api/get-items/${requestParams.id}`,
  apiName: 'getItems',
  requestParams: { id: 'id' } as TypeRequest,
  validatorRequest: validators.TypeRequest,
  validatorResponse: validators.TypeResponse,
}).then((response: TypeResponse) => {
  // response is validated and cleared, put it in store or use as you need
  
  successHandler(response);
}).catch((error: Error | AxiosError) => {
  // if it's validation error then
  // error.name === errors.VALIDATION
  // error.message smth. like 
  // 'validateRequest: request.id is missing for "getItems"'
  // 'validateResponse: response.data is not a string for "getItems"'
  
  errorHandler(error);
});

Params

url - string | ((request: any) => string) - full url

apiName - string - just for logging

requestParams - Record<string, any> & { formData?: any; downloadAsFile?: boolean }

If you need to send FormData include it in formData. Other params will not be sent, but could still be used in url function.

If you need to download file passed as blob by backend use downloadAsFile: true, this way response validation will be omitted.

mock (optional) - TypeResponse, there will be no actual request to API, but validations are applied

method (optional, default POST) - 'GET' | 'POST' | 'PUT' | 'DELETE'

headers (optional) - Record<string, any>

extraneousLogger (optional) - logger for dk-checker-remove-extraneous

validatorRequest (optional) - Checker

validatorResponse (optional) - Checker

disableCredentials (optional) - boolean - restrict or allow sending of cookies along with request

omitResponseValidation (optional) - boolean - restrict or allow response validation

afterRequestInterceptor (optional) - (response: AxiosResponse) => Promise<void> - a method for performing some manipulations with response, like checking response.headers (ex. server sends JWT token in headers)

afterRequestInterceptor: (axiosResponse) => {
  const newToken = axiosResponse.headers.authorization;

  if (newToken) setTokenToStore(newToken);

  return Promise.resolve();
}

downloadFileNameGetter (optional) - (response: AxiosResponse) => string - a method for defining custom downloaded file name, ex.

downloadFileNameGetter: (axiosResponse) => {
  return (
    axiosResponse.headers['content-disposition']?.split('filename=')?.[1]?.replaceAll('"', '') ||
    'result.csv'
  );
}

For access to response headers like content-disposition don't forget expose-headers header in server response

3.2.9

29 days ago

3.2.13

29 days ago

3.2.12

29 days ago

3.2.14

29 days ago

3.2.11

29 days ago

3.2.10

29 days ago

3.2.8

1 month ago

3.2.7

1 month ago

3.2.6

1 month ago

3.2.6-alpha.0

1 month ago

3.2.2

1 month ago

3.2.3

1 month ago

3.2.1

1 month ago

3.2.0

1 month ago

3.1.43

3 months ago

3.1.42

3 months ago

3.1.41

3 months ago

3.1.40

3 months ago

3.1.39

3 months ago

3.1.38

4 months ago

3.1.16

10 months ago

3.1.15

10 months ago

3.1.18

10 months ago

3.1.17

10 months ago

3.1.23

9 months ago

3.1.22

10 months ago

3.1.25

9 months ago

3.1.24

9 months ago

3.1.27

7 months ago

3.1.26

9 months ago

3.1.29

7 months ago

3.1.28

7 months ago

3.1.21

10 months ago

3.1.20

10 months ago

3.1.19

10 months ago

3.1.34

6 months ago

3.1.33

6 months ago

3.1.36

5 months ago

3.1.35

6 months ago

3.1.37

5 months ago

3.1.30

7 months ago

3.1.32

6 months ago

3.1.31

6 months ago

3.1.12

11 months ago

3.1.11

11 months ago

3.1.14

11 months ago

3.1.13

11 months ago

3.1.9

11 months ago

3.1.8

11 months ago

3.0.18

11 months ago

3.0.19

11 months ago

3.0.9

11 months ago

3.0.4

11 months ago

3.1.2

11 months ago

3.0.13

11 months ago

3.1.1

11 months ago

3.0.10

11 months ago

3.1.0

11 months ago

3.0.11

11 months ago

3.1.7

11 months ago

3.0.16

11 months ago

3.0.8

11 months ago

3.1.6

11 months ago

3.0.17

11 months ago

3.0.7

11 months ago

3.1.5

11 months ago

3.0.14

11 months ago

3.0.6

11 months ago

3.1.4

11 months ago

3.0.15

11 months ago

2.1.6

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago