-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (127 loc) · 3.65 KB
/
index.js
File metadata and controls
148 lines (127 loc) · 3.65 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const fetch = require("node-fetch");
const sha256 = require("js-sha256");
const defaultDomain = "https://api.generatebanners.com";
function signWithHmac(toSign, secret) {
const hmac = sha256.hmac(secret, toSign);
return toSign + `&hmac=${hmac}`;
}
function encodeVariables(variables = {}) {
return Object.keys(variables).map(
(key) => encodeURIComponent(key) + "=" + encodeURIComponent(variables[key])
).join`&`;
}
function checkStatus(res) {
if (res.ok) {
// res.status >= 200 && res.status < 300
return res;
} else {
throw new Error(res.statusText);
}
}
class Template {
constructor({ apiKey, apiSecret, domain = defaultDomain }) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.domain = domain;
}
async browse({ page, limit }) {
return await fetch(
`${this.domain}/api/v1/${this.apiKey}/template?page=${page}&limit=${limit}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${this.apiSecret}`,
"Content-Type": "application/json",
},
body: JSON.stringify(template),
}
)
.then(checkStatus)
.then((r) => r.json());
}
async add({ template }) {
return await fetch(`${this.domain}/api/v1/${this.apiKey}/template`, {
method: "POST",
headers: {
Authorization: `Bearer ${this.apiSecret}`,
"Content-Type": "application/json",
},
body: JSON.stringify(template),
})
.then(checkStatus)
.then((r) => r.json());
}
async delete({ templateId }) {
if (!templateId) {
throw new Error(`The value 'templateId' can't be null`);
}
return await fetch(
`${this.domain}/api/v1/${this.apiKey}/template/${templateId}`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${this.apiSecret}`,
"Content-Type": "application/json",
},
}
).then(checkStatus);
}
}
class Image {
constructor({ apiKey, apiSecret, domain = defaultDomain }) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.domain = domain;
}
url({ templateId, variables, version = "v1" }) {
return `/api/${version}/${
this.apiKey
}/template/${templateId}/render?${encodeVariables(variables)}`;
}
signedUrl({ templateId, variables, version = "v1" }) {
const url = this.url({ templateId, variables, version });
return this.domain + signWithHmac(url, this.apiSecret);
}
}
class Screenshot {
constructor({ apiKey, apiSecret, domain = defaultDomain }) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.domain = domain;
}
url({ variables, version = "v1" }) {
return `/api/${version}/${this.apiKey}/screenshot/render?${encodeVariables(
variables
)}`;
}
signedUrl({ variables, version = "v1" }) {
const url = this.url({ variables, version });
return this.domain + signWithHmac(url, this.apiSecret);
}
}
class Utils {
constructor({ apiKey, apiSecret, domain = defaultDomain }) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.domain = domain;
}
async download(url) {
return await fetch(url, {
method: "GET",
})
.then(checkStatus)
.then((r) => r.buffer());
}
}
class GenerateBannersSDK {
constructor({ apiKey, apiSecret, domain = defaultDomain }) {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.domain = domain;
this.template = new Template({ apiKey, apiSecret, domain });
this.image = new Image({ apiKey, apiSecret, domain });
this.screenshot = new Screenshot({ apiKey, apiSecret, domain });
this.utils = new Utils({ apiKey, apiSecret, domain });
}
}
module.exports = GenerateBannersSDK;