semrush-api-client 0.0.6 → 0.1.1
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/README.md +1 -4
- package/dist/backlinks/backlinksOverview.d.ts +9 -0
- package/dist/backlinks/backlinksOverview.js +27 -0
- package/dist/client/index.d.ts +17 -0
- package/dist/client/index.js +35 -4
- package/dist/domain/getDomainRank.d.ts +10 -0
- package/dist/domain/getDomainRank.js +3 -2
- package/dist/index.d.ts +1 -0
- package/dist/keyword/getPhrase.d.ts +10 -0
- package/dist/keyword/getPhrase.js +3 -2
- package/dist/type/general/database.d.ts +1 -0
- package/dist/type/general/exportColumns.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
# semrush-api-client
|
|
2
|
-
|
|
3
|
-
[](https://github.com/RenKoya1/semrush-api-client/releases)
|
|
4
|
-
|
|
5
|
-
[](https://bundlephobia.com/package/semrush-api-client)
|
|
2
|
+
[](https://github.com//RenKoya1/semrush-api-client)
|
|
6
3
|
|
|
7
4
|
A Node.js/TypeScript client for accessing the SEMrush API easily.
|
|
8
5
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SemrushAPIClient } from "../client";
|
|
2
|
+
export type BacklinksTargetType = "root_domain" | "domain" | "url";
|
|
3
|
+
export type BacklinkstExportColumns = "ascore" | "total" | "domains_num" | "urls_num" | "ips_num" | "ipclassc_num" | "follows_num" | "nofollows_num" | "sponsored_num" | "ugc_num" | "texts_num" | "images_num" | "forms_num" | "frames_num";
|
|
4
|
+
export declare function backlinksOverview(this: SemrushAPIClient, { target, target_type, export_columns, outputObj, }: {
|
|
5
|
+
target: string;
|
|
6
|
+
target_type?: BacklinksTargetType;
|
|
7
|
+
export_columns?: BacklinkstExportColumns[];
|
|
8
|
+
outputObj?: boolean;
|
|
9
|
+
}): Promise<string>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.backlinksOverview = backlinksOverview;
|
|
4
|
+
async function backlinksOverview({ target, target_type = "domain", export_columns = [
|
|
5
|
+
"ascore",
|
|
6
|
+
"total",
|
|
7
|
+
"domains_num",
|
|
8
|
+
"urls_num",
|
|
9
|
+
"ips_num",
|
|
10
|
+
"ipclassc_num",
|
|
11
|
+
"follows_num",
|
|
12
|
+
"nofollows_num",
|
|
13
|
+
"sponsored_num",
|
|
14
|
+
"ugc_num",
|
|
15
|
+
"texts_num",
|
|
16
|
+
"images_num",
|
|
17
|
+
"forms_num",
|
|
18
|
+
"frames_num",
|
|
19
|
+
], outputObj = true, }) {
|
|
20
|
+
const params = {
|
|
21
|
+
type: "backlinks_overview",
|
|
22
|
+
target,
|
|
23
|
+
target_type,
|
|
24
|
+
export_columns: export_columns.join(","),
|
|
25
|
+
};
|
|
26
|
+
return this.get(this.ANALYTICS_URL, params, outputObj);
|
|
27
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getDomainRank } from "../domain/getDomainRank";
|
|
2
|
+
import { getPhraseAll } from "../keyword/getPhrase";
|
|
3
|
+
import { backlinksOverview } from "../backlinks/backlinksOverview";
|
|
4
|
+
export declare class SemrushAPIClient {
|
|
5
|
+
private client;
|
|
6
|
+
private api_key;
|
|
7
|
+
BASE_URL: string;
|
|
8
|
+
ANALYTICS_URL: string;
|
|
9
|
+
constructor({ api_key }: {
|
|
10
|
+
api_key: string;
|
|
11
|
+
});
|
|
12
|
+
parseCsvToObjects(csvString: string, delimiter?: string): Record<string, string>[];
|
|
13
|
+
get<T>(url: string, params?: Record<string, any>, outputObj?: boolean): Promise<T>;
|
|
14
|
+
getDomainRank: typeof getDomainRank;
|
|
15
|
+
getPhrase: typeof getPhraseAll;
|
|
16
|
+
backlinksOverview: typeof backlinksOverview;
|
|
17
|
+
}
|
package/dist/client/index.js
CHANGED
|
@@ -7,23 +7,54 @@ exports.SemrushAPIClient = void 0;
|
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
8
|
const getDomainRank_1 = require("../domain/getDomainRank");
|
|
9
9
|
const getPhrase_1 = require("../keyword/getPhrase");
|
|
10
|
+
const backlinksOverview_1 = require("../backlinks/backlinksOverview");
|
|
10
11
|
class SemrushAPIClient {
|
|
11
12
|
client;
|
|
12
13
|
api_key;
|
|
13
14
|
BASE_URL = "https://api.semrush.com/";
|
|
15
|
+
ANALYTICS_URL = "https://api.semrush.com/analytics/v1/";
|
|
14
16
|
constructor({ api_key }) {
|
|
15
17
|
this.api_key = api_key;
|
|
16
18
|
this.client = axios_1.default.create({
|
|
17
|
-
baseURL: this.BASE_URL,
|
|
18
19
|
timeout: 5000,
|
|
19
20
|
});
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
+
parseCsvToObjects(csvString, delimiter = ";") {
|
|
23
|
+
const [headerLine, ...lines] = csvString.trim().split("\n");
|
|
24
|
+
const headers = headerLine
|
|
25
|
+
.split(delimiter)
|
|
26
|
+
.map((h) => h.trim().toLowerCase().replace(/\s+/g, "_"));
|
|
27
|
+
return lines.map((line) => {
|
|
28
|
+
const values = line.split(delimiter);
|
|
29
|
+
const obj = {};
|
|
30
|
+
headers.forEach((key, i) => {
|
|
31
|
+
obj[key] = values[i]?.trim() ?? "";
|
|
32
|
+
});
|
|
33
|
+
return obj;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async get(url, params, outputObj = true) {
|
|
22
37
|
const allParams = { ...params, key: this.api_key };
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
try {
|
|
39
|
+
const response = await this.client.get(url, { params: allParams });
|
|
40
|
+
if (outputObj) {
|
|
41
|
+
return this.parseCsvToObjects(response.data);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
return response.data;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (axios_1.default.isAxiosError(error)) {
|
|
49
|
+
throw new Error(`API request failed: ${error.message}`);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
throw new Error(`Unexpected error: ${error}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
25
55
|
}
|
|
26
56
|
getDomainRank = getDomainRank_1.getDomainRank;
|
|
27
57
|
getPhrase = getPhrase_1.getPhraseAll;
|
|
58
|
+
backlinksOverview = backlinksOverview_1.backlinksOverview;
|
|
28
59
|
}
|
|
29
60
|
exports.SemrushAPIClient = SemrushAPIClient;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SemrushAPIClient } from "../client";
|
|
2
|
+
import { Database } from "../type/general/database";
|
|
3
|
+
import { ExportColumns } from "../type/general/exportColumns";
|
|
4
|
+
export declare function getDomainRank(this: SemrushAPIClient, { domain, export_columns, database, display_limit, outputObj, }: {
|
|
5
|
+
domain: string;
|
|
6
|
+
export_columns?: ExportColumns[];
|
|
7
|
+
database?: Database;
|
|
8
|
+
display_limit?: number;
|
|
9
|
+
outputObj?: boolean;
|
|
10
|
+
}): Promise<string>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getDomainRank = getDomainRank;
|
|
4
|
-
async function getDomainRank({ domain, export_columns = ["Dn", "Or", "Rk", "Ot", "Oc"], database = "us", display_limit = 1000,
|
|
4
|
+
async function getDomainRank({ domain, export_columns = ["Dn", "Or", "Rk", "Ot", "Oc"], database = "us", display_limit = 1000, outputObj = true, // Default to true for output object
|
|
5
|
+
}) {
|
|
5
6
|
const params = {
|
|
6
7
|
type: "domain_ranks",
|
|
7
8
|
export_columns: export_columns.join(","),
|
|
@@ -9,5 +10,5 @@ async function getDomainRank({ domain, export_columns = ["Dn", "Or", "Rk", "Ot",
|
|
|
9
10
|
database,
|
|
10
11
|
display_limit,
|
|
11
12
|
};
|
|
12
|
-
return this.get(
|
|
13
|
+
return this.get(this.BASE_URL, params, outputObj);
|
|
13
14
|
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./client";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { SemrushAPIClient } from "../client";
|
|
2
|
+
import { Database } from "../type/general/database";
|
|
3
|
+
type KeywordExportColumns = "Dt" | "Db" | "Ph" | "Nq" | "Cp" | "Co" | "Nr" | "In" | "Kd";
|
|
4
|
+
export declare function getPhraseAll(this: SemrushAPIClient, { phrase, export_columns, database, outputObj, }: {
|
|
5
|
+
phrase: string;
|
|
6
|
+
export_columns?: KeywordExportColumns[];
|
|
7
|
+
database?: Database;
|
|
8
|
+
outputObj?: boolean;
|
|
9
|
+
}): Promise<string>;
|
|
10
|
+
export {};
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPhraseAll = getPhraseAll;
|
|
4
|
-
async function getPhraseAll({ phrase, export_columns = ["Dt", "Db", "Ph", "Nq", "Cp", "Co", "Nr"], database = "us",
|
|
4
|
+
async function getPhraseAll({ phrase, export_columns = ["Dt", "Db", "Ph", "Nq", "Cp", "Co", "Nr"], database = "us", outputObj = true, // Default to true for output object
|
|
5
|
+
}) {
|
|
5
6
|
const params = {
|
|
6
7
|
type: "phrase_all",
|
|
7
8
|
export_columns: export_columns.join(","),
|
|
8
9
|
phrase,
|
|
9
10
|
database,
|
|
10
11
|
};
|
|
11
|
-
return this.get(
|
|
12
|
+
return this.get(this.BASE_URL, params, outputObj);
|
|
12
13
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Database = "us" | "uk" | "ca" | "ru" | "de" | "fr" | "es" | "it" | "br" | "au" | "ar" | "be" | "ch" | "dk" | "fi" | "hk" | "ie" | "il" | "mx" | "nl" | "no" | "pl" | "se" | "sg" | "tr" | "jp" | "in" | "hu" | "af" | "al" | "dz" | "ao" | "am" | "at" | "az" | "bh" | "bd" | "by" | "bz" | "bo" | "ba" | "bw" | "bn" | "bg" | "cv" | "kh" | "cm" | "cl" | "co" | "cr" | "hr" | "cy" | "cz" | "cd" | "do" | "ec" | "eg" | "sv" | "ee" | "et" | "ge" | "gh" | "gr" | "gt" | "gy" | "ht" | "hn" | "is" | "id" | "jm" | "jo" | "kz" | "kw" | "lv" | "lb" | "lt" | "lu" | "mg" | "my" | "mt" | "mu" | "md" | "mn" | "me" | "ma" | "mz" | "na" | "np" | "nz" | "ni" | "ng" | "om" | "py" | "pe" | "ph" | "pt" | "ro" | "sa" | "sn" | "rs" | "sk" | "si" | "za" | "kr" | "lk" | "th" | "bs" | "tt" | "tn" | "ua" | "ae" | "uy" | "ve" | "vn" | "zm" | "zw" | "ly" | "mobile-us" | "mobile-uk" | "mobile-ca" | "mobile-de" | "mobile-fr" | "mobile-es" | "mobile-it" | "mobile-br" | "mobile-au" | "mobile-dk" | "mobile-mx" | "mobile-nl" | "mobile-se" | "mobile-tr" | "mobile-in" | "mobile-id" | "mobile-il" | "il-ext" | "tr-ext" | "dk-ext" | "no-ext" | "se-ext" | "fi-ext" | "ch-ext" | "mobile-il-ext" | "pa" | "pk" | "tw" | "qa";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ExportColumns = "Db" | "Dt" | "Dn" | "Rk" | "Or" | "Ot" | "Oc" | "Ad" | "At" | "Ac" | "Sh" | "Sv" | "FKn" | "FPn" | "Sr" | "Srb" | "St" | "Stb" | "Sc" | "Srn" | "Srl";
|