gamebrary/functions/index.js

282 lines
6.7 KiB
JavaScript
Raw Normal View History

2020-10-21 23:03:09 +00:00
// firebase emulators:start --only functions
// TODO: INJECT TOKEN USING AXIOS MIDDLEWARE
// Add json object in .runtimeconfig.json to use env variables locally
2018-10-19 05:15:28 +00:00
const functions = require('firebase-functions');
2020-10-21 23:03:09 +00:00
const axios = require('axios');
2020-10-21 16:26:00 +00:00
const admin = require('firebase-admin');
2020-10-21 23:03:09 +00:00
2020-10-21 16:26:00 +00:00
admin.initializeApp({
apiKey: 'AIzaSyA6MsmnLtqT4b11r-j15wwreRypO3AodcA',
authDomain: 'gamebrary.com',
databaseURL: 'https://gamebrary-8c736.firebaseio.com',
projectId: 'gamebrary-8c736',
storageBucket: 'gamebrary-8c736.appspot.com',
messagingSenderId: '324529217902',
});
2021-01-08 06:52:45 +00:00
exports.customSearch = functions.https.onRequest((req, res) => {
res.set('Access-Control-Allow-Origin', "*");
if (!req.query.token) {
return res.status(400).json({ error: 'missing searchText or token' });
}
// TODO: exclude games? & id != (${excludedGames});
const query = req.query.platforms
? `where platforms = (${req.query.platforms}) & rating != null;`
: '';
const sort = req.query.sortQuery
? `sort ${req.query.sortQuery};`
: '';
const limit = req.query.limit
? `limit ${req.query.limit};`
: 'limit 50;';
2021-05-10 22:41:16 +00:00
const search = req.query.searchText
? `search "${req.query.searchText}";`
: '';
const fields = req.query.fields
? `fields ${req.query.fields};`
: 'fields id,name,slug,rating,name,cover.image_id,first_release_date;';
2021-01-08 06:52:45 +00:00
const data = `
2021-05-10 22:41:16 +00:00
${search}
${fields}
${sort}
${limit}
2021-05-10 22:41:16 +00:00
${query}`;
2021-01-08 06:52:45 +00:00
axios({
url: 'https://api.igdb.com/v4/games',
method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': functions.config().twitch.clientid,
'Authorization': `Bearer ${req.query.token}`,
},
data,
})
.then(({ data }) => res.status(200).send(data))
.catch((error) => res.status(400).send(error));
});
2018-10-19 05:15:28 +00:00
exports.search = functions.https.onRequest((req, res) => {
2020-08-18 18:56:25 +00:00
res.set('Access-Control-Allow-Origin', "*")
2020-10-21 16:26:00 +00:00
const { search, platform, token } = req.query;
2020-10-21 23:03:09 +00:00
const missingFields = [search, platform, token].filter((field) => !field).length > 0;
2020-10-21 16:26:00 +00:00
2020-10-21 23:03:09 +00:00
if (missingFields) {
return res.status(400).json({ error: 'missing required params (search OR platform OR token)' });
2020-10-21 16:26:00 +00:00
}
2020-08-18 18:56:25 +00:00
const data = `
2020-10-21 23:03:09 +00:00
search "${search}";
fields id,name,slug,rating,release_dates.*,name,cover.image_id;
limit 50;
where platforms = (${platform});
`
2020-08-18 18:56:25 +00:00
axios({
2020-10-21 16:26:00 +00:00
url: 'https://api.igdb.com/v4/games',
2020-08-18 18:56:25 +00:00
method: 'POST',
headers: {
'Accept': 'application/json',
2020-10-21 23:03:09 +00:00
'Client-ID': functions.config().twitch.clientid,
2020-10-21 16:26:00 +00:00
'Authorization': `Bearer ${token}`,
2020-08-18 18:56:25 +00:00
},
data,
})
.then(({ data }) => { res.status(200).send(data) })
.catch((error) => { res.send(error) });
});
2020-10-21 16:26:00 +00:00
// TODO: update to run once a month instead of once a week
exports.refreshToken = functions.pubsub.schedule('0 0 * * 0')
.onRun((context) => {
const id = functions.config().twitch.clientid;
const secret = functions.config().twitch.clientsecret;
const url = `https://id.twitch.tv/oauth2/token?client_id=${id}&client_secret=${secret}&grant_type=client_credentials`;
axios({
url,
method: 'POST',
headers: {
'Accept': 'application/json',
},
})
.then(({ data }) => {
const db = admin.firestore();
return db.collection('app').doc('twitch').set(data, { merge: true })
.then((res) => {
return res.status(200).send(res);
})
.catch((e) => {
return res.status(200).send(res);
});
})
.catch(() => {});
});
2020-08-18 18:56:25 +00:00
exports.platforms = functions.https.onRequest((req, res) => {
2021-05-10 22:41:16 +00:00
res.set('Access-Control-Allow-Origin', "*");
2020-08-18 18:56:25 +00:00
2020-10-21 16:26:00 +00:00
const { token } = req.query;
if (!token) {
2020-10-21 23:03:09 +00:00
return res.status(400).send('missing token');
2020-10-21 16:26:00 +00:00
}
2020-08-18 18:56:25 +00:00
const data = `
fields category,generation,name,alternative_name,slug;
limit 200;
2020-10-21 23:03:09 +00:00
`;
2020-08-18 18:56:25 +00:00
axios({
2020-10-21 16:26:00 +00:00
url: 'https://api.igdb.com/v4/platforms',
2020-08-18 18:56:25 +00:00
method: 'POST',
headers: {
'Accept': 'application/json',
2020-10-21 23:03:09 +00:00
'Client-ID': functions.config().twitch.clientid,
2020-10-21 16:26:00 +00:00
'Authorization': `Bearer ${token}`,
2020-08-18 18:56:25 +00:00
},
data,
})
2020-10-21 16:26:00 +00:00
.then(({ data }) => {
res.status(200).send(data)
})
2020-08-18 18:56:25 +00:00
.catch((error) => { res.send(error) });
2019-04-02 23:42:43 +00:00
});
exports.games = functions.https.onRequest((req, res) => {
2020-08-18 18:56:25 +00:00
res.set('Access-Control-Allow-Origin', "*")
2020-10-21 16:26:00 +00:00
const { games, token } = req.query;
if (!token) {
return res.status(400).send('missing token');
}
2020-08-18 18:56:25 +00:00
if (!games) {
2020-10-21 23:03:09 +00:00
return res.status(400).send('missing games');
2020-08-18 18:56:25 +00:00
}
const data = `fields
id,
name,
slug,
rating,
release_dates.*,
name,
cover.image_id;
where id = (${ games });
limit 500;`;
axios({
2020-10-21 16:26:00 +00:00
url: 'https://api.igdb.com/v4/games',
2020-08-18 18:56:25 +00:00
method: 'POST',
headers: {
'Accept': 'application/json',
2020-10-21 23:03:09 +00:00
'Client-ID': functions.config().twitch.clientid,
2020-10-21 16:26:00 +00:00
'Authorization': `Bearer ${token}`,
2020-08-18 18:56:25 +00:00
},
data,
})
.then(({ data }) => { res.status(200).send(data) })
.catch((error) => { res.send(error) });
2019-04-25 18:46:25 +00:00
});
exports.game = functions.https.onRequest((req, res) => {
2020-08-18 18:56:25 +00:00
res.set('Access-Control-Allow-Origin', "*")
2020-10-21 16:26:00 +00:00
const { gameId, token } = req.query;
if (!token) {
return res.status(400).send('missing token');
}
2020-08-18 18:56:25 +00:00
if (!gameId) {
2020-10-21 23:03:09 +00:00
res.status(400).send('missing gameId');
2020-08-18 18:56:25 +00:00
}
const data = `fields
2021-03-04 17:14:02 +00:00
age_ratings.*,
alternative_names.*,
bundles.*,
collection.*,
collection.games.*,
2020-08-18 18:56:25 +00:00
cover.image_id,
2021-03-04 17:14:02 +00:00
external_games.*,
game_modes.name,
genres.name,
2020-08-18 18:56:25 +00:00
involved_companies.company.name,
involved_companies.developer,
involved_companies.publisher,
2021-03-04 17:14:02 +00:00
name,
platforms.id,
platforms.name,
player_perspectives.name,
rating,
2020-08-18 18:56:25 +00:00
release_dates.date,
2021-03-04 17:14:02 +00:00
release_dates.platform,
screenshots.image_id,
2021-02-16 03:14:30 +00:00
similar_games,
2021-03-04 17:14:02 +00:00
summary,
videos.video_id,
websites.category,
websites.url;
2020-08-18 18:56:25 +00:00
where id = ${ gameId };`;
axios({
2020-10-21 16:26:00 +00:00
url: 'https://api.igdb.com/v4/games',
2020-08-18 18:56:25 +00:00
method: 'POST',
headers: {
'Accept': 'application/json',
2020-10-21 23:03:09 +00:00
'Client-ID': functions.config().twitch.clientid,
2020-10-21 16:26:00 +00:00
'Authorization': `Bearer ${token}`,
2020-08-18 18:56:25 +00:00
},
data,
})
.then(({ data }) => { res.status(200).send(data) })
2020-10-21 23:02:41 +00:00
.catch((error) => { res.status(400).send(error) });
2020-09-15 22:39:03 +00:00
});
exports.email = functions.https.onRequest((req, res) => {
2020-08-18 18:56:25 +00:00
res.set('Access-Control-Allow-Origin', "*")
const { template_id, address } = req.query;
if (!template_id || !address) {
res.send(400);
}
const data = {
recipients: [
{ address },
],
content: { template_id },
};
axios({
url: 'https://api.sparkpost.com/api/v1/transmissions',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json',
'Authorization': functions.config().sparkpost.key,
},
data,
})
.then(({ data }) => { res.status(200).send(data) })
.catch((error) => { res.send(error) });
2019-04-25 18:46:25 +00:00
});