dk-checker-remove-extraneous v3.2.14
Utility for removing extraneous params via ts-interface-checker
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));
29 days ago
29 days ago
29 days ago
29 days ago
29 days ago
29 days ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
4 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
9 months ago
9 months ago
7 months ago
9 months ago
7 months ago
7 months ago
10 months ago
10 months ago
10 months ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
7 months ago
6 months ago
6 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago