From 254e01a1b528920e1129a1f65e6100c565cc7102 Mon Sep 17 00:00:00 2001 From: Regalijan <72576136+Regalijan@users.noreply.github.com> Date: Mon, 7 Aug 2023 15:40:53 -0400 Subject: [PATCH] Add getGamePassProductInfo() (#655) * Create getGamePassProductInfo() * Add getGamePassProductInfo test * Add GamePassProductInfo and getGamePassProductInfo types * Add cache settings for GamePassProduct * Add GamePassProductInfo to jsdocs typings * Update gamepass product info url * Omit new ugc/collectible properties * Fix category * Make function types alphabetical * Add failure test case for getGamePassProductInfo * Typo moment * Fix test again --------- Co-authored-by: Wolftallemo <72576136+Wolftallemo@users.noreply.github.com> Co-authored-by: suufi <12759332+suufi@users.noreply.github.com> Co-authored-by: Josh --- lib/asset/getGamePassProductInfo.js | 51 +++++++++++++++++++++++++++++ settings.json | 6 ++++ test/asset.test.js | 17 +++++++++- typings/index.d.ts | 7 ++++ typings/jsDocs.ts | 5 +++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 lib/asset/getGamePassProductInfo.js diff --git a/lib/asset/getGamePassProductInfo.js b/lib/asset/getGamePassProductInfo.js new file mode 100644 index 000000000..762f64645 --- /dev/null +++ b/lib/asset/getGamePassProductInfo.js @@ -0,0 +1,51 @@ +// Includes +const http = require('../util/http.js').func +const cache = require('../cache') + +// Args +exports.required = ['gamepass'] + +// Docs +/** + * ✅ Get the info of an gamepass. + * @category Asset + * @param {number} gamePassId - The id of the asset. + * @returns {Promise} + * @example const noblox = require("noblox.js") + * const gamePassInfo = await noblox.getGamePassProductInfo(2919875) +**/ + +// Define +function getGamePassProductInfo (gamepass) { + return new Promise((resolve, reject) => { + const httpOpt = { + url: `//apis.roblox.com/game-passes/v1/game-passes/${gamepass}/product-info`, + options: { + resolveWithFullResponse: true, + method: 'GET' + } + } + + return http(httpOpt) + .then(function (res) { + const data = JSON.parse(res.body) + + if (res.statusCode === 200) { + resolve(data) + } else { + const errors = data.errors.map((e) => e.message) + + reject(new Error(`${res.statusCode} ${errors.join(', ')}`)) + } + }) + .catch(error => reject(error)) + }) +} + +exports.func = function (args) { + const gamepass = args.gamepass + + return cache.wrap('GamePassProduct', gamepass, function () { + return getGamePassProductInfo(gamepass) + }) +} diff --git a/settings.json b/settings.json index f013c41b7..d0875e354 100644 --- a/settings.json +++ b/settings.json @@ -74,6 +74,12 @@ "desc": "Disabled by default for security (price checks). If you are only working with ROBLOX assets, however, you can set this to something high (since ROBLOX product info rarely changes)." }, + "GamePassProduct": { + "expire": 86400, + "refresh": false, + "desc": "This should normally be fine unless the information of GamePasses you are working with changes frequently." + }, + "NameFromID": { "expire": false, "refresh": false, diff --git a/test/asset.test.js b/test/asset.test.js index b4900987d..d6294977b 100644 --- a/test/asset.test.js +++ b/test/asset.test.js @@ -1,4 +1,4 @@ -const { buy, deleteFromInventory, getProductInfo, uploadAnimation, uploadItem, uploadModel, setCookie, getOwnership, getCurrentUser } = require('../lib') +const { buy, deleteFromInventory, getGamePassProductInfo, getProductInfo, uploadAnimation, uploadItem, uploadModel, setCookie, getOwnership, getCurrentUser } = require('../lib') const fs = require('fs') beforeAll(() => { @@ -21,6 +21,21 @@ describe('Asset Methods', () => { return await expect(deleteFromInventory(1778181)).rejects.toThrow() }) + it('getGamePassProductInfo() successfully returns a gamepass\'s information', () => { + return getGamePassProductInfo(2919875).then((res) => { + return expect(res).toMatchObject({ + Name: expect.any(String), + Description: expect.any(String), + Creator: expect.any(Object), + PriceInRobux: expect.toBeOneOf([expect.any(Number), null]) + }) + }) + }) + + it('getGamePassProductInfo() errors when returning a product\'s information that does not exist', async () => { + return await expect(getGamePassProductInfo(0)).rejects.toThrow() + }) + it('getProductInfo() successfully returns a product\'s information', () => { return getProductInfo(1989194006).then((res) => { return expect(res).toMatchObject({ diff --git a/typings/index.d.ts b/typings/index.d.ts index 7ba1a5fe5..2f54859a1 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -215,6 +215,8 @@ declare module "noblox.js" { CollectibleItemId?: number; } + type GamePassProductInfo = Omit; + interface BuyProductInfo { ProductId: number; Creator: { Id: number }; @@ -1399,6 +1401,11 @@ declare module "noblox.js" { */ function deleteFromInventory(assetId: number, jar?: CookieJar): Promise; + /** + * ✅ Gets `info` of `gamepass` and caches according to settings. + */ + function getGamePassProductInfo(gamepass: number): Promise; + /** * ✅ Gets `info` of `asset` and caches according to settings. */ diff --git a/typings/jsDocs.ts b/typings/jsDocs.ts index e987761d2..3f8e2c864 100644 --- a/typings/jsDocs.ts +++ b/typings/jsDocs.ts @@ -189,6 +189,11 @@ type ProductInfo = { CollectibleItemId?: number; } +/** + * @typedef +*/ +type GamePassProductInfo = Omit; + /** * @typedef */