Comparing version 0.0.22 to 0.0.23
66
index.js
@@ -7,22 +7,22 @@ import axios from 'axios' | ||
const api = { | ||
init: (cfg) => { | ||
console.log('api cfg: ', cfg) | ||
init: ( cfg ) => { | ||
console.log( 'api cfg: ', cfg ) | ||
const headersDef = { | ||
// 'Access-Control-Allow-Credentials':'true', | ||
'Accept': 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json' | ||
} | ||
const headers = {...headersDef, ...cfg.headers} | ||
srv = axios.create({ | ||
// 'Access-Control-Allow-Credentials':'true', | ||
'Accept': 'application/json, text/plain, */*', | ||
'Content-Type': 'application/json' | ||
} | ||
const headers = { ...headersDef, ...cfg.headers } | ||
srv = axios.create( { | ||
baseURL: cfg.url, | ||
headers: headers | ||
}) | ||
console.log('API SERVICE initialization srv:', srv) | ||
} ) | ||
console.log( 'API SERVICE initialization srv:', srv ) | ||
srv.interceptors.request.use(req => { | ||
console.log('interceptor request:', req) | ||
srv.interceptors.request.use( req => { | ||
console.log( 'interceptor request:', req ) | ||
return req | ||
}) | ||
srv.interceptors.response.use(res => { | ||
console.log('interceptor response: ', res) | ||
} ) | ||
srv.interceptors.response.use( res => { | ||
console.log( 'interceptor response: ', res ) | ||
return res.data | ||
@@ -32,3 +32,3 @@ }, | ||
const { config } = error | ||
console.log('Error FWK-API!!!! :', error) | ||
console.log( 'Error FWK-API!!!! :', error ) | ||
} | ||
@@ -50,30 +50,32 @@ | ||
}, | ||
async setHeaders(headers){ | ||
srv.defaults.headers = {...srv.defaults.headers, ...headers} | ||
async setHeaders ( headers ) { | ||
console.log( 'setHeaders :', headers ) | ||
srv.defaults.headers = { ...srv.defaults.headers, ...headers } | ||
}, | ||
setMockFlag: (flag) => { | ||
setMockFlag: ( flag ) => { | ||
console.log( 'setMockFlag :', flag ) | ||
delete srv.defaults.headers.mockmode | ||
srv.defaults.headers.mockmode = flag | ||
}, | ||
async callSrv (method, path, params) { | ||
async callSrv ( method, path, params ) { | ||
let res | ||
if (method == "GET") res = await srv.get(path) | ||
if (method == "POST") res = await srv.post(path, JSON.stringify(params)) | ||
if (method == "FORM") res = await srv.post(path, params) | ||
if ( method === "GET" ) res = await srv.get( path ) | ||
if ( method === "POST" ) res = await srv.post( path, JSON.stringify( params ) ) | ||
if ( method === "FORM" ) res = await srv.post( path, params ) | ||
return res | ||
}, | ||
async sendFile (path, params) { | ||
formData.append("data", params.data) | ||
async sendFile ( path, params ) { | ||
formData.append( "data", params.data ) | ||
const ctype = srv.defaults.headers['Content-Type'] | ||
delete srv.defaults.headers['Content-Type'] | ||
srv.defaults.headers['Content-Type'] = 'multipart/form-data' | ||
const ctype = srv.defaults.headers[ 'Content-Type' ] | ||
delete srv.defaults.headers[ 'Content-Type' ] | ||
srv.defaults.headers[ 'Content-Type' ] = 'multipart/form-data' | ||
const res = await srv.post(path, formData) | ||
srv.defaults.headers['Content-Type'] = ctype | ||
const res = await srv.post( path, formData ) | ||
srv.defaults.headers[ 'Content-Type' ] = ctype | ||
return res | ||
}, | ||
async info () { | ||
const res = await srv.get('info') | ||
console.log('info:', res) | ||
const res = await srv.get( 'info' ) | ||
console.log( 'info:', res ) | ||
return res | ||
@@ -80,0 +82,0 @@ } |
{ | ||
"name": "fwk-api", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"description": "Framework with api services access(only javascript)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
2808
75