-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
92 lines (83 loc) · 2.3 KB
/
index.js
File metadata and controls
92 lines (83 loc) · 2.3 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
/**
* 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";
async function _fetch(path, options = {}) {
const url = `${BASE_URL}${path}`;
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 }),
});
}
/**
* 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, reportUrl };