-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (113 loc) · 3.42 KB
/
index.js
File metadata and controls
124 lines (113 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* SEO Score API - Node.js Client
* Audit any URL for SEO issues with one function call.
* https://seoscoreapi.com
*/
const BASE_URL = "https://seoscoreapi.com";
const VERSION = "1.1.0";
const UA = `seoscoreapi-node/${VERSION}`;
async function _fetch(path, options = {}) {
const url = `${BASE_URL}${path}`;
options.headers = { "User-Agent": UA, ...options.headers };
const res = await fetch(url, options);
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || `HTTP ${res.status}`);
}
return res.json();
}
/**
* Sign up for a free API key.
* @param {string} email
* @returns {Promise<string>} The API key (save it — shown only once)
*/
async function signup(email) {
const data = await _fetch("/signup", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
return data.api_key;
}
/**
* Run an SEO audit on a URL.
* @param {string} url - URL to audit
* @param {string} apiKey - Your API key
* @returns {Promise<Object>} Audit result with score, grade, checks, priorities
*/
async function audit(url, apiKey) {
return _fetch(`/audit?url=${encodeURIComponent(url)}`, {
headers: { "X-API-Key": apiKey },
});
}
/**
* Audit multiple URLs (paid plans only).
* @param {string[]} urls
* @param {string} apiKey
* @returns {Promise<Object>}
*/
async function batchAudit(urls, apiKey) {
return _fetch("/audit/batch", {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": apiKey },
body: JSON.stringify({ urls }),
});
}
/**
* Check your API usage and limits.
* @param {string} apiKey
* @returns {Promise<Object>}
*/
async function usage(apiKey) {
return _fetch("/usage", { headers: { "X-API-Key": apiKey } });
}
/**
* Set up score monitoring for a URL (paid plans only).
* @param {string} url
* @param {string} apiKey
* @param {string} [frequency="daily"]
* @returns {Promise<Object>}
*/
async function addMonitor(url, apiKey, frequency = "daily") {
return _fetch("/monitors", {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": apiKey },
body: JSON.stringify({ url, frequency }),
});
}
/**
* Opt in or out of the public SEO scoreboard.
* @param {string} apiKey
* @param {boolean} [optOut=true] - true to hide, false to show
* @returns {Promise<Object>}
*/
async function scoreboardOptOut(apiKey, optOut = true) {
return _fetch(`/scoreboard/opt-out?opt_out=${optOut}`, {
method: "PUT",
headers: { "X-API-Key": apiKey },
});
}
/**
* Run a head-to-head competitive audit (Pro plan or higher).
* @param {string} url - Your page URL
* @param {string} competitorUrl - Competitor's page URL
* @param {string} keyword - Target keyword to compare on
* @param {string} apiKey - Your API key
* @returns {Promise<Object>} Gap score, per-check diffs, and action items
*/
async function competitiveAudit(url, competitorUrl, keyword, apiKey) {
return _fetch("/audit/competitive", {
method: "POST",
headers: { "Content-Type": "application/json", "X-API-Key": apiKey },
body: JSON.stringify({ url, competitor_url: competitorUrl, keyword }),
});
}
/**
* Get shareable report URL for a domain.
* @param {string} domain
* @returns {string}
*/
function reportUrl(domain) {
return `${BASE_URL}/report/${domain}`;
}
module.exports = { signup, audit, batchAudit, usage, addMonitor, scoreboardOptOut, competitiveAudit, reportUrl };