All files / src/apps/dictionaries api.ts

10% Statements 1/10
0% Branches 0/8
0% Functions 0/9
10% Lines 1/10

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                      1x                                                                                                                                                                  
import {
  APIDictionaryVersion,
  DictionaryVersion,
  EditableDictionaryFields,
  NewAPIDictionary
} from "./types";
import { authenticatedInstance, unAuthenticatedInstance } from "../../api";
import { AxiosResponse } from "axios";
import { buildPartialSearchQuery, CUSTOM_VALIDATION_SCHEMA } from "../../utils";
import { default as containerAPI } from "../containers/api";
 
const api = {
  ...containerAPI,
  create: (ownerUrl: string, data: NewAPIDictionary) =>
    authenticatedInstance.post(`${ownerUrl}collections/`, data),
  update: (dictionaryUrl: string, data: EditableDictionaryFields) =>
    authenticatedInstance.put(dictionaryUrl, data),
  dictionaries: {
    retrieve: {
      public: (dictionariesUrl: string, q: string = "", limit = 20, page = 1) =>
        unAuthenticatedInstance.get(dictionariesUrl, {
          params: {
            limit,
            page,
            verbose: true,
            q: buildPartialSearchQuery(q),
            customValidationSchema: CUSTOM_VALIDATION_SCHEMA,
            timestamp: new Date().getTime() // work around seemingly unhelpful caching
          }
        }),
      private: (
        dictionariesUrl: string,
        q: string = "",
        limit = 20,
        page = 1
      ) =>
        authenticatedInstance.get(dictionariesUrl, {
          params: {
            limit,
            page,
            verbose: true,
            q: buildPartialSearchQuery(q),
            customValidationSchema: CUSTOM_VALIDATION_SCHEMA,
            timestamp: new Date().getTime() // work around seemingly unhelpful caching
          }
        })
    }
  },
  versions: {
    ...containerAPI.versions,
    create: (
      dictionaryUrl: string,
      data: DictionaryVersion
    ): Promise<AxiosResponse<APIDictionaryVersion>> =>
      authenticatedInstance.post(`${dictionaryUrl}versions/`, data),
    update: (
      dictionaryUrl: string,
      data: DictionaryVersion
    ): Promise<AxiosResponse<APIDictionaryVersion>> =>
      authenticatedInstance.put(`${dictionaryUrl}${data.id}/`, data)
  },
  retrieveMappings: (dictionaryUrl: string, fromConceptIds: string[]) =>
    authenticatedInstance.get(`${dictionaryUrl}mappings/`, {
      params: {
        fromConcept: fromConceptIds.join(","),
        limit: 0 // bad, todo optimize this
      }
    }),
  references: {
    add: (
      dictionaryUrl: string,
      references?: string[],
      cascade: string = "sourcemappings"
    ) =>
      authenticatedInstance.put(
        `${dictionaryUrl}references/`,
        { data: { expressions: references } }, // the nesting is not an error. Check API docs
        { params: { cascade: cascade } }
      ),
    delete: (
      dictionaryUrl: string,
      references: string[],
      cascade: string = "sourcemappings"
    ) =>
      authenticatedInstance.delete(`${dictionaryUrl}references/`, {
        data: { references: references }, // again, Check the API docs
        params: { cascade: cascade }
      })
  }
};
 
export default api;