Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add country as Global opts #57

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Spacefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ micros:
commands:
- npm run generateoas
run: node server.js
dev: node server.js
dev: node server.js
presets:
env:
- name: COUNTRY_OF_QUERY
description: Country parameter to be defaulted for all requests
default: "US"
5 changes: 4 additions & 1 deletion fly.production.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ primary_region = "sin"
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
processes = ["app"]

[env]
COUNTRY_OF_QUERY = "IN"
6 changes: 5 additions & 1 deletion fly.staging.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ primary_region = "sin"
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]
processes = ["app"]

[env]
COUNTRY_OF_QUERY = "IN"
LOGGING = true
27 changes: 19 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ const router = Express.Router();

const toList = (apps) => ({ results: apps });


router.use((req, res, next) => {

if (!req.query.country) {
req.query.country = process.env.COUNTRY_OF_QUERY || 'US';;
}

next();
});


const cleanUrls = (req) => (app) => Object.assign({}, app, {
playstoreUrl: app.url,
url: buildUrl(req, 'apps/' + app.appId),
Expand All @@ -32,7 +43,7 @@ router.get('/apps/', function (req, res, next) {
return next();
}

const opts = Object.assign({ term: req.query.q }, req.query);
const opts = Object.assign({ term: req.query.q, country: req.query.country }, req.query);

gplay.search(opts)
.then((apps) => apps.map(cleanUrls(req)))
Expand Down Expand Up @@ -87,7 +98,7 @@ router.get('/apps/', function (req, res, next) {

/* App detail */
router.get('/apps/:appId', function (req, res, next) {
const opts = Object.assign({ appId: req.params.appId }, req.query);
const opts = Object.assign({ appId: req.params.appId, country: req.query.country }, req.query);
gplay.app(opts)
.then(cleanUrls(req))
.then(res.json.bind(res))
Expand All @@ -96,7 +107,7 @@ router.get('/apps/:appId', function (req, res, next) {

/* Similar apps */
router.get('/apps/:appId/similar', function (req, res, next) {
const opts = Object.assign({ appId: req.params.appId }, req.query);
const opts = Object.assign({ appId: req.params.appId, country: req.query.country }, req.query);
gplay.similar(opts)
.then((apps) => apps.map(cleanUrls(req)))
.then(toList)
Expand All @@ -106,7 +117,7 @@ router.get('/apps/:appId/similar', function (req, res, next) {

/* Data Safety */
router.get('/apps/:appId/datasafety', function (req, res, next) {
const opts = Object.assign({ appId: req.params.appId }, req.query);
const opts = Object.assign({ appId: req.params.appId, country: req.query.country }, req.query);
gplay.datasafety(opts)
.then(toList)
.then(res.json.bind(res))
Expand All @@ -115,7 +126,7 @@ router.get('/apps/:appId/datasafety', function (req, res, next) {

/* App permissions */
router.get('/apps/:appId/permissions', function (req, res, next) {
const opts = Object.assign({ appId: req.params.appId }, req.query);
const opts = Object.assign({ appId: req.params.appId, country: req.query.country }, req.query);
gplay.permissions(opts)
.then(toList)
.then(res.json.bind(res))
Expand All @@ -124,7 +135,7 @@ router.get('/apps/:appId/permissions', function (req, res, next) {

/* App reviews */
router.get('/apps/:appId/reviews', function (req, res, next) {
const opts = Object.assign({ appId: req.params.appId }, req.query);
const opts = Object.assign({ appId: req.params.appId, country: req.query.country }, req.query);
const includeUserData = req.query.userdata === 'true';
const includeReplies = req.query.replies == 'true'

Expand Down Expand Up @@ -194,7 +205,7 @@ router.get('/apps/:appId/reviews', function (req, res, next) {
});
/* Apps by developer */
router.get('/developers/:devId/', function (req, res, next) {
const opts = Object.assign({ devId: req.params.devId }, req.query);
const opts = Object.assign({ devId: req.params.devId, country: req.query.country }, req.query);

gplay.developer(opts)
.then((apps) => apps.map(cleanUrls(req)))
Expand All @@ -215,7 +226,7 @@ router.get('/developers/', (req, res) =>

/* List of apps by category and collection */
router.get('/lists/', function (req, res, next) {
const opts = Object.assign({ term: req.query.q }, req.query);
const opts = Object.assign({ term: req.query.q, country: req.query.country }, req.query);
if (!req.query.category || !req.query.collection) {
return res.status(400).json({ message: 'Both category and collection parameters are required.' });
}
Expand Down
Loading