yao-node-client
Version:
A node client for yao application development
45 lines • 1.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RemoteRequest = void 0;
const sync_fetch_1 = __importDefault(require("sync-fetch"));
let apiKey = ""; //process.env.YAO_API_ACCESS_KEY;
let proxyServerUrl = ""; //process.env.YAO_APP_PROXY_ENDPOINT; //"http://localhost:5199/api/proxy/call";
require("dotenv").config();
if (process.env.YAO_API_ACCESS_KEY) {
apiKey = process.env.YAO_API_ACCESS_KEY;
}
if (process.env.YAO_APP_PROXY_ENDPOINT) {
proxyServerUrl = process.env.YAO_APP_PROXY_ENDPOINT;
}
function RemoteRequest(payload) {
if (process.env.NODE_CLIENT_LOG) {
console.log(`Remote Request Call:${payload.type}:${payload.method}`);
}
const apiKey = process.env.YAO_API_ACCESS_KEY;
let proxyServerUrl = process.env.YAO_APP_PROXY_ENDPOINT;
if (!proxyServerUrl || !proxyServerUrl.length) {
throw new Error("请配置环境变量YAO_APP_PROXY_ENDPOINT");
}
if (!apiKey || !apiKey.length) {
throw new Error("请配置环境变量YAO_API_ACCESS_KEY");
}
const es = (0, sync_fetch_1.default)(proxyServerUrl, {
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + apiKey,
},
method: "POST",
body: JSON.stringify(payload),
});
let ret = es.json();
if (ret.code != 200) {
throw Error(`远程程序执行异常:代码:${ret.code},消息:${ret.message}`);
}
return ret.data;
}
exports.RemoteRequest = RemoteRequest;
exports.default = RemoteRequest;
//# sourceMappingURL=request.js.map