@gscdump/sdk 0.11.5 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +15 -12
- package/dist/index.mjs +301 -215
- package/dist/query.d.mts +1 -0
- package/dist/query.mjs +2 -0
- package/package.json +15 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AddPartnerTeamMemberParams, AnalyticsClient, AnalyticsClient as AnalyticsClient$1, BackfillRange, BackfillResponse, BindPartnerSiteTeamParams, BuilderState, BulkRegisterPartnerSiteResult, BulkRegisterPartnerSitesParams, BulkRegisterPartnerSitesResponse, CANONICAL_WEBHOOK_EVENTS, CanonicalWebhookEventType,
|
|
1
|
+
import { AddPartnerTeamMemberParams, AnalyticsClient, AnalyticsClient as AnalyticsClient$1, BackfillRange, BackfillResponse, BindPartnerSiteTeamParams, BuilderState, BulkRegisterPartnerSiteResult, BulkRegisterPartnerSitesParams, BulkRegisterPartnerSitesResponse, CANONICAL_WEBHOOK_EVENTS, CanonicalWebhookEventType, CreatePartnerTeamParams, CreateWebhookEnvelopeOptions, DataDetailOptions, DataQueryOptions, DeletePartnerUserResponse, GscComparisonFilter, GscdumpAnalysisParams, GscdumpAnalysisPreset, GscdumpAnalysisResponse, GscdumpAnalysisSourcesResponse, GscdumpAvailableSite, GscdumpCanonicalMismatchesResponse, GscdumpDataDetailResponse, GscdumpDataResponse, GscdumpDataRow, GscdumpDateRangeParams, GscdumpIndexPercentResponse, GscdumpIndexingDiagnosticsResponse, GscdumpIndexingResponse, GscdumpIndexingUrlStatus, GscdumpIndexingUrlsResponse, GscdumpKeywordSparklinesParams, GscdumpKeywordSparklinesResponse, GscdumpMeta, GscdumpPermissionRecovery, GscdumpQueryTrendParams, GscdumpQueryTrendResponse, GscdumpSiteRegistration, GscdumpSitemap, GscdumpSitemapChangesResponse, GscdumpSitemapHistory, GscdumpSitemapsResponse, GscdumpSyncStatusResponse, GscdumpSyncStatusResponse as GscdumpSyncStatusResponse$1, GscdumpTeamMemberRow, GscdumpTeamRow, GscdumpTopAssociationParams, GscdumpTopAssociationResponse, GscdumpTotals, GscdumpUserRegistration, GscdumpUserSettings, GscdumpUserSite, GscdumpUserSite as GscdumpUserSite$1, GscdumpUserStatus, GscdumpUserTokenUpdate, IndexingUrlsParams, InspectionHistoryResponse, InspectionIndex, PartnerClient, PartnerClient as PartnerClient$1, PartnerLifecycleAccount, PartnerLifecycleResponse, PartnerLifecycleResponse as PartnerLifecycleResponse$1, PartnerLifecycleSite, PartnerLifecycleSite as PartnerLifecycleSite$1, PartnerRealtimeEvent, PartnerRealtimeEvent as PartnerRealtimeEvent$1, PartnerRealtimeEventType, PartnerRealtimeMessage, PartnerRealtimeMessage as PartnerRealtimeMessage$1, PartnerWebhookData, PartnerWebhookHeaders, PartnerWebhookHeaders as PartnerWebhookHeaders$1, RealtimeAuthFailedEvent, RealtimeAuthRequiredMessage, RealtimeConnectedMessage, RealtimeEnrichmentCompleteEvent, RealtimeErrorMessage, RealtimeJobFailedEvent, RealtimeNeedsReauthEvent, RealtimePongMessage, RealtimeSiteAddedEvent, RealtimeSiteRemovedEvent, RealtimeSubscribedMessage, RealtimeSyncCompleteEvent, RealtimeSyncFailedEvent, RealtimeSyncJobCompleteEvent, RealtimeSyncProgressEvent, RealtimeSyncSiteCompleteEvent, RegisterPartnerSiteParams, RegisterPartnerUserParams, RollupEnvelope, UpdatePartnerUserTokensParams, VALID_WEBHOOK_EVENTS, WEBHOOK_CONTRACT_VERSION, WEBHOOK_CONTRACT_VERSION_HEADER, WEBHOOK_DELIVERY_HEADER, WEBHOOK_EVENT_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_TIMESTAMP_HEADER, WebhookEnvelope, WebhookEnvelope as WebhookEnvelope$1, WebhookEventType, WhoamiResponse } from "@gscdump/contracts";
|
|
2
2
|
export * from "@gscdump/contracts";
|
|
3
3
|
type PartnerFetch = <T = unknown>(request: string, options?: PartnerFetchOptions) => Promise<T>;
|
|
4
4
|
type PartnerHeaders = HeadersInit | (() => HeadersInit | Promise<HeadersInit>);
|
|
@@ -50,16 +50,10 @@ declare class PartnerApiError extends Error {
|
|
|
50
50
|
constructor(info: PartnerErrorInfo);
|
|
51
51
|
}
|
|
52
52
|
declare function toPartnerError(error: unknown): PartnerApiError;
|
|
53
|
-
declare function
|
|
54
|
-
declare function
|
|
55
|
-
declare function
|
|
56
|
-
declare function
|
|
57
|
-
secret?: string;
|
|
58
|
-
signature?: string | null;
|
|
59
|
-
headers?: PartnerWebhookHeaders$1 | Headers;
|
|
60
|
-
validateSignature?: boolean;
|
|
61
|
-
}): Promise<WebhookEnvelope$1<TData>>;
|
|
62
|
-
declare function readWebhookHeaders(headers: Headers | PartnerWebhookHeaders$1 | null | undefined): Required<PartnerWebhookHeaders$1>;
|
|
53
|
+
declare function analyticsStatusToSyncStatus(status: PartnerLifecycleSite$1['analytics']['status']): GscdumpUserSite$1['syncStatus'];
|
|
54
|
+
declare function lifecycleSiteToUserSite(site: PartnerLifecycleSite$1): GscdumpUserSite$1;
|
|
55
|
+
declare function lifecycleSiteToSyncStatus(site: PartnerLifecycleSite$1): GscdumpSyncStatusResponse$1;
|
|
56
|
+
declare function findLifecycleSite(lifecycle: PartnerLifecycleResponse$1, siteIdOrPropertyUrl: string): PartnerLifecycleSite$1 | null;
|
|
63
57
|
type PartnerRealtimeScope = 'partner' | 'user';
|
|
64
58
|
type PartnerRealtimeStatus = 'idle' | 'connecting' | 'open' | 'authenticated' | 'closed' | 'error';
|
|
65
59
|
type PartnerRealtimeHandler<T> = (value: T) => void;
|
|
@@ -110,4 +104,13 @@ interface PartnerRealtimeClient {
|
|
|
110
104
|
}
|
|
111
105
|
declare function createPartnerRealtimeClient(options: PartnerRealtimeOptions): PartnerRealtimeClient;
|
|
112
106
|
declare const createGscdumpRealtimeClient: typeof createPartnerRealtimeClient;
|
|
113
|
-
|
|
107
|
+
declare function serializeWebhookPayload(payload: string | object): string;
|
|
108
|
+
declare function verifyWebhookSignature(payload: string | object, signature: string | null | undefined, secret: string): Promise<boolean>;
|
|
109
|
+
declare function parseWebhookPayload<TData extends Record<string, unknown> = Record<string, unknown>>(payload: string | object, options?: {
|
|
110
|
+
secret?: string;
|
|
111
|
+
signature?: string | null;
|
|
112
|
+
headers?: PartnerWebhookHeaders$1 | Headers;
|
|
113
|
+
validateSignature?: boolean;
|
|
114
|
+
}): Promise<WebhookEnvelope$1<TData>>;
|
|
115
|
+
declare function readWebhookHeaders(headers: Headers | PartnerWebhookHeaders$1 | null | undefined): Required<PartnerWebhookHeaders$1>;
|
|
116
|
+
export { type AddPartnerTeamMemberParams, type AnalyticsClient, type AnalyticsClientOptions, type AnalyticsFetch, type AnalyticsFetchOptions, type AnalyticsHeaders, type BackfillRange, type BackfillResponse, type BindPartnerSiteTeamParams, type BuilderState, type BulkRegisterPartnerSiteResult, type BulkRegisterPartnerSitesParams, type BulkRegisterPartnerSitesResponse, CANONICAL_WEBHOOK_EVENTS, type CanonicalWebhookEventType, type CreatePartnerTeamParams, type CreateWebhookEnvelopeOptions, type DataDetailOptions, type DataQueryOptions, type DeletePartnerUserResponse, type GscComparisonFilter, type GscdumpAnalysisParams, type GscdumpAnalysisPreset, type GscdumpAnalysisResponse, type GscdumpAnalysisSourcesResponse, type GscdumpAvailableSite, type GscdumpCanonicalMismatchesResponse, type GscdumpDataDetailResponse, type GscdumpDataResponse, type GscdumpDataRow, type GscdumpDateRangeParams, type GscdumpIndexPercentResponse, type GscdumpIndexingDiagnosticsResponse, type GscdumpIndexingResponse, type GscdumpIndexingUrlStatus, type GscdumpIndexingUrlsResponse, type GscdumpKeywordSparklinesParams, type GscdumpKeywordSparklinesResponse, type GscdumpMeta, type GscdumpPermissionRecovery, type GscdumpQueryTrendParams, type GscdumpQueryTrendResponse, type GscdumpSiteRegistration, type GscdumpSitemap, type GscdumpSitemapChangesResponse, type GscdumpSitemapHistory, type GscdumpSitemapsResponse, type GscdumpSyncStatusResponse, type GscdumpTeamMemberRow, type GscdumpTeamRow, type GscdumpTopAssociationParams, type GscdumpTopAssociationResponse, type GscdumpTotals, type GscdumpUserRegistration, type GscdumpUserSettings, type GscdumpUserSite, type GscdumpUserStatus, type GscdumpUserTokenUpdate, type IndexingUrlsParams, type InspectionHistoryResponse, type InspectionIndex, PartnerApiError, type PartnerClient, type PartnerClientOptions, type PartnerErrorInfo, type PartnerErrorKind, type PartnerFetch, type PartnerFetchOptions, type PartnerHeaders, type PartnerLifecycleAccount, type PartnerLifecycleResponse, type PartnerLifecycleSite, type PartnerRealtimeClient, type PartnerRealtimeEvent, type PartnerRealtimeEventType, type PartnerRealtimeHandler, type PartnerRealtimeMessage, type PartnerRealtimeOptions, type PartnerRealtimeScope, type PartnerRealtimeStatus, type PartnerWebSocketConstructor, type PartnerWebSocketLike, type PartnerWebhookData, type PartnerWebhookHeaders, type RealtimeAuthFailedEvent, type RealtimeAuthRequiredMessage, type RealtimeConnectedMessage, type RealtimeEnrichmentCompleteEvent, type RealtimeErrorMessage, type RealtimeJobFailedEvent, type RealtimeNeedsReauthEvent, type RealtimePongMessage, type RealtimeSiteAddedEvent, type RealtimeSiteRemovedEvent, type RealtimeSubscribedMessage, type RealtimeSyncCompleteEvent, type RealtimeSyncFailedEvent, type RealtimeSyncJobCompleteEvent, type RealtimeSyncProgressEvent, type RealtimeSyncSiteCompleteEvent, type RegisterPartnerSiteParams, type RegisterPartnerUserParams, type RollupEnvelope, type UpdatePartnerUserTokensParams, VALID_WEBHOOK_EVENTS, WEBHOOK_CONTRACT_VERSION, WEBHOOK_CONTRACT_VERSION_HEADER, WEBHOOK_DELIVERY_HEADER, WEBHOOK_EVENT_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_TIMESTAMP_HEADER, type WebhookEnvelope, type WebhookEventType, type WhoamiResponse, analyticsStatusToSyncStatus, createAnalyticsClient, createGscdumpClient, createGscdumpRealtimeClient, createPartnerClient, createPartnerRealtimeClient, findLifecycleSite, lifecycleSiteToSyncStatus, lifecycleSiteToUserSite, parseWebhookPayload, readWebhookHeaders, serializeWebhookPayload, toPartnerError, verifyWebhookSignature };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { CANONICAL_WEBHOOK_EVENTS, VALID_WEBHOOK_EVENTS, WEBHOOK_CONTRACT_VERSION, WEBHOOK_CONTRACT_VERSION_HEADER, WEBHOOK_CONTRACT_VERSION_HEADER as WEBHOOK_CONTRACT_VERSION_HEADER$1, WEBHOOK_DELIVERY_HEADER, WEBHOOK_DELIVERY_HEADER as WEBHOOK_DELIVERY_HEADER$1, WEBHOOK_EVENT_HEADER, WEBHOOK_EVENT_HEADER as WEBHOOK_EVENT_HEADER$1, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_HEADER as WEBHOOK_SIGNATURE_HEADER$1, WEBHOOK_TIMESTAMP_HEADER, WEBHOOK_TIMESTAMP_HEADER as WEBHOOK_TIMESTAMP_HEADER$1, analyticsRoutes, partnerEndpointSchemas, partnerRoutes, partnerWebhookEnvelopeSchema } from "@gscdump/contracts";
|
|
1
2
|
import { ofetch } from "ofetch";
|
|
2
|
-
import { CANONICAL_WEBHOOK_EVENTS, LEGACY_WEBHOOK_EVENTS, VALID_WEBHOOK_EVENTS, WEBHOOK_CONTRACT_VERSION, WEBHOOK_CONTRACT_VERSION_HEADER, WEBHOOK_CONTRACT_VERSION_HEADER as WEBHOOK_CONTRACT_VERSION_HEADER$1, WEBHOOK_DELIVERY_HEADER, WEBHOOK_DELIVERY_HEADER as WEBHOOK_DELIVERY_HEADER$1, WEBHOOK_EVENT_ALIASES, WEBHOOK_EVENT_ALIASES as WEBHOOK_EVENT_ALIASES$1, WEBHOOK_EVENT_HEADER, WEBHOOK_EVENT_HEADER as WEBHOOK_EVENT_HEADER$1, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_SIGNATURE_HEADER as WEBHOOK_SIGNATURE_HEADER$1, WEBHOOK_TIMESTAMP_HEADER, WEBHOOK_TIMESTAMP_HEADER as WEBHOOK_TIMESTAMP_HEADER$1, analyticsRoutes, partnerEndpointSchemas, partnerRoutes, partnerWebhookEnvelopeSchema } from "@gscdump/contracts";
|
|
3
3
|
export * from "@gscdump/contracts";
|
|
4
4
|
var PartnerApiError = class extends Error {
|
|
5
5
|
kind;
|
|
@@ -47,10 +47,10 @@ function toPartnerError(error) {
|
|
|
47
47
|
const TRAILING_SLASH_RE$2 = /\/+$/;
|
|
48
48
|
const LEADING_SLASH_RE$1 = /^\/+/;
|
|
49
49
|
function trimApiBase$1(apiBase) {
|
|
50
|
-
return (apiBase ?? "
|
|
50
|
+
return (apiBase ?? "").replace(TRAILING_SLASH_RE$2, "");
|
|
51
51
|
}
|
|
52
52
|
function buildPath$1(apiBase, path) {
|
|
53
|
-
if (!apiBase) return
|
|
53
|
+
if (!apiBase) return path.startsWith("/") ? path : `/${path}`;
|
|
54
54
|
return `${apiBase}/${path.replace(LEADING_SLASH_RE$1, "")}`;
|
|
55
55
|
}
|
|
56
56
|
function mergeHeaders$1(base, extra) {
|
|
@@ -59,9 +59,124 @@ function mergeHeaders$1(base, extra) {
|
|
|
59
59
|
return headers;
|
|
60
60
|
}
|
|
61
61
|
async function resolveHeaders$1(options) {
|
|
62
|
+
return typeof options.headers === "function" ? await options.headers() : options.headers;
|
|
63
|
+
}
|
|
64
|
+
function shouldValidate$1(options, phase) {
|
|
65
|
+
return options.validate === true || options.validate === phase;
|
|
66
|
+
}
|
|
67
|
+
function parseWith$1(schema, value) {
|
|
68
|
+
return schema ? schema.parse(value) : value;
|
|
69
|
+
}
|
|
70
|
+
function tablesQuery$1(tables) {
|
|
71
|
+
if (!tables) return void 0;
|
|
72
|
+
return { tables: Array.isArray(tables) ? tables.join(",") : tables };
|
|
73
|
+
}
|
|
74
|
+
function indexingUrlsQuery$1(params = {}) {
|
|
75
|
+
const query = {};
|
|
76
|
+
if (params.limit != null) query.limit = params.limit;
|
|
77
|
+
if (params.offset != null) query.offset = params.offset;
|
|
78
|
+
if (params.status) query.status = params.status;
|
|
79
|
+
if (params.issue) query.issue = params.issue;
|
|
80
|
+
if (params.search) query.search = params.search;
|
|
81
|
+
return query;
|
|
82
|
+
}
|
|
83
|
+
function createAnalyticsClient(options = {}) {
|
|
84
|
+
const fetchImpl = options.fetch ?? ofetch;
|
|
85
|
+
const apiBase = trimApiBase$1(options.apiBase);
|
|
86
|
+
async function request(path, init = {}, responseSchema) {
|
|
87
|
+
const headers = mergeHeaders$1(await resolveHeaders$1(options), init.headers);
|
|
88
|
+
try {
|
|
89
|
+
const out = await fetchImpl(buildPath$1(apiBase, path), {
|
|
90
|
+
...init,
|
|
91
|
+
headers
|
|
92
|
+
});
|
|
93
|
+
return shouldValidate$1(options, "response") ? parseWith$1(responseSchema, out) : out;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
throw toPartnerError(error);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
whoami() {
|
|
100
|
+
return request(analyticsRoutes.whoami, {}, partnerEndpointSchemas.analyticsWhoami.response);
|
|
101
|
+
},
|
|
102
|
+
listSites() {
|
|
103
|
+
return request(analyticsRoutes.sites, {}, partnerEndpointSchemas.analyticsSites.response);
|
|
104
|
+
},
|
|
105
|
+
getSourceInfo(siteId) {
|
|
106
|
+
return request(analyticsRoutes.site.sourceInfo(siteId), {}, partnerEndpointSchemas.analyticsSourceInfo.response);
|
|
107
|
+
},
|
|
108
|
+
getAnalysisSources(siteId, tables) {
|
|
109
|
+
return request(analyticsRoutes.site.analysisSources(siteId), { query: tablesQuery$1(tables) }, partnerEndpointSchemas.analyticsAnalysisSources.response);
|
|
110
|
+
},
|
|
111
|
+
analyze(siteId, params) {
|
|
112
|
+
return request(analyticsRoutes.site.analyze(siteId), {
|
|
113
|
+
method: "POST",
|
|
114
|
+
body: params
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
queryRows(siteId, state) {
|
|
118
|
+
return request(analyticsRoutes.site.rows(siteId), {
|
|
119
|
+
method: "POST",
|
|
120
|
+
body: state
|
|
121
|
+
}, partnerEndpointSchemas.analyticsRows.response);
|
|
122
|
+
},
|
|
123
|
+
getRollup(siteId, rollupId, params) {
|
|
124
|
+
return request(analyticsRoutes.site.rollup(siteId, rollupId), { query: params }, partnerEndpointSchemas.analyticsRollup.response);
|
|
125
|
+
},
|
|
126
|
+
requestBackfill(siteId, range) {
|
|
127
|
+
const body = shouldValidate$1(options, "request") ? partnerEndpointSchemas.analyticsBackfill.body.parse(range) : range;
|
|
128
|
+
return request(analyticsRoutes.site.backfill(siteId), {
|
|
129
|
+
method: "POST",
|
|
130
|
+
body
|
|
131
|
+
}, partnerEndpointSchemas.analyticsBackfill.response);
|
|
132
|
+
},
|
|
133
|
+
getSitemaps(siteId) {
|
|
134
|
+
return request(analyticsRoutes.site.sitemaps(siteId), {}, partnerEndpointSchemas.analyticsSitemaps.response);
|
|
135
|
+
},
|
|
136
|
+
getSitemapHistory(siteId, hash) {
|
|
137
|
+
return request(analyticsRoutes.site.sitemapHistory(siteId, hash), {}, partnerEndpointSchemas.analyticsSitemapHistory.response);
|
|
138
|
+
},
|
|
139
|
+
getSitemapChanges(siteId, params = {}) {
|
|
140
|
+
return request(analyticsRoutes.site.sitemapChanges(siteId), { query: params }, partnerEndpointSchemas.analyticsSitemapChanges.response);
|
|
141
|
+
},
|
|
142
|
+
getInspections(siteId) {
|
|
143
|
+
return request(analyticsRoutes.site.inspections(siteId), {}, partnerEndpointSchemas.analyticsInspections.response);
|
|
144
|
+
},
|
|
145
|
+
getInspectionHistory(siteId, hash) {
|
|
146
|
+
return request(analyticsRoutes.site.inspectionHistory(siteId, hash), {}, partnerEndpointSchemas.analyticsInspectionHistory.response);
|
|
147
|
+
},
|
|
148
|
+
getIndexingUrls(siteId, params = {}) {
|
|
149
|
+
return request(analyticsRoutes.site.indexingUrls(siteId), { query: indexingUrlsQuery$1(params) }, partnerEndpointSchemas.analyticsIndexingUrls.response);
|
|
150
|
+
},
|
|
151
|
+
getIndexingDiagnostics(siteId) {
|
|
152
|
+
return request(analyticsRoutes.site.indexingDiagnostics(siteId), {}, partnerEndpointSchemas.analyticsIndexingDiagnostics.response);
|
|
153
|
+
},
|
|
154
|
+
getCountries(siteId, range) {
|
|
155
|
+
return request(analyticsRoutes.site.countries(siteId), { query: range }, partnerEndpointSchemas.analyticsCountries.response);
|
|
156
|
+
},
|
|
157
|
+
getSearchAppearance(siteId, range) {
|
|
158
|
+
return request(analyticsRoutes.site.searchAppearance(siteId), { query: range }, partnerEndpointSchemas.analyticsSearchAppearance.response);
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const TRAILING_SLASH_RE$1 = /\/+$/;
|
|
163
|
+
const LEADING_SLASH_RE = /^\/+/;
|
|
164
|
+
function trimApiBase(apiBase) {
|
|
165
|
+
return (apiBase ?? "/api").replace(TRAILING_SLASH_RE$1, "");
|
|
166
|
+
}
|
|
167
|
+
function buildPath(apiBase, path) {
|
|
168
|
+
if (!apiBase) return `/${path.replace(LEADING_SLASH_RE, "")}`;
|
|
169
|
+
return `${apiBase}/${path.replace(LEADING_SLASH_RE, "")}`;
|
|
170
|
+
}
|
|
171
|
+
function mergeHeaders(base, extra) {
|
|
172
|
+
const headers = new Headers(base);
|
|
173
|
+
if (extra) for (const [key, value] of new Headers(extra).entries()) headers.set(key, value);
|
|
174
|
+
return headers;
|
|
175
|
+
}
|
|
176
|
+
async function resolveHeaders(options) {
|
|
62
177
|
const resolved = typeof options.headers === "function" ? await options.headers() : options.headers;
|
|
63
178
|
if (!options.apiKey) return resolved;
|
|
64
|
-
return mergeHeaders
|
|
179
|
+
return mergeHeaders(resolved, { "x-api-key": options.apiKey });
|
|
65
180
|
}
|
|
66
181
|
function dataQuery(state, options) {
|
|
67
182
|
const query = { q: JSON.stringify(state) };
|
|
@@ -95,7 +210,7 @@ function analysisQuery(params) {
|
|
|
95
210
|
if (params.maxCtr != null) query.maxCtr = params.maxCtr;
|
|
96
211
|
return query;
|
|
97
212
|
}
|
|
98
|
-
function indexingUrlsQuery
|
|
213
|
+
function indexingUrlsQuery(params = {}) {
|
|
99
214
|
const query = {};
|
|
100
215
|
if (params.limit != null) query.limit = params.limit;
|
|
101
216
|
if (params.offset != null) query.offset = params.offset;
|
|
@@ -104,7 +219,7 @@ function indexingUrlsQuery$1(params = {}) {
|
|
|
104
219
|
if (params.search) query.search = params.search;
|
|
105
220
|
return query;
|
|
106
221
|
}
|
|
107
|
-
function tablesQuery
|
|
222
|
+
function tablesQuery(tables) {
|
|
108
223
|
if (!tables) return void 0;
|
|
109
224
|
return { tables: Array.isArray(tables) ? tables.join(",") : tables };
|
|
110
225
|
}
|
|
@@ -123,10 +238,10 @@ function queryTrendQuery(params) {
|
|
|
123
238
|
if (params.prevEndDate) query.prevEndDate = params.prevEndDate;
|
|
124
239
|
return query;
|
|
125
240
|
}
|
|
126
|
-
function shouldValidate
|
|
241
|
+
function shouldValidate(options, phase) {
|
|
127
242
|
return options.validate === true || options.validate === phase;
|
|
128
243
|
}
|
|
129
|
-
function parseWith
|
|
244
|
+
function parseWith(schema, value) {
|
|
130
245
|
return schema ? schema.parse(value) : value;
|
|
131
246
|
}
|
|
132
247
|
function sleep(ms) {
|
|
@@ -134,29 +249,29 @@ function sleep(ms) {
|
|
|
134
249
|
}
|
|
135
250
|
function createPartnerClient(options = {}) {
|
|
136
251
|
const fetchImpl = options.fetch ?? ofetch;
|
|
137
|
-
const apiBase = trimApiBase
|
|
252
|
+
const apiBase = trimApiBase(options.apiBase);
|
|
138
253
|
async function request(path, init = {}, responseSchema) {
|
|
139
|
-
const headers = mergeHeaders
|
|
254
|
+
const headers = mergeHeaders(await resolveHeaders(options), init.headers);
|
|
140
255
|
try {
|
|
141
|
-
const out = await fetchImpl(buildPath
|
|
256
|
+
const out = await fetchImpl(buildPath(apiBase, path), {
|
|
142
257
|
...init,
|
|
143
258
|
headers
|
|
144
259
|
});
|
|
145
|
-
return shouldValidate
|
|
260
|
+
return shouldValidate(options, "response") ? parseWith(responseSchema, out) : out;
|
|
146
261
|
} catch (error) {
|
|
147
262
|
throw toPartnerError(error);
|
|
148
263
|
}
|
|
149
264
|
}
|
|
150
265
|
return {
|
|
151
266
|
registerUser(params) {
|
|
152
|
-
const body = shouldValidate
|
|
267
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.registerUser.body.parse(params) : params;
|
|
153
268
|
return request(partnerRoutes.users.register, {
|
|
154
269
|
method: "POST",
|
|
155
270
|
body
|
|
156
271
|
}, partnerEndpointSchemas.registerUser.response);
|
|
157
272
|
},
|
|
158
273
|
updateUserTokens(userId, params) {
|
|
159
|
-
const body = shouldValidate
|
|
274
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.updateUserTokens.body.parse(params) : params;
|
|
160
275
|
return request(partnerRoutes.users.tokens(userId), {
|
|
161
276
|
method: "PATCH",
|
|
162
277
|
body
|
|
@@ -188,14 +303,14 @@ function createPartnerClient(options = {}) {
|
|
|
188
303
|
return request(partnerRoutes.users.availableSites(userId), {}, partnerEndpointSchemas.getAvailableSites.response);
|
|
189
304
|
},
|
|
190
305
|
registerSite(params) {
|
|
191
|
-
const body = shouldValidate
|
|
306
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.registerSite.body.parse(params) : params;
|
|
192
307
|
return request(partnerRoutes.partner.sites.register, {
|
|
193
308
|
method: "POST",
|
|
194
309
|
body
|
|
195
310
|
}, partnerEndpointSchemas.registerSite.response);
|
|
196
311
|
},
|
|
197
312
|
bulkRegisterSites(params) {
|
|
198
|
-
const body = shouldValidate
|
|
313
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.bulkRegisterSites.body.parse(params) : params;
|
|
199
314
|
return request(partnerRoutes.partner.sites.bulkRegister, {
|
|
200
315
|
method: "POST",
|
|
201
316
|
body
|
|
@@ -208,20 +323,20 @@ function createPartnerClient(options = {}) {
|
|
|
208
323
|
return request(partnerRoutes.sites.byId(siteId), { method: "DELETE" });
|
|
209
324
|
},
|
|
210
325
|
getAnalysisSources(siteId, tables) {
|
|
211
|
-
return request(partnerRoutes.sites.analysisSources(siteId), { query: tablesQuery
|
|
326
|
+
return request(partnerRoutes.sites.analysisSources(siteId), { query: tablesQuery(tables) }, partnerEndpointSchemas.getAnalysisSources.response);
|
|
212
327
|
},
|
|
213
328
|
getSiteSyncStatus(siteId) {
|
|
214
329
|
return request(partnerRoutes.sites.syncStatus(siteId));
|
|
215
330
|
},
|
|
216
331
|
getData(siteId, state, queryOptions) {
|
|
217
|
-
if (shouldValidate
|
|
332
|
+
if (shouldValidate(options, "request")) {
|
|
218
333
|
partnerEndpointSchemas.getData.state.parse(state);
|
|
219
334
|
partnerEndpointSchemas.getData.options.parse(queryOptions);
|
|
220
335
|
}
|
|
221
336
|
return request(partnerRoutes.sites.data(siteId), { query: dataQuery(state, queryOptions) }, partnerEndpointSchemas.getData.response);
|
|
222
337
|
},
|
|
223
338
|
getDataDetail(siteId, state, queryOptions) {
|
|
224
|
-
if (shouldValidate
|
|
339
|
+
if (shouldValidate(options, "request")) {
|
|
225
340
|
partnerEndpointSchemas.getDataDetail.state.parse(state);
|
|
226
341
|
partnerEndpointSchemas.getDataDetail.options.parse(queryOptions);
|
|
227
342
|
}
|
|
@@ -229,7 +344,7 @@ function createPartnerClient(options = {}) {
|
|
|
229
344
|
},
|
|
230
345
|
getAnalysis(siteId, params) {
|
|
231
346
|
assertAnalysisParams(params);
|
|
232
|
-
const query = shouldValidate
|
|
347
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getAnalysis.query.parse(params) : params;
|
|
233
348
|
return request(partnerRoutes.sites.analysis(siteId), { query: analysisQuery(query) }, partnerEndpointSchemas.getAnalysis.response);
|
|
234
349
|
},
|
|
235
350
|
getSitemaps(siteId) {
|
|
@@ -257,8 +372,8 @@ function createPartnerClient(options = {}) {
|
|
|
257
372
|
return request(partnerRoutes.sites.indexing(siteId), { query: { days } }, partnerEndpointSchemas.getIndexing.response);
|
|
258
373
|
},
|
|
259
374
|
getIndexingUrls(siteId, params = {}) {
|
|
260
|
-
const parsed = shouldValidate
|
|
261
|
-
return request(partnerRoutes.sites.indexingUrls(siteId), { query: indexingUrlsQuery
|
|
375
|
+
const parsed = shouldValidate(options, "request") ? partnerEndpointSchemas.getIndexingUrls.query.parse(params) : params;
|
|
376
|
+
return request(partnerRoutes.sites.indexingUrls(siteId), { query: indexingUrlsQuery(parsed) }, partnerEndpointSchemas.getIndexingUrls.response);
|
|
262
377
|
},
|
|
263
378
|
getIndexingDiagnostics(siteId) {
|
|
264
379
|
return request(partnerRoutes.sites.indexingDiagnostics(siteId), {}, partnerEndpointSchemas.getIndexingDiagnostics.response);
|
|
@@ -267,7 +382,7 @@ function createPartnerClient(options = {}) {
|
|
|
267
382
|
return request(partnerRoutes.settings.user, {}, partnerEndpointSchemas.getUserSettings.response);
|
|
268
383
|
},
|
|
269
384
|
patchUserSettings(body) {
|
|
270
|
-
const parsed = shouldValidate
|
|
385
|
+
const parsed = shouldValidate(options, "request") ? partnerEndpointSchemas.patchUserSettings.body.parse(body) : body;
|
|
271
386
|
return request(partnerRoutes.settings.user, {
|
|
272
387
|
method: "PATCH",
|
|
273
388
|
body: parsed
|
|
@@ -277,18 +392,18 @@ function createPartnerClient(options = {}) {
|
|
|
277
392
|
return request(partnerRoutes.sites.recoverPermission(siteId), { method: "POST" }, partnerEndpointSchemas.recoverPermission.response);
|
|
278
393
|
},
|
|
279
394
|
getTopAssociation(siteId, params) {
|
|
280
|
-
const query = shouldValidate
|
|
395
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getTopAssociation.query.parse(params) : params;
|
|
281
396
|
return request(partnerRoutes.sites.topAssociation(siteId), { query }, partnerEndpointSchemas.getTopAssociation.response);
|
|
282
397
|
},
|
|
283
398
|
getKeywordSparklines(siteId, params) {
|
|
284
|
-
const body = shouldValidate
|
|
399
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.getKeywordSparklines.body.parse(params) : params;
|
|
285
400
|
return request(partnerRoutes.sites.keywordSparklines(siteId), {
|
|
286
401
|
method: "POST",
|
|
287
402
|
body
|
|
288
403
|
}, partnerEndpointSchemas.getKeywordSparklines.response);
|
|
289
404
|
},
|
|
290
405
|
getQueryTrend(siteId, params) {
|
|
291
|
-
const query = shouldValidate
|
|
406
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getQueryTrend.query.parse(params) : params;
|
|
292
407
|
return request(partnerRoutes.sites.queryTrend(siteId), { query: queryTrendQuery(query) }, partnerEndpointSchemas.getQueryTrend.response);
|
|
293
408
|
},
|
|
294
409
|
getCanonicalMismatches(siteId) {
|
|
@@ -298,31 +413,31 @@ function createPartnerClient(options = {}) {
|
|
|
298
413
|
return request(partnerRoutes.sites.contentVelocity(siteId), { query: days == null ? void 0 : { days } });
|
|
299
414
|
},
|
|
300
415
|
getCtrCurve(siteId, params) {
|
|
301
|
-
const query = shouldValidate
|
|
416
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getDateRangeInsight.query.parse(params) : params;
|
|
302
417
|
return request(partnerRoutes.sites.ctrCurve(siteId), { query: dateRangeQuery(query) });
|
|
303
418
|
},
|
|
304
419
|
getDarkTraffic(siteId, params) {
|
|
305
|
-
const query = shouldValidate
|
|
420
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getDateRangeInsight.query.parse(params) : params;
|
|
306
421
|
return request(partnerRoutes.sites.darkTraffic(siteId), { query: dateRangeQuery(query) });
|
|
307
422
|
},
|
|
308
423
|
getDeviceGap(siteId, params) {
|
|
309
|
-
const query = shouldValidate
|
|
424
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getDateRangeInsight.query.parse(params) : params;
|
|
310
425
|
return request(partnerRoutes.sites.deviceGap(siteId), { query: dateRangeQuery(query) });
|
|
311
426
|
},
|
|
312
427
|
getIndexPercent(siteId, params = {}) {
|
|
313
|
-
const query = shouldValidate
|
|
428
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getIndexPercent.query.parse(params) : params;
|
|
314
429
|
return request(partnerRoutes.sites.indexPercent(siteId), { query }, partnerEndpointSchemas.getIndexPercent.response);
|
|
315
430
|
},
|
|
316
431
|
getKeywordBreadth(siteId, params) {
|
|
317
|
-
const query = shouldValidate
|
|
432
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getDateRangeInsight.query.parse(params) : params;
|
|
318
433
|
return request(partnerRoutes.sites.keywordBreadth(siteId), { query: dateRangeQuery(query) });
|
|
319
434
|
},
|
|
320
435
|
getPositionDistribution(siteId, params) {
|
|
321
|
-
const query = shouldValidate
|
|
436
|
+
const query = shouldValidate(options, "request") ? partnerEndpointSchemas.getDateRangeInsight.query.parse(params) : params;
|
|
322
437
|
return request(partnerRoutes.sites.positionDistribution(siteId), { query: dateRangeQuery(query) });
|
|
323
438
|
},
|
|
324
439
|
createTeam(params) {
|
|
325
|
-
const body = shouldValidate
|
|
440
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.createTeam.body.parse(params) : params;
|
|
326
441
|
return request(partnerRoutes.teams.create, {
|
|
327
442
|
method: "POST",
|
|
328
443
|
body
|
|
@@ -341,7 +456,7 @@ function createPartnerClient(options = {}) {
|
|
|
341
456
|
return request(partnerRoutes.teams.members(teamId), {}, partnerEndpointSchemas.listTeamMembers.response);
|
|
342
457
|
},
|
|
343
458
|
addTeamMember(teamId, params) {
|
|
344
|
-
const body = shouldValidate
|
|
459
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.addTeamMember.body.parse(params) : params;
|
|
345
460
|
return request(partnerRoutes.teams.members(teamId), {
|
|
346
461
|
method: "POST",
|
|
347
462
|
body
|
|
@@ -357,7 +472,7 @@ function createPartnerClient(options = {}) {
|
|
|
357
472
|
return request(partnerRoutes.teams.member(teamId, userId), { method: "DELETE" });
|
|
358
473
|
},
|
|
359
474
|
bindSiteToTeam(userId, siteId, params) {
|
|
360
|
-
const body = shouldValidate
|
|
475
|
+
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.bindSiteToTeam.body.parse(params) : params;
|
|
361
476
|
return request(partnerRoutes.partner.users.siteTeam(userId, siteId), {
|
|
362
477
|
method: "PATCH",
|
|
363
478
|
body
|
|
@@ -366,192 +481,89 @@ function createPartnerClient(options = {}) {
|
|
|
366
481
|
};
|
|
367
482
|
}
|
|
368
483
|
const createGscdumpClient = createPartnerClient;
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
if (
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
function parseWith(schema, value) {
|
|
390
|
-
return schema ? schema.parse(value) : value;
|
|
391
|
-
}
|
|
392
|
-
function tablesQuery(tables) {
|
|
393
|
-
if (!tables) return void 0;
|
|
394
|
-
return { tables: Array.isArray(tables) ? tables.join(",") : tables };
|
|
395
|
-
}
|
|
396
|
-
function indexingUrlsQuery(params = {}) {
|
|
397
|
-
const query = {};
|
|
398
|
-
if (params.limit != null) query.limit = params.limit;
|
|
399
|
-
if (params.offset != null) query.offset = params.offset;
|
|
400
|
-
if (params.status) query.status = params.status;
|
|
401
|
-
if (params.issue) query.issue = params.issue;
|
|
402
|
-
if (params.search) query.search = params.search;
|
|
403
|
-
return query;
|
|
404
|
-
}
|
|
405
|
-
function createAnalyticsClient(options = {}) {
|
|
406
|
-
const fetchImpl = options.fetch ?? ofetch;
|
|
407
|
-
const apiBase = trimApiBase(options.apiBase);
|
|
408
|
-
async function request(path, init = {}, responseSchema) {
|
|
409
|
-
const headers = mergeHeaders(await resolveHeaders(options), init.headers);
|
|
410
|
-
try {
|
|
411
|
-
const out = await fetchImpl(buildPath(apiBase, path), {
|
|
412
|
-
...init,
|
|
413
|
-
headers
|
|
414
|
-
});
|
|
415
|
-
return shouldValidate(options, "response") ? parseWith(responseSchema, out) : out;
|
|
416
|
-
} catch (error) {
|
|
417
|
-
throw toPartnerError(error);
|
|
418
|
-
}
|
|
484
|
+
function normalizeLifecycleUrl(url) {
|
|
485
|
+
return (url || "").replace(/^sc-domain:/, "").replace(/^https?:\/\//, "").replace(/^www\./, "").replace(/\/$/, "").toLowerCase();
|
|
486
|
+
}
|
|
487
|
+
function normalizeGscPropertyKey(url) {
|
|
488
|
+
const value = url || "";
|
|
489
|
+
if (!value) return "";
|
|
490
|
+
if (value.startsWith("sc-domain:")) return `domain:${normalizeLifecycleUrl(value)}`;
|
|
491
|
+
if (!/^https?:\/\//.test(value)) return "";
|
|
492
|
+
return `url:${normalizeLifecycleUrl(value)}`;
|
|
493
|
+
}
|
|
494
|
+
function analyticsStatusToSyncStatus(status) {
|
|
495
|
+
switch (status) {
|
|
496
|
+
case "ready":
|
|
497
|
+
case "queryable_live":
|
|
498
|
+
case "queryable_partial": return "synced";
|
|
499
|
+
case "syncing":
|
|
500
|
+
case "preparing": return "syncing";
|
|
501
|
+
case "failed": return "error";
|
|
502
|
+
default: return "pending";
|
|
419
503
|
}
|
|
504
|
+
}
|
|
505
|
+
function lifecycleSiteToUserSite(site) {
|
|
506
|
+
const syncStatus = analyticsStatusToSyncStatus(site.analytics.status);
|
|
420
507
|
return {
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
method: "POST",
|
|
436
|
-
body: params
|
|
437
|
-
});
|
|
438
|
-
},
|
|
439
|
-
queryRows(siteId, state) {
|
|
440
|
-
return request(analyticsRoutes.site.rows(siteId), {
|
|
441
|
-
method: "POST",
|
|
442
|
-
body: state
|
|
443
|
-
}, partnerEndpointSchemas.analyticsRows.response);
|
|
444
|
-
},
|
|
445
|
-
getRollup(siteId, rollupId, params) {
|
|
446
|
-
return request(analyticsRoutes.site.rollup(siteId, rollupId), { query: params }, partnerEndpointSchemas.analyticsRollup.response);
|
|
447
|
-
},
|
|
448
|
-
requestBackfill(siteId, range) {
|
|
449
|
-
const body = shouldValidate(options, "request") ? partnerEndpointSchemas.analyticsBackfill.body.parse(range) : range;
|
|
450
|
-
return request(analyticsRoutes.site.backfill(siteId), {
|
|
451
|
-
method: "POST",
|
|
452
|
-
body
|
|
453
|
-
}, partnerEndpointSchemas.analyticsBackfill.response);
|
|
454
|
-
},
|
|
455
|
-
getSitemaps(siteId) {
|
|
456
|
-
return request(analyticsRoutes.site.sitemaps(siteId), {}, partnerEndpointSchemas.analyticsSitemaps.response);
|
|
457
|
-
},
|
|
458
|
-
getSitemapHistory(siteId, hash) {
|
|
459
|
-
return request(analyticsRoutes.site.sitemapHistory(siteId, hash), {}, partnerEndpointSchemas.analyticsSitemapHistory.response);
|
|
460
|
-
},
|
|
461
|
-
getSitemapChanges(siteId, params = {}) {
|
|
462
|
-
return request(analyticsRoutes.site.sitemapChanges(siteId), { query: params }, partnerEndpointSchemas.analyticsSitemapChanges.response);
|
|
463
|
-
},
|
|
464
|
-
getInspections(siteId) {
|
|
465
|
-
return request(analyticsRoutes.site.inspections(siteId), {}, partnerEndpointSchemas.analyticsInspections.response);
|
|
466
|
-
},
|
|
467
|
-
getInspectionHistory(siteId, hash) {
|
|
468
|
-
return request(analyticsRoutes.site.inspectionHistory(siteId, hash), {}, partnerEndpointSchemas.analyticsInspectionHistory.response);
|
|
469
|
-
},
|
|
470
|
-
getIndexingUrls(siteId, params = {}) {
|
|
471
|
-
return request(analyticsRoutes.site.indexingUrls(siteId), { query: indexingUrlsQuery(params) }, partnerEndpointSchemas.analyticsIndexingUrls.response);
|
|
472
|
-
},
|
|
473
|
-
getIndexingDiagnostics(siteId) {
|
|
474
|
-
return request(analyticsRoutes.site.indexingDiagnostics(siteId), {}, partnerEndpointSchemas.analyticsIndexingDiagnostics.response);
|
|
475
|
-
}
|
|
508
|
+
siteId: site.siteId,
|
|
509
|
+
siteUrl: site.gscPropertyUrl || site.requestedUrl,
|
|
510
|
+
analyticsSyncStatus: syncStatus,
|
|
511
|
+
analyticsSyncProgress: site.analytics.progress,
|
|
512
|
+
syncStatus,
|
|
513
|
+
syncProgress: site.analytics.progress,
|
|
514
|
+
indexingEligible: site.indexing.eligible,
|
|
515
|
+
indexingIneligibleReason: site.indexing.reason,
|
|
516
|
+
indexingPermissionLevel: site.permissionLevel,
|
|
517
|
+
indexingStatus: site.indexing.status === "ready" ? "complete" : site.indexing.status === "not_requested" ? "not_started" : "indexing",
|
|
518
|
+
indexingProgress: site.indexing.progress,
|
|
519
|
+
lastSyncAt: site.updatedAt ? Date.parse(site.updatedAt) : null,
|
|
520
|
+
newestDateSynced: site.analytics.syncedRange.newest,
|
|
521
|
+
oldestDateSynced: site.analytics.syncedRange.oldest
|
|
476
522
|
};
|
|
477
523
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
485
|
-
for (let i = 0; i < bytes.length; i++) bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
486
|
-
return bytes;
|
|
487
|
-
}
|
|
488
|
-
function constantTimeEqual(a, b) {
|
|
489
|
-
if (a.length !== b.length) return false;
|
|
490
|
-
let diff = 0;
|
|
491
|
-
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
492
|
-
return diff === 0;
|
|
493
|
-
}
|
|
494
|
-
function signatureHex(signature) {
|
|
495
|
-
const trimmed = signature.trim();
|
|
496
|
-
return trimmed.startsWith("sha256=") ? trimmed.slice(7) : null;
|
|
497
|
-
}
|
|
498
|
-
async function hmacSha256(payload, secret) {
|
|
499
|
-
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
500
|
-
name: "HMAC",
|
|
501
|
-
hash: "SHA-256"
|
|
502
|
-
}, false, ["sign"]);
|
|
503
|
-
return crypto.subtle.sign("HMAC", key, encoder.encode(payload));
|
|
504
|
-
}
|
|
505
|
-
function toCanonicalWebhookEvent(event) {
|
|
506
|
-
return WEBHOOK_EVENT_ALIASES$1[event] ?? event;
|
|
507
|
-
}
|
|
508
|
-
function serializeWebhookPayload(payload) {
|
|
509
|
-
return toPayloadString(payload);
|
|
510
|
-
}
|
|
511
|
-
async function verifyWebhookSignature(payload, signature, secret) {
|
|
512
|
-
if (!signature) return false;
|
|
513
|
-
const hex = signatureHex(signature);
|
|
514
|
-
if (!hex) return false;
|
|
515
|
-
const received = hexToBytes(hex);
|
|
516
|
-
if (!received) return false;
|
|
517
|
-
return constantTimeEqual(new Uint8Array(await hmacSha256(toPayloadString(payload), secret)), received);
|
|
518
|
-
}
|
|
519
|
-
async function parseWebhookPayload(payload, options = {}) {
|
|
520
|
-
const payloadString = toPayloadString(payload);
|
|
521
|
-
const signature = options.signature ?? readWebhookHeaders(options.headers).signature;
|
|
522
|
-
if (options.secret && (options.validateSignature ?? true)) {
|
|
523
|
-
if (!await verifyWebhookSignature(payloadString, signature, options.secret)) throw new PartnerApiError({
|
|
524
|
-
kind: "auth",
|
|
525
|
-
statusCode: 401,
|
|
526
|
-
message: "Invalid webhook signature"
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
const parsed = typeof payload === "string" ? JSON.parse(payload) : payload;
|
|
530
|
-
return partnerWebhookEnvelopeSchema.parse(parsed);
|
|
531
|
-
}
|
|
532
|
-
function readWebhookHeaders(headers) {
|
|
533
|
-
if (!headers) return {
|
|
534
|
-
event: null,
|
|
535
|
-
delivery: null,
|
|
536
|
-
contractVersion: null,
|
|
537
|
-
timestamp: null,
|
|
538
|
-
signature: null
|
|
539
|
-
};
|
|
540
|
-
if (headers instanceof Headers) return {
|
|
541
|
-
event: headers.get(WEBHOOK_EVENT_HEADER$1),
|
|
542
|
-
delivery: headers.get(WEBHOOK_DELIVERY_HEADER$1),
|
|
543
|
-
contractVersion: headers.get(WEBHOOK_CONTRACT_VERSION_HEADER$1),
|
|
544
|
-
timestamp: headers.get(WEBHOOK_TIMESTAMP_HEADER$1),
|
|
545
|
-
signature: headers.get(WEBHOOK_SIGNATURE_HEADER$1)
|
|
546
|
-
};
|
|
524
|
+
function lifecycleSiteToSyncStatus(site) {
|
|
525
|
+
const syncStatus = analyticsStatusToSyncStatus(site.analytics.status);
|
|
526
|
+
const completed = site.analytics.progress.completed;
|
|
527
|
+
const failed = site.analytics.progress.failed;
|
|
528
|
+
const total = site.analytics.progress.total;
|
|
529
|
+
const queued = Math.max(total - completed - failed, 0);
|
|
547
530
|
return {
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
531
|
+
siteUrl: site.gscPropertyUrl || site.requestedUrl,
|
|
532
|
+
syncStatus,
|
|
533
|
+
oldestDateAvailable: site.analytics.syncedRange.oldest,
|
|
534
|
+
oldestDateSynced: site.analytics.syncedRange.oldest,
|
|
535
|
+
newestDateSynced: site.analytics.syncedRange.newest,
|
|
536
|
+
lastSyncAt: site.updatedAt ? Date.parse(site.updatedAt) : null,
|
|
537
|
+
lastError: site.latestError?.message ?? null,
|
|
538
|
+
jobs: {
|
|
539
|
+
queued,
|
|
540
|
+
processing: [
|
|
541
|
+
"queued",
|
|
542
|
+
"preparing",
|
|
543
|
+
"syncing"
|
|
544
|
+
].includes(site.analytics.status) ? 1 : 0,
|
|
545
|
+
completed,
|
|
546
|
+
failed
|
|
547
|
+
},
|
|
548
|
+
progress: site.analytics.progress.percent,
|
|
549
|
+
jobProgress: site.analytics.progress.percent,
|
|
550
|
+
daysSynced: completed,
|
|
551
|
+
daysAvailable: total,
|
|
552
|
+
isSyncing: [
|
|
553
|
+
"queued",
|
|
554
|
+
"preparing",
|
|
555
|
+
"syncing"
|
|
556
|
+
].includes(site.analytics.status),
|
|
557
|
+
hasData: site.analytics.queryable,
|
|
558
|
+
isComplete: site.analytics.queryable && site.analytics.status === "ready",
|
|
559
|
+
tables: {}
|
|
553
560
|
};
|
|
554
561
|
}
|
|
562
|
+
function findLifecycleSite(lifecycle, siteIdOrPropertyUrl) {
|
|
563
|
+
const normalized = normalizeLifecycleUrl(siteIdOrPropertyUrl);
|
|
564
|
+
const propertyKey = normalizeGscPropertyKey(siteIdOrPropertyUrl);
|
|
565
|
+
return lifecycle.sites.find((site) => site.siteId === siteIdOrPropertyUrl || site.externalSiteId === siteIdOrPropertyUrl || !!propertyKey && normalizeGscPropertyKey(site.gscPropertyUrl) === propertyKey || !site.gscPropertyUrl && normalizeLifecycleUrl(site.requestedUrl) === normalized || !propertyKey && normalizeLifecycleUrl(site.requestedUrl) === normalized) ?? null;
|
|
566
|
+
}
|
|
555
567
|
const HTTP_PROTOCOL_RE = /^http/i;
|
|
556
568
|
const API_SUFFIX_RE = /\/api\/?$/;
|
|
557
569
|
const TRAILING_SLASH_RE = /\/+$/;
|
|
@@ -681,4 +693,78 @@ function createPartnerRealtimeClient(options) {
|
|
|
681
693
|
};
|
|
682
694
|
}
|
|
683
695
|
const createGscdumpRealtimeClient = createPartnerRealtimeClient;
|
|
684
|
-
|
|
696
|
+
const encoder = new TextEncoder();
|
|
697
|
+
function toPayloadString(payload) {
|
|
698
|
+
return typeof payload === "string" ? payload : JSON.stringify(payload);
|
|
699
|
+
}
|
|
700
|
+
function hexToBytes(hex) {
|
|
701
|
+
if (!/^[\da-f]+$/i.test(hex) || hex.length % 2 !== 0) return null;
|
|
702
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
703
|
+
for (let i = 0; i < bytes.length; i++) bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
|
|
704
|
+
return bytes;
|
|
705
|
+
}
|
|
706
|
+
function constantTimeEqual(a, b) {
|
|
707
|
+
if (a.length !== b.length) return false;
|
|
708
|
+
let diff = 0;
|
|
709
|
+
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
710
|
+
return diff === 0;
|
|
711
|
+
}
|
|
712
|
+
function signatureHex(signature) {
|
|
713
|
+
const trimmed = signature.trim();
|
|
714
|
+
return trimmed.startsWith("sha256=") ? trimmed.slice(7) : null;
|
|
715
|
+
}
|
|
716
|
+
async function hmacSha256(payload, secret) {
|
|
717
|
+
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), {
|
|
718
|
+
name: "HMAC",
|
|
719
|
+
hash: "SHA-256"
|
|
720
|
+
}, false, ["sign"]);
|
|
721
|
+
return crypto.subtle.sign("HMAC", key, encoder.encode(payload));
|
|
722
|
+
}
|
|
723
|
+
function serializeWebhookPayload(payload) {
|
|
724
|
+
return toPayloadString(payload);
|
|
725
|
+
}
|
|
726
|
+
async function verifyWebhookSignature(payload, signature, secret) {
|
|
727
|
+
if (!signature) return false;
|
|
728
|
+
const hex = signatureHex(signature);
|
|
729
|
+
if (!hex) return false;
|
|
730
|
+
const received = hexToBytes(hex);
|
|
731
|
+
if (!received) return false;
|
|
732
|
+
return constantTimeEqual(new Uint8Array(await hmacSha256(toPayloadString(payload), secret)), received);
|
|
733
|
+
}
|
|
734
|
+
async function parseWebhookPayload(payload, options = {}) {
|
|
735
|
+
const payloadString = toPayloadString(payload);
|
|
736
|
+
const signature = options.signature ?? readWebhookHeaders(options.headers).signature;
|
|
737
|
+
if (options.secret && (options.validateSignature ?? true)) {
|
|
738
|
+
if (!await verifyWebhookSignature(payloadString, signature, options.secret)) throw new PartnerApiError({
|
|
739
|
+
kind: "auth",
|
|
740
|
+
statusCode: 401,
|
|
741
|
+
message: "Invalid webhook signature"
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
const parsed = typeof payload === "string" ? JSON.parse(payload) : payload;
|
|
745
|
+
return partnerWebhookEnvelopeSchema.parse(parsed);
|
|
746
|
+
}
|
|
747
|
+
function readWebhookHeaders(headers) {
|
|
748
|
+
if (!headers) return {
|
|
749
|
+
event: null,
|
|
750
|
+
delivery: null,
|
|
751
|
+
contractVersion: null,
|
|
752
|
+
timestamp: null,
|
|
753
|
+
signature: null
|
|
754
|
+
};
|
|
755
|
+
if (headers instanceof Headers) return {
|
|
756
|
+
event: headers.get(WEBHOOK_EVENT_HEADER$1),
|
|
757
|
+
delivery: headers.get(WEBHOOK_DELIVERY_HEADER$1),
|
|
758
|
+
contractVersion: headers.get(WEBHOOK_CONTRACT_VERSION_HEADER$1),
|
|
759
|
+
timestamp: headers.get(WEBHOOK_TIMESTAMP_HEADER$1),
|
|
760
|
+
signature: headers.get(WEBHOOK_SIGNATURE_HEADER$1)
|
|
761
|
+
};
|
|
762
|
+
return {
|
|
763
|
+
event: headers.event ?? null,
|
|
764
|
+
delivery: headers.delivery ?? null,
|
|
765
|
+
contractVersion: headers.contractVersion ?? null,
|
|
766
|
+
timestamp: headers.timestamp ?? null,
|
|
767
|
+
signature: headers.signature ?? null
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
export { CANONICAL_WEBHOOK_EVENTS, PartnerApiError, VALID_WEBHOOK_EVENTS, WEBHOOK_CONTRACT_VERSION, WEBHOOK_CONTRACT_VERSION_HEADER, WEBHOOK_DELIVERY_HEADER, WEBHOOK_EVENT_HEADER, WEBHOOK_SIGNATURE_HEADER, WEBHOOK_TIMESTAMP_HEADER, analyticsStatusToSyncStatus, createAnalyticsClient, createGscdumpClient, createGscdumpRealtimeClient, createPartnerClient, createPartnerRealtimeClient, findLifecycleSite, lifecycleSiteToSyncStatus, lifecycleSiteToUserSite, parseWebhookPayload, readWebhookHeaders, serializeWebhookPayload, toPartnerError, verifyWebhookSignature };
|
package/dist/query.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "gscdump/query";
|
package/dist/query.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gscdump/sdk",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.12.0",
|
|
5
5
|
"description": "Consumer SDK for hosted gscdump.com integrations.",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Harlan Wilton",
|
|
@@ -25,6 +25,11 @@
|
|
|
25
25
|
"types": "./dist/index.d.mts",
|
|
26
26
|
"import": "./dist/index.mjs",
|
|
27
27
|
"default": "./dist/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./query": {
|
|
30
|
+
"types": "./dist/query.d.mts",
|
|
31
|
+
"import": "./dist/query.mjs",
|
|
32
|
+
"default": "./dist/query.mjs"
|
|
28
33
|
}
|
|
29
34
|
},
|
|
30
35
|
"main": "./dist/index.mjs",
|
|
@@ -35,10 +40,18 @@
|
|
|
35
40
|
"engines": {
|
|
36
41
|
"node": ">=18"
|
|
37
42
|
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"gscdump": "0.12.0"
|
|
45
|
+
},
|
|
46
|
+
"peerDependenciesMeta": {
|
|
47
|
+
"gscdump": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
},
|
|
38
51
|
"dependencies": {
|
|
39
52
|
"ofetch": "^1.5.1",
|
|
40
53
|
"zod": "^4.4.3",
|
|
41
|
-
"@gscdump/contracts": "0.
|
|
54
|
+
"@gscdump/contracts": "0.12.0"
|
|
42
55
|
},
|
|
43
56
|
"devDependencies": {
|
|
44
57
|
"typescript": "^6.0.3",
|