Skip to content
Open
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
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions packages/spacecat-shared-google-client/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,65 @@ export default class GoogleClient {
*}
* */
listSites(): Promise<JSON>;

/**
* Retrieves information about a specific site in Google Search Console.
*
* @param {string} siteUrl - The URL of the site to retrieve information for.
* @returns {Promise<JSON>} A promise that resolves to the site information.
* @throws {Error} If an error occurs while retrieving the site information.
* Format: {
* "data": {
* "siteUrl": string,
* "permissionLevel": string
* }
*}
* */
getSite(siteUrl: string): Promise<JSON>;

/**
* Lists all sitemaps submitted for a site in Google Search Console.
*
* @param {string} [sitemapIndex] - Optional sitemap index URL to filter results.
* @returns {Promise<JSON>} A promise that resolves to the list of sitemaps.
* @throws {Error} If an error occurs while listing the sitemaps.
* Format: {
* "data": {
* "sitemap": [
* {
* "path": string,
* "lastSubmitted": string,
* "isPending": boolean,
* "isSitemapsIndex": boolean,
* "type": string,
* "lastDownloaded": string,
* "warnings": string,
* "errors": string
* }
* ]
* }
*}
* */
listSitemaps(sitemapIndex?: string): Promise<JSON>;

/**
* Retrieves information about a specific sitemap in Google Search Console.
*
* @param {string} sitemapUrl - The URL of the sitemap to retrieve information for.
* @returns {Promise<JSON>} A promise that resolves to the sitemap information.
* @throws {Error} If an error occurs while retrieving the sitemap information.
* Format: {
* "data": {
* "path": string,
* "lastSubmitted": string,
* "isPending": boolean,
* "isSitemapsIndex": boolean,
* "type": string,
* "lastDownloaded": string,
* "warnings": string,
* "errors": string
* }
*}
* */
getSitemap(sitemapUrl: string): Promise<JSON>;
}
72 changes: 70 additions & 2 deletions packages/spacecat-shared-google-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export default class GoogleClient {
if (!isInteger(rowLimit) || !isInteger(startRow)) {
throw new Error('Error retrieving organic search data from Google API: Invalid row limit or start row format');
}
if (rowLimit > 1000 || rowLimit < 1) {
throw new Error('Error retrieving organic search data from Google API: Row limit must be between 1 and 1000');
if (rowLimit > 25000 || rowLimit < 1) {
throw new Error('Error retrieving organic search data from Google API: Row limit must be between 1 and 25000');
}
if (startRow < 0) {
throw new Error('Error retrieving organic search data from Google API: Start row must be greater than or equal to 0');
Expand Down Expand Up @@ -186,6 +186,25 @@ export default class GoogleClient {
}
}

async getSite(siteUrl) {
if (!isValidUrl(siteUrl) && !siteUrl?.startsWith('sc-domain')) {
throw new Error(`Error retrieving site information: Invalid site URL format (${siteUrl})`);
}

await this.#refreshTokenIfExpired();

const webmasters = google.webmasters({
version: 'v3',
auth: this.authClient,
});
try {
return await webmasters.sites.get({ siteUrl });
} catch (error) {
this.log.error('Error retrieving site information:', error.message);
throw new Error(`Error retrieving site information from Google API: ${error.message}`);
}
}

async listSites() {
await this.#refreshTokenIfExpired();

Expand All @@ -200,4 +219,53 @@ export default class GoogleClient {
throw new Error(`Error retrieving sites from Google API: ${error.message}`);
}
}

async getSitemap(sitemapUrl) {
if (!isValidUrl(sitemapUrl)) {
throw new Error(`Error retrieving sitemap information: Invalid sitemap URL format (${sitemapUrl})`);
}

await this.#refreshTokenIfExpired();

const webmasters = google.webmasters({
version: 'v3',
auth: this.authClient,
});
try {
return await webmasters.sitemaps.get({
siteUrl: this.siteUrl,
feedpath: sitemapUrl,
});
} catch (error) {
this.log.error('Error retrieving sitemap information:', error.message);
throw new Error(`Error retrieving sitemap information from Google API: ${error.message}`);
}
}

async listSitemaps(sitemapIndex) {
await this.#refreshTokenIfExpired();

const webmasters = google.webmasters({
version: 'v3',
auth: this.authClient,
});

const params = {
siteUrl: this.siteUrl,
};

if (sitemapIndex) {
if (!isValidUrl(sitemapIndex)) {
throw new Error(`Error listing sitemaps: Invalid sitemap index URL format (${sitemapIndex})`);
}
params.sitemapIndex = sitemapIndex;
}

try {
return await webmasters.sitemaps.list(params);
} catch (error) {
this.log.error('Error listing sitemaps:', error.message);
throw new Error(`Error listing sitemaps from Google API: ${error.message}`);
}
}
}
Loading