ec.fdk 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var M=Object.defineProperty,G=Object.defineProperties;var U=Object.getOwnPropertyDescriptors;var L=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var _=(e,t,n)=>t in e?M(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,a=(e,t)=>{for(var n in t||(t={}))B.call(t,n)&&_(e,n,t[n]);if(L)for(var n of L(t))F.call(t,n)&&_(e,n,t[n]);return e},p=(e,t)=>G(e,U(t));var i=(e,t,n)=>new Promise((o,r)=>{var c=u=>{try{d(n.next(u))}catch(y){r(y)}},s=u=>{try{d(n.throw(u))}catch(y){r(y)}},d=u=>u.done?o(u.value):Promise.resolve(u.value).then(c,s);d((n=n.apply(e,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const H=require("@nanostores/persistent");function g(o){return i(this,arguments,function*(e,t={},n={}){const{token:r,rawRes:c}=t;r&&(n.headers=p(a({},n.headers||{}),{Authorization:`Bearer ${r}`}));const s=yield fetch(e,n);if(console.log("fetch",e,n),!s.ok)throw new Error(s.statusText);return c?s:yield s.json()})}const q={production:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"};function h(e,t="stage"){const n=q[t];if(!n)throw new Error(`env "${t}" not found. Try one of ${Object.keys(q).join(", ")}`);return n+e}function b(e,t=!0){return Object.entries(e).sort((n,o)=>n[0].localeCompare(o[0])).map(([n,o])=>`${n}=${o}`).join("&")}function l(e){Object.entries(e).forEach(([t,n])=>{if(n===void 0)throw new Error(`expected ${t} to be set!`)})}const v={stage:"https://accounts.cachena.entrecode.de/",production:"https://accounts.cachena.entrecode.de/"};function m(e){return i(this,null,function*(){let{env:t,dmShortID:n,email:o,password:r}=e;l({env:t,dmShortID:n,email:o,password:r});const c=h(`api/${n}/_auth/login?clientID=rest`,t);return yield g(c,{},{method:"POST",body:JSON.stringify({email:o,password:r}),headers:{"Content-Type":"application/json"}})})}function O(e){return i(this,null,function*(){let{env:t,email:n,password:o}=e;l({env:t,email:n,password:o});const r=`${v[t]}auth/login?clientID=rest`;return yield g(r,{},{method:"POST",body:JSON.stringify({email:n,password:o}),headers:{"Content-Type":"application/json"}})})}function j(e){return i(this,null,function*(){let{dmShortID:t,env:n}=e;l({dmShortID:t,env:n,token});const o=h(`api/${t}/_auth/logout?clientID=rest&token=${token}`,n);return yield g(o,{dmShortID:t,rawRes:!0},{method:"POST"})})}function T(e){return i(this,null,function*(){let{env:t,token:n}=e;l({env:t});const o=`${v[t]}auth/logout?clientID=rest`;return yield g(o,{rawRes:!0,token:n},{method:"POST"})})}function x(e){return i(this,null,function*(){let{env:t,dmShortID:n,model:o,options:r={}}=e;l({env:t,dmShortID:n,model:o}),r=a({size:50,page:1,_list:!0},r);const c=b(r),s=h(`api/${n}/${o}?${c}`,t),{count:d,total:u,_embedded:y}=yield g(s,e),k=y?y[`${n}:${o}`]:[];return{count:d,total:u,items:k}})}function J({env:e,dmShortID:t,model:n,entryID:o,token:r}){l({env:e,dmShortID:t,model:n,entryID:o});const c=b({_id:o}),s=h(`api/${t}/${n}?${c}`,e);return g(s,{dmShortID:t,token:r})}function N(c){return i(this,arguments,function*({env:e,dmShortID:t,model:n,value:o,token:r}){l({env:e,dmShortID:t,model:n,value:o}),console.log("create entry",t,n,o);const s=h(`api/${t}/${n}`,e);return console.log("url",s),yield g(s,{env:e,dmShortID:t,token:r},{method:"POST",body:JSON.stringify(o),headers:{"Content-Type":"application/json"}})})}function R(s){return i(this,arguments,function*({env:e,dmShortID:t,model:n,entryID:o,value:r,token:c}){l({env:e,dmShortID:t,model:n,entryID:o,value:r}),console.log("edit entry",t,n,o,r)})}function z(c){return i(this,arguments,function*({env:e,dmShortID:t,assetGroup:n,assetID:o,token:r}){l({env:e,dmShortID:t,assetGroup:n,assetID:o});const s=b({assetID:o}),d=h(`a/${t}/${n}?${s}`,e);return(yield g(d,{dmShortID:t,token:r}))._embedded["ec:dm-asset"]})}function C(e){return i(this,null,function*(){let{env:t,dmShortID:n,assetGroup:o,token:r,options:c={}}=e;l({env:t,dmShortID:n,assetGroup:o}),c=a({size:50,page:1,_list:!0},c);const s=b(c),d=h(`a/${n}/${o}?${s}`,t),{count:u,total:y,_embedded:k}=yield g(d,{dmShortID:n,token:r}),K=k?k["ec:dm-asset"]:[];return{count:u,total:y,items:K}})}const w=Object.freeze(Object.defineProperty({__proto__:null,assetList:C,createEntry:N,editEntry:R,entryList:x,getAsset:z,getEntry:J,loginEc:O,loginPublic:m,logoutEc:T,logoutPublic:j},Symbol.toStringTag,{value:"Module"})),{entryList:Q,getEntry:V,getAsset:W,assetList:X,createEntry:D,editEntry:I,loginPublic:Y,loginEc:Z,logoutEc:tt,logoutPublic:et}=w;function nt(e){const{action:t}=e;if(l({action:t}),!w[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys(w).join(", ")}`);return w[t](e)}class ${constructor(t){this.config=t}set(t){return new $(a(a({},this.config),t))}handle(t){const n=Object.keys(this.config),[o,r]=Object.entries(t).find(([c,s])=>n.includes(c))||[];if(!r)throw new Error(`you need to first set ${Object.keys(t).join(" | ")}`);return r(this.config)}entries(t){return Q(p(a({},this.config),{options:t}))}assets(t){return X(p(a({},this.config),{options:t}))}get(){return this.handle({assetID:W,entryID:V})}del(){}createEntry(t){return D(p(a({},this.config),{value:t}))}editEntry(t){return I(p(a({},this.config),{value:t}))}create(t){return this.set({value:t}).handle({model:D})}edit(t){return this.set({value:t}).handle({entryID:I})}loginPublic(t){return Y(a(a({},this.config),t))}logoutPublic(){return et(this.config)}loginEc(t){return Z(a(a({},this.config),t))}logoutEc(){return tt(this.config)}}const E=(...e)=>{const[t]=e;e.forEach(n=>{$.prototype[n]=function(o){return this.set({[t]:o})}})},A=(e,t)=>{$.prototype[e]=$.prototype[t]};E("dmShortID","dm","dmshortid");E("model");E("token");E("entryID","entry");E("assetGroup");E("assetID","asset");A("assetgroup","assetGroup");A("entryList","entries");A("assetList","assets");const ot=e=>new $({env:e}),f=H.persistentMap("fdk.auth",{},{encode:JSON.stringify,decode:JSON.parse});function P(e){var n;let{dmShortID:t}=e;if(t)return(n=f.get())==null?void 0:n[t]}function S(e){var n;let{env:t}=e;if(t)return(n=f.get())==null?void 0:n[t]}function rt(e){return S(e)||P(e)}function st(e){var t,n;return!!((t=S(e))!=null&&t.token)||!!((n=P(e))!=null&&n.token)}function ct(e){var t;return!!((t=S(e))!=null&&t.token)}function it(e){var t;return!!((t=P(e))!=null&&t.token)}function at(e){return i(this,null,function*(){const t=yield m(e);return f.setKey(e.dmShortID,{token:t.token}),t})}function ut(e){return i(this,null,function*(){const t=yield O(e);return f.setKey(e.env,{token:t.token}),t})}function lt(e){return i(this,null,function*(){const t=yield j(e);return f.setKey(e.dmShortID,{}),t})}function dt(e){return i(this,null,function*(){const t=yield T(e);return f.setKey(e.env,{token:t.token}),t})}exports.act=nt;exports.apiURL=h;exports.assetList=C;exports.auth=f;exports.createEntry=N;exports.editEntry=R;exports.entryList=x;exports.expect=l;exports.fetcher=g;exports.getAsset=z;exports.getAuth=rt;exports.getEcAuth=S;exports.getEntry=J;exports.getPublicAuth=P;exports.hasAnyToken=st;exports.hasEcToken=ct;exports.hasPublicToken=it;exports.loginEc=O;exports.loginEcStored=ut;exports.loginPublic=m;exports.loginPublicStored=at;exports.logoutEc=T;exports.logoutEcStored=dt;exports.logoutPublic=j;exports.logoutPublicStored=lt;exports.query=b;exports.sdk=ot;
1
+ "use strict";var v=Object.defineProperty,G=Object.defineProperties;var M=Object.getOwnPropertyDescriptors;var P=Object.getOwnPropertySymbols;var F=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var m=(n,t,e)=>t in n?v(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,c=(n,t)=>{for(var e in t||(t={}))F.call(t,e)&&m(n,e,t[e]);if(P)for(var e of P(t))H.call(t,e)&&m(n,e,t[e]);return n},d=(n,t)=>G(n,M(t));var a=(n,t,e)=>new Promise((o,s)=>{var r=g=>{try{h(e.next(g))}catch(y){s(y)}},i=g=>{try{h(e.throw(g))}catch(y){s(y)}},h=g=>g.done?o(g.value):Promise.resolve(g.value).then(r,i);h((e=e.apply(n,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function l(o){return a(this,arguments,function*(n,t={},e={}){const{token:s,rawRes:r}=t;s&&(e.headers=d(c({},e.headers||{}),{Authorization:`Bearer ${s}`}));const i=yield fetch(n,e);if(!i.ok)throw new Error(i.statusText);return r?i:yield i.json()})}const j={production:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"};function f(n,t="stage"){const e=j[t];if(!e)throw new Error(`env "${t}" not found. Try one of ${Object.keys(j).join(", ")}`);return e+n}function E(n,t=!0){return Object.entries(n).sort((e,o)=>e[0].localeCompare(o[0])).map(([e,o])=>`${e}=${o}`).join("&")}function u(n){Object.entries(n).forEach(([t,e])=>{if(e===void 0)throw new Error(`expected ${t} to be set!`)})}const L={stage:"https://accounts.cachena.entrecode.de/",production:"https://accounts.cachena.entrecode.de/"};function S(n){return a(this,null,function*(){let{env:t,dmShortID:e,email:o,password:s}=n;u({env:t,dmShortID:e,email:o,password:s});const r=f(`api/${e}/_auth/login?clientID=rest`,t);return yield l(r,{},{method:"POST",body:JSON.stringify({email:o,password:s}),headers:{"Content-Type":"application/json"}})})}function K(n){return a(this,null,function*(){let{env:t,email:e,password:o}=n;u({env:t,email:e,password:o});const s=`${L[t]}auth/login?clientID=rest`;return yield l(s,{},{method:"POST",body:JSON.stringify({email:e,password:o}),headers:{"Content-Type":"application/json"}})})}function q(n){return a(this,null,function*(){let{dmShortID:t,env:e,token:o}=n;u({dmShortID:t,env:e,token:o});const s=f(`api/${t}/_auth/logout?clientID=rest&token=${o}`,e);return yield l(s,{dmShortID:t,rawRes:!0},{method:"POST"})})}function x(n){return a(this,null,function*(){let{env:t,token:e}=n;u({env:t,token:e});const o=`${L[t]}auth/logout?clientID=rest`;return yield l(o,{rawRes:!0,token:e},{method:"POST"})})}function B({dmShortID:n}){return u({dmShortID:n}),n}function C({env:n}){return u({env:n}),n}function R(n){return a(this,null,function*(){let{env:t,dmShortID:e,model:o,options:s={}}=n;u({env:t,dmShortID:e,model:o}),s=c({size:50,page:1,_list:!0},s);const r=E(s),i=f(`api/${e}/${o}?${r}`,t),{count:h,total:g,_embedded:y}=yield l(i,n),b=y?y[`${e}:${o}`]:[];return{count:h,total:g,items:b}})}function z({env:n,dmShortID:t,model:e,entryID:o,token:s}){u({env:n,dmShortID:t,model:e,entryID:o});const r=E({_id:o}),i=f(`api/${t}/${e}?${r}`,n);return l(i,{dmShortID:t,token:s})}function J(r){return a(this,arguments,function*({env:n,dmShortID:t,model:e,value:o,token:s}){u({env:n,dmShortID:t,model:e,value:o}),console.log("create entry",t,e,o);const i=f(`api/${t}/${e}`,n);return console.log("url",i),yield l(i,{env:n,dmShortID:t,token:s},{method:"POST",body:JSON.stringify(o),headers:{"Content-Type":"application/json"}})})}function N(i){return a(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:o,value:s,token:r}){u({env:n,dmShortID:t,model:e,entryID:o,value:s});const h=f(`api/${t}/${e}?_id=${o}`,n);return yield l(h,{token:r},{method:"PUT",body:JSON.stringify(s),headers:{"Content-Type":"application/json"}})})}function D(r){return a(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:o,token:s}){u({env:n,dmShortID:t,assetGroup:e,assetID:o});const i=E({assetID:o}),h=f(`a/${t}/${e}?${i}`,n);return(yield l(h,{dmShortID:t,token:s}))._embedded["ec:dm-asset"]})}function I(n){return a(this,null,function*(){let{env:t,dmShortID:e,assetGroup:o,token:s,options:r={}}=n;u({env:t,dmShortID:e,assetGroup:o}),r=c({size:50,page:1,_list:!0},r);const i=E(r),h=f(`a/${e}/${o}?${i}`,t),{count:g,total:y,_embedded:b}=yield l(h,{dmShortID:e,token:s}),U=b?b["ec:dm-asset"]:[];return{count:g,total:y,items:U}})}const k=Object.freeze(Object.defineProperty({__proto__:null,assetList:I,createEntry:J,editEntry:N,entryList:R,getAsset:D,getEcAuthKey:C,getEntry:z,getPublicAuthKey:B,loginEc:K,loginPublic:S,logoutEc:x,logoutPublic:q},Symbol.toStringTag,{value:"Module"})),{entryList:Q,getEntry:V,getAsset:W,assetList:X,createEntry:O,editEntry:_,loginPublic:Y,loginEc:Z,logoutEc:tt,logoutPublic:et,getEcAuthKey:w,getPublicAuthKey:A}=k;function nt(n){const{action:t}=n;if(u({action:t}),!k[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys(k).join(", ")}`);return k[t](n)}class p{constructor(t){this.config=t}set(t){return new p(c(c({},this.config),t))}handle(t){return a(this,null,function*(){const e=Object.keys(this.config),o=yield this.getBestToken(),[s,r]=Object.entries(t).find(([i,h])=>e.includes(i))||[];if(!r)throw new Error(`you need to first set ${Object.keys(t).join(" | ")}`);return r(d(c({},this.config),{token:o}))})}entries(t){return a(this,null,function*(){const e=yield this.getBestToken();return Q(d(c({},this.config),{options:t,token:e}))})}assets(t){return a(this,null,function*(){const e=yield this.getBestToken();return X(d(c({},this.config),{options:t,token:e}))})}get(){return this.handle({assetID:W,entryID:V})}del(){}createEntry(t){return O(d(c({},this.config),{value:t}))}editEntry(t){return _(d(c({},this.config),{value:t}))}create(t){return this.set({value:t}).handle({model:O})}edit(t){return this.set({value:t}).handle({entryID:_})}authAdapter(t){return this.set({authAdapter:t})}setAuth(t){return e=>{if(!this.config.authAdapter)throw new Error("cannot setAuth: no authAdapter defined!");const{set:o}=this.config.authAdapter;return o(t,e.token),e}}unsetAuth(t){return e=>{if(console.log("unset auth",e),!this.config.authAdapter)throw new Error("cannot unsetAuth: no authAdapter defined!");const{remove:o}=this.config.authAdapter;return o(t),e}}getAuth(t){if(!this.config.authAdapter)throw new Error("cannot getAuth: no authAdapter defined!");const{get:e}=this.config.authAdapter;return e(t)}loginEc(t){return Z(c(c({},this.config),t)).then(this.setAuth(w(this.config)))}loginPublic(t){return Y(c(c({},this.config),t)).then(this.setAuth(A(this.config)))}logoutPublic(){const t=this.getPublicToken();return console.log("token",t),et(d(c({},this.config),{token:t})).then(this.unsetAuth(A(this.config)))}logoutEc(){const t=this.getEcToken();return console.log("token",t),tt(d(c({},this.config),{token:t})).then(this.unsetAuth(w(this.config)))}getPublicToken(){return this.config.token||this.getAuth(A(this.config))}getEcToken(){return this.config.token||this.getAuth(w(this.config))}hasPublicToken(){return!!this.getPublicToken()}hasEcToken(){return!!this.getEcToken()}hasAnyToken(){return!!this.getEcToken()||!!this.getPublicToken()}getBestToken(){return this.getEcToken()||this.getPublicToken()}}const $=(...n)=>{const[t]=n;n.forEach(e=>{p.prototype[e]=function(o){return this.set({[t]:o})}})},T=(n,t)=>{p.prototype[n]=p.prototype[t]};$("dmShortID","dm","dmshortid");$("model");$("token");$("entryID","entry");$("assetGroup");$("assetID","asset");T("assetgroup","assetGroup");T("entryList","entries");T("assetList","assets");const ot=n=>new p({env:n});exports.act=nt;exports.apiURL=f;exports.assetList=I;exports.createEntry=J;exports.editEntry=N;exports.entryList=R;exports.expect=u;exports.fetcher=l;exports.getAsset=D;exports.getEcAuthKey=C;exports.getEntry=z;exports.getPublicAuthKey=B;exports.loginEc=K;exports.loginPublic=S;exports.logoutEc=x;exports.logoutPublic=q;exports.query=E;exports.sdk=ot;
package/dist/index.mjs CHANGED
@@ -1,80 +1,79 @@
1
- var J = Object.defineProperty, N = Object.defineProperties;
2
- var z = Object.getOwnPropertyDescriptors;
3
- var S = Object.getOwnPropertySymbols;
4
- var C = Object.prototype.hasOwnProperty, K = Object.prototype.propertyIsEnumerable;
5
- var j = (e, t, n) => t in e ? J(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, a = (e, t) => {
6
- for (var n in t || (t = {}))
7
- C.call(t, n) && j(e, n, t[n]);
8
- if (S)
9
- for (var n of S(t))
10
- K.call(t, n) && j(e, n, t[n]);
11
- return e;
12
- }, f = (e, t) => N(e, z(t));
13
- var i = (e, t, n) => new Promise((o, s) => {
14
- var c = (u) => {
1
+ var K = Object.defineProperty, q = Object.defineProperties;
2
+ var x = Object.getOwnPropertyDescriptors;
3
+ var P = Object.getOwnPropertySymbols;
4
+ var B = Object.prototype.hasOwnProperty, C = Object.prototype.propertyIsEnumerable;
5
+ var m = (n, t, e) => t in n ? K(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e, c = (n, t) => {
6
+ for (var e in t || (t = {}))
7
+ B.call(t, e) && m(n, e, t[e]);
8
+ if (P)
9
+ for (var e of P(t))
10
+ C.call(t, e) && m(n, e, t[e]);
11
+ return n;
12
+ }, d = (n, t) => q(n, x(t));
13
+ var a = (n, t, e) => new Promise((s, o) => {
14
+ var r = (h) => {
15
15
  try {
16
- d(n.next(u));
17
- } catch (g) {
18
- s(g);
16
+ u(e.next(h));
17
+ } catch (f) {
18
+ o(f);
19
19
  }
20
- }, r = (u) => {
20
+ }, i = (h) => {
21
21
  try {
22
- d(n.throw(u));
23
- } catch (g) {
24
- s(g);
22
+ u(e.throw(h));
23
+ } catch (f) {
24
+ o(f);
25
25
  }
26
- }, d = (u) => u.done ? o(u.value) : Promise.resolve(u.value).then(c, r);
27
- d((n = n.apply(e, t)).next());
26
+ }, u = (h) => h.done ? s(h.value) : Promise.resolve(h.value).then(r, i);
27
+ u((e = e.apply(n, t)).next());
28
28
  });
29
- import { persistentMap as R } from "@nanostores/persistent";
30
- function y(o) {
31
- return i(this, arguments, function* (e, t = {}, n = {}) {
32
- const { token: s, rawRes: c } = t;
33
- s && (n.headers = f(a({}, n.headers || {}), {
34
- Authorization: `Bearer ${s}`
29
+ function l(s) {
30
+ return a(this, arguments, function* (n, t = {}, e = {}) {
31
+ const { token: o, rawRes: r } = t;
32
+ o && (e.headers = d(c({}, e.headers || {}), {
33
+ Authorization: `Bearer ${o}`
35
34
  }));
36
- const r = yield fetch(e, n);
37
- if (console.log("fetch", e, n), !r.ok)
38
- throw new Error(r.statusText);
39
- return c ? r : yield r.json();
35
+ const i = yield fetch(n, e);
36
+ if (!i.ok)
37
+ throw new Error(i.statusText);
38
+ return r ? i : yield i.json();
40
39
  });
41
40
  }
42
- const T = {
41
+ const j = {
43
42
  production: "https://datamanager.entrecode.de/",
44
43
  stage: "https://datamanager.cachena.entrecode.de/"
45
44
  };
46
- function h(e, t = "stage") {
47
- const n = T[t];
48
- if (!n)
45
+ function y(n, t = "stage") {
46
+ const e = j[t];
47
+ if (!e)
49
48
  throw new Error(
50
- `env "${t}" not found. Try one of ${Object.keys(T).join(", ")}`
49
+ `env "${t}" not found. Try one of ${Object.keys(j).join(", ")}`
51
50
  );
52
- return n + e;
51
+ return e + n;
53
52
  }
54
- function k(e, t = !0) {
55
- return Object.entries(e).sort((n, o) => n[0].localeCompare(o[0])).map(([n, o]) => `${n}=${o}`).join("&");
53
+ function b(n, t = !0) {
54
+ return Object.entries(n).sort((e, s) => e[0].localeCompare(s[0])).map(([e, s]) => `${e}=${s}`).join("&");
56
55
  }
57
- function l(e) {
58
- Object.entries(e).forEach(([t, n]) => {
59
- if (n === void 0)
56
+ function g(n) {
57
+ Object.entries(n).forEach(([t, e]) => {
58
+ if (e === void 0)
60
59
  throw new Error(`expected ${t} to be set!`);
61
60
  });
62
61
  }
63
- const A = {
62
+ const L = {
64
63
  stage: "https://accounts.cachena.entrecode.de/",
65
64
  production: "https://accounts.cachena.entrecode.de/"
66
65
  };
67
- function D(e) {
68
- return i(this, null, function* () {
69
- let { env: t, dmShortID: n, email: o, password: s } = e;
70
- l({ env: t, dmShortID: n, email: o, password: s });
71
- const c = h(`api/${n}/_auth/login?clientID=rest`, t);
72
- return yield y(
73
- c,
66
+ function z(n) {
67
+ return a(this, null, function* () {
68
+ let { env: t, dmShortID: e, email: s, password: o } = n;
69
+ g({ env: t, dmShortID: e, email: s, password: o });
70
+ const r = y(`api/${e}/_auth/login?clientID=rest`, t);
71
+ return yield l(
72
+ r,
74
73
  {},
75
74
  {
76
75
  method: "POST",
77
- body: JSON.stringify({ email: o, password: s }),
76
+ body: JSON.stringify({ email: s, password: o }),
78
77
  headers: {
79
78
  "Content-Type": "application/json"
80
79
  }
@@ -82,17 +81,17 @@ function D(e) {
82
81
  );
83
82
  });
84
83
  }
85
- function I(e) {
86
- return i(this, null, function* () {
87
- let { env: t, email: n, password: o } = e;
88
- l({ env: t, email: n, password: o });
89
- const s = `${A[t]}auth/login?clientID=rest`;
90
- return yield y(
91
- s,
84
+ function J(n) {
85
+ return a(this, null, function* () {
86
+ let { env: t, email: e, password: s } = n;
87
+ g({ env: t, email: e, password: s });
88
+ const o = `${L[t]}auth/login?clientID=rest`;
89
+ return yield l(
90
+ o,
92
91
  {},
93
92
  {
94
93
  method: "POST",
95
- body: JSON.stringify({ email: n, password: o }),
94
+ body: JSON.stringify({ email: e, password: s }),
96
95
  headers: {
97
96
  "Content-Type": "application/json"
98
97
  }
@@ -100,15 +99,15 @@ function I(e) {
100
99
  );
101
100
  });
102
101
  }
103
- function q(e) {
104
- return i(this, null, function* () {
105
- let { dmShortID: t, env: n } = e;
106
- l({ dmShortID: t, env: n, token });
107
- const o = h(
108
- `api/${t}/_auth/logout?clientID=rest&token=${token}`,
109
- n
102
+ function N(n) {
103
+ return a(this, null, function* () {
104
+ let { dmShortID: t, env: e, token: s } = n;
105
+ g({ dmShortID: t, env: e, token: s });
106
+ const o = y(
107
+ `api/${t}/_auth/logout?clientID=rest&token=${s}`,
108
+ e
110
109
  );
111
- return yield y(
110
+ return yield l(
112
111
  o,
113
112
  { dmShortID: t, rawRes: !0 },
114
113
  {
@@ -117,16 +116,16 @@ function q(e) {
117
116
  );
118
117
  });
119
118
  }
120
- function v(e) {
121
- return i(this, null, function* () {
122
- let { env: t, token: n } = e;
123
- l({ env: t });
124
- const o = `${A[t]}auth/logout?clientID=rest`;
125
- return yield y(
126
- o,
119
+ function R(n) {
120
+ return a(this, null, function* () {
121
+ let { env: t, token: e } = n;
122
+ g({ env: t, token: e });
123
+ const s = `${L[t]}auth/logout?clientID=rest`;
124
+ return yield l(
125
+ s,
127
126
  {
128
127
  rawRes: !0,
129
- token: n
128
+ token: e
130
129
  },
131
130
  {
132
131
  method: "POST"
@@ -134,29 +133,35 @@ function v(e) {
134
133
  );
135
134
  });
136
135
  }
137
- function G(e) {
138
- return i(this, null, function* () {
139
- let { env: t, dmShortID: n, model: o, options: s = {} } = e;
140
- l({ env: t, dmShortID: n, model: o }), s = a({ size: 50, page: 1, _list: !0 }, s);
141
- const c = k(s), r = h(`api/${n}/${o}?${c}`, t), { count: d, total: u, _embedded: g } = yield y(r, e), b = g ? g[`${n}:${o}`] : [];
142
- return { count: d, total: u, items: b };
136
+ function D({ dmShortID: n }) {
137
+ return g({ dmShortID: n }), n;
138
+ }
139
+ function I({ env: n }) {
140
+ return g({ env: n }), n;
141
+ }
142
+ function G(n) {
143
+ return a(this, null, function* () {
144
+ let { env: t, dmShortID: e, model: s, options: o = {} } = n;
145
+ g({ env: t, dmShortID: e, model: s }), o = c({ size: 50, page: 1, _list: !0 }, o);
146
+ const r = b(o), i = y(`api/${e}/${s}?${r}`, t), { count: u, total: h, _embedded: f } = yield l(i, n), E = f ? f[`${e}:${s}`] : [];
147
+ return { count: u, total: h, items: E };
143
148
  });
144
149
  }
145
- function M({ env: e, dmShortID: t, model: n, entryID: o, token: s }) {
146
- l({ env: e, dmShortID: t, model: n, entryID: o });
147
- const c = k({ _id: o }), r = h(`api/${t}/${n}?${c}`, e);
148
- return y(r, { dmShortID: t, token: s });
150
+ function U({ env: n, dmShortID: t, model: e, entryID: s, token: o }) {
151
+ g({ env: n, dmShortID: t, model: e, entryID: s });
152
+ const r = b({ _id: s }), i = y(`api/${t}/${e}?${r}`, n);
153
+ return l(i, { dmShortID: t, token: o });
149
154
  }
150
- function B(c) {
151
- return i(this, arguments, function* ({ env: e, dmShortID: t, model: n, value: o, token: s }) {
152
- l({ env: e, dmShortID: t, model: n, value: o }), console.log("create entry", t, n, o);
153
- const r = h(`api/${t}/${n}`, e);
154
- return console.log("url", r), yield y(
155
- r,
156
- { env: e, dmShortID: t, token: s },
155
+ function v(r) {
156
+ return a(this, arguments, function* ({ env: n, dmShortID: t, model: e, value: s, token: o }) {
157
+ g({ env: n, dmShortID: t, model: e, value: s }), console.log("create entry", t, e, s);
158
+ const i = y(`api/${t}/${e}`, n);
159
+ return console.log("url", i), yield l(
160
+ i,
161
+ { env: n, dmShortID: t, token: o },
157
162
  {
158
163
  method: "POST",
159
- body: JSON.stringify(o),
164
+ body: JSON.stringify(s),
160
165
  headers: {
161
166
  "Content-Type": "application/json"
162
167
  }
@@ -164,88 +169,112 @@ function B(c) {
164
169
  );
165
170
  });
166
171
  }
167
- function U(r) {
168
- return i(this, arguments, function* ({
169
- env: e,
172
+ function M(i) {
173
+ return a(this, arguments, function* ({
174
+ env: n,
170
175
  dmShortID: t,
171
- model: n,
172
- entryID: o,
173
- value: s,
174
- token: c
176
+ model: e,
177
+ entryID: s,
178
+ value: o,
179
+ token: r
175
180
  }) {
176
- l({ env: e, dmShortID: t, model: n, entryID: o, value: s }), console.log("edit entry", t, n, o, s);
181
+ g({ env: n, dmShortID: t, model: e, entryID: s, value: o });
182
+ const u = y(`api/${t}/${e}?_id=${s}`, n);
183
+ return yield l(
184
+ u,
185
+ { token: r },
186
+ {
187
+ method: "PUT",
188
+ body: JSON.stringify(o),
189
+ headers: {
190
+ "Content-Type": "application/json"
191
+ }
192
+ }
193
+ );
177
194
  });
178
195
  }
179
- function F(c) {
180
- return i(this, arguments, function* ({ env: e, dmShortID: t, assetGroup: n, assetID: o, token: s }) {
181
- l({ env: e, dmShortID: t, assetGroup: n, assetID: o });
182
- const r = k({ assetID: o }), d = h(`a/${t}/${n}?${r}`, e);
183
- return (yield y(d, { dmShortID: t, token: s }))._embedded["ec:dm-asset"];
196
+ function F(r) {
197
+ return a(this, arguments, function* ({ env: n, dmShortID: t, assetGroup: e, assetID: s, token: o }) {
198
+ g({ env: n, dmShortID: t, assetGroup: e, assetID: s });
199
+ const i = b({ assetID: s }), u = y(`a/${t}/${e}?${i}`, n);
200
+ return (yield l(u, { dmShortID: t, token: o }))._embedded["ec:dm-asset"];
184
201
  });
185
202
  }
186
- function H(e) {
187
- return i(this, null, function* () {
188
- let { env: t, dmShortID: n, assetGroup: o, token: s, options: c = {} } = e;
189
- l({ env: t, dmShortID: n, assetGroup: o }), c = a({ size: 50, page: 1, _list: !0 }, c);
190
- const r = k(c), d = h(`a/${n}/${o}?${r}`, t), { count: u, total: g, _embedded: b } = yield y(d, { dmShortID: n, token: s }), x = b ? b["ec:dm-asset"] : [];
191
- return { count: u, total: g, items: x };
203
+ function H(n) {
204
+ return a(this, null, function* () {
205
+ let { env: t, dmShortID: e, assetGroup: s, token: o, options: r = {} } = n;
206
+ g({ env: t, dmShortID: e, assetGroup: s }), r = c({ size: 50, page: 1, _list: !0 }, r);
207
+ const i = b(r), u = y(`a/${e}/${s}?${i}`, t), { count: h, total: f, _embedded: E } = yield l(u, { dmShortID: e, token: o }), S = E ? E["ec:dm-asset"] : [];
208
+ return { count: h, total: f, items: S };
192
209
  });
193
210
  }
194
- const w = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
211
+ const k = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
195
212
  __proto__: null,
196
213
  assetList: H,
197
- createEntry: B,
198
- editEntry: U,
214
+ createEntry: v,
215
+ editEntry: M,
199
216
  entryList: G,
200
217
  getAsset: F,
201
- getEntry: M,
202
- loginEc: I,
203
- loginPublic: D,
204
- logoutEc: v,
205
- logoutPublic: q
218
+ getEcAuthKey: I,
219
+ getEntry: U,
220
+ getPublicAuthKey: D,
221
+ loginEc: J,
222
+ loginPublic: z,
223
+ logoutEc: R,
224
+ logoutPublic: N
206
225
  }, Symbol.toStringTag, { value: "Module" })), {
207
226
  entryList: Q,
208
227
  getEntry: V,
209
228
  getAsset: W,
210
229
  assetList: X,
211
- createEntry: _,
212
- editEntry: L,
230
+ createEntry: O,
231
+ editEntry: _,
213
232
  loginPublic: Y,
214
233
  loginEc: Z,
215
234
  logoutEc: tt,
216
- logoutPublic: et
217
- } = w;
218
- function st(e) {
219
- const { action: t } = e;
220
- if (l({ action: t }), !w[t])
235
+ logoutPublic: et,
236
+ getEcAuthKey: w,
237
+ getPublicAuthKey: A
238
+ } = k;
239
+ function st(n) {
240
+ const { action: t } = n;
241
+ if (g({ action: t }), !k[t])
221
242
  throw new Error(
222
- `"${t}" does not exist! try one of ${Object.keys(w).join(
243
+ `"${t}" does not exist! try one of ${Object.keys(k).join(
223
244
  ", "
224
245
  )}`
225
246
  );
226
- return w[t](e);
247
+ return k[t](n);
227
248
  }
228
249
  class p {
229
250
  constructor(t) {
230
251
  this.config = t;
231
252
  }
232
253
  set(t) {
233
- return new p(a(a({}, this.config), t));
254
+ return new p(c(c({}, this.config), t));
234
255
  }
235
256
  handle(t) {
236
- const n = Object.keys(this.config), [o, s] = Object.entries(t).find(([c, r]) => n.includes(c)) || [];
237
- if (!s)
238
- throw new Error(
239
- `you need to first set ${Object.keys(t).join(" | ")}`
240
- );
241
- return s(this.config);
257
+ return a(this, null, function* () {
258
+ const e = Object.keys(this.config), s = yield this.getBestToken(), [o, r] = Object.entries(t).find(([i, u]) => e.includes(i)) || [];
259
+ if (!r)
260
+ throw new Error(
261
+ `you need to first set ${Object.keys(t).join(" | ")}`
262
+ );
263
+ return r(d(c({}, this.config), { token: s }));
264
+ });
242
265
  }
243
266
  ///
244
267
  entries(t) {
245
- return Q(f(a({}, this.config), { options: t }));
268
+ return a(this, null, function* () {
269
+ const e = yield this.getBestToken();
270
+ return Q(d(c({}, this.config), { options: t, token: e }));
271
+ });
246
272
  }
247
273
  assets(t) {
248
- return X(f(a({}, this.config), { options: t }));
274
+ return a(this, null, function* () {
275
+ const e = yield this.getBestToken();
276
+ return X(d(c({}, this.config), { options: t, token: e }));
277
+ });
249
278
  }
250
279
  get() {
251
280
  return this.handle({
@@ -256,46 +285,99 @@ class p {
256
285
  del() {
257
286
  }
258
287
  createEntry(t) {
259
- return _(f(a({}, this.config), { value: t }));
288
+ return O(d(c({}, this.config), { value: t }));
260
289
  }
261
290
  editEntry(t) {
262
- return L(f(a({}, this.config), { value: t }));
291
+ return _(d(c({}, this.config), { value: t }));
263
292
  }
264
293
  create(t) {
265
294
  return this.set({ value: t }).handle({
266
295
  //assetID: createAsset,
267
- model: _
296
+ model: O
268
297
  });
269
298
  }
270
299
  edit(t) {
271
300
  return this.set({ value: t }).handle({
272
301
  //assetID: createAsset,
273
- entryID: L
302
+ entryID: _
274
303
  });
275
304
  }
276
- //
277
- loginPublic(t) {
278
- return Y(a(a({}, this.config), t));
305
+ // TODO: rename authAdapter -> storageAdapter
306
+ authAdapter(t) {
307
+ return this.set({ authAdapter: t });
279
308
  }
280
- logoutPublic() {
281
- return et(this.config);
309
+ setAuth(t) {
310
+ return (e) => {
311
+ if (!this.config.authAdapter)
312
+ throw new Error("cannot setAuth: no authAdapter defined!");
313
+ const { set: s } = this.config.authAdapter;
314
+ return s(t, e.token), e;
315
+ };
316
+ }
317
+ unsetAuth(t) {
318
+ return (e) => {
319
+ if (console.log("unset auth", e), !this.config.authAdapter)
320
+ throw new Error("cannot unsetAuth: no authAdapter defined!");
321
+ const { remove: s } = this.config.authAdapter;
322
+ return s(t), e;
323
+ };
324
+ }
325
+ getAuth(t) {
326
+ if (!this.config.authAdapter)
327
+ throw new Error("cannot getAuth: no authAdapter defined!");
328
+ const { get: e } = this.config.authAdapter;
329
+ return e(t);
282
330
  }
283
331
  loginEc(t) {
284
- return Z(a(a({}, this.config), t));
332
+ return Z(c(c({}, this.config), t)).then(
333
+ this.setAuth(w(this.config))
334
+ );
335
+ }
336
+ loginPublic(t) {
337
+ return Y(c(c({}, this.config), t)).then(
338
+ this.setAuth(A(this.config))
339
+ );
340
+ }
341
+ logoutPublic() {
342
+ const t = this.getPublicToken();
343
+ return console.log("token", t), et(d(c({}, this.config), { token: t })).then(
344
+ this.unsetAuth(A(this.config))
345
+ );
285
346
  }
286
347
  logoutEc() {
287
- return tt(this.config);
348
+ const t = this.getEcToken();
349
+ return console.log("token", t), tt(d(c({}, this.config), { token: t })).then(
350
+ this.unsetAuth(w(this.config))
351
+ );
352
+ }
353
+ getPublicToken() {
354
+ return this.config.token || this.getAuth(A(this.config));
355
+ }
356
+ getEcToken() {
357
+ return this.config.token || this.getAuth(w(this.config));
358
+ }
359
+ hasPublicToken() {
360
+ return !!this.getPublicToken();
361
+ }
362
+ hasEcToken() {
363
+ return !!this.getEcToken();
364
+ }
365
+ hasAnyToken() {
366
+ return !!this.getEcToken() || !!this.getPublicToken();
367
+ }
368
+ getBestToken() {
369
+ return this.getEcToken() || this.getPublicToken();
288
370
  }
289
371
  }
290
- const $ = (...e) => {
291
- const [t] = e;
292
- e.forEach((n) => {
293
- p.prototype[n] = function(o) {
294
- return this.set({ [t]: o });
372
+ const $ = (...n) => {
373
+ const [t] = n;
374
+ n.forEach((e) => {
375
+ p.prototype[e] = function(s) {
376
+ return this.set({ [t]: s });
295
377
  };
296
378
  });
297
- }, m = (e, t) => {
298
- p.prototype[e] = p.prototype[t];
379
+ }, T = (n, t) => {
380
+ p.prototype[n] = p.prototype[t];
299
381
  };
300
382
  $("dmShortID", "dm", "dmshortid");
301
383
  $("model");
@@ -303,94 +385,27 @@ $("token");
303
385
  $("entryID", "entry");
304
386
  $("assetGroup");
305
387
  $("assetID", "asset");
306
- m("assetgroup", "assetGroup");
307
- m("entryList", "entries");
308
- m("assetList", "assets");
309
- const rt = (e) => new p({ env: e }), E = R(
310
- "fdk.auth",
311
- {},
312
- {
313
- encode: JSON.stringify,
314
- decode: JSON.parse
315
- }
316
- );
317
- function P(e) {
318
- var n;
319
- let { dmShortID: t } = e;
320
- if (t)
321
- return (n = E.get()) == null ? void 0 : n[t];
322
- }
323
- function O(e) {
324
- var n;
325
- let { env: t } = e;
326
- if (t)
327
- return (n = E.get()) == null ? void 0 : n[t];
328
- }
329
- function ct(e) {
330
- return O(e) || P(e);
331
- }
332
- function it(e) {
333
- var t, n;
334
- return !!((t = O(e)) != null && t.token) || !!((n = P(e)) != null && n.token);
335
- }
336
- function at(e) {
337
- var t;
338
- return !!((t = O(e)) != null && t.token);
339
- }
340
- function ut(e) {
341
- var t;
342
- return !!((t = P(e)) != null && t.token);
343
- }
344
- function lt(e) {
345
- return i(this, null, function* () {
346
- const t = yield D(e);
347
- return E.setKey(e.dmShortID, { token: t.token }), t;
348
- });
349
- }
350
- function dt(e) {
351
- return i(this, null, function* () {
352
- const t = yield I(e);
353
- return E.setKey(e.env, { token: t.token }), t;
354
- });
355
- }
356
- function yt(e) {
357
- return i(this, null, function* () {
358
- const t = yield q(e);
359
- return E.setKey(e.dmShortID, {}), t;
360
- });
361
- }
362
- function gt(e) {
363
- return i(this, null, function* () {
364
- const t = yield v(e);
365
- return E.setKey(e.env, { token: t.token }), t;
366
- });
367
- }
388
+ T("assetgroup", "assetGroup");
389
+ T("entryList", "entries");
390
+ T("assetList", "assets");
391
+ const ot = (n) => new p({ env: n });
368
392
  export {
369
393
  st as act,
370
- h as apiURL,
394
+ y as apiURL,
371
395
  H as assetList,
372
- E as auth,
373
- B as createEntry,
374
- U as editEntry,
396
+ v as createEntry,
397
+ M as editEntry,
375
398
  G as entryList,
376
- l as expect,
377
- y as fetcher,
399
+ g as expect,
400
+ l as fetcher,
378
401
  F as getAsset,
379
- ct as getAuth,
380
- O as getEcAuth,
381
- M as getEntry,
382
- P as getPublicAuth,
383
- it as hasAnyToken,
384
- at as hasEcToken,
385
- ut as hasPublicToken,
386
- I as loginEc,
387
- dt as loginEcStored,
388
- D as loginPublic,
389
- lt as loginPublicStored,
390
- v as logoutEc,
391
- gt as logoutEcStored,
392
- q as logoutPublic,
393
- yt as logoutPublicStored,
394
- k as query,
395
- rt as sdk
402
+ I as getEcAuthKey,
403
+ U as getEntry,
404
+ D as getPublicAuthKey,
405
+ J as loginEc,
406
+ z as loginPublic,
407
+ R as logoutEc,
408
+ N as logoutPublic,
409
+ b as query,
410
+ ot as sdk
396
411
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ec.fdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "ec frontend sdk",
5
5
  "main": "dist/index.cjs",
6
6
  "directories": {