huridocs/uwazi

View on GitHub
app/react/V2/api/pages/index.ts

Summary

Maintainability
A
0 mins
Test Coverage
import { IncomingHttpHeaders } from 'http';
import api from 'app/utils/api';
import { RequestParams } from 'app/utils/RequestParams';
import { Page } from 'V2/shared/types';
import { FetchResponseError } from 'shared/JSONRequest';

const get = async (headers?: IncomingHttpHeaders): Promise<Page[]> => {
  try {
    const requestParams = new RequestParams({}, headers);
    if (headers && headers['Content-Language']) {
      api.locale(headers['Content-Language']);
    }
    const response = await api.get('pages', requestParams);
    return response.json;
  } catch (e) {
    return e;
  }
};

const getBySharedId = async (sharedId: string, headers?: IncomingHttpHeaders): Promise<Page> => {
  try {
    const requestParams = new RequestParams({ sharedId }, headers);
    if (headers && headers['Content-Language']) {
      api.locale(headers['Content-Language']);
    }
    const response = await api.get('page', requestParams);
    return response.json;
  } catch (e) {
    return e;
  }
};

const save = async (
  page: Page,
  headers?: IncomingHttpHeaders
): Promise<Page | FetchResponseError> => {
  try {
    const requestParams = new RequestParams(page, headers);
    const response = await api.post('pages', requestParams);
    return response.json;
  } catch (e) {
    return e;
  }
};

const deleteBySharedId = async (sharedId: string, headers?: IncomingHttpHeaders): Promise<Page> => {
  try {
    const response = await api.delete('pages', new RequestParams({ sharedId }, headers));
    return response.json;
  } catch (e) {
    return e;
  }
};

export { get, getBySharedId, deleteBySharedId, save };