0.6.1 • Published 1 month ago

@matchlighter/fetcher v0.6.1

Weekly downloads
14
License
MIT
Repository
-
Last release
1 month ago

@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'],
}
0.6.1

1 month ago

0.3.0

3 years ago

0.2.7

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago