-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
131 lines (100 loc) · 3.55 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
// API documentation:
// https://developer.chrome.com/docs/webstore/api
// https://developer.chrome.com/docs/webstore/using-api
const rootURI = 'https://www.googleapis.com';
export const refreshTokenURI = 'https://www.googleapis.com/oauth2/v4/token';
const uploadExistingURI = id =>
`${rootURI}/upload/chromewebstore/v1.1/items/${id}`;
const publishURI = (id, target) =>
`${rootURI}/chromewebstore/v1.1/items/${id}/publish?publishTarget=${target}`;
const getURI = (id, projection) =>
`${rootURI}/chromewebstore/v1.1/items/${id}?projection=${projection}`;
const requiredFields = ['extensionId', 'clientId', 'refreshToken'];
function throwIfNotOk(request, response) {
if (!request.ok) {
const error = new Error(request.statusText ?? 'Unknown error');
error.response = response;
throw error;
}
}
class APIClient {
constructor(options) {
if (typeof fetch !== 'function') {
throw new TypeError('`chrome-webstore-upload` requires Node.js 18.0 or newer because it relies on the global `fetch` function.');
}
for (const field of requiredFields) {
if (!options[field]) {
throw new Error(`Option "${field}" is required`);
}
this[field] = options[field];
}
if ('clientSecret' in options) {
this.clientSecret = options.clientSecret;
}
}
async uploadExisting(readStream, token = this.fetchToken()) {
if (!readStream) {
throw new Error('Read stream missing');
}
const { extensionId } = this;
const request = await fetch(uploadExistingURI(extensionId), {
method: 'PUT',
headers: this._headers(await token),
duplex: 'half',
body: readStream,
});
const response = await request.json();
throwIfNotOk(request, response);
return response;
}
async publish(target = 'default', token = this.fetchToken()) {
const { extensionId } = this;
const request = await fetch(publishURI(extensionId, target), {
method: 'POST',
headers: this._headers(await token),
});
const response = await request.json();
throwIfNotOk(request, response);
return response;
}
async get(projection = 'DRAFT', token = this.fetchToken()) {
const { extensionId } = this;
const request = await fetch(getURI(extensionId, projection), {
method: 'GET',
headers: this._headers(await token),
});
const response = await request.json();
throwIfNotOk(request, response);
return response;
}
async fetchToken() {
const { clientId, clientSecret, refreshToken } = this;
const json = {
client_id: clientId,
refresh_token: refreshToken,
grant_type: 'refresh_token',
};
if (clientSecret) {
json.client_secret = clientSecret;
}
const request = await fetch(refreshTokenURI, {
method: 'POST',
body: JSON.stringify(json),
headers: {
'Content-Type': 'application/json',
},
});
await throwIfNotOk(request);
const response = await request.json();
return response.access_token;
}
_headers(token) {
return {
Authorization: `Bearer ${token}`,
'x-goog-api-version': '2',
};
}
}
export default function chromeWebstoreUpload(...args) {
return new APIClient(...args);
}