diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b283ebdcd87..4b8585d4db36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Contributed: Changes: +- Use Promise resolve/reject to track state in typegen on-chain metadata retrievals - Basic support for markdown generation for different chains (only Kusama & Polkadot currently added) diff --git a/packages/typegen/src/util/wsMeta.ts b/packages/typegen/src/util/wsMeta.ts index ba53e83b5fdb..f80b20563147 100644 --- a/packages/typegen/src/util/wsMeta.ts +++ b/packages/typegen/src/util/wsMeta.ts @@ -3,27 +3,24 @@ import type { HexString } from '@polkadot/util/types'; +import { promiseTracker } from '@polkadot/api/promise/decorateMethod'; import { WebSocket } from '@polkadot/x-ws'; async function getWsData (endpoint: string, method: 'rpc_methods' | 'state_getMetadata' | 'state_getRuntimeVersion'): Promise { - return new Promise((resolve): void => { + return new Promise((resolve, reject): void => { + const tracker = promiseTracker(resolve, reject); + try { const websocket = new WebSocket(endpoint); websocket.onclose = (event: { code: number; reason: string }): void => { - const msg = `disconnected, code: '${event.code}' reason: '${event.reason}'`; - - if (event.code === 1000) { - console.log(msg); - } else { - console.error(msg); - process.exit(1); + if (event.code !== 1000) { + tracker.reject(new Error(`disconnected, code: '${event.code}' reason: '${event.reason}'`)); } }; websocket.onerror = (event: unknown): void => { - console.error(event); - process.exit(1); + tracker.reject(new Error(`WebSocket error:: ${JSON.stringify(event)}`)); }; websocket.onopen = (): void => { @@ -32,14 +29,16 @@ async function getWsData (endpoint: string, method: 'rpc_methods' | 'state_g }; websocket.onmessage = (message: { data: string }): void => { - resolve((JSON.parse(message.data) as { result: T }).result); + try { + tracker.resolve((JSON.parse(message.data) as { result: T }).result); + } catch (error) { + tracker.reject(error as Error); + } + websocket.close(); }; } catch (error) { - console.error(); - console.error(error); - console.error(); - process.exit(1); + tracker.reject(error as Error); } }); } diff --git a/tsconfig.base.json b/tsconfig.base.json index 16049a8702c0..14859b9a39a5 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -6,6 +6,7 @@ "@polkadot/api": ["api/src/index.ts"], "@polkadot/api/base": ["api/src/base/index.ts"], "@polkadot/api/packageInfo": ["api/src/packageInfo.ts"], + "@polkadot/api/promise/decorateMethod": ["api/src/promise/decorateMethod.ts"], "@polkadot/api/submittable/*": ["api/src/submittable/*.ts"], "@polkadot/api/types": ["api/src/types/index.ts"], "@polkadot/api/types/*": ["api/src/types/*.ts"],