From fd71fa276c2f2030b60a6d41470641d80043618d Mon Sep 17 00:00:00 2001 From: Eden Kirin Date: Mon, 10 Apr 2023 20:02:23 +0200 Subject: [PATCH] Product purchase functions --- javascript/fh_sdk.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/javascript/fh_sdk.js b/javascript/fh_sdk.js index f86f145..09e849d 100644 --- a/javascript/fh_sdk.js +++ b/javascript/fh_sdk.js @@ -121,6 +121,34 @@ class FairHopper { } return await r.json(); } + + async getProducts() { + const r = await fetch(this.formatUrl("/products"), { + headers: this.defaultHeaders, + }); + return await r.json(); + } + + async purchaseProduct(playerId, productId) { + const url = this.formatUrl(`/player/${playerId}/product/purchase`); + const postData = { + product_id: productId, + }; + const r = await fetch(url, { + method: "post", + headers: this.defaultHeaders, + body: JSON.stringify(postData), + }); + switch (r.status) { + case 403: + throw new PlayerInactiveError(); + case 404: + throw new PlayerNotFoundError(); + case 409: + throw new PositionError(); + } + return await r.json(); + } } module.exports = {