Giter Club home page Giter Club logo

returned's Introduction

returned

Simple caching & async query validation using zod, inspired by Tanstack Query

import r from "returned";
import z from "zod";

const { returned, mutated } = r();

const queryResult = await returned(
  // The return schema you expect
  z.string(),
  // Any async unit function
  async () => await fetch("www.foobar.com").json(),
  // A key by which to identify the query
  "myQuery"
);

if (queryResult.success) {
  const { data } = queryResult; // data: number

  // ...do something with 'result'
}

const mutationResult = await mutated(
  // The return schema you expect
  z.string(),
  // Any async unit function
  async () => await fetch("www.foobar.com", { method: "POST" }).json(),
  // An array of keys to invalidate upon success
  ["myQuery"]
);

if (mutationResult.success) {
  const { data } = mutationResult; // data: number

  // ...do something with 'result'
}

Custom Stores

Returned provides a store interface to facilitate caching:

type ReturnedStore = {
  get: <T>(key: string) => z.SafeParseSuccess<T> | undefined;
  set: <T>(key: string, value: z.SafeParseSuccess<T>) => void;
  del: (key: string) => void;
  clear: () => void;
};

Out of the box, 2 ReturnedStore implementations are provided:

STORE.VOID

A no-op, if you wish to disable caching.

import r, { STORE } from "returned";

const { returned, mutated } = r({ store: STORE.VOID() });

STORE.MEMORY

An in-memory LRU cache, with customizable max size.

This is the default behavior, with size 100.

import r, { STORE } from "returned";

const { returned, mutated } = r({ store: STORE.MEMORY(100) });

returned's People

Contributors

fuiste avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.