Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@matchlighter/fetcher

Package Overview
Dependencies
Maintainers
0
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@matchlighter/fetcher

Awesome declarative data "fetchers" for Class-Based React Components.

  • 0.6.5
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@matchlighter/fetcher

Awesome declarative data "fetchers" for Class-Based React Components.

Fetcher leverages MobX and @matchlighter/meta_components to provide a clean, effective API.

Usage

NB: Be sure to decorate your Class Components with @with_meta_components from @matchlighter/meta_components.

@fetched(func: (params) => data, options?: { ... })

A basic fetcher that executes an async function when params change and sets the return value to the property. The basic fetcher provides options for debouncing, throttling, polling and more. See the Type definitions for the options parameter for details.

@api_fetched(url, transformer?: (response) => data, options?: { ... })

Fetcher includes a pre-built @api_fetched decorator that leverages Axios. @api_fetched provides most of the same options that @fetched does.

It may be used by decorating an accessor like so:

@with_meta_components // Note that this decoration is required. If using MobX, place it _above_ `@observer`.
class SomeComponent extends Component {
    @api_fetched("api/v1/some_class/:id")
    accessor api_result: ApiResultType;
}

It will automatically resolve :parameters in the URL string against a configurable set of sources. The default resolution order is as follows:

  • this
  • this.props
  • this.props.match.params (Intended for use with React Router)
  • config.token_context
Configuration

@api_fetched can be configured in a number of ways. This can be done like so:

import { config } from '@matchlighter/fetcher'
config.api.token_context = {}; // Provide a global data store for use when resolving `:parameter`s
config.api.backend = AxiosInstance; // Set the Axios Instance for use when making API calls
config.api.transform_response = (response) => transform_response; // Set a default response transformer

// Configure default options for `@api_fetched` Fetchers
config.api.defaults = {
    // Any Option that can be passed to Axios can be included here as well.
    ...AxiosRequestConfig,

    // Set the Request method that should be used
    method: 'GET',

    /* Action to take when a URL can't resolve all of its tokens.
     * - 'skip' - Don't make the request
     * - 'error' - Throw and Error
     * - 'continue' - Make the request anyway, without substituting the token
     * - May also be a function: (missingTokens: string[]) => MissingTokenAction
     */
    missingTokens: 'skip',

    /* May be a list of sources for token resolution, or a custom function.
     * Function Signature: (token: string, extraParams, component) => string
     * If an array is supplied, it may contain any of the following values:
     * - `this` - The Component instance
     * - `$token_context` - The global config.api.token_context object
     * - `$params` - Fetcher-local Params
     * - `$react_props` - React Props Hash
     * - `$react_state` - React State Hash
     * - `$route_params` - React Router route params
     */
    resolveTokens: ['this', '$react_props', '$route_params', '$token_context'],
}

FAQs

Package last updated on 23 Sep 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc