-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (128 loc) · 4.06 KB
/
index.js
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
149
150
151
152
153
154
155
const https = require('https');
const axios = require('axios');
class LemonSqueezy {
static defaultOptions () {
return {
apiKey: '',
storeId: '',
apiUrl: 'https://api.lemonsqueezy.com/v1/',
insecure: false,
}
}
constructor (api, options) {
this.options = options;
if (!options.apiKey) {
throw new Error(`Missing apiKey option.`);
}
if (!options.storeId) {
throw new Error(`Missing storeId option.`);
}
let axiosConfig = {
baseURL: options.apiUrl,
headers: {
'Accept': 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json',
'Authorization': `Bearer ${options.apiKey}`,
}
};
if (options.insecure) {
axiosConfig.httpsAgent = new https.Agent({ rejectUnauthorized: false });
}
this.client = axios.create(axiosConfig);
api.loadSource(async actions => {
this.store = actions;
console.log(`Loading data from ${options.apiUrl}`);
await this.getStore(actions);
await this.getPages(actions);
await this.getProducts(actions);
});
api.createManagedPages(async actions => {
await this.createPages(actions);
})
}
async getStore(actions) {
const res = await this.fetch(`stores/${this.options.storeId}`);
actions.addMetadata('store', {
...res.data.attributes,
id: res.data.id,
});
}
async getProducts(actions) {
const res = await this.fetchPaged(`stores/${this.options.storeId}/products`);
const products = actions.addCollection('Products');
res.forEach(product => {
products.addNode({
...product.attributes,
id: product.id,
});
});
}
async getPages(actions) {
const res = await this.fetchPaged(`stores/${this.options.storeId}/pages`);
const pages = actions.addCollection('Pages');
res.forEach(page => {
pages.addNode({
...page.attributes,
id: page.id,
});
});
}
async createPages(actions) {
const res = await this.fetchPaged(`stores/${this.options.storeId}/pages`);
res.forEach(page => {
const slug = page.attributes.is_homepage ? '' : page.attributes.slug;
actions.createPage({
path: `/${slug}`,
component: `./src/templates/Page.vue`,
context: {
...page.attributes,
id: page.id,
}
});
});
}
async fetch(url, config = {}) {
let res;
try {
res = await this.client.request({
url: url,
...config
});
} catch ({ response, code, config }) {
if (!response && code) {
throw new Error(`${code} - ${config.url}`);
}
throw new Error(`${response.status} - ${config.url}`);
}
return res.data;
}
async fetchPaged(url) {
let res;
let results = [];
try {
res = await this.fetch(url, {
params: { 'page[number]': 1, 'page[size]': 100 }
});
results.push(...res.data);
} catch (err) {
console.error(err.message);
return;
}
const totalPages = parseInt(res.meta.page.lastPage, 10);
if (totalPages <= 1) {
return results;
}
for (let page = 2; page <= totalPages; page++) {
try {
res = await this.fetch(url, {
params: { 'page[number]': page, 'page[size]': 100 }
});
results.push(...res.data);
} catch (err) {
console.error(err.message);
}
}
return results;
}
}
module.exports = LemonSqueezy