Options
All
  • Public
  • Public/Protected
  • All
Menu
example
import { isString } from 'lodash';
import { fetch, createRequest } from '~/path/to/datalayer';

const operation = {
method: 'GET',
base: 'my-app',
path: '/some/data',
};

const transformer = {
response(data) {
try {
return JSON.parse(data);
} catch (e) {
return data;
}
}
};

const attempt = data.utils.withTransform(fetch, transformer);

export async function getJSONData() {
const request = createRequest(operation);
const response = await attempt(request);
return response.data;
}

Hierarchy

  • Transformer

Index

Methods

  • request(data: any, headers: Record<string, string>): any
  • Enables developers to modify the data of a request prior to sending. The current body data and headers map will be passed to this method, and the return value will be used as the new request body data. You can also mutate the headers map (e.g. by adding or deleting values) prior to the request being sent.

    Parameters

    • data: any

      The payload passed to DataLayer.createRequest. Whatever you return from this function will be used as the new request payload.

    • headers: Record<string, string>

      A key-value collection of header names to header values. You can modify this object directly (e.g. by adding or deleting values) prior to the request being sent.

    Returns any

    The new body to send with the request.

  • response(data: any): any
  • Enables developers to modify the data of a response before it is returned to callers.

    Parameters

    • data: any

      The response payload returned from the server. Whatever value you return from this function will replace Response.data.

    Returns any

    The data to return to callers.