UNPKG

@plasmohq/edge-addons-api

Version:
2 lines (1 loc) 2.38 kB
import{createReadStream as c}from"fs";import r from"got";var u={productId:"Product ID is required. To get one, go to: https://partner.microsoft.com/en-us/dashboard/microsoftedge/{product-id}/package/dashboard",clientId:"Client ID is required. To get one: https://partner.microsoft.com/en-us/dashboard/microsoftedge/publishapi",apiKey:"API Key is required. To get one: https://partner.microsoft.com/en-us/dashboard/microsoftedge/publishapi"},h=Object.keys(u),g=i=>new Promise(t=>setTimeout(t,i)),m="https://api.addons.microsoftedge.microsoft.com",d=class{options={};constructor(t){for(let e of h)if(!t[e])throw new Error(u[e]);this.options={...t}}get productEndpoint(){return`${m}/v1/products/${this.options.productId}`}get publishEndpoint(){return`${this.productEndpoint}/submissions`}get uploadEndpoint(){return`${this.publishEndpoint}/draft/package`}async submit({filePath:t="",notes:e=""}){let o=await this.upload(c(t));if(await this.waitForUpload(o),!this.options.uploadOnly)return this.publish(e)}async publish(t=""){let e={headers:{...this.getPublishApiDefaultHeaders(),"Content-Type":"application/x-www-form-urlencoded"}};t.length>0&&(e.body=`{ "notes"="${t}" }`);let o=await r.post(this.publishEndpoint,e);return this.handleTempStatus(o.statusCode,"Submit"),o.headers.location}async upload(t=null){let e=await r.post(this.uploadEndpoint,{body:t,headers:{...this.getPublishApiDefaultHeaders(),"Content-Type":"application/zip"}});return this.handleTempStatus(e.statusCode,"Upload"),e.headers.location}async getPublishStatus(t){let e=`${this.publishEndpoint}/operations/${t}`;return r.get(e,{headers:this.getPublishApiDefaultHeaders()}).json()}async waitForUpload(t,e=5,o=3e3){let l=`${this.uploadEndpoint}/operations/${t}`,a,n,p=0;for(;n!=="Succeeded"&&p<e;){let s=await r.get(l,{headers:this.getPublishApiDefaultHeaders()}).json();if(s.status==="Failed")throw new Error(s.message||s.errorCode+":"+(s.errors||[]).join(","));s.status==="InProgress"?await g(o):s.status==="Succeeded"&&(a=s.message),n=s.status,p++}return a}handleTempStatus=(t,e)=>{if(t!==202)throw t>=500?new Error("Edge server error, please try again later"):new Error(`${e} failed, double check your api credentials`)};getPublishApiDefaultHeaders(){return{Authorization:`ApiKey ${this.options.apiKey}`,"X-ClientID":this.options.clientId}}};export{d as EdgeAddonsAPI,u as errorMap,h as requiredFields};