npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elm-port-schema

v0.1.2

Published

Bring type-safety to the border between js and elm.

Downloads

2

Readme

:rotating_light: NOTE: This project is in early development :rotating_light:

Lots of very breaking changes are likley to occur and im sure there's many bugs to be discovered!

elm-port-schema

Bring type-safety to the interface between js and elm! Automatially generate decoders/encoders and typescript definitions from your elm types.

Why i made this tool

Elm is great! But not everything can be done in pure elm. Sometimes we need to pass over into js-land and to do that we have ports. If our elm app happends to be very port-heavy for whatever reason, we might find ourselved spending increasing amounts of time writing json decoders/encoders, syncing that with the js-side and debugging whenever a typo or something small like that slipps through. Elm and typescript helps us write code with fewer errors, lets bring them to the interface between elm and js!

Installation

npm install --save-dev elm-port-schema

How it works

NOTE: We will use typescript instead of javascript. Typescript is essentially javascript with types, which is exacly what wen need.

Everything revolves around a schema located at src/Schema.elm and the two types that need to be in it: FromElmMessage and ToElmMessage. if your schema looks like this:

module Schema exposing (..)

type FromElmMessage = ...

type ToElmMessage = ...

and you run elm-port-schema it will generate two files.

  1. src/Port.elm. You can import this module from the rest of your elm to send and recive messages. It looks a bit like this:
port module Port exposing (..)

type FromElmMessage = ...

type ToElmMessage = ...

send : FromElmMessage -> Cmd msg
send = ...

recive : (Result Json.Decode.Error ToElmMessage -> msg) -> Sub msg
recive = ...

...
  1. src/Main.d.ts. This file makes typescript aware of what data can be sent to and from elm. It look a little like this:
export let Elm: { Main: { init: (flags?: any) => ElmApp } }

export interface ElmApp {
    ports: {
        toElm: { send: (msg: ToElmMessage) => void },
        fromElm: { subscribe: (f: (msg: FromElmMessage) => void) => void },
    }
}

...

Whenever you want to change how data is passed between elm and typescript, just update Schema.elm, run elm-port-schema and fix all compiler errors in your elm and typescript. Piece of cake!

How elm and typescript maps to eachother

  • Bool <=> bool
  • Int, Float <=> number
  • Char, String <=> string
  • List <=> Array
  • ( Int, String ) <=> [ number, string ]
  • { foo : Int, bar : String } <=> { foo: number, bar: string }
  • type Foo = Bar Int String <=> { variant: "bar", _0: number, _1: string }

Maybe and Result values map to typescript just the same as any other custom type would, i.e Nothing maps to { variant: "Nothing" }, not null!

Example

https://github.com/hugobastas/elm-port-schema-starter has the boilerplate needed to get up and runing with elm-port-schema.

Important notes / Words of caution

Before using this tool, know that it WILL add complexity to your project. compared to plain js and elm you will need typescript, a bundler like webpack with a typescript and elm loader. If you're only goin to spend a couple hours interfacing js and elm this additional complexity might not be worth it. This comic commes to mind.

Also note that elm-port-schema is NOT intended for data interchange between client and server. Thats a a different problem and something like elm-graphql would be more appropriate to solve it. Also read this vission for data interchange in elm. I found it to be very enlightening!

License

MIT