UNPKG

bananas-commerce

Version:

A client for bananas-commerce with support for TypeScript

147 lines (146 loc) 5.79 kB
import { casedObjectKeys, } from "../util/casedObjectKeys.js"; import { formatVariableResult, } from "../util/formatVariableResult.js"; import { Extension } from "../extension.js"; /** * A client for `Pos` only using public API:s. */ export class Pos extends 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: casedObjectKeys(body, "snake"), path: { site_code: siteCode }, })); return formatVariableResult({ ...response, data: 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: 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 formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), path: { site_code: siteCode }, })); return formatVariableResult({ ...response, data: 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: casedObjectKeys(body, "snake"), path: { site_code: siteCode }, })); return formatVariableResult({ ...response, data: 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"; } }); } }