65 lines
2.9 KiB
JavaScript
65 lines
2.9 KiB
JavaScript
"use strict";
|
|
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());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.test = exports.Magento = exports.getToken = void 0;
|
|
const configuration = require("./configuration");
|
|
const os_1 = require("os");
|
|
const read_1 = require("@plastichub/fs/read");
|
|
const path = require("path");
|
|
const api_1 = require("./api");
|
|
exports.getToken = (url, user, password) => __awaiter(void 0, void 0, void 0, function* () {
|
|
const api = new api_1.IntegrationAdminTokenServiceV1Api({
|
|
basePath: url
|
|
});
|
|
return api.integrationAdminTokenServiceV1CreateAdminAccessTokenPost({
|
|
username: user,
|
|
password: password
|
|
});
|
|
});
|
|
class Magento {
|
|
}
|
|
exports.Magento = Magento;
|
|
Magento.token = null;
|
|
Magento.apiConfig = null;
|
|
Magento.init = (baseUrl, username, password) => __awaiter(void 0, void 0, void 0, function* () {
|
|
const token = yield exports.getToken(baseUrl, username, password);
|
|
api_1.setAuth({
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
});
|
|
Magento.token = token;
|
|
Magento.apiConfig = new configuration.Configuration({
|
|
basePath: baseUrl,
|
|
accessToken: token
|
|
});
|
|
return Magento.token;
|
|
});
|
|
Magento.initWithProfile = (profilePath, baseUrl) => __awaiter(void 0, void 0, void 0, function* () {
|
|
if (profilePath[0] === '~') {
|
|
profilePath = path.join(os_1.homedir(), profilePath.slice(1));
|
|
}
|
|
const profile = read_1.sync(path.resolve(profilePath), 'json') || {};
|
|
return Magento.init(baseUrl || profile.baseUrl, profile.username, profile.password);
|
|
});
|
|
exports.test = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
process.on('unhandledRejection', (reason) => {
|
|
console.error('Unhandled rejection, reason: ', reason);
|
|
});
|
|
yield Magento.initWithProfile('~/.magento.json', 'https://shop.plastic-hub.com/rest/all');
|
|
// await Magento.init('https://shop.plastic-hub.com/rest/all', 'admin', '');
|
|
console.log('Magento API Config', Magento.apiConfig);
|
|
const storeApi = new api_1.StoreStoreRepositoryV1Api(Magento.apiConfig);
|
|
const stores = yield storeApi.storeStoreRepositoryV1GetListGet({});
|
|
console.log('stores', stores);
|
|
});
|
|
exports.API = require("./api");
|
|
//# sourceMappingURL=index.js.map
|