@plasmohq/chrome-webstore-api
Advanced tools
Comparing version 2.6.2 to 2.7.0
@@ -1,97 +0,2 @@ | ||
// index.ts | ||
import { createReadStream } from "fs"; | ||
import got from "got"; | ||
var baseAPIUrl = "https://www.googleapis.com"; | ||
var refreshTokenURI = `${baseAPIUrl}/oauth2/v4/token`; | ||
var errorMap = { | ||
extId: "No extension ID provided, e.g. https://chrome.google.com/webstore/detail/EXT_ID", | ||
clientId: "No client ID provided. To get one: https://github.com/PlasmoHQ/chrome-webstore-api/blob/main/token.md", | ||
clientSecret: "No client secret provided. To get one: https://github.com/PlasmoHQ/chrome-webstore-api/blob/main/token.md", | ||
refreshToken: "No refresh token provided. To get one: https://github.com/PlasmoHQ/chrome-webstore-api/blob/main/token.md" | ||
}; | ||
var requiredFields = Object.keys(errorMap); | ||
var ChromeWebstoreAPI = class { | ||
options = {}; | ||
constructor(options) { | ||
for (const field of requiredFields) { | ||
if (!options[field]) { | ||
throw new Error(errorMap[field]); | ||
} | ||
this.options[field] = options[field]; | ||
} | ||
} | ||
get uploadEndpoint() { | ||
return `${baseAPIUrl}/upload/chromewebstore/v1.1/items/${this.options.extId}`; | ||
} | ||
getPublishEndpoint(target) { | ||
return `${baseAPIUrl}/chromewebstore/v1.1/items/${this.options.extId}/publish?publishTarget=${target}`; | ||
} | ||
getInfoEndpoint(projection) { | ||
return `${baseAPIUrl}/chromewebstore/v1.1/items/${this.options.extId}?projection=${projection}`; | ||
} | ||
async submit({ filePath = "", target = "default" }) { | ||
const accessToken = await this.getAccessToken(); | ||
const { uploadState, itemError } = await this.upload( | ||
{ | ||
readStream: createReadStream(filePath) | ||
}, | ||
accessToken | ||
); | ||
if (uploadState === "FAILURE" || uploadState === "NOT_FOUND") { | ||
throw new Error( | ||
itemError.map(({ error_detail }) => error_detail).join("\n") | ||
); | ||
} | ||
return this.publish( | ||
{ | ||
target | ||
}, | ||
accessToken | ||
); | ||
} | ||
async upload({ readStream = null }, _accessToken = "") { | ||
if (!readStream) { | ||
throw new Error("Read stream missing"); | ||
} | ||
const accessToken = _accessToken || await this.getAccessToken(); | ||
return got.put(this.uploadEndpoint, { | ||
headers: this.getHeaders(accessToken), | ||
body: readStream, | ||
throwHttpErrors: false | ||
}).json(); | ||
} | ||
async publish({ target = "default" }, _accessToken = "") { | ||
const accessToken = _accessToken || await this.getAccessToken(); | ||
return got.post(this.getPublishEndpoint(target), { | ||
headers: this.getHeaders(accessToken) | ||
}).json(); | ||
} | ||
async get({ projection = "DRAFT" }, _accessToken = "") { | ||
const accessToken = _accessToken || await this.getAccessToken(); | ||
return got.get(this.getInfoEndpoint(projection), { | ||
headers: this.getHeaders(accessToken) | ||
}).json(); | ||
} | ||
async getAccessToken() { | ||
const response = await got.post(refreshTokenURI, { | ||
json: { | ||
client_id: this.options.clientId, | ||
refresh_token: this.options.refreshToken, | ||
grant_type: "refresh_token", | ||
client_secret: this.options.clientSecret | ||
} | ||
}).json(); | ||
return response.access_token; | ||
} | ||
getHeaders(token) { | ||
return { | ||
Authorization: `Bearer ${token}`, | ||
"x-goog-api-version": "2" | ||
}; | ||
} | ||
}; | ||
export { | ||
ChromeWebstoreAPI, | ||
errorMap, | ||
requiredFields | ||
}; | ||
import{createReadStream as h}from"fs";import s from"got";var r="https://www.googleapis.com",d=`${r}/oauth2/v4/token`,a={extId:"No extension ID provided, e.g. https://chrome.google.com/webstore/detail/EXT_ID",clientId:"No client ID provided. To get one: https://github.com/PlasmoHQ/chrome-webstore-api/blob/main/token.md",clientSecret:"No client secret provided. To get one: https://github.com/PlasmoHQ/chrome-webstore-api/blob/main/token.md",refreshToken:"No refresh token provided. To get one: https://github.com/PlasmoHQ/chrome-webstore-api/blob/main/token.md"},l=Object.keys(a),i=class{options={};constructor(e){for(let t of l){if(!e[t])throw new Error(a[t]);this.options[t]=e[t]}}get uploadEndpoint(){return`${r}/upload/chromewebstore/v1.1/items/${this.options.extId}`}getPublishEndpoint(e){return`${r}/chromewebstore/v1.1/items/${this.options.extId}/publish?publishTarget=${e}`}getInfoEndpoint(e){return`${r}/chromewebstore/v1.1/items/${this.options.extId}?projection=${e}`}async submit({filePath:e="",target:t="default"}){let o=await this.getAccessToken(),{uploadState:n,itemError:c}=await this.upload({readStream:h(e)},o);if(n==="FAILURE"||n==="NOT_FOUND")throw new Error(c.map(({error_detail:p})=>p).join(` | ||
`));return this.publish({target:t},o)}async upload({readStream:e=null},t=""){if(!e)throw new Error("Read stream missing");let o=t||await this.getAccessToken();return s.put(this.uploadEndpoint,{headers:this.getHeaders(o),body:e,throwHttpErrors:!1}).json()}async publish({target:e="default"},t=""){let o=t||await this.getAccessToken();return s.post(this.getPublishEndpoint(e),{headers:this.getHeaders(o)}).json()}async get({projection:e="DRAFT"},t=""){let o=t||await this.getAccessToken();return s.get(this.getInfoEndpoint(e),{headers:this.getHeaders(o)}).json()}async getAccessToken(){return(await s.post(d,{json:{client_id:this.options.clientId,refresh_token:this.options.refreshToken,grant_type:"refresh_token",client_secret:this.options.clientSecret}}).json()).access_token}getHeaders(e){return{Authorization:`Bearer ${e}`,"x-goog-api-version":"2"}}};export{i as ChromeWebstoreAPI,a as errorMap,l as requiredFields}; |
{ | ||
"name": "@plasmohq/chrome-webstore-api", | ||
"version": "2.6.2", | ||
"version": "2.7.0", | ||
"description": "Chrome webstore API", | ||
@@ -27,19 +27,17 @@ "exports": "./dist/index.js", | ||
"devDependencies": { | ||
"@plasmohq/prettier-plugin-sort-imports": "3.5.1", | ||
"@plasmohq/rps": "1.5.1", | ||
"@types/node": "18.7.20", | ||
"@plasmohq/prettier-plugin-sort-imports": "3.5.4", | ||
"@plasmohq/rps": "1.5.3", | ||
"@types/node": "18.11.9", | ||
"prettier": "2.7.1", | ||
"rimraf": "3.0.2", | ||
"tsup": "6.2.3", | ||
"typescript": "4.8.3" | ||
"tsup": "6.4.0", | ||
"typescript": "4.8.4" | ||
}, | ||
"dependencies": { | ||
"got": "12.5.0" | ||
"got": "12.5.2" | ||
}, | ||
"scripts": { | ||
"dev": "tsup index.ts --format esm --watch --sourcemap inline", | ||
"build": "tsup index.ts --format esm --dts-resolve", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"clean": "rimraf dist" | ||
"build": "tsup index.ts --format esm --dts-resolve --minify --clean", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
6
8479
55
+ Addedcacheable-lookup@7.0.0(transitive)
+ Addedgot@12.5.2(transitive)
- Removedcacheable-lookup@6.1.0(transitive)
- Removedgot@12.5.0(transitive)
Updatedgot@12.5.2