0.1.4 • Published 8 months ago

ggtype v0.1.4

Weekly downloads
-
License
-
Repository
-
Last release
8 months ago

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.

0.0.37

11 months ago

0.0.32

12 months ago

0.0.33

12 months ago

0.0.34

12 months ago

0.0.35

11 months ago

0.0.36

11 months ago

0.1.0

8 months ago

0.1.2

8 months ago

0.1.1

8 months ago

0.1.4

8 months ago

0.1.3

8 months ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.29

1 year ago

0.0.28

1 year ago

0.0.27

1 year ago

0.0.26

1 year ago

0.0.25

1 year ago

0.0.24

1 year ago

0.0.23

1 year ago

0.0.22

1 year ago

0.0.21

1 year ago

0.0.20

1 year ago

0.0.19

1 year ago

0.0.18

1 year ago

0.0.17

1 year ago

0.0.16

1 year ago

0.0.15

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago