@rest-hooks/rest
    TypeScript icon, indicating that this package has built-in type declarations

    3.0.1 • Public • Published

    Rest Hooks for REST

    CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome Chat

    Extensible CRUD patterns for REST APIs.

    Simple TypeScript definition

    class ArticleResource extends Resource {
      readonly id: number | undefined = undefined;
      readonly title: string = '';
      readonly body: string = '';
    
      pk() { return this.id; }
      static urlRoot = '/articles/';
    }

    Standard CRUD Endpoints

    Reads

    const article = useResource(ArticleResource.detail(), { id: 5 });
    const articles = useResource(ArticleResource.list(), {});

    Mutates

    const updateArticle = useFetcher(ArticleResource.update());
    const partialUpdateArticle = useFetcher(ArticleResource.partialUpdate());
    const createArticle = useFetcher(ArticleResource.create());
    const deleteArticle = useFetcher(ArticleResource.delete());

    DRY Customization

    import { Resource } from '@rest-hooks/rest';
    import { useAuth } from 'my-auth-lib';
    
    abstract class AuthdResource extends Resource {
      static useFetchInit = (init: RequestInit) => {
        const { session } = useAuth();
        return {
        ...options,
          headers: {
            ...options.headers,
            'Access-Token': session,
          },
        }
      };
    }
    import type { ReadEndpoint, FetchFunction } from '@rest-hooks/endpoint';
    
    import AuthdResource from './AuthdResource';
    
    export default class UserResource extends AuthdResource {
      /** Retrieves current logged in user */
      static current<T extends typeof Resource>(this: T): ReadEndpoint<FetchFunction, T> {
        return super.detail().extend({ url: () => '/current_user' });
      }
    }

    Prior Art

    Install

    npm i @rest-hooks/rest

    DownloadsWeekly Downloads

    5,049

    Version

    3.0.1

    License

    Apache-2.0

    Unpacked Size

    260 kB

    Total Files

    66

    Last publish

    Collaborators

    • ntucker
    • ljharb