# All files / src API.ts

93.89% Statements 123/131
68.75% Branches 11/16
100% Functions 5/5
93.89% Lines 123/131

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    1321x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    1x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    9x
    12x

    9x
    12x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    12x
    12x
    12x
    60x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    4x
    4x
    12x
    12x
    12x

    12x
    12x
    12x
    12x
    11x
    11x
    12x
    12x
    12x
    12x

    12x
    12x
    12x
    12x
    8x
    8x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x
    1x
     import superagent from 'superagent';
    import Cookies from 'universal-cookie';
    import { addHeadersFactory } from './utils';
    import type request from 'superagent';

    const methods = ['get', 'post', 'put', 'patch', 'del'];

    export type ApiRequestParams = {
      params?: any;
      data?: any;
      type?: any;
      headers?: any;
      checkUrl?: boolean;
    };

    function getBackendURL(path: string) {
      const apiPath =
        // process.env.API_PATH ||
        // process.env.RAZZLE_API_PATH ||
        // (window && window.env.API_PATH) ||
        // (window && window.env.RAZZLE_API_PATH) ||
        'http://localhost:55001/plone' || 'http://localhost:8080/Plone';

      const APISUFIX = '/++api++';

      if (path.startsWith('http://') || path.startsWith('https://')) return path;

      const adjustedPath = path[0] !== '/' ? `/${path}` : path;

      return `${apiPath}${APISUFIX}${adjustedPath}`;
    }

    export async function handleRequest(
      method: string,
      path: string,
      options?: ApiRequestParams,
    ): Promise<any> {
      const fetcher = new API();
      const response = await fetcher[method](path, options);

      if (!response.ok) {
        throw new Error('Network response was not ok');
      }
      return response.body;
    }

    export default class API {
      [m: string]: (
        path: string,
        options?: ApiRequestParams,
      ) => Promise<request.Response>;

      constructor(req?: Request) {
        const cookies = new Cookies();

        methods.forEach((method) => {
          this[method] = async (
            path,
            {
              params,
              data,
              type,
              headers = {},
              checkUrl = false,
            }: ApiRequestParams = {},
          ) => {
            let request: request.SuperAgentRequest;

            // @ts-ignore
            request = superagent[method](getBackendURL(path));

            if (params) {
              request.query(params);
            }

            let authToken;
            if (req) {
              // @ts-ignore
              // We are in SSR
              authToken = req.universalCookies.get('auth_token');
              request.use(addHeadersFactory(req));
            } else {
              authToken = cookies.get('auth_token');
            }
            if (authToken) {
              request.set('Authorization', `Bearer ${authToken}`);
            }

            request.set('Accept', 'application/json');

            if (type) {
              request.type(type);
            }

            Object.keys(headers).forEach((key) => request.set(key, headers[key]));

            if (data) {
              request.send(data);
            }

            return request;

            // request.end((err, response) => {
            //   if (
            //     checkUrl &&
            //     request.url &&
            //     request.xhr &&
            //     stripQuerystring(request.url) !==
            //       stripQuerystring(request.xhr.responseURL)
            //   ) {
            //     if (request.xhr.responseURL?.length === 0) {
            //       return reject({
            //         code: 408,
            //         status: 408,
            //         url: request.xhr.responseURL,
            //       });
            //     }
            //     return reject({
            //       code: 301,
            //       url: request.xhr.responseURL,
            //     });
            //   }
            //   return err ? reject(err) : resolve(response.body || response.text);
            // });

            // promise.request = request;
            // return promise;
          };
        });
      }
    }