Skip to content

Commit 73879e0

Browse files
committedJul 10, 2024·
scripts: remove fetch
1 parent 1be3456 commit 73879e0

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed
 

‎scripts/get-snapshot-version.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from 'axios';
2-
import { readFile, mkdir, writeFile } from 'fs/promises';
2+
import { mkdir, writeFile } from 'fs/promises';
33
import { join, dirname } from 'path';
44
import { fileURLToPath } from 'url';
55

‎scripts/pack-utils/cws.mjs

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { createReadStream } from 'fs';
2-
import fetch from 'node-fetch';
1+
import axios from 'axios';
2+
import { readFile } from 'fs/promises';
3+
import { Blob } from 'buffer';
34
import { extension } from '../config.mjs';
45

56
const webStoreId = process.env.CWS_CLIENT_ID;
@@ -11,19 +12,18 @@ async function getToken() {
1112
if (_webStoreToken) {
1213
return _webStoreToken;
1314
}
14-
const resp = await fetch('https://www.googleapis.com/oauth2/v4/token', {
15-
method: 'POST',
15+
const post = new URLSearchParams({
16+
client_id: webStoreId,
17+
client_secret: webStoreSecret,
18+
refresh_token: webStoreToken,
19+
grant_type: 'refresh_token',
20+
});
21+
const resp = await axios.post('https://www.googleapis.com/oauth2/v4/token', post.toString(), {
1622
headers: {
1723
'Content-Type': 'application/x-www-form-urlencoded',
1824
},
19-
body: new URLSearchParams({
20-
client_id: webStoreId,
21-
client_secret: webStoreSecret,
22-
refresh_token: webStoreToken,
23-
grant_type: 'refresh_token',
24-
}).toString(),
2525
});
26-
const res = await resp.json();
26+
const res = resp.data;
2727
if (res.access_token) {
2828
_webStoreToken = res.access_token;
2929
return _webStoreToken;
@@ -32,30 +32,28 @@ async function getToken() {
3232
}
3333
}
3434

35-
async function upload(readStream, token) {
36-
const res = await fetch(`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extension.chrome.id}`, {
37-
method: 'PUT',
35+
async function upload(content, token) {
36+
const blob = new Blob(content);
37+
const res = await axios.put(`https://www.googleapis.com/upload/chromewebstore/v1.1/items/${extension.chrome.id}`, blob, {
3838
headers: {
3939
Authorization: `Bearer ${token}`,
4040
'x-goog-api-version': '2',
4141
},
42-
body: readStream,
4342
});
4443

45-
return res.json();
44+
return res.data;
4645
}
4746

4847
async function publish(target = 'default', token) {
4948
const url = `https://www.googleapis.com/chromewebstore/v1.1/items/${extension.chrome.id}/publish?publishTarget=${target}`;
50-
const res = await fetch(url, {
51-
method: 'POST',
49+
const res = await axios.post(url, '', {
5250
headers: {
5351
Authorization: `Bearer ${token}`,
5452
'x-goog-api-version': '2',
5553
},
5654
});
5755

58-
return res.json();
56+
return res.data;
5957
}
6058

6159
async function packCws(zipPath) {
@@ -69,9 +67,9 @@ async function packCws(zipPath) {
6967
return Promise.reject(new Error('CWS_TOKEN not found'));
7068
}
7169

72-
const distStream = createReadStream(zipPath);
70+
const distContent = await readFile(zipPath);
7371
const token = await getToken();
74-
await upload(distStream, token);
72+
await upload(distContent, token);
7573
return publish('default', token);
7674
}
7775

0 commit comments

Comments
 (0)
Please sign in to comment.