UNPKG

@azteam/google-api

Version:

N/A

85 lines (72 loc) 2.22 kB
import GoogleOAuth2API from './GoogleOAuth2API'; import {INDEXING_SCOPE} from './scope'; const INDEXING_ENDPOINT = 'https://indexing.googleapis.com/v3'; class GoogleIndexingAPI extends GoogleOAuth2API { constructor(accountType, credential, options = {}) { super( accountType, { ...credential, scope: INDEXING_SCOPE, }, options ); } getAuthLink(redirectURI, state, arrayScope = [INDEXING_SCOPE]) { return super.getAuthLink(redirectURI, state, arrayScope); } async hasScope(arrayScope = [INDEXING_SCOPE]) { return super.hasScope(arrayScope); } async publishUrl(url) { const client = await this._getAuthClient(), res = await client.post( `${INDEXING_ENDPOINT}/urlNotifications:publish`, { url, type: 'URL_UPDATED', }, false ); if (res.error) { throw new Error(res.error.message); } if (res.urlNotificationMetadata) { return res.urlNotificationMetadata; } return null; } async removeUrl(url) { const client = await this._getAuthClient(), res = await client.post( `${INDEXING_ENDPOINT}/urlNotifications:publish`, { url, type: 'URL_DELETED', }, false ); if (res.error) { throw new Error(res.error.message); } if (res.urlNotificationMetadata) { return res.urlNotificationMetadata; } return null; } async getStatus(url) { const client = await this._getAuthClient(), res = await client.get(`${INDEXING_ENDPOINT}/urlNotifications/metadata`, { url, }); if (res.error) { if (res.error.status !== 'NOT_FOUND') { throw new Error(res.error.message); } else { return null; } } return res; } } export default GoogleIndexingAPI;