@ducvnv/mirai-cms-role
Version:
<p align="center"> <a href="https://mirailabs.co/" target="blank"><img src="https://mirailabs.co/_next/static/media/logo.2a6685c4.svg" width="120" alt="Nest Logo" /></a> </p>
119 lines (118 loc) • 3.94 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApi = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const postApi = (url, body, token, apiKey) => {
try {
return new Promise((resolve, reject) => {
console.info(`[INFO] start sync router at : ${new Date().getTime()}ms`);
console.info(`body: ${JSON.stringify(body)}`);
(0, node_fetch_1.default)(url, {
method: 'POST',
headers: token === null ? {
'Content-Type': 'application/json',
'x-api-key': apiKey
} : {
'Content-Type': 'application/json',
'Authorization': `Bearer `.concat(token),
'x-api-key': apiKey
},
body: JSON.stringify(body),
})
.then((response) => {
return response.json();
})
.then((data) => {
resolve(data);
})
.catch((error) => {
reject(error);
});
});
}
catch (e) {
console.error(`[Error] request log: ${e}`);
}
};
const putApi = (url, body, token) => {
return new Promise((resolve, reject) => {
(0, node_fetch_1.default)(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(body),
})
.then((response) => {
return response.json();
})
.then((data) => {
resolve(data);
})
.catch((error) => {
reject(error);
});
});
};
const deleteApi = (url, body, token) => {
return new Promise((resolve, reject) => {
(0, node_fetch_1.default)(url, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(body),
})
.then((response) => {
return response.json();
})
.then((data) => {
resolve(data);
})
.catch((error) => {
reject(error);
});
});
};
const getApi = (url, token) => {
return new Promise((resolve, reject) => __awaiter(void 0, void 0, void 0, function* () {
yield (0, node_fetch_1.default)(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
})
.then((response) => {
return response.json();
})
.then((data) => {
resolve(data);
})
.catch((error) => {
reject(error);
});
}));
};
exports.getApi = getApi;
const API = {
postApi,
putApi,
deleteApi,
getApi: exports.getApi,
};
exports.default = API;