paypal-rest-api
Version:
A typescript module for integrating with PayPal REST APIs.
78 lines (77 loc) • 2.8 kB
JavaScript
"use strict";
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 });
const lodash_1 = require("lodash");
class Model {
constructor(_model, _schema) {
this._model = _model;
this._schema = _schema;
}
get api() {
return this._api;
}
set api(api) {
this._api = api;
}
get model() {
return this._model;
}
set model(model) {
if (this._schema && model !== null) {
model = this.api.schemaValidate(model, this._schema, { allowUnknown: true });
}
this._model = model;
}
create(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (this.model.id) {
throw new Error("Model has id");
}
options.body = this.model;
const response = yield this.api.create(options);
this.model = response.body;
return this;
});
}
update(payload, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.model.id) {
throw new Error("Model does not have an id. Call create first");
}
const response = yield this.api.update(this.model.id, payload, options);
this.model = response.body;
return this;
});
}
delete(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.model.id) {
throw new Error("Model does not have an id. Call create first");
}
const response = yield this.api.delete(this.model.id, options);
this.model = null;
return this;
});
}
get(options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.model.id) {
throw new Error("Model does not have an id. Call create first");
}
const response = yield this.api.get(this.model.id, options);
this.model = response.body;
return this;
});
}
getModelProperty(path) {
return lodash_1.get(this.model, path);
}
}
exports.Model = Model;