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

ggtype

v0.0.14

Published

[![Build](https://github.com/samuelgja/ggtype/actions/workflows/build.yml/badge.svg)](https://github.com/samuelgja/ggtype/actions/workflows/build.yml) [![Code Quality Check](https://github.com/samuelgja/ggtype/actions/workflows/code-check.yml/badge.svg)](

Downloads

760

Readme

ggtype

Build Code Quality Check Build Size

🚀 ggtype is a high-performance TypeScript library designed to make data validation and action management both simple and efficient. Inspired by popular libraries like tRPC and Zod, ggtype offers a streamlined API that is easy to use, yet powerful enough to handle complex business logic.

With ggtype, you can:

  • Validate Data: Ensure that your data is correctly structured and adheres to your defined types, giving you confidence in the integrity of your data across your application.
  • Manage Actions: Define and execute actions that operate on your data models, whether they are simple operations or part of a more complex workflow.
  • Create Type-Safe APIs: Just like tRPC, ggtype allows you to build type-safe APIs without needing to write a lot of boilerplate code.
  • Handle Complex Logic: Use ggtype to combine multiple actions into a workflow, managing dependencies and execution order effortlessly.

Note: This library is still in beta. Feedback and contributions are welcome! Also there are missing client and server libraries, this is more like core library.

Key Features

  • Define Data Models: Easily create models to define the structure of your data.
  • Type Inference: Automatically generate TypeScript types from your models, so you don't have to manually write them.
  • Manage Actions: Create actions that work with your data models, supporting both simple and complex tasks.
  • Combine Actions: Group multiple actions together and run them in a specific order.
  • Handle Errors: Built-in error handling for predictable and clear error management.

Installation

You can install ggtype using Bun, Yarn, or npm:

Using Bun:

bun add ggtype

Using Yarn or npm:

yarn add ggtype  # or npm i ggtype

Quick Start Guide

Here's a simple example to help you get started:

Define a Model and Action

  1. Create a Data Model: Define what your data should look like.
import { m } from 'ggtype'

const userModel = m.object({
  id: m.string().isRequired(),
  name: m.string(),
})
  1. Create an Action: Set up an action that uses this model.
import { action } from 'ggtype'

const createUserAction = action(userModel, async ({ params }) => {
  return {
    isJohnDoe: params.name === 'John Doe',
  }
})
  1. Combine Actions into a Graph: Group your actions together for more complex workflows.
import { graph } from 'ggtype'

const app = graph({
  createUser: createUserAction,
})

const result = await app.parse({
  createUser: { id: '1', name: 'John Doe' },
})

console.log(result.createUser.ok?.isJohnDoe) // Output: true

Full Example

Here's how to define a model, compile it for validation, and use it in actions:

import { compileModel, m, action, graph, type Infer } from 'ggtype'

// Define your data model
const userModel = m.object({
  id: m.string().isRequired(),
  name: m.string(),
  profile: m.object({
    age: m.number().isRequired(),
  }),
  friends: m.array(m.object({
    user_id: m.string().isRequired(),
  })),
})

// Compile the model for validation
const compiledModel = compileModel(userModel)

// Use the model in an action
const createUserAction = action(userModel, ({ params }) => {
  return params.name
})

// Combine actions in a graph
const app = graph({
  createUser: createUserAction,
})

// Run the graph with some data
const result = await app.parse({
  createUser: {
    id: '1',
    name: 'John Doe',
    profile: { age: 30 },
    friends: [{ user_id: '2' }],
  },
})

console.log(result.createUser.ok) // Output: John Doe

Performance Note

The validation part of ggtype uses AJV, which is significantly faster than other libraries like Zod. Benchmarks for tRPC don't exist yet, but ggtype is designed to be more performant while being just as easy to use.

API Overview

  • m.object: Define a structured data model.
  • action: Create actions that work with your models.
  • graph: Group actions into workflows.
  • ValidationError: Handle validation errors easily.

Testing

Testing with ggtype is straightforward. Here’s a simple example:

import { object, string, action } from 'ggtype'

describe('action', () => {
  it('should fail with invalid params', () => {
    const userModel = object({
      id: string().isRequired(),
      createdAt: string(),
    })

    const someAction = action(userModel, ({ params }) => {
      return params.createdAt
    })

    expect(() => {
      someAction.fn({
        context: {},
        params: {} as unknown as { id: string },
      })
    }).toThrow(ValidationError)
  })
})

Contributing

We welcome contributions! If you find a bug or have a suggestion, please open an issue. To contribute code, fork the repository and submit a pull request.

License

ggtype is licensed under the MIT License. See the LICENSE file for details.