printify-sdk-js
Version:
Node.js SDK for the Printify API.
3 lines (2 loc) • 11.6 kB
JavaScript
;function t(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=t(require("axios"));function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}const n=new Set(["ENOTFOUND","ENETUNREACH","UNABLE_TO_GET_ISSUER_CERT","UNABLE_TO_GET_CRL","UNABLE_TO_DECRYPT_CERT_SIGNATURE","UNABLE_TO_DECRYPT_CRL_SIGNATURE","UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY","CERT_SIGNATURE_FAILURE","CRL_SIGNATURE_FAILURE","CERT_NOT_YET_VALID","CERT_HAS_EXPIRED","CRL_NOT_YET_VALID","CRL_HAS_EXPIRED","ERROR_IN_CERT_NOT_BEFORE_FIELD","ERROR_IN_CERT_NOT_AFTER_FIELD","ERROR_IN_CRL_LAST_UPDATE_FIELD","ERROR_IN_CRL_NEXT_UPDATE_FIELD","OUT_OF_MEM","DEPTH_ZERO_SELF_SIGNED_CERT","SELF_SIGNED_CERT_IN_CHAIN","UNABLE_TO_GET_ISSUER_CERT_LOCALLY","UNABLE_TO_VERIFY_LEAF_SIGNATURE","CERT_CHAIN_TOO_LONG","CERT_REVOKED","INVALID_CA","PATH_LENGTH_EXCEEDED","INVALID_PURPOSE","CERT_UNTRUSTED","CERT_REJECTED","HOSTNAME_MISMATCH"]);var i=e((t=>!n.has(t&&t.code)));const o="axios-retry";function r(t){return!t.response&&(!!t.code&&(!["ERR_CANCELED","ECONNABORTED"].includes(t.code)&&i(t)))}const h=["get","head","options"],u=h.concat(["put","delete"]);function a(t){return"ECONNABORTED"!==t.code&&(!t.response||429===t.response.status||t.response.status>=500&&t.response.status<=599)}function d(t){return!!t.config?.method&&(a(t)&&-1!==u.indexOf(t.config.method))}function c(t){return r(t)||d(t)}function p(t=void 0){const s=t?.response?.headers["retry-after"];if(!s)return 0;let e=1e3*(Number(s)||0);return 0===e&&(e=(new Date(s).valueOf()||0)-Date.now()),Math.max(0,e)}const l={retries:3,retryCondition:c,retryDelay:function(t=0,s=void 0){return Math.max(0,p(s))},shouldResetTimeout:!1,onRetry:()=>{},onMaxRetryTimesExceeded:()=>{},validateResponse:null};function E(t,s,e=!1){const n=function(t,s){return{...l,...s,...t[o]}}(t,s||{});return n.retryCount=n.retryCount||0,n.lastRequestTime&&!e||(n.lastRequestTime=Date.now()),t[o]=n,n}const g=(t,s)=>{const e=t.interceptors.request.use((t=>(E(t,s,!0),t[o]?.validateResponse&&(t.validateStatus=()=>!1),t))),n=t.interceptors.response.use(null,(async e=>{const{config:n}=e;if(!n)return Promise.reject(e);const i=E(n,s);return e.response&&i.validateResponse?.(e.response)?e.response:await async function(t,s){const{retries:e,retryCondition:n}=t,i=(t.retryCount||0)<e&&n(s);if("object"==typeof i)try{return!1!==await i}catch(t){return!1}return i}(i,e)?async function(t,s,e,n){s.retryCount+=1;const{retryDelay:i,shouldResetTimeout:o,onRetry:r}=s,h=i(s.retryCount,e);if(function(t,s){t.defaults.agent===s.agent&&delete s.agent,t.defaults.httpAgent===s.httpAgent&&delete s.httpAgent,t.defaults.httpsAgent===s.httpsAgent&&delete s.httpsAgent}(t,n),!o&&n.timeout&&s.lastRequestTime){const t=Date.now()-s.lastRequestTime,i=n.timeout-t-h;if(i<=0)return Promise.reject(e);n.timeout=i}return n.transformRequest=[t=>t],await r(s.retryCount,e,n),n.signal?.aborted?Promise.resolve(t(n)):new Promise((s=>{const e=()=>{clearTimeout(i),s(t(n))},i=setTimeout((()=>{s(t(n)),n.signal?.removeEventListener&&n.signal.removeEventListener("abort",e)}),h);n.signal?.addEventListener&&n.signal.addEventListener("abort",e,{once:!0})}))}(t,i,e,n):(await async function(t,s){t.retryCount>=t.retries&&await t.onMaxRetryTimesExceeded(s,t.retryCount)}(i,e),Promise.reject(e))}));return{requestInterceptorId:e,responseInterceptorId:n}};g.isNetworkError=r,g.isSafeRequestError=function(t){return!!t.config?.method&&(a(t)&&-1!==h.indexOf(t.config.method))},g.isIdempotentRequestError=d,g.isNetworkOrIdempotentRequestError=c,g.exponentialDelay=function(t=0,s=void 0,e=100){const n=2**t*e,i=Math.max(n,p(s));return i+.2*i*Math.random()},g.linearDelay=function(t=100){return(s=0,e=void 0)=>{const n=s*t;return Math.max(n,p(e))}},g.isRetryableError=a,g(s.default,{retries:3,retryDelay:g.exponentialDelay});class T{constructor(t){var s;this.accessToken=t.accessToken,this.shopId=t.shopId,this.host=t.host||"api.printify.com",this.timeout=t.timeout||5e3,this.enableLogging=null===(s=t.enableLogging)||void 0===s||s,this.baseUrl=`https://${this.host}`}logError(t){this.enableLogging&&console.error(t)}logRequest(t,s){this.enableLogging&&console.log(`Request: ${t.toUpperCase()} ${this.baseUrl}${s}`)}async request(t,e={}){var n,i;const o={"Content-Type":"application/json",Authorization:`Bearer ${this.accessToken}`},r=Object.assign(Object.assign({},e),{method:e.method||"GET",baseURL:this.baseUrl,timeout:this.timeout,headers:Object.assign(Object.assign({},o),e.headers||{})}),h=(e.method||"GET").toLowerCase();this.logRequest(h,t);try{let n;switch(h){case"post":n=await s.default.post(t,e.data,r);break;case"put":n=await s.default.put(t,e.data,r);break;case"delete":n=await s.default.delete(t,r);break;default:n=await s.default.get(t,r)}return n.data}catch(s){let e="Printify SDK Error";s.isAxiosError&&(e=`Printify SDK: ${null===(n=s.response)||void 0===n?void 0:n.status} ${null===(i=s.response)||void 0===i?void 0:i.statusText} - Requested URL: ${this.baseUrl}${t}`),this.logError(e);const o=new Error(e);throw o.stack=void 0,o}}}const f=function(){return this.request("/v1/catalog/blueprints.json",{method:"GET"})},_=function(t){return this.request(`/v1/catalog/blueprints/${t}.json`,{method:"GET"})},m=function(t){return this.request(`/v1/catalog/blueprints/${t}/print_providers.json`,{method:"GET"})},b=function(t,s){return this.request(`/v1/catalog/blueprints/${t}/print_providers/${s}/variants.json`,{method:"GET"})},v=function(t,s){return this.request(`/v1/catalog/blueprints/${t}/print_providers/${s}/shipping.json`,{method:"GET"})},R=function(){return this.request("/v1/catalog/print_providers.json",{method:"GET"})},O=function(t){return this.request(`/v1/catalog/print_providers/${t}.json`,{method:"GET"})};class S extends T{constructor(t){super(t),this.listBlueprints=f.bind(this),this.getBlueprint=_.bind(this),this.getBlueprintProviders=m.bind(this),this.getBlueprintVariants=b.bind(this),this.getVariantShipping=v.bind(this),this.listProviders=R.bind(this),this.getProvider=O.bind(this)}}const I=function(t,s){return this.request(`/v2/catalog/blueprints/${t}/print_providers/${s}/shipping/economy.json`,{method:"GET"})},$=function(t,s){return this.request(`/v2/catalog/blueprints/${t}/print_providers/${s}/shipping/express.json`,{method:"GET"})},y=function(t,s){return this.request(`/v2/catalog/blueprints/${t}/print_providers/${s}/shipping/priority.json`,{method:"GET"})},j=function(t,s){return this.request(`/v2/catalog/blueprints/${t}/print_providers/${s}/shipping.json`,{method:"GET"})},q=function(t,s){return this.request(`/v2/catalog/blueprints/${t}/print_providers/${s}/shipping/standard.json`,{method:"GET"})};class N extends T{constructor(t){super(t),this.getShippingListInfo=j.bind(this),this.getStandardShippingInfo=q.bind(this),this.getPriorityShippingInfo=y.bind(this),this.getExpressShippingInfo=$.bind(this),this.getEconomyShippingInfo=I.bind(this)}}const L=function(t={}){const{page:s,limit:e,status:n,sku:i}=t,o=new URLSearchParams(Object.assign(Object.assign(Object.assign(Object.assign({},void 0!==s&&{page:s.toString()}),void 0!==e&&{limit:e.toString()}),void 0!==n&&{status:n}),void 0!==i&&{sku:i})).toString();return this.request(`/v1/shops/${this.shopId}/orders.json${o?`?${o}`:""}`,{method:"GET"})},C=function(t){return this.request(`/v1/shops/${this.shopId}/orders/${t}.json`,{method:"GET"})},A=function(t){return this.request(`/v1/shops/${this.shopId}/orders.json`,{method:"POST",data:JSON.stringify(t)})},P=function(t){return this.request(`/v1/shops/${this.shopId}/express.json`,{method:"POST",data:JSON.stringify(t)})},D=function(t){return this.request(`/v1/shops/${this.shopId}/orders/${t}/send_to_production.json`,{method:"POST"})},U=function(t){return this.request(`/v1/shops/${this.shopId}/orders/shipping.json`,{method:"POST",data:JSON.stringify(t)})},w=function(t){return this.request(`/v1/shops/${this.shopId}/orders/${t}/cancel.json`,{method:"POST"})};class G extends T{constructor(t){super(t),this.list=L.bind(this),this.getOne=C.bind(this),this.submit=A.bind(this),this.submitExpress=P.bind(this),this.sendToProduction=D.bind(this),this.calculateShipping=U.bind(this),this.cancelUnpaid=w.bind(this)}}const x=function(t){return this.request(`/v1/shops/${this.shopId}/products.json`,{method:"POST",data:JSON.stringify(t)})},k=function(t){return this.request(`/v1/shops/${this.shopId}/products/${t}.json`,{method:"DELETE"})},B=function(t){return this.request(`/v1/shops/${this.shopId}/products/${t}.json`,{method:"GET"})},F=function(t={}){const{page:s,limit:e}=t,n=new URLSearchParams(Object.assign(Object.assign({},void 0!==s&&{page:s.toString()}),void 0!==e&&{limit:e.toString()})).toString(),i=n.toString()?`?${n.toString()}`:"";return this.request(`/v1/shops/${this.shopId}/products.json${i}`,{method:"GET"})},M=function(t){return this.request(`/v1/shops/${this.shopId}/products/${t}/unpublish.json`,{method:"POST"})},J=function(t,s){return this.request(`/v1/shops/${this.shopId}/products/${t}/publish.json`,{method:"POST",data:JSON.stringify(s)})},H=function(t,s){return this.request(`/v1/shops/${this.shopId}/products/${t}/publishing_failed.json`,{method:"POST",data:JSON.stringify(s)})},V=function(t,s){return this.request(`/v1/shops/${this.shopId}/products/${t}/publishing_succeeded.json`,{method:"POST",data:JSON.stringify(s)})},Y=function(t,s){return this.request(`/v1/shops/${this.shopId}/products/${t}.json`,{method:"PUT",data:JSON.stringify(s)})};class K extends T{constructor(t){super(t),this.create=x.bind(this),this.deleteOne=k.bind(this),this.getOne=B.bind(this),this.list=F.bind(this),this.notifyUnpublished=M.bind(this),this.publishOne=J.bind(this),this.setPublishFailed=H.bind(this),this.setPublishSucceeded=V.bind(this),this.updateOne=Y.bind(this)}}const X=function(t){return this.request(`/v1/shops/${null!=t?t:this.shopId}/connection.json`,{method:"DELETE"})},z=function(){return this.request("/v1/shops.json",{method:"GET"})};class Z extends T{constructor(t){super(t),this.deleteOne=X.bind(this),this.list=z.bind(this)}}const Q=function(t){return this.request(`/v1/uploads/${t}/archive.json`,{method:"POST"})},W=function(t){return this.request(`/v1/uploads/${t}.json`,{method:"GET"})},tt=function(t,s){const e=new URLSearchParams;void 0!==t&&e.append("page",t.toString()),void 0!==s&&e.append("limit",s.toString());const n="/v1/uploads.json"+(e.toString()?`?${e.toString()}`:"");return this.request(n,{method:"GET"})},st=function(t){return this.request("/v1/uploads/images.json",{method:"POST",data:JSON.stringify(t)})};class et extends T{constructor(t){super(t),this.archive=Q.bind(this),this.getById=W.bind(this),this.list=tt.bind(this),this.uploadImage=st.bind(this)}}const nt=function(){return this.request(`/v1/shops/${this.shopId}/webhooks.json`,{method:"GET"})},it=function(t){return this.request(`/v1/shops/${this.shopId}/webhooks.json`,{method:"POST",data:JSON.stringify(t)})},ot=function(t,s){return this.request(`/v1/shops/${this.shopId}/webhooks/${t}.json`,{method:"PUT",data:JSON.stringify(s)})},rt=function(t){return this.request(`/v1/shops/${this.shopId}/webhooks/${t}.json`,{method:"DELETE"})};class ht extends T{constructor(t){super(t),this.list=nt.bind(this),this.create=it.bind(this),this.updateOne=ot.bind(this),this.deleteOne=rt.bind(this)}}module.exports=class{constructor(t){if(!t.accessToken)throw new Error("accessToken is required");this.shopId=t.shopId,this.catalog=new S(t),this.v2={catalog:new N(t)},this.orders=new G(t),this.products=new K(t),this.shops=new Z(t),this.uploads=new et(t),this.webhooks=new ht(t)}};
//# sourceMappingURL=index.cjs.js.map