3.2.14 • Published 29 days ago

dk-checker-remove-extraneous v3.2.14

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

Utility for removing extraneous params via ts-interface-checker

coverage npm license

Installation

Add dk-checker-remove-extraneous to package.json and install.

Usage

This lib works with ts-interface-checker. You can use dk-file-generator for suitable validators generation.

Add to places where validators are used, ex. for API response:

import _ from 'lodash';
import { createCheckers } from 'ts-interface-checker';
import { removeExtraneousParams } from 'dk-checker-remove-extraneous';

import * as apiValidatorsRaw from 'validators/api';

const apiValidators = _.mapValues(apiValidatorsRaw, (value) => createCheckers(value));

function validateResponse(
  apiName: string,
  response: any
) {
  const validators = apiValidators[apiName];

  try {
    // Check mandatory params, throw on error
    validators.TypeResponse.check(response);

    // Clear extraneous params for compatibility (mutable operation)
    removeExtraneousParams({
      data: response,
      validators: validators.TypeResponse, // TypeResponse is a checker, use your name
      logger: ({ extraneousPaths }) => {
        console.warn(`Omitted extraneous ${JSON.stringify(extraneousPaths)} for "api/${apiName}"`);
      },
    });

    // Now response is carefully checked and extraneous params are removed,
    // it's safe to use it as expected
    return response;
  } catch (error) {
    throw new Error(`validateResponse: ${error.message} for "api/${apiName}"`);
  }
}

// Use in fetch
fetch(url)
  .then(fetchResponse => fetchResponse.json())
  .then(response => validateResponse('someApi', response))
  .then(validatedResponse => someLogic(validatedResponse))
  .catch((error) => someErrorHandler(error));
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.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