Skip to content

Commit

Permalink
Added store page URL option (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Jan 7, 2023
1 parent 636746f commit 5e8655f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,14 @@ What to do if a price is missing. Either "useZero", "useNull" or "useEmptyString
| Type | Default value | Possible values | Required |
| --- | --- | --- | --- |
| `string` | `"useNull"` | `"useZero"`, `"useNull"` or `"useEmptyString"` | Yes |
</details>

<details>
<summary><code>storePage</code></summary>

Whether or not to include the game's store page URL. NOTE: THIS IS NOT GUARANTEED TO ALWAYS RESULT IN A WORKING URL, AS IT NEEDS TO BE INFERRED AND IS NOT AVAILABLE THROUGH THE API.

| Type | Default value | Possible values | Required |
| --- | --- | --- | --- |
| `boolean` | `true` | `true` or `false` | No |
</details>
3 changes: 2 additions & 1 deletion config.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"WholesalePrice"
],
"missingPricePolicy": "useNull"
}
},
"storePage": true
}
}
5 changes: 5 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,11 @@
"missingPricePolicy"
],
"additionalProperties": false
},
"storePage": {
"description": "Whether or not to include the game's store page URL. NOTE: THIS IS NOT GUARANTEED TO ALWAYS RESULT IN A WORKING URL, AS IT NEEDS TO BE INFERRED AND IS NOT AVAILABLE THROUGH THE API.",
"type": "boolean",
"default": true
}
},
"additionalProperties": false,
Expand Down
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ function getPropertyValue(game, property, propertyValue) {
case "categories":
result = getCategories(game, propertyValue);
break;
case "storePage":
result = getStorePageUrl(game, propertyValue);
break;
default:
// Due to our config validation, this should never happen, but just in case...
console.log(`Invalid property: ${property}`);
Expand Down Expand Up @@ -369,4 +372,20 @@ function getCategories(game, categoriesProperty) {
}

return categories;
}

function getStorePageUrl(game, storePageUrlProperty) {
if (!storePageUrlProperty) { return undefined; }

if(!game.LocalizedProperties[0].ProductTitle || !game.ProductId) {
return undefined;
}

// 1. Convert to lowercase
// 2. Replace all non-alphanumeric characters with a dash
// 3. Replace multiple dashes with a single dash
// 4. Remove leading and trailing dashes
const formattedGameName = game.LocalizedProperties[0].ProductTitle.toLowerCase().replace(/[^a-z0-9]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '');

return `https://www.xbox.com/${CONFIG.language}/games/store/${formattedGameName}/${game.ProductId}`;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "game-pass-api",
"type": "module",
"version": "1.1.0",
"version": "1.1.1",
"description": "Utility for fetching all games currently available for Xbox Game Pass and formatting the resulting data according to user requirements.",
"main": "index.js",
"dependencies": {
Expand Down

0 comments on commit 5e8655f

Please sign in to comment.