From 81270b3d10c7cfec74c839da55969cf5f9eb8ddd Mon Sep 17 00:00:00 2001 From: Alexis Ashley Date: Fri, 6 Jun 2025 16:49:10 +0100 Subject: [PATCH 1/2] add Typescript type hints To allow the ESM build to be more easily used in Typescript applications, provide type hints for the main decode() function. --- index.d.ts | 17 +++++++++++++++++ package.json | 7 ++++++- tsconfig.json | 10 ++++++++++ webpack-esm.cjs | 11 ++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 index.d.ts create mode 100644 tsconfig.json diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e387bb6 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,17 @@ +export interface CodecParserResult { + decode: string; +} + +export interface DecodedCodec { + parsed: CodecParserResult[] | null; + error?: string; + label: string; +} + +export interface CodecDecodeResults { + decodes: DecodedCodec[]; + error?: string; + toHTML: () => string; +} + +export function decode(val: string): CodecDecodeResults; diff --git a/package.json b/package.json index 328663a..4f6c2d3 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "sideEffects": [ "./src/decode.js" ], + "types": "index.d.ts", "keywords": [], "author": "paul_higgs@hotmail.com", "license": "BSD", @@ -51,5 +52,9 @@ "repository": { "type": "git", "url": "https://github.com/paulhiggs/codec-string" - } + }, + "files": [ + "index.d.ts", + "dist" + ] } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..9905673 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "include": [ + "index.d.ts" + ], + "compilerOptions": { + "noImplicitAny": true, + "strict": true, + "skipLibCheck": false + } +} diff --git a/webpack-esm.cjs b/webpack-esm.cjs index dc94ddf..0479645 100644 --- a/webpack-esm.cjs +++ b/webpack-esm.cjs @@ -1,6 +1,6 @@ const path = require('path'); - const { merge } = require('webpack-merge'); +const CopyPlugin = require('copy-webpack-plugin'); const common = require('./webpack.common.cjs'); @@ -19,6 +19,15 @@ module.exports = merge( module: true, path: path.resolve(__dirname, 'dist/esm'), }, + plugins: [ + new CopyPlugin({ + patterns: [ + { + from: 'index.d.ts', + }, + ], + }), + ], experiments: { outputModule: true, }, From b7a9179ddfea85e44728e8cacc32b8b15dd6caf4 Mon Sep 17 00:00:00 2001 From: Alexis Ashley Date: Fri, 6 Jun 2025 16:57:03 +0100 Subject: [PATCH 2/2] add Typescript example --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 2c69ce6..f7d7753 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,38 @@ elt.innerHTML = result.toHTML(); document.body.append(elt); ``` +### Typescript + +See [index.d.ts](./index.d.ts) for Typescript types for this library. + +```typescript +import { decode } from 'codec-string'; + +interface CodecDetails { + label: string; + error?: string; + details: string[]; +} + +interface CodecInformation { + codec: string; + error?: string; + details: CodecDetails[]; +} + +const codec = 'avc1.64002A'; +const { error, decodes } = decode(codec); +const ci: CodecInformation = { + codec, + error, + details: decodes.map(({parsed, error, label}) => ({ + label, + error, + details: parsed.map(p => p.decode), + })), +}; +``` + ### CommonJS module in Node.js ```javascript