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 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, },