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

@gapu/solid-query

v0.8.0

Published

Primitives for managing rest api integrations in solid.js

Downloads

9

Readme

Primitives for managing rest api integrations in solid.js

npm (scoped) npm bundle size (scoped) NPM

Install

npm install @gapu/solid-query

Exported functions

See the usage example

createQuery

A key is a function which returns: an object, a set, a map or a primitive type like string, number, null, etc.. The ordering of parameters does not matter if the key is an object, a set or a map, but it does for arrays, e.g:

|Structure A |Structure B |Are equal| |--------------------------------|-------------------------------|---------| |{ a: 1, b: 2 } |{ b: 1, a: 2 } |true | |[1, 2, 3] |[1, 3, 2] |false | |new Set([1, 2]) |new Set([2, 1]) |true | |new Set([1, 2]) |new Set([2, 1, 3]) |false | |new Map([[1, 2], [3, 4]])) |new Map([[1, 2], [3, 4]]) |true | |new Map([[1, 2], [3, 4]])) |new Map([[1, 2], [3, 4, 5]]) |false | |"A" |"A" |true | |"A" |"a" |false | |1 |1 |true | |1 |1.2 |false | |new Date('2024-01-01') |new Date('2024-01-01') |true | |new Date('2024-01-01') |new Date('2024-01-02') |false |

Warning
Do not consume a signal of a key directly in the queryFn, instead take the key as an argument as shown in the example

import axios from 'axios';
import { createQuery } from '@gapu/solid-query';
import { createSignal } from 'solid-js';

type QueryResponse = {
  id: number;
  title: string;
  body: string;
  userId: number;
};

const [postId, setPostId] = createSignal(1);
const [isEnabled, setIsEnabled] = createSignal(true)

export const {
  data,
  error,
  isError,
  isLoading,
  setEntry,
  refetch,
  emptyCache,
} = createQuery({
  key: () => postId(),
  enabled: () => isEnabled(),
  queryFn: async (key) => {
    const { data: post } = await axios.get<QueryResponse>(
      `https://jsonplaceholder.typicode.com/posts/${key}`
    );
    return post;
  },
});

const onNext = () => setPostId((currentId) => currentId + 1);
const onPrev = () => setPostId((currentId) => currentId - 1);
const onToggle = () => setIsEnabled((enabled) => !enabled)
const onRefetchSecond = () => refetch(2);
const onUpdateThird = () => {
  setEntry({
    data: {
      id: 1000,
      body: 'Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ratione quas voluptate similique ducimus tempora, vel odit! Debitis sequi enim numquam?',
      title: 'Lorem ipsum dolor',
      userId: 1
    }
  }, 3);
}

createMutation

import axios from "axios";
import { createMutation } from "@gapu/solid-query"
import { refetch } from "./examples/query"

type RequestBody = {
    title: string
    body: string
    userId: number
}

type ResponseBody = {
    id: number
    title: string
    body: string
    userId: number
}

export const { isLoading, mutate: createPost } = createMutation<RequestBody, ResponseBody>({
    mutationFn: async (body) => {
        const { data } = await axios.post("https://jsonplaceholder.typicode.com/posts", body);
        return data;
    },
    onSuccess(data) {
        refetch();
    }
});

// Mutation example
createPost({
    userId: 1,
    title: "Hello",
    body: "World",
});

Type definitions

import { Accessor } from 'solid-js';
import { NotUndefined } from 'object-hash';

type MutationOptions<Argument = void, Response = any, Error = any> = {
    onSuccess?: (data: Response) => void;
    onSettled?: () => void;
    onError?: (error: Error) => void;
    mutationFn: (arg: Argument) => Promise<Response>;
};
type CreateMutationReturn<Argument = void, Response = any> = {
    isLoading: Accessor<boolean>;
    mutate: (arg: Argument) => Promise<Response | undefined>;
};
declare function createMutation<
  Argument = void, 
  Response = any, 
  Error = any
>(options: MutationOptions<Argument, Response, Error>): CreateMutationReturn<Argument, Response>;

type QueryOptions<Response = any, Error = any, Key extends NotUndefined = any> = {
    key: Accessor<Key>;
    enabled?: () => boolean;
    queryFn: (key: Key) => Promise<Response>;
    onSettled?: (key: Key) => void;
    onSuccess?: (key: Key, data: Response) => void;
    onError?: (key: Key, error: Error) => void;
};
type QueryState<Response = any, Error = any> = {
    isLoading?: boolean;
    data?: Response;
    error?: Error;
};
type Update<T> = T | ((arg: T) => T);
type CreateQueryReturn<Response = any, Error = any, Key extends NotUndefined = any> = {
    data: (key?: Key) => Response | undefined;
    error: (key?: Key) => Error | undefined;
    isError: (key?: Key) => boolean;
    isLoading: (key?: Key) => boolean;
    setEntry: (update: Update<QueryState<Response, Error>>, key?: Key) => void;
    refetch: (key?: Key) => Promise<Response | undefined>;
    emptyCache: () => void;
};
declare function createQuery<
  Response = any, 
  Error = any, 
  Key extends NotUndefined = any
>(options: QueryOptions<Response, Error, Key>): CreateQueryReturn<Response, Error, Key>;

export { 
  CreateMutationReturn, 
  CreateQueryReturn, 
  MutationOptions, 
  QueryOptions, 
  QueryState, 
  Update, 
  createMutation, 
  createQuery 
};