hgi-gindex
Version:
Module used to send indexing requests to Google service
39 lines (29 loc) • 1.06 kB
JavaScript
const { google } = require('googleapis');
const sendNotification = require('./sendNotification');
const validateList = require('./validate')
const notifyGoogle = async (urlList, credentials, oldUrlList = null) => {
validateList(urlList);
if (oldUrlList) validateList(oldUrlList);
const credentialsJSON = JSON.parse(credentials);
const auth = new google.auth.GoogleAuth({
credentials: credentialsJSON,
scopes: ["https://www.googleapis.com/auth/indexing"],
});
if (!auth) throw new Error("Google auth error");
const indexingAPI = google.indexing({ version: "v3", auth });
const currentUrls = new Set(urlList);
const previousUrls = new Set(oldUrlList || []);
for (const urlRoute of currentUrls) {
if (!previousUrls.has(urlRoute)) {
await sendNotification(indexingAPI, urlRoute);
}
}
if (oldUrlList) {
for (const urlRoute of previousUrls) {
if (!currentUrls.has(urlRoute)) {
await sendNotification(indexingAPI, urlRoute, "URL_DELETED");
}
}
}
}
module.exports = notifyGoogle;