UNPKG

bananas-commerce

Version:

A client for bananas-commerce with support for TypeScript

151 lines (150 loc) 6.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pos = void 0; const casedObjectKeys_js_1 = require("../util/casedObjectKeys.js"); const formatVariableResult_js_1 = require("../util/formatVariableResult.js"); const extension_js_1 = require("../extension.js"); /** * A client for `Pos` only using public API:s. */ class Pos extends extension_js_1.Extension { constructor() { super(...arguments); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: "pos" }); } /** Lists the items of the group with the specified `groupReference`. */ async items(args) { this.assertLoaded(); const { siteCode = this.defaults.siteCode, ...body } = args; if (!siteCode) { return { _type: "internal-error", detail: "A site code must be specified either in the constructor of BananasCommerce " + "or in the arguments passed to BananasCommerce.items.", }; } const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/{site_code}/pos/items/").method("get")({ query: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), path: { site_code: siteCode }, })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 400: return "invalid-args"; case 503: return "internal-error"; } }); } async checkout(args) { this.assertLoaded(); const { siteCode = this.defaults.siteCode, ...body } = args; const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/{site_code}/pos/checkout/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), path: { site_code: siteCode, }, })); if (!siteCode) { return { _type: "internal-error", detail: "A site code must be specified either in the constructor of BananasCommerce " + "or in the arguments passed to BananasCommerce.checkout.", }; } return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: case 201: return "success"; case 400: return "invalid-args"; case 404: return "not-found"; } }); } async receipt(args) { this.assertLoaded(); const { ...body } = args; const response = await this.catchApiError(this.fetcher .endpoint("/api/v1/pos/checkout/receipt/{checkout_session}/") .method("get")({ path: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 404: return "not-found"; } }); } /** Get the availability of the group with the specified `groupReference`s. */ async availability(args) { this.assertLoaded(); const { siteCode = this.defaults.siteCode, ...body } = args; if (!siteCode) { return { _type: "internal-error", detail: "A site code must be specified either in the constructor of BananasCommerce " + "or in the arguments passed to BananasCommerce.items.", }; } const response = await this.catchApiError(this.fetcher .endpoint("/api/v1/{site_code}/pos/availability/") .method("get")({ query: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), path: { site_code: siteCode }, })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 503: return "internal-error"; } }); } /** * Returns additional information for the specified `items` of the cart, such as `name`, * `variant`, `total`, `error`, and `warning`. The errors or warnings can for instance be defined * if the item in question is out of stock. */ async cart(args) { this.assertLoaded(); const { siteCode = this.defaults.siteCode, ...body } = args; if (!siteCode) { return { _type: "internal-error", detail: "A site code must be specified either in the constructor of BananasCommerce " + "or in the arguments passed to BananasCommerce.cart.", }; } const response = await this.catchApiError(this.fetcher.endpoint("/api/v1/{site_code}/pos/cart/").method("post")({ body: (0, casedObjectKeys_js_1.casedObjectKeys)(body, "snake"), path: { site_code: siteCode }, })); return (0, formatVariableResult_js_1.formatVariableResult)({ ...response, data: (0, casedObjectKeys_js_1.casedObjectKeys)(response.data, "camel") }, (response) => { switch (response.status) { case 200: return "success"; case 400: return "invalid-args"; case 404: return "not-found"; case 503: return "internal-error"; } }); } } exports.Pos = Pos;