Skip to content

Commit

Permalink
Add getGamePassProductInfo() (#655)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: suufi <[email protected]>
Co-authored-by: Josh <[email protected]>
  • Loading branch information
4 people authored Aug 7, 2023
1 parent 8314b1a commit 254e01a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
51 changes: 51 additions & 0 deletions lib/asset/getGamePassProductInfo.js
Original file line number Diff line number Diff line change
@@ -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<GamePassProductInfo>}
* @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)
})
}
6 changes: 6 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 16 additions & 1 deletion test/asset.test.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand All @@ -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({
Expand Down
7 changes: 7 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ declare module "noblox.js" {
CollectibleItemId?: number;
}

type GamePassProductInfo = Omit<ProductInfo, "ContentRatingTypeId" | "SaleAvailabilityLocations" | "SaleLocation" | "CollectibleItemId">;

interface BuyProductInfo {
ProductId: number;
Creator: { Id: number };
Expand Down Expand Up @@ -1399,6 +1401,11 @@ declare module "noblox.js" {
*/
function deleteFromInventory(assetId: number, jar?: CookieJar): Promise<void>;

/**
* ✅ Gets `info` of `gamepass` and caches according to settings.
*/
function getGamePassProductInfo(gamepass: number): Promise<GamePassProductInfo>;

/**
* ✅ Gets `info` of `asset` and caches according to settings.
*/
Expand Down
5 changes: 5 additions & 0 deletions typings/jsDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ type ProductInfo = {
CollectibleItemId?: number;
}

/**
* @typedef
*/
type GamePassProductInfo = Omit<ProductInfo, "ContentRatingTypeId" | "SaleAvailabilityLocations" | "SaleLocation" | "CollectibleItemId">;

/**
* @typedef
*/
Expand Down

0 comments on commit 254e01a

Please sign in to comment.