Skip to content

Documents: setupEnvironment

Nguyen Tung edited this page Oct 13, 2018 · 1 revision

First step when start with react-restful, you need to call setupEnvironment

import { setupEnvironment, Store } from 'react-restful';

setupEnvironment({
    store: new Store(),
    beforeFetch: (url, requestInit) => {
        return requestInit;
    },
    // others props...
})

setupEnvironment using FetcherProps for props type definition:

interface FetcherProps {
    /**
     * Store instance
     */
    store: Store;

    /**
     * Base endpoint URI. For example: 'https://api.domain.com/'.
     * It will be grafted to the beginning of Resource's URL before request.
     * Only used when Resource's URL start with '/'. 
     */
    entry?: string;

    /**
     * Convert your request body before send
     * @param {string} bodyKey - body member key
     * @param {any} value - body member value, pair with key
     */
    requestBodyParser?: (bodyKey: string, value: any) => any;

    /**
     * Excute before making a request. 
     * You can put your header e.g: 'Authorization - Bearer eyJhbGc...' into requestInit at this point.
     * @param {string} url - Request URL.
     * @param {RequestInit} requestInit - Origin RequestInit instance.
     * @returns {RequestInit} Modified RequestInit instance.
     */
    beforeFetch?: (url: string, requestInit: RequestInit) => RequestInit;

    /**
     * Get json data form Response instance after fetch.
     * Will not used if Resource has own getResponseData method.
     * If this props has not set and no Resource's getResponseData, `await response.json()` will be use.
     * @param {Response} response - fetch Response instance.
     * @param {RequestInfo} requestInfo - object contains helpful infomation
     */
    getResponseData?: (requestInfo: RequestInfo) => Promise<any>;

    /**
     * Excute after fetch process
     * It is suitable for side-effect processing when the request fails.
     * @param {RequestInfo} requestInfo
     */
    afterFetch?: (requestInfo: RequestInfo) => void;

    /**
     * If used RequestHelper, your have option to make a confirmation message before perform the request.
     * Using onConfirm to allow you setup a defaul confirm method for every request.
     * @param {RequestConfirmInfo} confirmInfo - object contains confirm message, description and request resource.
     * @returns {Promise<boolean>} Promise resolve with an boolean, true synonymous with 'yes'.
     */
    onConfirm?: (confirmInfo: RequestConfirmInfo<{}>) => Promise<boolean>;
}

Clone this wiki locally