Commit f0f2942f by Beebuddy Builder

add Network request

1 parent 21b0df72
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
"moment": "2.29.1", "moment": "2.29.1",
"uuid": "3.3.2", "uuid": "3.3.2",
"mongodb": "3.1.8", "mongodb": "3.1.8",
"axios": "0.18.0",
"express": "4.16.4", "express": "4.16.4",
"cors": "2.8.4", "cors": "2.8.4",
"body-parser": "1.18.3", "body-parser": "1.18.3",
......
import {FilterQuery,UpdateQuery,FindAndModifyWriteOpResultObject,OptionalId,ClientSession,DeleteWriteOpResultObject,UpdateWriteOpResult} from 'mongodb' import {FilterQuery,UpdateQuery,FindAndModifyWriteOpResultObject,OptionalId,ClientSession,DeleteWriteOpResultObject,UpdateWriteOpResult} from 'mongodb'
import {AxiosPromise,AxiosRequestConfig} from 'axios'
export class Utils { export class Utils {
/** /**
...@@ -23,14 +23,6 @@ export const ConnectMongo ={ ...@@ -23,14 +23,6 @@ export const ConnectMongo ={
}; };
class ServiceData{
body:JSON;
headers:JSON;
}
class Processor {
process(sv:ServiceData) :JSON {}
}
export const AppApi = { export const AppApi = {
initial : Function(), initial : Function(),
addPostMethod : async function(uri:string,processor:Processor):void{} , addPostMethod : async function(uri:string,processor:Processor):void{} ,
...@@ -38,7 +30,21 @@ export const AppApi = { ...@@ -38,7 +30,21 @@ export const AppApi = {
startApp : function():void{} startApp : function():void{}
}; };
export class Network {
/**
*
* @param {String} url
* @param {*} params
* @param {AxiosRequestConfig} config
*/
static async requestPost(url, params = {}, config = {}):JSON {}
/**
*
* @param {String} urlWithParams
* @param {AxiosRequestConfig} config
*/
static async requestGet(urlWithParams, config = {}):JSON {}
}
/*** /***
...@@ -46,6 +52,17 @@ export const AppApi = { ...@@ -46,6 +52,17 @@ export const AppApi = {
* *
* External class * External class
*/ */
class ServiceData{
body:JSON;
headers:JSON;
}
class Processor {
process(sv:ServiceData) :JSON {}
}
class InterfaceDBAction { class InterfaceDBAction {
/** /**
......
...@@ -3,12 +3,14 @@ ...@@ -3,12 +3,14 @@
Object.defineProperty(exports, "__esModule", { Object.defineProperty(exports, "__esModule", {
value: true value: true
}); });
exports.AppApi = exports.ConnectMongo = exports.ENV = exports.Utils = void 0; exports.AppApi = exports.ConnectMongo = exports.Network = exports.ENV = exports.Utils = void 0;
var _dotenv = _interopRequireDefault(require("dotenv")); var _dotenv = _interopRequireDefault(require("dotenv"));
var _connect_db = _interopRequireDefault(require("./utils/mongodb/connect_db")); var _connect_db = _interopRequireDefault(require("./utils/mongodb/connect_db"));
var _Network = _interopRequireDefault(require("./utils/network/Network"));
var _app = require("./appapi/app"); var _app = require("./appapi/app");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
...@@ -50,6 +52,8 @@ var ENV = { ...@@ -50,6 +52,8 @@ var ENV = {
env: process.env env: process.env
}; };
exports.ENV = ENV; exports.ENV = ENV;
var Network = _Network.default;
exports.Network = Network;
var ConnectMongo = _connect_db.default; var ConnectMongo = _connect_db.default;
exports.ConnectMongo = ConnectMongo; exports.ConnectMongo = ConnectMongo;
var AppApi = { var AppApi = {
......
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _axios = _interopRequireWildcard(require("axios"));
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var CallAPI =
/*#__PURE__*/
function () {
function CallAPI() {
_classCallCheck(this, CallAPI);
}
_createClass(CallAPI, null, [{
key: "post",
/**
* @param {string} url
* @param {JSON} body
* @param {AxiosRequestConfig} customConfig
* @returns {AxiosPromise}
*/
value: function () {
var _post = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(url, body) {
var customConfig,
config,
_args = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
customConfig = _args.length > 2 && _args[2] !== undefined ? _args[2] : null;
_context.prev = 1;
/**
* @type {customConfig}
*/
config = this.getNetworkConfig();
if (customConfig != null) {
config = _objectSpread({}, config, customConfig);
}
_context.next = 6;
return _axios.default.post(url, body, config);
case 6:
return _context.abrupt("return", _context.sent);
case 9:
_context.prev = 9;
_context.t0 = _context["catch"](1);
throw {
message: _context.t0.message,
status: _context.t0.response ? _context.t0.response.status : "",
data: _context.t0.response ? _context.t0.response.data : "",
response: {
headers: _context.t0.response ? _context.t0.response.headers : ""
},
errorConfig: _context.t0.config
};
case 12:
case "end":
return _context.stop();
}
}
}, _callee, this, [[1, 9]]);
}));
return function post(_x, _x2) {
return _post.apply(this, arguments);
};
}()
/**
*
* @param {String} urlWithParam
* @param {AxiosRequestConfig} config
*/
}, {
key: "get",
value: function () {
var _get = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(urlWithParam) {
var config,
_args2 = arguments;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
config = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : null;
_context2.prev = 1;
_context2.next = 4;
return _axios.default.get(urlWithParam, config);
case 4:
return _context2.abrupt("return", _context2.sent);
case 7:
_context2.prev = 7;
_context2.t0 = _context2["catch"](1);
throw {
message: _context2.t0.message,
status: _context2.t0.response ? _context2.t0.response.status : "",
data: _context2.t0.response ? _context2.t0.response.data : "",
response: {
headers: _context2.t0.response ? _context2.t0.response.headers : ""
},
errorConfig: _context2.t0.config
};
case 10:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[1, 7]]);
}));
return function get(_x3) {
return _get.apply(this, arguments);
};
}()
/**
* @returns {AxiosRequestConfig}
*/
}, {
key: "getNetworkConfig",
value: function getNetworkConfig() {
/**
* @type {AxiosRequestConfig}
*/
var config = {
timeout: 1000 * 60 * 2
};
return config;
}
}]);
return CallAPI;
}();
var _default = CallAPI;
exports.default = _default;
\ No newline at end of file
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _axios = require("axios");
var _CallAPI = _interopRequireDefault(require("./CallAPI"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Network =
/*#__PURE__*/
function () {
function Network() {
_classCallCheck(this, Network);
}
_createClass(Network, null, [{
key: "requestPost",
/**
*
* @param {String} url
* @param {*} params
* @param {AxiosRequestConfig} config
*/
value: function () {
var _requestPost = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(url) {
var params,
config,
mergeConfig,
response,
status,
data,
message,
_args = arguments;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
params = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
config = _args.length > 2 && _args[2] !== undefined ? _args[2] : {};
_context.prev = 2;
mergeConfig = _objectSpread({
// default timeout
timeout: 30 * 60 * 1000
}, config);
_context.next = 6;
return _CallAPI.default.post(url, params, mergeConfig);
case 6:
response = _context.sent;
return _context.abrupt("return", response.data);
case 10:
_context.prev = 10;
_context.t0 = _context["catch"](2);
status = _context.t0.status, data = _context.t0.data, message = _context.t0.message;
if (!(status === 409)) {
_context.next = 17;
break;
}
throw data;
case 17:
throw message ? message : _context.t0;
case 18:
case "end":
return _context.stop();
}
}
}, _callee, null, [[2, 10]]);
}));
return function requestPost(_x) {
return _requestPost.apply(this, arguments);
};
}()
/**
*
* @param {String} urlWithParams
* @param {AxiosRequestConfig} config
*/
}, {
key: "requestGet",
value: function () {
var _requestGet = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee2(urlWithParams) {
var config,
mergeConfig,
response,
status,
data,
message,
_args2 = arguments;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
config = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
_context2.prev = 1;
mergeConfig = _objectSpread({
// default timeout
timeout: 30 * 60 * 1000
}, config);
_context2.next = 5;
return _CallAPI.default.get(urlWithParams, mergeConfig);
case 5:
response = _context2.sent;
return _context2.abrupt("return", response.data);
case 9:
_context2.prev = 9;
_context2.t0 = _context2["catch"](1);
status = _context2.t0.status, data = _context2.t0.data, message = _context2.t0.message;
if (!(status === 409)) {
_context2.next = 16;
break;
}
throw data;
case 16:
throw message ? message : _context2.t0;
case 17:
case "end":
return _context2.stop();
}
}
}, _callee2, null, [[1, 9]]);
}));
return function requestGet(_x2) {
return _requestGet.apply(this, arguments);
};
}()
}]);
return Network;
}();
exports.default = Network;
\ No newline at end of file
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
"moment": "2.29.1", "moment": "2.29.1",
"uuid": "3.3.2", "uuid": "3.3.2",
"mongodb": "3.1.8", "mongodb": "3.1.8",
"axios": "0.18.0",
"express": "4.16.4", "express": "4.16.4",
"cors": "2.8.4", "cors": "2.8.4",
"body-parser": "1.18.3", "body-parser": "1.18.3",
......
import {FilterQuery,UpdateQuery,FindAndModifyWriteOpResultObject,OptionalId,ClientSession,DeleteWriteOpResultObject,UpdateWriteOpResult} from 'mongodb' import {FilterQuery,UpdateQuery,FindAndModifyWriteOpResultObject,OptionalId,ClientSession,DeleteWriteOpResultObject,UpdateWriteOpResult} from 'mongodb'
import {AxiosPromise,AxiosRequestConfig} from 'axios'
export class Utils { export class Utils {
/** /**
...@@ -23,14 +23,6 @@ export const ConnectMongo ={ ...@@ -23,14 +23,6 @@ export const ConnectMongo ={
}; };
class ServiceData{
body:JSON;
headers:JSON;
}
class Processor {
process(sv:ServiceData) :JSON {}
}
export const AppApi = { export const AppApi = {
initial : Function(), initial : Function(),
addPostMethod : async function(uri:string,processor:Processor):void{} , addPostMethod : async function(uri:string,processor:Processor):void{} ,
...@@ -38,7 +30,21 @@ export const AppApi = { ...@@ -38,7 +30,21 @@ export const AppApi = {
startApp : function():void{} startApp : function():void{}
}; };
export class Network {
/**
*
* @param {String} url
* @param {*} params
* @param {AxiosRequestConfig} config
*/
static async requestPost(url, params = {}, config = {}):JSON {}
/**
*
* @param {String} urlWithParams
* @param {AxiosRequestConfig} config
*/
static async requestGet(urlWithParams, config = {}):JSON {}
}
/*** /***
...@@ -46,6 +52,17 @@ export const AppApi = { ...@@ -46,6 +52,17 @@ export const AppApi = {
* *
* External class * External class
*/ */
class ServiceData{
body:JSON;
headers:JSON;
}
class Processor {
process(sv:ServiceData) :JSON {}
}
class InterfaceDBAction { class InterfaceDBAction {
/** /**
......
import dotenv from 'dotenv' import dotenv from 'dotenv'
import ConnectMongoInstance from './utils/mongodb/connect_db'; import ConnectMongoInstance from './utils/mongodb/connect_db';
import NetworkInstance from './utils/network/Network'
import { initial, addPostMethod, addGetMethod, startApp } from './appapi/app' import { initial, addPostMethod, addGetMethod, startApp } from './appapi/app'
dotenv.config(); dotenv.config();
...@@ -21,6 +22,10 @@ export const ENV = { ...@@ -21,6 +22,10 @@ export const ENV = {
env: process.env env: process.env
} }
export const Network = NetworkInstance
export const ConnectMongo = ConnectMongoInstance; export const ConnectMongo = ConnectMongoInstance;
......
import Axios, { AxiosPromise, AxiosRequestConfig } from "axios";
class CallAPI {
/**
* @param {string} url
* @param {JSON} body
* @param {AxiosRequestConfig} customConfig
* @returns {AxiosPromise}
*/
static async post(url, body, customConfig = null) {
try {
/**
* @type {customConfig}
*/
let config = this.getNetworkConfig();
if (customConfig != null) {
config = { ...config, ...customConfig }
}
return await Axios.post(url, body, config);
} catch (error) {
throw {
message: error.message,
status: error.response ? error.response.status : "",
data: error.response ? error.response.data : "",
response: {
headers: error.response ? error.response.headers : ""
},
errorConfig: error.config
};
}
}
/**
*
* @param {String} urlWithParam
* @param {AxiosRequestConfig} config
*/
static async get(urlWithParam, config = null) {
try {
return await Axios.get(urlWithParam, config);
} catch (error) {
throw {
message: error.message,
status: error.response ? error.response.status : "",
data: error.response ? error.response.data : "",
response: {
headers: error.response ? error.response.headers : ""
},
errorConfig: error.config
};
}
}
/**
* @returns {AxiosRequestConfig}
*/
static getNetworkConfig() {
/**
* @type {AxiosRequestConfig}
*/
let config = {
timeout: 1000 * 60 * 2
}
return config
}
}
export default CallAPI;
\ No newline at end of file
import { AxiosRequestConfig } from "axios";
import CallAPI from "./CallAPI";
export default class Network {
/**
*
* @param {String} url
* @param {*} params
* @param {AxiosRequestConfig} config
*/
static async requestPost(url, params = {}, config = {}) {
try {
const mergeConfig = {
// default timeout
timeout: 30 * 60 * 1000, // 30 minutes
...config
};
const response = await CallAPI.post(url, params, mergeConfig);
return response.data;
} catch (error) {
const { status, data, message } = error;
if (status === 409) {
throw data;
} else {
throw message ? message : error;
}
}
}
/**
*
* @param {String} urlWithParams
* @param {AxiosRequestConfig} config
*/
static async requestGet(urlWithParams, config = {}) {
try {
const mergeConfig = {
// default timeout
timeout: 30 * 60 * 1000, // 30 minutes
...config
};
const response = await CallAPI.get(urlWithParams, mergeConfig);
return response.data;
} catch (error) {
const { status, data, message } = error;
if (status === 409) {
throw data;
} else {
throw message ? message : error;
}
}
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!