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

@wisdomstar94/react-api

v1.0.1

Published

본 레포지토리는 비동기 처리(api 연동 등)와 관련한 여러 상태들을 제공하는 리액트 훅 레포지토리 입니다. api 로부터 아직 응답을 받기 전까지는 추가 요청을 막아야 하는 처리를 일일이 해주는 게 불편하기도 해서 TanStack Query(React-Query) 구조에 영감을 받아 제작해보았습니다.

Downloads

23

Readme

react-api

본 레포지토리는 비동기 처리(api 연동 등)와 관련한 여러 상태들을 제공하는 리액트 훅 레포지토리 입니다. api 로부터 아직 응답을 받기 전까지는 추가 요청을 막아야 하는 처리를 일일이 해주는 게 불편하기도 해서 TanStack Query(React-Query) 구조에 영감을 받아 제작해보았습니다.

example code

"use client"

import { useApi } from "@wisdomstar94/react-api";
import axios from "axios";
import { useEffect } from "react";
import styles from './page.module.scss';

interface Item {
  status: {
    verified: boolean;
    sentCount: number;
  };
  _id: string;
  user: string;
  text: string;
  __v: number;
  source: string;
  updatedAt: string;
  type: string;
  createdAt: string;
  deleted: boolean;
  used: boolean;
}

export default function Page() {
  const api = useApi({
    api: () => {
      const cancelTokenSource = axios.CancelToken.source();
      const instance = axios.create({
        cancelToken: cancelTokenSource.token,
      });
      return {
        fn: async() => {
          const res = await instance({
            url: `https://cat-fact.herokuapp.com/facts/`,
            method: 'get',
            params: {
              a: 'b',
            },
          });
          const result: Item[] = res.data;
          return result;
        },
        cancel() {
          cancelTokenSource.cancel();
        },
      };
    },
    enabledAutoFetch: false,
    retryCount: 3,
  });

  useEffect(() => {
    if (api.data === undefined) return;
    console.log('@@@@@@@@ api.data', api.data);
  }, [api.data]);

  useEffect(() => {
    if (api.error === undefined) return;
    console.log('@@@@@@@@ api.error', api.error);
  }, [api.error]);

  return (
    <>
      <div className="w-full relative flex flex-wrap gap-2">
        <div className="w-full relative flex flex-wrap gap-2">
          <div className="w-full relative flex flex-wrap gap-2">
            <button onClick={() => api.fetch()} className="inline-flex px-3 py-1.5 text-xs text-slate-600 border border-slate-400 rounded-lg cursor-pointer hover:bg-slate-100">
              api fetch 하기
            </button>
          </div>
          <div className="w-full relative flex flex-wrap gap-2">
            <button onClick={() => api.cancel()} className="inline-flex px-3 py-1.5 text-xs text-slate-600 border border-slate-400 rounded-lg cursor-pointer hover:bg-slate-100">
              api fetch 취소 하기
            </button>
          </div>
        </div>
        <div className="w-full relative flex flex-wrap gap-2">
          { api.isLoading ? '로딩중...' : '' }
        </div>
        <ul className="w-full relative flex flex-wrap gap-2">
          {
            api.data?.map(item => {
              return (
                <li key={item._id} className="w-full flex flex-wrap relative">
                  <table className={styles['table']}>
                    <tbody>
                      <tr>
                        <th>
                          status
                        </th>
                        <td>
                          verified: { item.status.verified } <br />
                          sentCount: { item.status.sentCount }
                        </td>
                      </tr>
                      <tr>
                        <th>
                          _id
                        </th>
                        <td>
                          { item._id }
                        </td>
                      </tr>
                      <tr>
                        <th>
                          user
                        </th>
                        <td>
                          { item.user }
                        </td>
                      </tr>
                      <tr>
                        <th>
                          createdAt
                        </th>
                        <td>
                          { item.createdAt }
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </li>
              );
            })
          }
        </ul>
      </div>
    </>
  );
}

보다 더 자세한 예제 코드는 본 레포지토리의 src/app/test/* 경로를 참조해주세요.

test

각 기능들 별로 별도의 테스트 페이지를 만들어 두었습니다. 본 프로젝트를 pull 받아 로컬에서 구동하시고 "http://localhost:3330" 에 접근하시면 각 테스트 페이지로 이동할 수 있는 버튼들이 표시됩니다.