diff --git a/packages/spacecat-shared-google-client/CHANGELOG.md b/packages/spacecat-shared-google-client/CHANGELOG.md index 1a9f7a18d..d211b28fe 100644 --- a/packages/spacecat-shared-google-client/CHANGELOG.md +++ b/packages/spacecat-shared-google-client/CHANGELOG.md @@ -1,3 +1,10 @@ +# [@adobe/spacecat-shared-google-client-v1.0.2](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.0.1...@adobe/spacecat-shared-google-client-v1.0.2) (2024-05-22) + + +### Bug Fixes + +* getOrganicSearchData interface params ([#236](https://github.com/adobe/spacecat-shared/issues/236)) ([acfd48b](https://github.com/adobe/spacecat-shared/commit/acfd48baaaa7594d0b9d7f00f7c9d52118b137e9)) + # [@adobe/spacecat-shared-google-client-v1.0.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-google-client-v1.0.0...@adobe/spacecat-shared-google-client-v1.0.1) (2024-05-21) diff --git a/packages/spacecat-shared-google-client/package.json b/packages/spacecat-shared-google-client/package.json index 5f7034764..965b62db2 100644 --- a/packages/spacecat-shared-google-client/package.json +++ b/packages/spacecat-shared-google-client/package.json @@ -1,6 +1,6 @@ { "name": "@adobe/spacecat-shared-google-client", - "version": "1.0.1", + "version": "1.0.2", "description": "Shared modules of the Spacecat Services - Google Client", "type": "module", "main": "src/index.js", diff --git a/packages/spacecat-shared-google-client/src/index.d.ts b/packages/spacecat-shared-google-client/src/index.d.ts index ac32f8f49..2bd6a5c69 100644 --- a/packages/spacecat-shared-google-client/src/index.d.ts +++ b/packages/spacecat-shared-google-client/src/index.d.ts @@ -10,18 +10,17 @@ * governing permissions and limitations under the License. */ -import { UniversalContext } from '@adobe/helix-universal'; import { OAuth2Client } from 'google-auth-library'; export default class GoogleClient { /** * Static factory method to create an instance of GoogleClient. * - * @param {UniversalContext} context - An object containing the AWS Lambda context information + * @param {object} context - An object containing the AWS Lambda context information * @param {string} url - The URL of the site to be audited. * @returns An instance of GoogleClient. */ - static createFrom(context: UniversalContext, url: string): GoogleClient; + static createFrom(context: object, url: string): GoogleClient; /** * Constructor for creating an instance of GoogleClient. @@ -33,12 +32,13 @@ export default class GoogleClient { /** * Retrieves the Google Search Console data for the specified date range. * - * @param baseURL - The base URL of the site to be audited. - * @param startDate - The start date of the date range. - * @param endDate - The end date of the date range. - * @param dimensions - The dimensions to be included in the report. + * @param {Date} startDate - The start date of the date range. + * @param {Date} endDate - The end date of the date range. + * @param {Array} dimensions - The dimensions to be included in the report. * this parameter is optional and defaults to ['date'], * which means that the report will be grouped by date. + * @param {Number} rowLimit - The maximum number of rows to return, defaults to 1000. + * @param {Number} startRow - The row number to start from, defaults to 0. * @returns {Promise} The Google Search Console data. * Format: { * "rows": [ @@ -56,10 +56,11 @@ export default class GoogleClient { * } */ getOrganicSearchData( - baseURL: string, startDate: Date, endDate: Date, - dimensions: string[] + dimensions?: string[], + rowLimit?: number, + startRow?: number ): Promise; /** diff --git a/packages/spacecat-shared-google-client/src/index.js b/packages/spacecat-shared-google-client/src/index.js index 30f75537f..e38ab64e1 100644 --- a/packages/spacecat-shared-google-client/src/index.js +++ b/packages/spacecat-shared-google-client/src/index.js @@ -16,12 +16,16 @@ import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-sec import { isArray, isInteger, - isValidDate, + isValidDate, isValidUrl, resolveCustomerSecretsName, } from '@adobe/spacecat-shared-utils'; export default class GoogleClient { static async createFrom(context, baseURL) { + if (!isValidUrl(baseURL)) { + throw new Error('Error creating GoogleClient: Invalid base URL'); + } + try { const customerSecret = resolveCustomerSecretsName(baseURL, context); const client = new SecretsManagerClient({}); @@ -61,6 +65,10 @@ export default class GoogleClient { throw new Error('Missing refresh token in secret'); } + if (!isValidUrl(config.siteUrl)) { + throw new Error('Invalid site URL in secret'); + } + authClient.setCredentials({ access_token: config.accessToken, refresh_token: config.refreshToken, diff --git a/packages/spacecat-shared-google-client/test/index.test.js b/packages/spacecat-shared-google-client/test/index.test.js index 5786fdd7f..8bfa5bd50 100644 --- a/packages/spacecat-shared-google-client/test/index.test.js +++ b/packages/spacecat-shared-google-client/test/index.test.js @@ -90,6 +90,24 @@ describe('GoogleClient', () => { expect(error.message).to.equal('Error creating GoogleClient: Secrets Manager error'); } }); + + it('should throw an error if the base URL is invalid', async () => { + try { + await GoogleClient.createFrom(context, 'not a valid url'); + } catch (error) { + expect(error.message).to.equal('Error creating GoogleClient: Invalid base URL'); + } + }); + + it('should throw an error if the base URL in secrets is invalid', async () => { + stubSecretManager({ ...defaultConfig, site_url: 'not a valid url' }); + + try { + await GoogleClient.createFrom(context, baseURL); + } catch (error) { + expect(error.message).to.equal('Error creating GoogleClient: Invalid site URL in secret'); + } + }); }); describe('getOrganicSearchData', () => {