| 158 | * @returns A promise resolving to the status of indexing. |
| 159 | */ |
| 160 | export async function getPageIndexingStatus( |
| 161 | accessToken: string, |
| 162 | siteUrl: string, |
| 163 | inspectionUrl: string |
| 164 | ): Promise<Status> { |
| 165 | try { |
| 166 | const response = await fetchRetry(`https://searchconsole.googleapis.com/v1/urlInspection/index:inspect`, { |
| 167 | method: "POST", |
| 168 | headers: { |
| 169 | "Content-Type": "application/json", |
| 170 | Authorization: `Bearer ${accessToken}`, |
| 171 | }, |
| 172 | body: JSON.stringify({ |
| 173 | inspectionUrl, |
| 174 | siteUrl, |
| 175 | }), |
| 176 | }); |
| 177 | |
| 178 | if (response.status === 403) { |
| 179 | console.error(`🔐 This service account doesn't have access to this site.`); |
| 180 | console.error(await response.text()); |
| 181 | |
| 182 | return Status.Forbidden; |
| 183 | } |
| 184 | |
| 185 | if (response.status >= 300) { |
| 186 | if (response.status === 429) { |
| 187 | return Status.RateLimited; |
| 188 | } else { |
| 189 | console.error(`❌ Failed to get indexing status.`); |
| 190 | console.error(`Response was: ${response.status}`); |
| 191 | console.error(await response.text()); |
| 192 | |
| 193 | return Status.Error; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | const body = await response.json(); |
| 198 | return body.inspectionResult.indexStatusResult.coverageState; |
| 199 | } catch (error) { |
| 200 | console.error(`❌ Failed to get indexing status.`); |
| 201 | console.error(`Error was: ${error}`); |
| 202 | throw error; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Retrieves an emoji representation corresponding to the given status. |