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

layj

v0.0.3

Published

Layj, generate API types the lazy way

Downloads

4

Readme

👩🏻‍💻 Developer Ready: A comprehensive JavaScript solution to generate types for your projects. Works out of the box for most APIs.

🏃🏽 Instant Feedback: Fast, interactive watch mode.

📸 Snapshot Testing: Capture snapshots of your API types and be notified if the API responses change

Table of Contents

Getting Started

Install layj using npm

npm i layj

Let's get started by writing the type generators for themoviedb api, in a fille named api.layj.mjs:

import { layj } from "layj";


const get = async (url) => {
    return (await fetch(url)).json();
}

layj('movieType', (example) => {
   const API_KEY = "**API key**";
   example("request movie", async () => {
        return await get(`https://api.themoviedb.org/3/movie/11?api_key=${API_KEY}`);
    });

    example("request movie", async () => {
        return await get(`https://api.themoviedb.org/3/movie/110?api_key=${API_KEY}`);
    });

    example("request movie with video", async () => {
        return await get(`https://api.themoviedb.org/3/movie/157336?api_key=${API_KEY}&append_to_response=videos`);
    });

    example("request movie with video and images", async () => {
        return await get(`https://api.themoviedb.org/3/movie/157336?api_key=${API_KEY}&append_to_response=videos,images`);
    });


    example("request movie no api", async () => {
        return await get(`https://api.themoviedb.org/3/movie/11`);
    });
});

if you run this file, layj will generate the movieType type in a file movieType.ts located in the directory types and a file called movieType.snapshot in the directory snapshots, we will see how you can change this behaviour later.

Under the hood layj made the get requests then generated the types for the return values, at last it combined the types in a single type movieType using the | union type making sure that ovarlaping objects are combined.

Usage

import { layj } from "layj";

layj('UserResponse', (example) => {

    example("name only", async () => {
        return {
            name: "Jhon Doe"
        }
    });
    
    example("name and email address", async () => {
        return {
            name: "Jhon Doe",
            email: "[email protected]",
        }
    });

    example("error", async () => {
        return null;
    });
    

    example("user with age", async () => {
        return {
            name: "Jhon Doe",
            email: "[email protected]",
            age: 99,
        }
    });

    example("user likes", async () => {
        return {
            name: "Jhon Doe",
            likes: [1, "cats", false],
        }
    });
});

Running this file will produce this type:

export type UserResponse = {
      name: string,
      email: string | undefined,
      age: number | undefined,
      likes: (number | string | boolean)[] | undefined,
    }
  | null;

As you can see layj was able to combine the different responses into a single type.

Literals

Sometimes you now that a part of your respose is allways the same you would want to use literals instead of a more general type for example:

import { layj } from "layj";

layj('literalExample', (example) => {

    example("request 1", async () => {
        return {
            isError:false,
            data:"bla bla",
            dataType:"type 1",
        }
    });
    
    example("request 2", async () => {
        return {
            isError:false,
            data:"bla bla",
            dataType:"type 2",
        }
    });

    example("request 3", async () => {
        return {
            isError:false,
            data:"bla bla",
            dataType:"type 3",
        }
    });
    

    example("request 4", async () => {
        return {
            isError:false,
            data:"bla bla",
            dataType:"type 1",
        }
    });

    example("request 2", async () => {
        return {
            isError:true,
        }
    });
}, {
    literals:{
        isError:true,
        dataType:true,
    }
});

This will generate the current type

export type literalExample = {
  isError: true | false,
  data: undefined | string,
  dataType: "type 2" | "type 1" | "type 3" | undefined,
};

Other parameters

  • useXOR if set to true layj will combine the types usnig XOR
  • snapshot if set to false layj will skip creating the type snapshots
  • throwOnTypeChange if set to false layj wont throw an error if the generated type is different from the snapshot type
  • jsDoc if set to true layj will generate JSDoc comments instead of typescirpt files
  • snapshotsDir if set layj will use this directory for the snapshots
  • outDir if set layj will use this directory for the generated types
  • literals this is an object that specifies if the value under a specific path should be used as a literal type instead of the more general type (see example above)

CLI

layj commes with a cli that you can use to watch for changes to the type generator files and execute them. To use the cli you can add a watch commad in your pacage.json ex:

{
    "scriprs":{
        "watch-api-types":"layj watch",
        "generate-api-types":"layj generate",
    }
}

both watch and generate commands accept sevral flags to overwrite de default behaviour

  • --f ignores snapshot errors equivalent to setting throwOnTypeChange:false
  • --noSnapshots ignores snapshot errors equivalent to setting snapshot:false
  • --conf flag specifies a global conf file by default layj looks for layj.config.js
  • --outDir, --jsDoc, --useXOR, --snapshotsDir behave the same as their parameters equivalent

Watch mode

While you are working on a generator file and introduce changes that produce a type different than the one in the snapshot you can press r to ignore the errors and regenerate new snapshots.

Configuration

layj has 4 levels of configuration, layj combines those to a single config, in this order

  • the default config
  • the config from layj.config.js or the file specified by the --conf argument
  • the config from the command line arguments
  • the config provided as argument to the layj function

This means that parameters provided to the layj function will allways overwite all other parameters.

Contribution

  • Before making commits, please open an issue to make sure the changes made are in the scope of layj
  • Keep the code simple and short.
  • Make sure you don't introduce new unexpected behavour
  • Thats it, have fun