convert-slow-api
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -12,2 +12,4 @@ "use strict"; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
@@ -17,2 +19,6 @@ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
var axios = _axios["default"].create({ | ||
@@ -28,3 +34,5 @@ headers: { | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(url, params) { | ||
var config, | ||
var conf, | ||
config, | ||
res, | ||
_yield$axios$post, | ||
@@ -43,23 +51,43 @@ id, | ||
case 0: | ||
config = _args.length > 2 && _args[2] !== undefined ? _args[2] : { | ||
conf = _args.length > 2 && _args[2] !== undefined ? _args[2] : {}; | ||
config = _objectSpread({ | ||
optimized: true, | ||
interval: 1000 | ||
}; | ||
_context.next = 3; | ||
}, conf); | ||
if (config.optimized) { | ||
_context.next = 7; | ||
break; | ||
} | ||
_context.next = 5; | ||
return axios.post(url, params); | ||
case 3: | ||
case 5: | ||
res = _context.sent; | ||
return _context.abrupt("return", res.data); | ||
case 7: | ||
_context.next = 9; | ||
return axios.post(url, params, { | ||
params: { | ||
optimized: 'true' | ||
} | ||
}); | ||
case 9: | ||
_yield$axios$post = _context.sent; | ||
id = _yield$axios$post.data.id; | ||
case 5: | ||
case 11: | ||
if (!true) { | ||
_context.next = 21; | ||
_context.next = 27; | ||
break; | ||
} | ||
_context.next = 8; | ||
_context.next = 14; | ||
return waitTime(config.interval); | ||
case 8: | ||
_context.next = 10; | ||
case 14: | ||
_context.next = 16; | ||
return axios.get(url, { | ||
@@ -71,3 +99,3 @@ params: { | ||
case 10: | ||
case 16: | ||
_yield$axios$get = _context.sent; | ||
@@ -80,3 +108,3 @@ _yield$axios$get$data = _yield$axios$get.data; | ||
if (!finished) { | ||
_context.next = 19; | ||
_context.next = 25; | ||
break; | ||
@@ -86,3 +114,3 @@ } | ||
if (!error) { | ||
_context.next = 18; | ||
_context.next = 24; | ||
break; | ||
@@ -93,10 +121,10 @@ } | ||
case 18: | ||
case 24: | ||
return _context.abrupt("return", result); | ||
case 19: | ||
_context.next = 5; | ||
case 25: | ||
_context.next = 11; | ||
break; | ||
case 21: | ||
case 27: | ||
case "end": | ||
@@ -103,0 +131,0 @@ return _context.stop(); |
@@ -37,3 +37,4 @@ "use strict"; | ||
var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(req, res) { | ||
var param, id, cacheKey, cached, result; | ||
var param, optimized, result, id, cacheKey, cached, _result; | ||
return _regenerator["default"].wrap(function _callee$(_context) { | ||
@@ -43,3 +44,25 @@ while (1) { | ||
case 0: | ||
param = req.body; | ||
param = req.body, optimized = req.query.optimized; | ||
if (optimized) { | ||
_context.next = 12; | ||
break; | ||
} | ||
_context.prev = 2; | ||
_context.next = 5; | ||
return asyncFn(param); | ||
case 5: | ||
result = _context.sent; | ||
return _context.abrupt("return", res.json(result)); | ||
case 9: | ||
_context.prev = 9; | ||
_context.t0 = _context["catch"](2); | ||
return _context.abrupt("return", res.json({ | ||
error: _context.t0.toString() | ||
})); | ||
case 12: | ||
id = (0, _getId["default"])(); | ||
@@ -51,35 +74,35 @@ cacheKey = "".concat(CACHE_PREFIX).concat(id); | ||
}; | ||
_context.next = 6; | ||
_context.next = 17; | ||
return _cache["default"].set(cacheKey, cached); | ||
case 6: | ||
case 17: | ||
res.json(cached); | ||
_context.prev = 7; | ||
_context.next = 10; | ||
_context.prev = 18; | ||
_context.next = 21; | ||
return asyncFn(param); | ||
case 10: | ||
result = _context.sent; | ||
case 21: | ||
_result = _context.sent; | ||
cached = _objectSpread(_objectSpread({}, cached), {}, { | ||
result: result, | ||
result: _result, | ||
finished: true | ||
}); | ||
_context.next = 14; | ||
_context.next = 25; | ||
return _cache["default"].set(cacheKey, cached); | ||
case 14: | ||
_context.next = 21; | ||
case 25: | ||
_context.next = 32; | ||
break; | ||
case 16: | ||
_context.prev = 16; | ||
_context.t0 = _context["catch"](7); | ||
case 27: | ||
_context.prev = 27; | ||
_context.t1 = _context["catch"](18); | ||
cached = _objectSpread(_objectSpread({}, cached), {}, { | ||
error: _context.t0, | ||
error: _context.t1, | ||
finished: true | ||
}); | ||
_context.next = 21; | ||
_context.next = 32; | ||
return _cache["default"].set(cacheKey, cached); | ||
case 21: | ||
case 32: | ||
case "end": | ||
@@ -89,3 +112,3 @@ return _context.stop(); | ||
} | ||
}, _callee, null, [[7, 16]]); | ||
}, _callee, null, [[2, 9], [18, 27]]); | ||
})); | ||
@@ -92,0 +115,0 @@ |
{ | ||
"name": "convert-slow-api", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/get-router.js", |
@@ -9,4 +9,15 @@ import Axios from "axios" | ||
export default async (url, params, config = { interval: 1000 }) => { | ||
let { data: { id } } = await axios.post(url, params) | ||
export default async (url, params, conf = {}) => { | ||
let config = { | ||
optimized: true, | ||
interval: 1000, | ||
...conf, | ||
} | ||
if (!config.optimized) { | ||
let res = await axios.post(url, params) | ||
return res.data | ||
} | ||
let { data: { id } } = await axios.post(url, params, { | ||
params: { optimized: 'true' } | ||
}) | ||
while (true) { | ||
@@ -13,0 +24,0 @@ await waitTime(config.interval) |
@@ -15,3 +15,13 @@ import { Router } from 'express' | ||
router.post('/', async (req, res) => { | ||
let param = req.body | ||
let param = req.body, { optimized } = req.query | ||
if (!optimized) { | ||
try { | ||
let result = await asyncFn(param) | ||
return res.json(result) | ||
} catch (e) { | ||
return res.json({ | ||
error: e.toString() | ||
}) | ||
} | ||
} | ||
let id = getId() | ||
@@ -50,3 +60,3 @@ let cacheKey = `${CACHE_PREFIX}${id}` | ||
let cached = await cache.get(cacheKey) | ||
if (!cached) return res.json({ | ||
@@ -57,3 +67,3 @@ error: `id=${id} job not found`, | ||
res.json(cached) | ||
const { finished } = cached | ||
@@ -60,0 +70,0 @@ if (finished) { |
const fetcher = require('../dist/fetcher').default | ||
; | ||
(async () => { | ||
let result = await fetcher("http://localhost:3000/api", { | ||
greeting: 'hi josh', | ||
}) | ||
console.log(result) | ||
{ | ||
let result = await fetcher("http://localhost:3000/api", { | ||
greeting: 'hi josh', | ||
}) | ||
console.log(result) | ||
} | ||
{ | ||
let result = await fetcher("http://localhost:3000/api", { | ||
greeting: 'hi josh', | ||
}, { optimized: false }) | ||
console.log(result) | ||
} | ||
})() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16918
449