UNPKG

@myparcel/sdk

Version:

JavaScript SDK to connect to the MyParcel API via Node.js or browser

2 lines (1 loc) 12.1 kB
"use strict";var G=Object.defineProperty;var _=(s,t,r)=>t in s?G(s,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):s[t]=r;var e=(s,t,r)=>_(s,typeof t!="symbol"?t+"":t,r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@myparcel/ts-utils");class O{constructor(){e(this,"fns");this.fns=[]}delete(t){const r=this.fns.indexOf(t);r!==-1&&(this.fns=[...this.fns.slice(0,r),...this.fns.slice(r+1)])}use(t){this.fns=[...this.fns,t]}}class d extends Error{constructor(r){super(r);e(this,"name","user");Object.setPrototypeOf(this,d.prototype),this.name="UserException"}}class m extends Error{constructor(r){super(r.message);e(this,"data");Object.setPrototypeOf(this,m.prototype),this.name="ApiException",this.data=r}}const T=(s,t)=>{if(t){const r=Object.entries(t).map(([a,o])=>`${a}=${o}`);r.length&&(s+=`?${r.join("&")}`)}return s},A="https://api.myparcel.nl",k=["POST","PUT"];class C{constructor(t){e(this,"interceptors");e(this,"baseUrl");e(this,"headers");e(this,"parameters");e(this,"options");e(this,"_requiredHeaders",[]);this.baseUrl=((t==null?void 0:t.baseUrl)??A).replace(/\/+$/,""),this.headers=(t==null?void 0:t.headers)??{},this.parameters=(t==null?void 0:t.parameters)??{},this.options=(t==null?void 0:t.options)??{},this.interceptors={request:new O,response:new O}}get requiredHeaders(){return this._requiredHeaders}set requiredHeaders(t){this._requiredHeaders=t}async doRequest(t,r){const a=this.normalizeOptions(t,{...r,...this.options});this.validateHeaders(t,a);let o=await this.request(t,a);for(const n of this.interceptors.response.fns)o=await n(o);if(l.isOfType(o,"errors"))throw new m(o);return this.getResponseBody(t,o)}getResponseBody(t,r){if(!l.isOfType(r,"data"))return r;const a=t.getResponseProperty();if(!a)return r.data;const{page:o,size:n,results:p}=r.data;return o===void 0&&n===void 0&&p===void 0?r.data[a]:{[a]:r.data[a],...o!==void 0&&{page:o},...n!==void 0&&{size:n},...p!==void 0&&{results:p}}}getHeaders(){return{Accept:"application/json",...this.headers}}createUrl(t,r){let a=t.getPath();if(a.length&&!a.startsWith("/")&&(a=`/${a}`),r!=null&&r.path&&(a=this.substitutePath(a,r.path)),a.includes("/:"))throw new d(`One or more path variables are missing in ${a}`);return r!=null&&r.parameters&&(a=T(a,r.parameters)),this.baseUrl+a}substitutePath(t,r){if(r&&Object.entries(r).forEach(([a,o])=>{t=t.replace(`:${a}`,String(o))}),t.includes(":")){const a=t.match(/:\w+?\?/g);a==null||a.forEach(o=>{t=t.replace(`/${o}`,"")})}return t}validateHeaders(t,r){const a=Object.entries(r.headers).reduce((n,[p,h])=>({...n,[p.toLowerCase()]:h}),{}),o=this.requiredHeaders.filter(n=>!(n.toLowerCase()in a));if(o.length)throw new d(`Required headers are missing: ${o.join(", ")}`)}normalizeOptions(t,r){const a={...r,parameters:{...this.parameters,...r.parameters,...t.getParameters()},headers:{...k.includes(t.method)?{"Content-Type":"application/json"}:{},...this.getHeaders(),...r.headers,...t.getHeaders()}};if(l.isOfType(r,"body")){const o=t.getProperty();if(r.body instanceof FormData)return a.body=r.body,delete a.headers["Content-Type"],a;o===void 0?a.body={data:r.body}:a.body={data:{[o]:r.body}}}return a}}const f=s=>{try{return JSON.parse(s),!0}catch{return!1}};class E extends C{constructor(r){super(r);e(this,"request",async(r,a)=>{var P,g,x;for(const c of this.interceptors.request.fns)a=await c(a);const o=r.getTimeout()??a.timeout,n=new AbortController,p={method:r.method,headers:a.headers,signal:n.signal};let h;o&&(h=setTimeout(()=>{n.abort()},o)),l.isOfType(a,"body")&&(a.body instanceof FormData?p.body=a.body:p.body=JSON.stringify(a.body));try{const c=await fetch(this.createUrl(r,a),p);if(c.body){if((P=c.headers.get("Content-Disposition"))!=null&&P.includes("attachment")||(g=c.headers.get("Content-Type"))!=null&&g.includes("application/pdf"))return c.blob();const y=await c.text();return(x=c.headers.get("Content-Type"))!=null&&x.includes("application/json")&&f(y)?JSON.parse(y):y}}finally{h&&clearTimeout(h)}})}}class b{constructor(t){e(this,"method","GET");e(this,"property");e(this,"responseProperty");e(this,"timeout");e(this,"headers");e(this,"parameters");this.headers=(t==null?void 0:t.headers)??{},this.parameters=(t==null?void 0:t.parameters)??{}}getHeaders(){return this.headers}getParameters(){return this.parameters}getPath(){return this.path}getProperty(){return this.property}getResponseProperty(){return this.responseProperty??this.property}getTimeout(){return this.timeout}}class i extends b{}class u extends b{}function S(s,t){if(!t.length)throw new d("At least one endpoint must be passed.");return{...t.reduce((r,a)=>({...r,[a.name]:async o=>s.doRequest(a,o??{})}),{}),client:s}}const w=(s,t)=>(s.requiredHeaders=["Authorization"],S(s,t)),v=(s,t)=>S(s,t);class D extends i{constructor(){super(...arguments);e(this,"method","DELETE");e(this,"name","deleteAccountMessage");e(this,"path","account_messages/:id");e(this,"property","messages")}}class H extends i{constructor(){super(...arguments);e(this,"name","getAccountMessages");e(this,"path","account_messages");e(this,"property","messages")}}class q extends i{constructor(){super(...arguments);e(this,"name","getAccount");e(this,"path","accounts/:id");e(this,"property","accounts")}}class U extends i{constructor(){super(...arguments);e(this,"name","getAccounts");e(this,"path","accounts");e(this,"property","accounts")}}class L extends i{constructor(){super(...arguments);e(this,"method","PUT");e(this,"name","putAccount");e(this,"path","accounts");e(this,"property","accounts")}}class j extends i{constructor(){super(...arguments);e(this,"name","getApiKeys");e(this,"path","keys");e(this,"property","api_keys")}}class M extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postApiKeys");e(this,"path","keys");e(this,"property","api_keys")}}class W extends i{constructor(){super(...arguments);e(this,"method","DELETE");e(this,"name","deleteCarrierOption");e(this,"path","accounts/:account_id/carrier_options/:contract_id");e(this,"property","carrier_options")}}class R extends i{constructor(){super(...arguments);e(this,"name","getCarrierOptions");e(this,"path","carrier_management/accounts/:account_id/carrier_options");e(this,"property","carrier_options")}}class $ extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postCarrierOptions");e(this,"path","accounts/:account_id/carrier_options");e(this,"property","carrier_options");e(this,"responseProperty","ids")}}class K extends i{constructor(){super(...arguments);e(this,"name","putCarrierOptions");e(this,"path","carrier_management/accounts/:account_id/carrier_options");e(this,"property","carrier_options");e(this,"method","PUT")}}class J extends i{constructor(){super(...arguments);e(this,"name","getLocations");e(this,"path","locations");e(this,"property","locations")}}class N extends i{constructor(){super(...arguments);e(this,"name","getShipment");e(this,"path","shipments/:id");e(this,"property","shipments")}}class z extends i{constructor(){super(...arguments);e(this,"name","getShipments");e(this,"path","shipments");e(this,"property","shipments")}}class B extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postShipments");e(this,"path","shipments");e(this,"property","shipments");e(this,"responseProperty","ids")}getHeaders(){return{...super.getHeaders(),"Content-Type":"application/vnd.shipment+json;charset=utf-8;version=1.1"}}}class F extends i{constructor(){super(...arguments);e(this,"name","getShop");e(this,"path","shops/:id");e(this,"property","shops")}}class I extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postShop");e(this,"path","shops/duplicate");e(this,"property","shops");e(this,"responseProperty","ids")}}class Q extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postShopDuplicate");e(this,"path","shops/duplicate");e(this,"property","ids")}}class X extends i{constructor(){super(...arguments);e(this,"method","PUT");e(this,"name","putShop");e(this,"path","shops");e(this,"property","shops")}}class Y extends i{constructor(){super(...arguments);e(this,"name","getSubscriptionsCapabilities");e(this,"path","subscriptions/capabilities");e(this,"property","capabilities")}}class Z extends i{constructor(){super(...arguments);e(this,"method","DELETE");e(this,"name","deleteSubscription");e(this,"path","subscriptions/:id");e(this,"property","subscriptions")}}class V extends i{constructor(){super(...arguments);e(this,"name","getSubscriptions");e(this,"path","subscriptions");e(this,"property","subscriptions")}}class ee extends i{constructor(){super(...arguments);e(this,"method","PATCH");e(this,"name","patchSubscriptions");e(this,"path","subscriptions");e(this,"property","subscriptions");e(this,"responseProperty","ids")}}class te extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postSubscriptions");e(this,"path","subscriptions");e(this,"property","subscriptions");e(this,"responseProperty","ids")}}class se extends i{constructor(){super(...arguments);e(this,"name","getSystemCountryCodes");e(this,"path","system_country_codes");e(this,"property","countries")}}class re extends i{constructor(){super(...arguments);e(this,"name","getSystemMessages");e(this,"path","system_messages");e(this,"property","messages")}}class ae extends i{constructor(){super(...arguments);e(this,"name","getTrackAndTrace");e(this,"path","tracktraces/:shipment_id");e(this,"property","tracktraces")}}class ie extends i{constructor(){super(...arguments);e(this,"method","DELETE");e(this,"name","deleteWebhookSubscriptions");e(this,"path","webhook_subscriptions/:ids");e(this,"property","webhook_subscriptions")}}class oe extends i{constructor(){super(...arguments);e(this,"name","getWebhookSubscriptions");e(this,"path","webhook_subscriptions");e(this,"property","webhook_subscriptions")}}class ne extends i{constructor(){super(...arguments);e(this,"method","POST");e(this,"name","postWebhookSubscriptions");e(this,"path","webhook_subscriptions");e(this,"property","webhook_subscriptions");e(this,"responseProperty","ids")}}class pe extends u{constructor(){super(...arguments);e(this,"name","getCarrier");e(this,"path","carriers/:carrier");e(this,"property","carriers")}}class ce extends u{constructor(){super(...arguments);e(this,"name","getCarriers");e(this,"path","carriers");e(this,"property","carriers")}}class de extends u{constructor(){super(...arguments);e(this,"name","getDeliveryOptions");e(this,"path","delivery_options");e(this,"property","deliveries")}getHeaders(){return{...super.getHeaders(),Accept:"application/json;version=2.0"}}}class he extends u{constructor(){super(...arguments);e(this,"name","getPickupLocations");e(this,"path","pickup_locations");e(this,"property","pickup_locations")}}exports.AbstractClient=C;exports.AbstractEndpoint=b;exports.AbstractPrivateEndpoint=i;exports.AbstractPublicEndpoint=u;exports.ApiException=m;exports.BASE_URL=A;exports.DeleteAccountMessage=D;exports.DeleteCarrierOptions=W;exports.DeleteSubscription=Z;exports.DeleteWebhookSubscriptions=ie;exports.FetchClient=E;exports.GetAccount=q;exports.GetAccountMessages=H;exports.GetAccounts=U;exports.GetApiKeys=j;exports.GetCarrier=pe;exports.GetCarrierOptions=R;exports.GetCarriers=ce;exports.GetDeliveryOptions=de;exports.GetLocations=J;exports.GetPickupLocations=he;exports.GetShipment=N;exports.GetShipments=z;exports.GetShop=F;exports.GetSubscriptions=V;exports.GetSubscriptionsCapabilities=Y;exports.GetSystemCountryCodes=se;exports.GetSystemMessages=re;exports.GetTrackAndTrace=ae;exports.GetWebhookSubscriptions=oe;exports.PatchSubscriptions=ee;exports.PostApiKeys=M;exports.PostCarrierOptions=$;exports.PostShipments=B;exports.PostShop=I;exports.PostShopDuplicate=Q;exports.PostSubscriptions=te;exports.PostWebhookSubscriptions=ne;exports.PutAccount=L;exports.PutCarrierOptions=K;exports.PutShop=X;exports.UserException=d;exports.addParameters=T;exports.createMyParcelSdk=S;exports.createPrivateSdk=w;exports.createPublicSdk=v;exports.isJson=f;