From bd16030b806168bd05f9cf274193eadb19410ea1 Mon Sep 17 00:00:00 2001 From: Aleksandr Kraiz Date: Wed, 24 May 2023 12:38:13 +0400 Subject: [PATCH] laconic parse --- package.json | 4 ++-- src/utils/index.ts | 1 + src/utils/laconic-parse.ts | 12 ++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 src/utils/laconic-parse.ts diff --git a/package.json b/package.json index d14dd3a..6803578 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@orionprotocol/sdk", - "version": "0.19.4", + "version": "0.19.5", "description": "Orion Protocol SDK", "main": "./lib/index.cjs", "module": "./lib/index.js", @@ -105,4 +105,4 @@ "files": [ "lib/**/*" ] -} +} \ No newline at end of file diff --git a/src/utils/index.ts b/src/utils/index.ts index f401e9c..1a7aea2 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -13,6 +13,7 @@ export { default as toLowerCase } from './toLowerCase.js'; export { default as isUppercasedNetworkCode } from './isUppercasedNetworkCode.js'; export { default as getNativeCryptocurrencyName } from './getNativeCryptocurrencyName.js'; +export { default as laconicParse } from './laconic-parse.js'; export { default as isValidChainId } from './isValidChainId.js'; export { default as isKnownEnv } from './isKnownEnv.js'; // export { default as HttpError } from './httpError'; diff --git a/src/utils/laconic-parse.ts b/src/utils/laconic-parse.ts new file mode 100644 index 0000000..5fe4cd6 --- /dev/null +++ b/src/utils/laconic-parse.ts @@ -0,0 +1,12 @@ +import type { ZodTypeDef, Schema } from 'zod'; + +const laconicParse = (data: DataIn, schema: Schema) => { + const payload = schema.safeParse(data); + if (!payload.success) { + const issuesMessages = payload.error.issues.map(issue => `[${issue.path.join('.')}] ${issue.message}`).join(', '); + throw new Error(`Can't recognize payload: ${issuesMessages}`); + } + return payload.data; +} + +export default laconicParse;