UNPKG

paypal-rest-api

Version:

A typescript module for integrating with PayPal REST APIs.

104 lines (103 loc) 3.87 kB
"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 joi = require("joi"); class Api { constructor(_client, _schemas, _paths) { this._client = _client; this._schemas = _schemas; this._paths = _paths; } get client() { return this._client; } get schemas() { return this._schemas; } get paths() { return this._paths; } get(id, options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.paths.get) { throw new Error("Get path not available for this api"); } if (this.schemas.id) { id = this.schemaValidate(id, this.schemas.id); } options.uri = this.parsePath(this.paths.get, { id }); options = this.schemaValidate(options, this.schemas.get); return this.client.request(options); }); } create(options = {}) { if (!this.paths.create) { throw new Error("Create path not available for this api"); } options.uri = this.paths.create; options = this.schemaValidate(options, this.schemas.create); return this.client.request(options); } delete(id, options = {}) { if (!this.paths.delete) { throw new Error("Delete path not available for this api"); } if (this.schemas.id) { id = this.schemaValidate(id, this.schemas.id); } options.uri = this.parsePath(this.paths.delete, { id }); options = this.schemaValidate(options, this.schemas.delete); return this.client.request(options); } list(options = {}) { return __awaiter(this, void 0, void 0, function* () { if (!this.paths.list) { throw new Error("Get path not available for this api"); } options.uri = this.paths.list; options = this.schemaValidate(options, this.schemas.list); return this.client.request(options); }); } search(options = {}) { if (!this.paths.search) { throw new Error("Search path not available for this api"); } options.uri = this.paths.search; options = this.schemaValidate(options, this.schemas.search); return this.client.request(options); } update(id, payload, options = {}) { if (!this.paths.update) { throw new Error("Update path not available for this api"); } if (this.schemas.id) { id = this.schemaValidate(id, this.schemas.id); } options.uri = this.parsePath(this.paths.update, { id }); options.body = payload; options = this.schemaValidate(options, this.schemas.update, { stripUnknown: true, }); return this.client.request(options); } parsePath(path, replacers) { const rpath = path.replace("{id}", replacers.id); return rpath; } schemaValidate(what, schema, additional = {}) { const validate = joi.validate(what, schema, additional); if (validate.error) { throw validate.error; } return validate.value; } } exports.Api = Api;