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

typeidl

v1.3.2

Published

TypeIDL is a TypeScript transformer for building clean API that can be safely exposed to users

Downloads

383

Readme

TypeIDL

npm version NPM Downloads

TypeIDL is a TypeScript transformer for building clean API that can be safely exposed to users. It is based on WebIDL standard that is used in every modern JavaScript engine. TypeIDL is more than just type-checker, it also provides a variety of security features like internal properties and methods and untrusted globals mode.

TypeIDL can help you build intrinsic-like APIs by writing regular TS code:

class API {
    stringProperty: string;

    readonly readonlyProperty: boolean = true;

    takesNumber(value: number) {
        if (typeof value === "number")
            console.log("I got a number", value);
        else
            console.log("I didn't get a number :(", value);
    }

    takesAPI(api: API) {
        console.log("I got", api.constructor.name);
    }
}

function consumer(api: any) {
    api.takesNumber(123); // Give API expected input
    api.takesNumber("123"); // API will automatically convert any given values to corresponding types

    api.takesAPI(api); // This method takes the API instance itself
    try {
        api.takesAPI({ /* ... */ }); // If the value cannot be converted, TypeError will be thrown
    } catch (err) {
        console.log(err);
    }

    api.stringProperty = "Hello world!"; // Properties are type-checked too!
    api.stringProperty = 123; // stringProperty is now "123"
    console.log("String property has value", api.stringProperty, "of type", typeof api.stringProperty);

    console.log("Readonly property is", api.readonlyProperty);
    try {
        api.readonlyProperty = false; // You cannot change readonly properties (will throw in strict mode)
    } catch (err) {
        console.log(err);
    }
    console.log("Readonly property is still", api.readonlyProperty);
}

consumer(new API()); // You can expose APIs compiled with TypeIDL to any JS consumer code

Features

WebIDL type conversion

TypeIDL inserts converters and validators to every method based on its declaration. TypeIDL makes sure that if the function executes, it gets exactly what it expects. TypeIDL works with unions, interfaces and classes, primitive types, type literals, and even provides basic template types validation. Note that TypeIDL does not perform type conversion in internal or private methods.

Internal functions

TypeIDL provides a way to create a function that can only be called from inside the project:

class API {
    /**
     * @internal
     */
    foo(bar: string) {
        console.log("foo'ing with", bar);
    }
}

new API().foo("bar"); // foo'ing with bar

(new API() as any).foo("bar"); // not working

If constructor is not declared explicitly, TypeIDL would mark it as internal by default. Classes with internal constructors cannot be constructed outside of the project.

Untrusted globals mode

If you want to run your code in an untrusted environment, you can setup TypeIDL to not trust the global scope. If you do so, TypeIDL will store globals at the beginning of the module and use stored references instead of the actual globals. You can also prefix global object access with globalThis. to get the current state.

Ignoring TypeIDL transformation

To ignore TypeIDL transformation when accessing properties, use square brackes: object["property"].

Installing

First, install TypeIDL, TypeScript and ts-patch:

npm i -D typescript ts-patch typeidl
npx ts-patch install

Then, add the TypeIDL transformer to your tsconfig.json:

{
  "compilerOptions": {
    "plugins": [{ "transform": "typeidl" }]
  }
}

After that, you can write TypeScript code and compile it to JavaScript with tsc like usual.

Config

You can pass options to TypeIDL to configure how it works:

{
  "compilerOptions": {
    "plugins": [{
      "transform": "typeidl",
      "treatMissingConstructorAsInternal": true, // Treat missing constructors in IDL classes as internal
      "useIDLDecorator": false, // Require @idl decorator to apply IDL validations to classes
      "trustGlobals": true // If set to false, TypeIDL will transform the source to store globals in the module scope
    }]
  }
}