-
Notifications
You must be signed in to change notification settings - Fork 0
/
gumroad.ts
81 lines (69 loc) · 1.97 KB
/
gumroad.ts
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
import axios, { AxiosInstance } from "axios";
class GumroadProduct {
[index: string]: any;
axios: AxiosInstance;
constructor(data: any, axiosInstance: AxiosInstance) {
Object.keys( data ).forEach(
( key:string ) => {
this[ key ] = data[ key ]
}
)
this.axios = axiosInstance
}
async get(){
const response = await this.axios.get(`/v2/products/${this.id}`)
return response.data.product
}
async delete(){
const response = await this.axios.delete(`/v2/products/${this.id}`)
return response.data
}
async enable(){
const response = await this.axios.put(`/v2/products/${this.id}/enable`)
return response.data.product
}
async disable(){
const response = await this.axios.put(`/v2/products/${this.id}/disable`)
return response.data.product
}
async variantCategories(data: any){
const response = await this.axios.post(`/v2/products/${this.id}/variant_categories`, data)
return response.data.product
}
}
class Gumroad {
token: string;
URL: string;
axios: AxiosInstance;
constructor(API_KEY: string){
if(!API_KEY) throw new Error('API_KEY is required for Gumroad')
const token = API_KEY
const baseURL = 'https://api.gumroad.com/'
this.token = token
this.URL = baseURL
this.axios = axios.create({
baseURL,
timeout: 1000,
headers: {
Authorization: `Bearer ${token}`
}
})
}
async products(){
const response = await this.axios.get('/v2/products')
return response.data.products.map((product: any) => new GumroadProduct(product, this.axios))
}
async sales(){
const response = await this.axios.get('/v2/sales')
return response.data.sales
}
async verifyLicense(data: any){
const response = await this.axios.post('/v2/licenses/verify', data)
return response.data
}
async enableLicense(data: any){
const response = await this.axios.post('/v2/licenses/enable', data)
return response.data
}
}
export default Gumroad