@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
class SomeComponent extends Component {
@api_fetched("api/v1/some_class/:id")
accessor api_result: ApiResultType;
}
It will automatically resolve :parameter
s 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 = {};
config.api.backend = AxiosInstance;
config.api.transform_response = (response) => transform_response;
config.api.defaults = {
...AxiosRequestConfig,
method: 'GET',
missingTokens: 'skip',
resolveTokens: ['this', '$react_props', '$route_params', '$token_context'],
}