Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the Context API and the helper function #33

Open
lilnasy opened this issue Jul 30, 2023 · 0 comments
Open

Document the Context API and the helper function #33

lilnasy opened this issue Jul 30, 2023 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@lilnasy
Copy link
Owner

lilnasy commented Jul 30, 2023

Basic Usage

import { defineContext, defineExtension } from "es-codec"

interface Logger {
    log(...args : any[]): void
}

const { encode, decode } = defineContext<Logger>().createCodec([
    defineExtension({
        name: "URL",
        when(x): x is URL { return x.constructor === URL },
        encode(url, context) {
            context.log("encoding url", url)
            return url.href
        },
        decode(href, context) {
            context.log("decoding url", href)
            return new URL(href)
        }
    })
])

const encodedUrl = encode(new URL("https://example.com"), console)
const decodedUrl = decode(encodedURL, console)
@lilnasy lilnasy added the documentation Improvements or additions to documentation label Jul 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant