11'use strict' ;
22
3- require ( 'isomorphic-fetch' ) ;
43const util = require ( './util' ) ;
54const Promise = require ( 'es6-promise' ) . Promise ;
5+ const axios = require ( 'axios' ) . default ;
66
77const getHeaders = function ( sitekey , privatekey ) {
88 return {
@@ -18,14 +18,13 @@ const getHeaders = function(sitekey, privatekey) {
1818var getDocument = function ( apiHostname , sitekey , privatekey , id ) {
1919 const promise = new Promise ( ( resolve , reject ) => {
2020
21- fetch ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id ,
21+ axios . get ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id ,
2222 {
23- method : 'GET' ,
2423 headers : getHeaders ( sitekey , privatekey )
2524 } )
2625 . then ( ( response ) => {
2726 if ( response . status == 200 ) {
28- resolve ( response . json ( ) ) ;
27+ resolve ( response . data ) ;
2928 }
3029 else {
3130 reject ( { status : response . status , text : response . statusText } ) ;
@@ -44,14 +43,16 @@ var getDocument = function(apiHostname, sitekey, privatekey, id) {
4443var saveDocument = function ( apiHostname , sitekey , privatekey , document ) {
4544
4645 // If the doc has id or url field, PUT instead of POST
46+
4747 const isPut = document . id || document . url ;
4848
4949 const promise = new Promise ( ( resolve , reject ) => {
50- fetch ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' ,
50+ axios (
5151 {
52- method : isPut ? 'PUT' : 'POST' ,
52+ url : 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' ,
53+ method : isPut ? 'put' : 'post' ,
5354 headers : getHeaders ( sitekey , privatekey ) ,
54- body : JSON . stringify ( document )
55+ data : document
5556 } )
5657 . then ( ( response ) => {
5758 if ( response . status == 202 ) {
@@ -61,8 +62,8 @@ var saveDocument = function(apiHostname, sitekey, privatekey, document) {
6162 reject ( { status : response . status , text : response . statusText } ) ;
6263 }
6364 } ) . catch ( ( ex ) => {
64- reject ( { status : 400 , text : ex } ) ;
65- } ) ;
65+ reject ( { status : 400 , text : ex } ) ;
66+ } ) ;
6667 } ) ;
6768
6869 return promise ;
@@ -75,11 +76,12 @@ var saveDocument = function(apiHostname, sitekey, privatekey, document) {
7576var saveDocumentsBatch = function ( apiHostname , sitekey , privatekey , documents ) {
7677
7778 const promise = new Promise ( ( resolve , reject ) => {
78- fetch ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch' ,
79+ axios (
7980 {
80- method : 'PUT' ,
81+ method : 'put' ,
82+ url : 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch' ,
8183 headers : getHeaders ( sitekey , privatekey ) ,
82- body : JSON . stringify ( documents )
84+ data : documents
8385 } )
8486 . then ( ( response ) => {
8587 if ( response . status == 202 ) {
@@ -102,9 +104,8 @@ var saveDocumentsBatch = function(apiHostname, sitekey, privatekey, documents) {
102104 */
103105var deleteDocument = function ( apiHostname , sitekey , privatekey , id ) {
104106 const promise = new Promise ( ( resolve , reject ) => {
105- fetch ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id ,
107+ axios . delete ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id ,
106108 {
107- method : 'DELETE' ,
108109 headers : getHeaders ( sitekey , privatekey )
109110 } )
110111 . then ( ( response ) => {
@@ -128,11 +129,10 @@ var deleteDocument = function(apiHostname, sitekey, privatekey, id) {
128129 */
129130var deleteDocumentsBatch = function ( apiHostname , sitekey , privatekey , batch ) {
130131 const promise = new Promise ( ( resolve , reject ) => {
131- fetch ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch' ,
132+ axios . delete ( 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch' ,
132133 {
133- method : 'DELETE' ,
134134 headers : getHeaders ( sitekey , privatekey ) ,
135- body : JSON . stringify ( batch )
135+ data : batch
136136 } )
137137 . then ( ( response ) => {
138138 if ( response . status == 202 ) {
0 commit comments