Options
All
  • Public
  • Public/Protected
  • All
Menu

The Proxy provides an intercept layer based on build- and run-time configurations to enable easier local development, impersonation, dynamic endpoints, static data redirects, and user- and environment-specific versioning.

const proxy = data.createProxy();

Hierarchy

  • DataProxy

Index

Methods

  • Modifies the input Request object according to any matching Proxy rules. Rules are applied in the order they were added to the Proxy, so later rules will always override earlier rules.

    NOTE: You will not typically call this method directly. Instead, the DataLayer.createRequest method will invoke this function on your behalf. See that method for details.

    see

    createRequest — invokes the apply method for you

    example
    import { proxy, createRequest, fetch } from '~/path/to/data';
    import switches from '../config/features.mjson';

    if (switches.useV2endpoint) {
    // switch from Remote to REST endpoint
    proxy.use({
    path: '/v2/endpoint',
    match: {
    base: 'my-project',
    path: '/endpoint',
    }
    });
    }

    export async function getEndpointData() {
    // createRequest modifies the Request
    // object generated by the DDO using
    // Proxy rules, including the one above
    const request = createRequest({
    base: 'my-project',
    path: '/endpoint',
    });
    const response = await fetch(request)
    .catch(errors.rethrow(errors.fatal()));
    return response.data;
    }

    Parameters

    • request: Request

      The request object whose key/value pairs will be used to determine which proxy rules should be used to determine the version.

    Returns Request

    The input Request object, with properties modified according to the matching Proxy rules.

  • url(...args: any[]): string
  • Uses the current proxy rules to construct a URL based on the given arguments.

    example
    const url = proxy.url('cdn', 'images', 'logo.svg');
    
    example
    const url = proxy.url({
    base: 'cdn',
    protocol: 'https',
    path: '/some/path'
    });
    example
    import { proxy } from '~/path/to/data';

    proxy.use({
    port: 8118,
    protocol: 'https',
    host: 'images.myserver.com',
    match: {
    base: 'images'
    }
    });
    <img src="{{ getImageURL('avatars', 'e13d429a') }}" alt="" />
    <!-- https://images.myserver.com:8118/avatars/e13d429a -->
    export function getImageURL(bucket, id) {
    return proxy.url('images', bucket, id);
    }

    Parameters

    • Rest ...args: any[]

    Returns string

    A URL with the appropriate protocol, host, port, and paths given the currently configured proxy rules.

  • Add rules to the proxy instance. The order rules are added determines the order they are applied.

    example
    import { proxy } from '~/path/to/data';

    // any <a href="main.data.Request.html">Requests</a> with base == 'files'
    // will be routed to https://files.myserver.com:8118
    proxy.use({
    port: 8118,
    protocol: 'https',
    host: 'files.myserver.com',
    match: {
    base: 'files'
    }
    });

    Parameters

    • Rest ...rules: ProxyRule[]

      The rules to use to configure this proxy instance.

    Returns void