@viyet/magento-client
Version:
Sotheby's Home Custom Magento API Client
102 lines • 4.21 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
var Auth;
(function (Auth) {
/**
* Auth API handles user authentication processes
*/
class API {
/**
* Auth API handles user authentication processes
*/
constructor(transport) {
this.transport = transport;
}
/**
* Checks if user is logged in
*
* @param headers - Session Headers
*
* @returns A Promise that resolves to a boolean value signaling if user is logged in
*/
isLoggedIn(headers) {
return __awaiter(this, void 0, void 0, function* () {
const route = {
key: "auth.sessionData",
};
const request = this.transport.getFromApiRoute(route, headers);
return yield request.then(response => !!response.data.isLoggedIn);
});
}
/**
* Finds a logged in user and returns basic user data
*
* @param headers - Session Headers
*
* @returns A Promise that resolves to an object with user data or false if user not found
*/
findLoggedInUserByHeaders(headers) {
return __awaiter(this, void 0, void 0, function* () {
const route = {
key: "auth.sessionData",
};
const request = this.transport.getFromApiRoute(route, headers);
return yield request.then(response => {
const userData = response.data;
return (userData.isLoggedIn) ?
{
cartCount: userData.cartCount,
email: userData.email,
firstName: userData.firstName,
isConsignor: userData.isConsignor,
isLoggedIn: userData.isLoggedIn,
lastName: userData.lastName,
userId: userData.userId
} : false;
});
});
}
/**
* Logs user in
*
* @param credentials - Credentials to authenticate against Magento
*
* @returns A Promise that resolves to an object with login result
*/
login(credentials) {
return __awaiter(this, void 0, void 0, function* () {
const route = {
key: "auth.login",
};
return yield this.transport.postToApiRoute(route, credentials);
});
}
register(userData) {
return __awaiter(this, void 0, void 0, function* () {
const route = {
key: "auth.register",
};
const userDataToSend = {
email: userData.email,
first_name: userData.firstName,
is_designer: !!userData.isDesigner,
last_name: userData.lastName,
password: userData.password,
phone: userData.phone || "",
v_sotheby_customer: !!userData.isSothebysCustomer
};
return yield this.transport.postToApiRoute(route, userDataToSend);
});
}
}
Auth.API = API;
})(Auth = exports.Auth || (exports.Auth = {}));
//# sourceMappingURL=Auth.js.map