import { IRemoteConfigs } from '../../nlpviewer' ; export interface APIDocument { id : string ; textPack : string ; ontology : string ; } export interface APIDocOntology { id : string ; textPack : string ; ontology : string ; } export interface APIDocConfig { id : string ; config : string ; } export function fetchDocuments ( ) { return fetch ( '/api/documents' ) . then ( r => r . json ( ) ) ; } export function fetchProjects ( ) { return fetch ( '/api/projects' ) . then ( r => r . json ( ) ) ; } export function fetchProject ( id ) { return fetch ( `/api/projects/${ id }` ) . then ( r => r . json ( ) ) ; } export function fetchDocument ( id ) { return fetch ( `/api/documents/${ id }` ) . then ( r => r . json ( ) ) ; } export function fetchDocOntology ( id ) { return fetch ( `/api/ontology_from_doc/${ id }` ) . then ( r => r . json ( ) ) ; } export function fetchDocProjectConfig ( id ) { return fetch ( `/api/config_from_doc/${ id }` ) . then ( r => r . json ( ) ) ; } export function nextDocument ( id ) { return fetch ( `/api/next_doc/${ id }` ) . then ( r => r . json ( ) ) ; } export function prevDocument ( id ) { return fetch ( `/api/prev_doc/${ id }` ) . then ( r => r . json ( ) ) ; } export function updateDocument ( id , name , textPack ) { return postData ( `/api/documents/${ id }/edit` , { name : name , textPack : textPack , } ) . then ( r => r . json ( ) ) ; } export function updateOntology ( id , ontology ) { return postData ( `/api/documents/${ id }/edit_ontology` , { ontology : ontology , } ) . then ( r => r . json ( ) ) ; } export function createDocument ( name , textPack , project_id ) { return postData ( '/api/documents/new' , { name : name , textPack : textPack , project_id : project_id , } ) . then ( r => r . json ( ) ) ; } export function createCrossDoc ( name , textPack , project_id ) { return postData ( '/api/crossdocs/new' , { name : name , textPack : textPack , project_id : project_id , } ) . then ( r => r . json ( ) ) ; } export function createProject ( type , name , ontology , config , multiOntology = '{}' ) { return postData ( '/api/projects/new' , { type : type , name : name , ontology : ontology , multiOntology : multiOntology , config : config , } ) . then ( r => r . json ( ) ) ; } export function deleteDocument ( id ) { return postData ( `/api/documents/${ id }/delete` ) ; } export function deleteCrossDoc ( id ) { return postData ( `/api/crossdocs/${ id }/delete` ) ; } export function deleteProject ( id ) { return postData ( `/api/projects/${ id }/delete` ) ; } export function fetchDocumentsProject ( id ) { return postData ( `/api/projects/${ id }/docs` ) . then ( r => r . json ( ) ) ; } export function fetchDocumentsAndMultiPacksProject ( id ) { return postData ( `/api/projects/${ id }/crossdocs` ) . then ( r => r . json ( ) ) ; } export function fetchUsers ( ) { return fetch ( '/api/users' ) . then ( r => r . json ( ) ) ; } export function fetchUser ( id ) { return fetch ( `/api/users/${ id }` ) . then ( r => r . json ( ) ) ; } export function updateUser ( id , name , password ) { return postData ( `/api/users/${ id }/edit` , { name , password , } ) . then ( r => r . json ( ) ) ; } export function deleteUser ( id ) { return postData ( `/api/users/${ id }/delete` ) ; } export function createUser ( name , password ) { return postData ( '/api/users/new' , { name , password , } ) . then ( r => r . json ( ) ) ; } export function editText ( documentId , new_text ) { return postData ( `/api/documents/${ documentId }/text/edit` , { new_text , } ) ; } export function addAnnotation ( documentId , data ) { return postData ( `/api/documents/${ documentId }/annotations/new` , { data , } ) . then ( r => r . json ( ) ) ; } export function editAnnotation ( documentId , annotationId , data ) { return postData ( `/api/documents/${ documentId }/annotations/${ annotationId }/edit` , { data , } ) ; } export function deleteAnnotation ( documentId , annotationId ) { return postData ( `/api/documents/${ documentId }/annotations/${ annotationId }/delete` , { } ) ; } export function addLink ( documentId , data ) { return postData ( `/api/documents/${ documentId }/links/new` , { data , } ) . then ( r => r . json ( ) ) ; } export function editLink ( documentId , linkId , data ) { return postData ( `/api/documents/${ documentId }/links/${ linkId }/edit` , { data , } ) ; } export function deleteLink ( documentId , linkId ) { return postData ( `/api/documents/${ documentId }/links/${ linkId }/delete` , { } ) ; } export function loadNlpModel ( remoteConfigs ) { return postData ( '/api/nlp/load' , { remoteConfigs } ) ; } export function runNlp ( documentId ) { return postData ( `/api/nlp/${ documentId }` , { } ) . then ( r => r . json ( ) ) ; } export function login ( name , password ) { return postData ( '/api/login' , { name , password , } ) ; } export function signup ( name , password ) { return postData ( '/api/signup' , { name , password , } ) ; } export function logout ( ) { return fetch ( '/api/logout' ) ; } export function completeDocument ( documentId ) { return fetch ( `/api/documents/${ documentId }/complete` ) ; } async function postData ( url = '' , data = { } ) { const response = await fetch ( url , { method : 'POST' , mode : 'cors' , cache : 'no-cache' , credentials : 'same-origin' , headers : { 'Content-Type' : 'application/json' , } , redirect : 'follow' , referrerPolicy : 'no-referrer' , body : JSON . stringify ( data ) , } ) ; if ( response . status >= 400 ) { console . error ( response . statusText ) ; return response ; } else { return response ; } } const w = window ; w . postData = postData ;