@transcend-io/type-utils
Advanced tools
Comparing version 1.3.0 to 1.4.1
@@ -11,3 +11,3 @@ import * as t from 'io-ts'; | ||
*/ | ||
export declare function decodeCodec<TCodec extends t.Any>(codec: TCodec, txt: string | object | object[] | null | unknown, parse?: boolean, customErrorFromContext?: (validationContext: t.Context) => string): t.TypeOf<TCodec>; | ||
export declare function decodeCodec<TCodec extends t.Any>(codec: TCodec, txt: string | object | object[] | null | unknown, parse?: boolean, customErrorFromContext?: ((validationContext: t.Context) => string) | undefined): t.TypeOf<TCodec>; | ||
//# sourceMappingURL=decodeCodec.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeCodec = exports.CODEC_ERROR_MESSAGE = void 0; | ||
// external | ||
const Either_1 = require("fp-ts/lib/Either"); | ||
const pipeable_1 = require("fp-ts/lib/pipeable"); | ||
// Determine the paths of the codec that are invalid | ||
const getPaths = (v) => (0, pipeable_1.pipe)(v, (0, Either_1.fold)((errors) => errors.map((error) => { | ||
const lastError = error.context[error.context.length - 1]; | ||
return `${error.context | ||
.map((context) => context.key) | ||
// Note: lastError.actual is helpful for debugging | ||
.join('.')} expected type '${lastError.type.name}'`; | ||
}), () => ['no errors'])); | ||
// Get custom error messages for the errors | ||
const getCustomErrors = (v, customErrorFromContext) => (0, pipeable_1.pipe)(v, (0, Either_1.fold)((errors) => errors.map((error) => customErrorFromContext(error.context)), () => ['no errors'])); | ||
/** | ||
* Determine the codec paths that are invalid. | ||
* @param v - Validation errors. | ||
* @returns The error strings formatted for human consumption. | ||
*/ | ||
function getPaths(v) { | ||
return (0, pipeable_1.pipe)(v, (0, Either_1.fold)((errors) => errors.map((error) => { | ||
const lastCtx = error.context.at(-1); | ||
const fullPath = error.context.map(({ key }) => key).join('.'); | ||
return `${fullPath} expected type '${lastCtx === null || lastCtx === void 0 ? void 0 : lastCtx.type.name}'`; | ||
}), () => ['no errors'])); | ||
} | ||
/** | ||
* Get custom error messages for the errors. | ||
* @param v - Validation errors. | ||
* @param customErrorFromContext - Custom mapper function. | ||
* @returns The custom error strings formatted for human consumption. | ||
*/ | ||
function getCustomErrors(v, customErrorFromContext) { | ||
return (0, pipeable_1.pipe)(v, (0, Either_1.fold)((errors) => errors.map((error) => customErrorFromContext(error.context)), () => ['no errors'])); | ||
} | ||
exports.CODEC_ERROR_MESSAGE = 'Failed to decode codec:'; | ||
@@ -26,11 +36,12 @@ /** | ||
*/ | ||
function decodeCodec(codec, txt, parse = true, | ||
// Warning: DO NOT LOG SENSITIVE DATA WITH THIS! | ||
customErrorFromContext = () => '') { | ||
function decodeCodec(codec, txt, parse = true, customErrorFromContext = undefined) { | ||
// uncomment the below to view the response pre-decode | ||
const decoded = codec.decode(parse && typeof txt === 'string' ? JSON.parse(txt) : txt); | ||
// Log errors on failure | ||
// eslint-disable-next-line no-underscore-dangle | ||
if (decoded._tag === 'Left') { | ||
throw new Error(`${exports.CODEC_ERROR_MESSAGE} \n\n${JSON.stringify(getPaths(decoded), null, 2)}${JSON.stringify(getCustomErrors(decoded, customErrorFromContext), null, 2)}`); | ||
if ((0, Either_1.isLeft)(decoded)) { | ||
const errorPaths = getPaths(decoded); | ||
const customError = customErrorFromContext !== undefined | ||
? JSON.stringify(getCustomErrors(decoded, customErrorFromContext), null, 2) | ||
: undefined; | ||
throw new Error(`${exports.CODEC_ERROR_MESSAGE} ${JSON.stringify(errorPaths, null, 2)}${customError !== null && customError !== void 0 ? customError : ''}`); | ||
} | ||
@@ -37,0 +48,0 @@ // Return the decoded codec |
@@ -19,3 +19,3 @@ /** | ||
*/ | ||
export type HttpMethod = typeof HttpMethod[keyof typeof HttpMethod]; | ||
export type HttpMethod = (typeof HttpMethod)[keyof typeof HttpMethod]; | ||
//# sourceMappingURL=enums.d.ts.map |
@@ -5,3 +5,3 @@ { | ||
"description": "Small package containing useful typescript utilities.", | ||
"version": "1.3.0", | ||
"version": "1.4.1", | ||
"homepage": "https://github.com/transcend-io/type-utils", | ||
@@ -23,3 +23,3 @@ "repository": { | ||
"clean": "yarn tsc --build --clean", | ||
"lint": "yarn eslint src --ext .ts", | ||
"lint": "yarn eslint ./src --ext .js --ext .ts --ext .tsx --ext .jsx", | ||
"prepublish": "yarn build", | ||
@@ -26,0 +26,0 @@ "test": "yarn mocha './src/**/*.test.ts'" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
139674
119
1540