Latest Threat ResearchGlassWorm Loader Hits Open VSX via Developer Account Compromise.Details
Socket
Book a DemoInstallSign in
Socket

@rest-hooks/rest

Package Overview
Dependencies
Maintainers
2
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rest-hooks/rest

Endpoints for REST APIs

Source
npmnpm
Version
4.1.0
Version published
Weekly downloads
1.8K
18.39%
Maintainers
2
Weekly downloads
 
Created
Source

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

Keywords

REST

FAQs

Package last updated on 23 Dec 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts