-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.ts
193 lines (171 loc) · 6.35 KB
/
mod.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { debugLog } from "./deps.ts";
import type {
ClientOptions,
GoogleAPITokenResponse,
GoogleAPIWebStoreItem,
GoogleAPIWebStorePublishResponse,
PublishTarget,
} from "./types.ts";
const rootHost = "www.googleapis.com" as const;
const rootURI = `https://${rootHost}` as const;
const refreshTokenURI = `${rootURI}/oauth2/v4/token` as const;
const addURI = `${rootURI}/upload/chromewebstore/v1.1/items` as const;
const uploadURI = (extensionId: string): string =>
`${rootURI}/upload/chromewebstore/v1.1/items/${extensionId}`;
const publishURI = (extensionId: string, target: PublishTarget): string => (
`${rootURI}/chromewebstore/v1.1/items/${extensionId}/publish?publishTarget=${target}`
);
const checkStatusURI = (extensionId: string): string =>
`${rootURI}/chromewebstore/v1.1/items/${extensionId}?projection=draft`;
export type APIClientOptions = ClientOptions;
/**
* API client for interacting with the Chrome Web Store.
*/
export class APIClient {
private readonly extensionId: string;
private readonly clientId: string;
private readonly clientSecret: string;
private readonly refreshToken: string;
constructor(options: APIClientOptions) {
if (!options.id) throw new Error("'id' is missing!");
if (!options.clientId) throw new Error("'clientId' is missing!");
if (!options.clientSecret) throw new Error("'clientSecret' is missing!");
if (!options.refreshToken) throw new Error("'refreshToken' is missing!");
this.extensionId = options.id;
this.clientId = options.clientId;
this.clientSecret = options.clientSecret;
this.refreshToken = options.refreshToken;
}
/**
* Get an OAuth2 access token.
* @returns {GoogleAPITokenResponse['access_token']} Access token.
*/
public async fetchToken(): Promise<GoogleAPITokenResponse["access_token"]> {
const { clientId, clientSecret, refreshToken } = this;
const debug = debugLog("cwa:token");
debug("fetching refresh token from google");
const request = await fetch(refreshTokenURI, {
method: "POST",
body: JSON.stringify({
client_id: clientId,
client_secret: clientSecret,
refresh_token: refreshToken,
grant_type: "refresh_token",
}),
});
debug("got refresh token from google");
const response = await request
.json() as GoogleAPITokenResponse;
debug("refresh token response %j", response);
return response.access_token;
}
/**
* Upload a new package to the store.
* @param {ReadableStream} readStream Readable stream of the file object to upload.
* @param {Promise<string>} token Access token.
* @returns {GoogleAPIWebStoreItem} Item upload state.
*/
public async uploadNew(
readStream: ReadableStream,
token: Promise<string> = this.fetchToken(),
): Promise<GoogleAPIWebStoreItem> {
if (!readStream) {
throw new Error("Nothing to upload!");
}
const debug = debugLog("cwa:upload");
debug("uploading new package to chrome store");
const request = await fetch(addURI, {
method: "POST",
headers: this.buildHeaders(await token),
body: readStream,
});
debug("finished uploading new package to chrome store");
const response = await request.json() as GoogleAPIWebStoreItem;
debug("upload response %j", response);
return response;
}
/**
* Upload a package to update an existing store item.
* @param {ReadableStream<Uint8Array>} readStream Readable stream of the file object to upload.
* @param {Promise<string>} token Access token.
* @returns {GoogleAPIWebStoreItem} Item upload state.
*/
public async uploadExisting(
readStream: ReadableStream<Uint8Array>,
token: Promise<string> = this.fetchToken(),
): Promise<GoogleAPIWebStoreItem> {
if (!readStream) {
throw new Error("Nothing to upload!");
}
const { extensionId } = this;
const debug = debugLog("cwa:upload");
debug("uploading existing package to chrome store");
const request = await fetch(uploadURI(extensionId), {
method: "PUT",
headers: this.buildHeaders(await token),
body: readStream,
});
debug("got uploading response from chrome store");
const response = await request.json() as GoogleAPIWebStoreItem;
debug("upload response %j", response);
return response;
}
/**
* Publish a given extension ID on the Chrome Web store.
* @param {PublishTarget} target Push to everyone or only to trusted testers.
* @param {Promise<string>} token Access token.
* @returns {GoogleAPIWebStorePublishResponse} Publish result.
*/
public async publish(
target: PublishTarget = "default",
token: Promise<string> = this.fetchToken(),
): Promise<GoogleAPIWebStorePublishResponse> {
const { extensionId } = this;
const debug = debugLog("cwa:publish");
debug("publishing package on chrome store");
const request = await fetch(publishURI(extensionId, target), {
method: "POST",
headers: this.buildHeaders(await token),
});
debug("done publishing package on chrome store");
const response = await request.json() as GoogleAPIWebStorePublishResponse;
debug("publish response %j", response);
return response;
}
/**
* Check the upload status of an item.
* @param {Promise<string>} token Access token.
* @returns {GoogleAPIWebStoreItem} Item upload state.
*/
public async checkUploadStatus(
token: Promise<string> = this.fetchToken(),
): Promise<GoogleAPIWebStoreItem> {
const { extensionId } = this;
const debug = debugLog("cwa:check");
debug("checking package upload status on chrome store");
const request = await fetch(checkStatusURI(extensionId), {
method: "GET",
headers: this.buildHeaders(await token),
});
debug("done checking package upload status on chrome store");
const response = await request.json() as GoogleAPIWebStoreItem;
debug("check upload status response %j", response);
return response;
}
/**
* Build authorization headers for Google APIs.
* @param {string} token Access token.
* @returns {Record<string, string>} Headers as object.
*/
private buildHeaders(token: string): Record<string, string> {
return {
Authorization: `Bearer ${token}`,
"x-goog-api-version": "2",
};
}
}
/**
* API client for interacting with the Chrome Web Store.
*/
const apiClient = (options: APIClientOptions) => new APIClient(options);
export default apiClient;