ec.fdk 0.1.0 → 0.1.2

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";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N=require("@nanostores/persistent");async function a(e,t={},n={}){const{token:o,rawRes:r}=t;o&&(n.headers={...n.headers||{},Authorization:`Bearer ${o}`});const s=await fetch(e,n);if(console.log("fetch",e,n),!s.ok)throw new Error(s.statusText);return r?s:await s.json()}const j={production:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"};function u(e,t="stage"){const n=j[t];if(!n)throw new Error(`env "${t}" not found. Try one of ${Object.keys(j).join(", ")}`);return n+e}function y(e,t=!0){return Object.entries(e).sort((n,o)=>n[0].localeCompare(o[0])).map(([n,o])=>`${n}=${o}`).join("&")}function c(e){Object.entries(e).forEach(([t,n])=>{if(n===void 0)throw new Error(`expected ${t} to be set!`)})}const L={stage:"https://accounts.cachena.entrecode.de/",production:"https://accounts.cachena.entrecode.de/"};async function w(e){let{env:t,dmShortID:n,email:o,password:r}=e;c({env:t,dmShortID:n,email:o,password:r});const s=u(`api/${n}/_auth/login?clientID=rest`,t);return await a(s,{},{method:"POST",body:JSON.stringify({email:o,password:r}),headers:{"Content-Type":"application/json"}})}async function P(e){let{env:t,email:n,password:o}=e;c({env:t,email:n,password:o});const r=`${L[t]}auth/login?clientID=rest`;return await a(r,{},{method:"POST",body:JSON.stringify({email:n,password:o}),headers:{"Content-Type":"application/json"}})}async function S(e){let{dmShortID:t,env:n}=e;c({dmShortID:t,env:n,token});const o=u(`api/${t}/_auth/logout?clientID=rest&token=${token}`,n);return await a(o,{dmShortID:t,rawRes:!0},{method:"POST"})}async function m(e){let{env:t,token:n}=e;c({env:t});const o=`${L[t]}auth/logout?clientID=rest`;return await a(o,{rawRes:!0,token:n},{method:"POST"})}async function _(e){let{env:t,dmShortID:n,model:o,options:r={}}=e;c({env:t,dmShortID:n,model:o}),r={size:50,page:1,_list:!0,...r};const s=y(r),i=u(`api/${n}/${o}?${s}`,t),{count:h,total:k,_embedded:f}=await a(i,e),p=f?f[`${n}:${o}`]:[];return{count:h,total:k,items:p}}function q({env:e,dmShortID:t,model:n,entryID:o,token:r}){c({env:e,dmShortID:t,model:n,entryID:o});const s=y({_id:o}),i=u(`api/${t}/${n}?${s}`,e);return a(i,{dmShortID:t,token:r})}async function D({env:e,dmShortID:t,model:n,value:o,token:r}){c({env:e,dmShortID:t,model:n,value:o}),console.log("create entry",t,n,o);const s=u(`api/${t}/${n}`,e);return console.log("url",s),await a(s,{env:e,dmShortID:t,token:r},{method:"POST",body:JSON.stringify(o),headers:{"Content-Type":"application/json"}})}async function I({env:e,dmShortID:t,model:n,entryID:o,value:r,token:s}){c({env:e,dmShortID:t,model:n,entryID:o,value:r}),console.log("edit entry",t,n,o,r)}async function v({env:e,dmShortID:t,assetGroup:n,assetID:o,token:r}){c({env:e,dmShortID:t,assetGroup:n,assetID:o});const s=y({assetID:o}),i=u(`a/${t}/${n}?${s}`,e);return(await a(i,{dmShortID:t,token:r}))._embedded["ec:dm-asset"]}async function x(e){let{env:t,dmShortID:n,assetGroup:o,token:r,options:s={}}=e;c({env:t,dmShortID:n,assetGroup:o}),s={size:50,page:1,_list:!0,...s};const i=y(s),h=u(`a/${n}/${o}?${i}`,t),{count:k,total:f,_embedded:p}=await a(h,{dmShortID:n,token:r}),J=p?p["ec:dm-asset"]:[];return{count:k,total:f,items:J}}const $=Object.freeze(Object.defineProperty({__proto__:null,assetList:x,createEntry:D,editEntry:I,entryList:_,getAsset:v,getEntry:q,loginEc:P,loginPublic:w,logoutEc:m,logoutPublic:S},Symbol.toStringTag,{value:"Module"})),{entryList:R,getEntry:z,getAsset:C,assetList:K,createEntry:T,editEntry:A,loginPublic:M,loginEc:G,logoutEc:U,logoutPublic:B}=$;function F(e){const{action:t}=e;if(c({action:t}),!$[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys($).join(", ")}`);return $[t](e)}class d{constructor(t){this.config=t}set(t){return new d({...this.config,...t})}handle(t){const n=Object.keys(this.config),[o,r]=Object.entries(t).find(([s,i])=>n.includes(s))||[];if(!r)throw new Error(`you need to first set ${Object.keys(t).join(" | ")}`);return r(this.config)}entries(t){return R({...this.config,options:t})}assets(t){return K({...this.config,options:t})}get(){return this.handle({assetID:C,entryID:z})}del(){}createEntry(t){return T({...this.config,value:t})}editEntry(t){return A({...this.config,value:t})}create(t){return this.set({value:t}).handle({model:T})}edit(t){return this.set({value:t}).handle({entryID:A})}loginPublic(t){return M({...this.config,...t})}logoutPublic(){return B(this.config)}loginEc(t){return G({...this.config,...t})}logoutEc(){return U(this.config)}}const g=(...e)=>{const[t]=e;e.forEach(n=>{d.prototype[n]=function(o){return this.set({[t]:o})}})},O=(e,t)=>{d.prototype[e]=d.prototype[t]};g("dmShortID","dm","dmshortid");g("model");g("token");g("entryID","entry");g("assetGroup");g("assetID","asset");O("assetgroup","assetGroup");O("entryList","entries");O("assetList","assets");const H=e=>new d({env:e}),l=N.persistentMap("fdk.auth",{},{encode:JSON.stringify,decode:JSON.parse});function E(e){let{dmShortID:t}=e;if(t)return l.get()?.[t]}function b(e){let{env:t}=e;if(t)return l.get()?.[t]}function Q(e){return b(e)||E(e)}function V(e){return!!b(e)?.token||!!E(e)?.token}function W(e){return!!b(e)?.token}function X(e){return!!E(e)?.token}async function Y(e){const t=await w(e);return l.setKey(e.dmShortID,{token:t.token}),t}async function Z(e){const t=await P(e);return l.setKey(e.env,{token:t.token}),t}async function tt(e){const t=await S(e);return l.setKey(e.dmShortID,{}),t}async function et(e){const t=await m(e);return l.setKey(e.env,{token:t.token}),t}exports.act=F;exports.apiURL=u;exports.assetList=x;exports.auth=l;exports.createEntry=D;exports.editEntry=I;exports.entryList=_;exports.expect=c;exports.fetcher=a;exports.getAsset=v;exports.getAuth=Q;exports.getEcAuth=b;exports.getEntry=q;exports.getPublicAuth=E;exports.hasAnyToken=V;exports.hasEcToken=W;exports.hasPublicToken=X;exports.loginEc=P;exports.loginEcStored=Z;exports.loginPublic=w;exports.loginPublicStored=Y;exports.logoutEc=m;exports.logoutEcStored=et;exports.logoutPublic=S;exports.logoutPublicStored=tt;exports.query=y;exports.sdk=H;
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;
package/dist/index.mjs CHANGED
@@ -1,192 +1,239 @@
1
- import { persistentMap as D } from "@nanostores/persistent";
2
- async function a(e, t = {}, n = {}) {
3
- const { token: o, rawRes: s } = t;
4
- o && (n.headers = {
5
- ...n.headers || {},
6
- Authorization: `Bearer ${o}`
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) => {
15
+ try {
16
+ d(n.next(u));
17
+ } catch (g) {
18
+ s(g);
19
+ }
20
+ }, r = (u) => {
21
+ try {
22
+ d(n.throw(u));
23
+ } catch (g) {
24
+ s(g);
25
+ }
26
+ }, d = (u) => u.done ? o(u.value) : Promise.resolve(u.value).then(c, r);
27
+ d((n = n.apply(e, t)).next());
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}`
35
+ }));
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();
7
40
  });
8
- const r = await fetch(e, n);
9
- if (console.log("fetch", e, n), !r.ok)
10
- throw new Error(r.statusText);
11
- return s ? r : await r.json();
12
41
  }
13
- const m = {
42
+ const T = {
14
43
  production: "https://datamanager.entrecode.de/",
15
44
  stage: "https://datamanager.cachena.entrecode.de/"
16
45
  };
17
- function u(e, t = "stage") {
18
- const n = m[t];
46
+ function h(e, t = "stage") {
47
+ const n = T[t];
19
48
  if (!n)
20
49
  throw new Error(
21
- `env "${t}" not found. Try one of ${Object.keys(m).join(", ")}`
50
+ `env "${t}" not found. Try one of ${Object.keys(T).join(", ")}`
22
51
  );
23
52
  return n + e;
24
53
  }
25
- function $(e, t = !0) {
54
+ function k(e, t = !0) {
26
55
  return Object.entries(e).sort((n, o) => n[0].localeCompare(o[0])).map(([n, o]) => `${n}=${o}`).join("&");
27
56
  }
28
- function c(e) {
57
+ function l(e) {
29
58
  Object.entries(e).forEach(([t, n]) => {
30
59
  if (n === void 0)
31
60
  throw new Error(`expected ${t} to be set!`);
32
61
  });
33
62
  }
34
- const S = {
63
+ const A = {
35
64
  stage: "https://accounts.cachena.entrecode.de/",
36
65
  production: "https://accounts.cachena.entrecode.de/"
37
66
  };
38
- async function j(e) {
39
- let { env: t, dmShortID: n, email: o, password: s } = e;
40
- c({ env: t, dmShortID: n, email: o, password: s });
41
- const r = u(`api/${n}/_auth/login?clientID=rest`, t);
42
- return await a(
43
- r,
44
- {},
45
- {
46
- method: "POST",
47
- body: JSON.stringify({ email: o, password: s }),
48
- headers: {
49
- "Content-Type": "application/json"
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,
74
+ {},
75
+ {
76
+ method: "POST",
77
+ body: JSON.stringify({ email: o, password: s }),
78
+ headers: {
79
+ "Content-Type": "application/json"
80
+ }
50
81
  }
51
- }
52
- );
82
+ );
83
+ });
53
84
  }
54
- async function T(e) {
55
- let { env: t, email: n, password: o } = e;
56
- c({ env: t, email: n, password: o });
57
- const s = `${S[t]}auth/login?clientID=rest`;
58
- return await a(
59
- s,
60
- {},
61
- {
62
- method: "POST",
63
- body: JSON.stringify({ email: n, password: o }),
64
- headers: {
65
- "Content-Type": "application/json"
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,
92
+ {},
93
+ {
94
+ method: "POST",
95
+ body: JSON.stringify({ email: n, password: o }),
96
+ headers: {
97
+ "Content-Type": "application/json"
98
+ }
66
99
  }
67
- }
68
- );
100
+ );
101
+ });
69
102
  }
70
- async function _(e) {
71
- let { dmShortID: t, env: n } = e;
72
- c({ dmShortID: t, env: n, token });
73
- const o = u(
74
- `api/${t}/_auth/logout?clientID=rest&token=${token}`,
75
- n
76
- );
77
- return await a(
78
- o,
79
- { dmShortID: t, rawRes: !0 },
80
- {
81
- method: "POST"
82
- }
83
- );
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
110
+ );
111
+ return yield y(
112
+ o,
113
+ { dmShortID: t, rawRes: !0 },
114
+ {
115
+ method: "POST"
116
+ }
117
+ );
118
+ });
84
119
  }
85
- async function L(e) {
86
- let { env: t, token: n } = e;
87
- c({ env: t });
88
- const o = `${S[t]}auth/logout?clientID=rest`;
89
- return await a(
90
- o,
91
- {
92
- rawRes: !0,
93
- token: n
94
- },
95
- {
96
- method: "POST"
97
- }
98
- );
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,
127
+ {
128
+ rawRes: !0,
129
+ token: n
130
+ },
131
+ {
132
+ method: "POST"
133
+ }
134
+ );
135
+ });
99
136
  }
100
- async function I(e) {
101
- let { env: t, dmShortID: n, model: o, options: s = {} } = e;
102
- c({ env: t, dmShortID: n, model: o }), s = { size: 50, page: 1, _list: !0, ...s };
103
- const r = $(s), i = u(`api/${n}/${o}?${r}`, t), { count: g, total: E, _embedded: h } = await a(i, e), f = h ? h[`${n}:${o}`] : [];
104
- return { count: g, total: E, items: f };
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 };
143
+ });
105
144
  }
106
- function q({ env: e, dmShortID: t, model: n, entryID: o, token: s }) {
107
- c({ env: e, dmShortID: t, model: n, entryID: o });
108
- const r = $({ _id: o }), i = u(`api/${t}/${n}?${r}`, e);
109
- return a(i, { dmShortID: t, token: s });
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 });
110
149
  }
111
- async function v({ env: e, dmShortID: t, model: n, value: o, token: s }) {
112
- c({ env: e, dmShortID: t, model: n, value: o }), console.log("create entry", t, n, o);
113
- const r = u(`api/${t}/${n}`, e);
114
- return console.log("url", r), await a(
115
- r,
116
- { env: e, dmShortID: t, token: s },
117
- {
118
- method: "POST",
119
- body: JSON.stringify(o),
120
- headers: {
121
- "Content-Type": "application/json"
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 },
157
+ {
158
+ method: "POST",
159
+ body: JSON.stringify(o),
160
+ headers: {
161
+ "Content-Type": "application/json"
162
+ }
122
163
  }
123
- }
124
- );
164
+ );
165
+ });
125
166
  }
126
- async function x({
127
- env: e,
128
- dmShortID: t,
129
- model: n,
130
- entryID: o,
131
- value: s,
132
- token: r
133
- }) {
134
- c({ env: e, dmShortID: t, model: n, entryID: o, value: s }), console.log("edit entry", t, n, o, s);
167
+ function U(r) {
168
+ return i(this, arguments, function* ({
169
+ env: e,
170
+ dmShortID: t,
171
+ model: n,
172
+ entryID: o,
173
+ value: s,
174
+ token: c
175
+ }) {
176
+ l({ env: e, dmShortID: t, model: n, entryID: o, value: s }), console.log("edit entry", t, n, o, s);
177
+ });
135
178
  }
136
- async function J({ env: e, dmShortID: t, assetGroup: n, assetID: o, token: s }) {
137
- c({ env: e, dmShortID: t, assetGroup: n, assetID: o });
138
- const r = $({ assetID: o }), i = u(`a/${t}/${n}?${r}`, e);
139
- return (await a(i, { dmShortID: t, token: s }))._embedded["ec:dm-asset"];
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"];
184
+ });
140
185
  }
141
- async function N(e) {
142
- let { env: t, dmShortID: n, assetGroup: o, token: s, options: r = {} } = e;
143
- c({ env: t, dmShortID: n, assetGroup: o }), r = { size: 50, page: 1, _list: !0, ...r };
144
- const i = $(r), g = u(`a/${n}/${o}?${i}`, t), { count: E, total: h, _embedded: f } = await a(g, { dmShortID: n, token: s }), A = f ? f["ec:dm-asset"] : [];
145
- return { count: E, total: h, items: A };
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 };
192
+ });
146
193
  }
147
- const p = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
194
+ const w = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
148
195
  __proto__: null,
149
- assetList: N,
150
- createEntry: v,
151
- editEntry: x,
152
- entryList: I,
153
- getAsset: J,
154
- getEntry: q,
155
- loginEc: T,
156
- loginPublic: j,
157
- logoutEc: L,
158
- logoutPublic: _
196
+ assetList: H,
197
+ createEntry: B,
198
+ editEntry: U,
199
+ entryList: G,
200
+ getAsset: F,
201
+ getEntry: M,
202
+ loginEc: I,
203
+ loginPublic: D,
204
+ logoutEc: v,
205
+ logoutPublic: q
159
206
  }, Symbol.toStringTag, { value: "Module" })), {
160
- entryList: z,
161
- getEntry: C,
162
- getAsset: K,
163
- assetList: R,
164
- createEntry: P,
165
- editEntry: O,
166
- loginPublic: G,
167
- loginEc: M,
168
- logoutEc: B,
169
- logoutPublic: U
170
- } = p;
171
- function H(e) {
207
+ entryList: Q,
208
+ getEntry: V,
209
+ getAsset: W,
210
+ assetList: X,
211
+ createEntry: _,
212
+ editEntry: L,
213
+ loginPublic: Y,
214
+ loginEc: Z,
215
+ logoutEc: tt,
216
+ logoutPublic: et
217
+ } = w;
218
+ function st(e) {
172
219
  const { action: t } = e;
173
- if (c({ action: t }), !p[t])
220
+ if (l({ action: t }), !w[t])
174
221
  throw new Error(
175
- `"${t}" does not exist! try one of ${Object.keys(p).join(
222
+ `"${t}" does not exist! try one of ${Object.keys(w).join(
176
223
  ", "
177
224
  )}`
178
225
  );
179
- return p[t](e);
226
+ return w[t](e);
180
227
  }
181
- class l {
228
+ class p {
182
229
  constructor(t) {
183
230
  this.config = t;
184
231
  }
185
232
  set(t) {
186
- return new l({ ...this.config, ...t });
233
+ return new p(a(a({}, this.config), t));
187
234
  }
188
235
  handle(t) {
189
- const n = Object.keys(this.config), [o, s] = Object.entries(t).find(([r, i]) => n.includes(r)) || [];
236
+ const n = Object.keys(this.config), [o, s] = Object.entries(t).find(([c, r]) => n.includes(c)) || [];
190
237
  if (!s)
191
238
  throw new Error(
192
239
  `you need to first set ${Object.keys(t).join(" | ")}`
@@ -195,71 +242,71 @@ class l {
195
242
  }
196
243
  ///
197
244
  entries(t) {
198
- return z({ ...this.config, options: t });
245
+ return Q(f(a({}, this.config), { options: t }));
199
246
  }
200
247
  assets(t) {
201
- return R({ ...this.config, options: t });
248
+ return X(f(a({}, this.config), { options: t }));
202
249
  }
203
250
  get() {
204
251
  return this.handle({
205
- assetID: K,
206
- entryID: C
252
+ assetID: W,
253
+ entryID: V
207
254
  });
208
255
  }
209
256
  del() {
210
257
  }
211
258
  createEntry(t) {
212
- return P({ ...this.config, value: t });
259
+ return _(f(a({}, this.config), { value: t }));
213
260
  }
214
261
  editEntry(t) {
215
- return O({ ...this.config, value: t });
262
+ return L(f(a({}, this.config), { value: t }));
216
263
  }
217
264
  create(t) {
218
265
  return this.set({ value: t }).handle({
219
266
  //assetID: createAsset,
220
- model: P
267
+ model: _
221
268
  });
222
269
  }
223
270
  edit(t) {
224
271
  return this.set({ value: t }).handle({
225
272
  //assetID: createAsset,
226
- entryID: O
273
+ entryID: L
227
274
  });
228
275
  }
229
276
  //
230
277
  loginPublic(t) {
231
- return G({ ...this.config, ...t });
278
+ return Y(a(a({}, this.config), t));
232
279
  }
233
280
  logoutPublic() {
234
- return U(this.config);
281
+ return et(this.config);
235
282
  }
236
283
  loginEc(t) {
237
- return M({ ...this.config, ...t });
284
+ return Z(a(a({}, this.config), t));
238
285
  }
239
286
  logoutEc() {
240
- return B(this.config);
287
+ return tt(this.config);
241
288
  }
242
289
  }
243
- const d = (...e) => {
290
+ const $ = (...e) => {
244
291
  const [t] = e;
245
292
  e.forEach((n) => {
246
- l.prototype[n] = function(o) {
293
+ p.prototype[n] = function(o) {
247
294
  return this.set({ [t]: o });
248
295
  };
249
296
  });
250
- }, b = (e, t) => {
251
- l.prototype[e] = l.prototype[t];
297
+ }, m = (e, t) => {
298
+ p.prototype[e] = p.prototype[t];
252
299
  };
253
- d("dmShortID", "dm", "dmshortid");
254
- d("model");
255
- d("token");
256
- d("entryID", "entry");
257
- d("assetGroup");
258
- d("assetID", "asset");
259
- b("assetgroup", "assetGroup");
260
- b("entryList", "entries");
261
- b("assetList", "assets");
262
- const Q = (e) => new l({ env: e }), y = D(
300
+ $("dmShortID", "dm", "dmshortid");
301
+ $("model");
302
+ $("token");
303
+ $("entryID", "entry");
304
+ $("assetGroup");
305
+ $("assetID", "asset");
306
+ m("assetgroup", "assetGroup");
307
+ m("entryList", "entries");
308
+ m("assetList", "assets");
309
+ const rt = (e) => new p({ env: e }), E = R(
263
310
  "fdk.auth",
264
311
  {},
265
312
  {
@@ -267,70 +314,83 @@ const Q = (e) => new l({ env: e }), y = D(
267
314
  decode: JSON.parse
268
315
  }
269
316
  );
270
- function w(e) {
317
+ function P(e) {
318
+ var n;
271
319
  let { dmShortID: t } = e;
272
320
  if (t)
273
- return y.get()?.[t];
321
+ return (n = E.get()) == null ? void 0 : n[t];
274
322
  }
275
- function k(e) {
323
+ function O(e) {
324
+ var n;
276
325
  let { env: t } = e;
277
326
  if (t)
278
- return y.get()?.[t];
327
+ return (n = E.get()) == null ? void 0 : n[t];
279
328
  }
280
- function V(e) {
281
- return k(e) || w(e);
329
+ function ct(e) {
330
+ return O(e) || P(e);
282
331
  }
283
- function W(e) {
284
- return !!k(e)?.token || !!w(e)?.token;
332
+ function it(e) {
333
+ var t, n;
334
+ return !!((t = O(e)) != null && t.token) || !!((n = P(e)) != null && n.token);
285
335
  }
286
- function X(e) {
287
- return !!k(e)?.token;
336
+ function at(e) {
337
+ var t;
338
+ return !!((t = O(e)) != null && t.token);
288
339
  }
289
- function Y(e) {
290
- return !!w(e)?.token;
340
+ function ut(e) {
341
+ var t;
342
+ return !!((t = P(e)) != null && t.token);
291
343
  }
292
- async function Z(e) {
293
- const t = await j(e);
294
- return y.setKey(e.dmShortID, { token: t.token }), t;
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
+ });
295
349
  }
296
- async function tt(e) {
297
- const t = await T(e);
298
- return y.setKey(e.env, { token: t.token }), t;
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
+ });
299
355
  }
300
- async function et(e) {
301
- const t = await _(e);
302
- return y.setKey(e.dmShortID, {}), t;
356
+ function yt(e) {
357
+ return i(this, null, function* () {
358
+ const t = yield q(e);
359
+ return E.setKey(e.dmShortID, {}), t;
360
+ });
303
361
  }
304
- async function nt(e) {
305
- const t = await L(e);
306
- return y.setKey(e.env, { token: t.token }), t;
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
+ });
307
367
  }
308
368
  export {
309
- H as act,
310
- u as apiURL,
311
- N as assetList,
312
- y as auth,
313
- v as createEntry,
314
- x as editEntry,
315
- I as entryList,
316
- c as expect,
317
- a as fetcher,
318
- J as getAsset,
319
- V as getAuth,
320
- k as getEcAuth,
321
- q as getEntry,
322
- w as getPublicAuth,
323
- W as hasAnyToken,
324
- X as hasEcToken,
325
- Y as hasPublicToken,
326
- T as loginEc,
327
- tt as loginEcStored,
328
- j as loginPublic,
329
- Z as loginPublicStored,
330
- L as logoutEc,
331
- nt as logoutEcStored,
332
- _ as logoutPublic,
333
- et as logoutPublicStored,
334
- $ as query,
335
- Q as sdk
369
+ st as act,
370
+ h as apiURL,
371
+ H as assetList,
372
+ E as auth,
373
+ B as createEntry,
374
+ U as editEntry,
375
+ G as entryList,
376
+ l as expect,
377
+ y as fetcher,
378
+ 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
336
396
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ec.fdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "ec frontend sdk",
5
5
  "main": "dist/index.cjs",
6
6
  "directories": {
package/src/index.mjs DELETED
@@ -1,4 +0,0 @@
1
- export * from "./lib/api.mjs";
2
- export * from "./lib/util.mjs";
3
- export * from "./lib/actions.mjs";
4
- export * from "./storage.mjs";
package/src/lib/README.md DELETED
@@ -1,7 +0,0 @@
1
- # lib
2
-
3
- everything in here is:
4
-
5
- - stateless
6
- - agnostic of environment (node, browser)
7
- - pure functions
@@ -1,3 +0,0 @@
1
- export * from "./auth.mjs";
2
- export * from "./entries.mjs";
3
- export * from "./assets.mjs";
package/src/lib/api.mjs DELETED
@@ -1,137 +0,0 @@
1
- import * as actions from "./actions.mjs";
2
- import { expect } from "./util.mjs";
3
- export * from "./util.mjs";
4
-
5
- const {
6
- entryList,
7
- getEntry,
8
- getAsset,
9
- assetList,
10
- createEntry,
11
- editEntry,
12
- loginPublic,
13
- loginEc,
14
- logoutEc,
15
- logoutPublic,
16
- } = actions;
17
-
18
- export function act(config) {
19
- const { action } = config;
20
- expect({ action });
21
- if (!actions[action]) {
22
- throw new Error(
23
- `"${action}" does not exist! try one of ${Object.keys(actions).join(
24
- ", "
25
- )}`
26
- );
27
- }
28
- return actions[action](config);
29
- }
30
-
31
- class Sdk {
32
- constructor(config) {
33
- this.config = config;
34
- }
35
-
36
- set(obj) {
37
- // "copy on write"
38
- return new Sdk({ ...this.config, ...obj });
39
- }
40
-
41
- handle(handlers) {
42
- const keys = Object.keys(this.config);
43
- const [_, handle] =
44
- Object.entries(handlers).find(([k, v]) => keys.includes(k)) || [];
45
- if (!handle) {
46
- throw new Error(
47
- `you need to first set ${Object.keys(handlers).join(" | ")}`
48
- );
49
- }
50
- return handle(this.config);
51
- }
52
-
53
- ///
54
-
55
- entries(options) {
56
- return entryList({ ...this.config, options });
57
- }
58
-
59
- assets(options) {
60
- return assetList({ ...this.config, options });
61
- }
62
-
63
- get() {
64
- return this.handle({
65
- assetID: getAsset,
66
- entryID: getEntry,
67
- });
68
- }
69
-
70
- del() {}
71
-
72
- createEntry(value) {
73
- return createEntry({ ...this.config, value });
74
- }
75
- editEntry(value) {
76
- return editEntry({ ...this.config, value });
77
- }
78
-
79
- create(value) {
80
- return this.set({ value }).handle({
81
- //assetID: createAsset,
82
- model: createEntry,
83
- });
84
- }
85
-
86
- edit(value) {
87
- return this.set({ value }).handle({
88
- //assetID: createAsset,
89
- entryID: editEntry,
90
- });
91
- }
92
-
93
- //
94
-
95
- loginPublic(config) {
96
- //
97
- return loginPublic({ ...this.config, ...config });
98
- }
99
-
100
- logoutPublic() {
101
- return logoutPublic(this.config);
102
- }
103
-
104
- loginEc(config) {
105
- return loginEc({ ...this.config, ...config });
106
- }
107
-
108
- logoutEc() {
109
- return logoutEc(this.config);
110
- }
111
- }
112
-
113
- const addSetter = (...args) => {
114
- const [main] = args;
115
-
116
- args.forEach((alias) => {
117
- Sdk.prototype[alias] = function (value) {
118
- return this.set({ [main]: value });
119
- };
120
- });
121
- };
122
-
123
- const addAlias = (a, b) => {
124
- Sdk.prototype[a] = Sdk.prototype[b];
125
- };
126
- // define setters with aliases
127
- addSetter("dmShortID", "dm", "dmshortid");
128
- addSetter("model");
129
- addSetter("token");
130
- addSetter("entryID", "entry");
131
- addSetter("assetGroup");
132
- addSetter("assetID", "asset");
133
- addAlias("assetgroup", "assetGroup");
134
- addAlias("entryList", "entries");
135
- addAlias("assetList", "assets");
136
-
137
- export const sdk = (env) => new Sdk({ env });
@@ -1,21 +0,0 @@
1
- import { expect, query, apiURL, fetcher } from "./util.mjs";
2
-
3
- export async function getAsset({ env, dmShortID, assetGroup, assetID, token }) {
4
- expect({ env, dmShortID, assetGroup, assetID });
5
- const q = query({ assetID: assetID });
6
- const url = apiURL(`a/${dmShortID}/${assetGroup}?${q}`, env);
7
- const list = await fetcher(url, { dmShortID, token });
8
- return list._embedded["ec:dm-asset"];
9
- }
10
-
11
- export async function assetList(config) {
12
- let { env, dmShortID, assetGroup, token, options = {} } = config;
13
- expect({ env, dmShortID, assetGroup });
14
- options = { size: 50, page: 1, _list: true, ...options };
15
- // name~ = search
16
- const q = query(options);
17
- const url = apiURL(`a/${dmShortID}/${assetGroup}?${q}`, env);
18
- const { count, total, _embedded } = await fetcher(url, { dmShortID, token });
19
- const items = _embedded ? _embedded[`ec:dm-asset`] : [];
20
- return { count, total, items };
21
- }
package/src/lib/auth.mjs DELETED
@@ -1,77 +0,0 @@
1
- import { expect, apiURL, fetcher } from "./util.mjs";
2
-
3
- const accountServer = {
4
- stage: "https://accounts.cachena.entrecode.de/",
5
- production: "https://accounts.cachena.entrecode.de/",
6
- };
7
-
8
- export async function loginPublic(config) {
9
- let { env, dmShortID, email, password } = config;
10
- expect({ env, dmShortID, email, password });
11
- // TODO: check if already logged in?
12
- const url = apiURL(`api/${dmShortID}/_auth/login?clientID=rest`, env);
13
- const res = await fetcher(
14
- url,
15
- {},
16
- {
17
- method: "POST",
18
- body: JSON.stringify({ email, password }),
19
- headers: {
20
- "Content-Type": "application/json",
21
- },
22
- }
23
- );
24
- return res;
25
- }
26
-
27
- export async function loginEc(config) {
28
- let { env, email, password } = config;
29
- expect({ env, email, password });
30
- const url = `${accountServer[env]}auth/login?clientID=rest`;
31
- const res = await fetcher(
32
- url,
33
- {},
34
- {
35
- method: "POST",
36
- body: JSON.stringify({ email, password }),
37
- headers: {
38
- "Content-Type": "application/json",
39
- },
40
- }
41
- );
42
- return res;
43
- }
44
-
45
- export async function logoutPublic(config) {
46
- let { dmShortID, env } = config;
47
- expect({ dmShortID, env, token });
48
- const url = apiURL(
49
- `api/${dmShortID}/_auth/logout?clientID=rest&token=${token}`,
50
- env
51
- );
52
- const res = await fetcher(
53
- url,
54
- { dmShortID, rawRes: true },
55
- {
56
- method: "POST",
57
- }
58
- );
59
- return res;
60
- }
61
-
62
- export async function logoutEc(config) {
63
- let { env, token } = config;
64
- expect({ env });
65
- const url = `${accountServer[env]}auth/logout?clientID=rest`;
66
- const res = await fetcher(
67
- url,
68
- {
69
- rawRes: true,
70
- token,
71
- },
72
- {
73
- method: "POST",
74
- }
75
- );
76
- return res;
77
- }
@@ -1,54 +0,0 @@
1
- import { expect, query, apiURL, fetcher } from "./util.mjs";
2
-
3
- export async function entryList(config) {
4
- let { env, dmShortID, model, options = {} } = config;
5
- expect({ env, dmShortID, model });
6
- options = { size: 50, page: 1, _list: true, ...options };
7
- // name~ = search
8
- const q = query(options);
9
- const url = apiURL(`api/${dmShortID}/${model}?${q}`, env);
10
- const { count, total, _embedded } = await fetcher(url, config);
11
-
12
- const items = _embedded ? _embedded[`${dmShortID}:${model}`] : [];
13
- return { count, total, items };
14
- }
15
-
16
- export function getEntry({ env, dmShortID, model, entryID, token }) {
17
- expect({ env, dmShortID, model, entryID });
18
- const q = query({ _id: entryID });
19
- const url = apiURL(`api/${dmShortID}/${model}?${q}`, env);
20
- return fetcher(url, { dmShortID, token });
21
- }
22
-
23
- export async function createEntry({ env, dmShortID, model, value, token }) {
24
- expect({ env, dmShortID, model, value });
25
- console.log("create entry", dmShortID, model, value);
26
- const url = apiURL(`api/${dmShortID}/${model}`, env);
27
- console.log("url", url);
28
- const res = await fetcher(
29
- url,
30
- { env, dmShortID, token },
31
- {
32
- method: "POST",
33
- body: JSON.stringify(value),
34
- headers: {
35
- "Content-Type": "application/json",
36
- },
37
- }
38
- );
39
- return res;
40
- }
41
-
42
- export async function editEntry({
43
- env,
44
- dmShortID,
45
- model,
46
- entryID,
47
- value,
48
- token,
49
- }) {
50
- expect({ env, dmShortID, model, entryID, value });
51
- console.log("edit entry", dmShortID, model, entryID, value);
52
- return;
53
- // https://datamanager.cachena.entrecode.de/api/83cc6374/
54
- }
package/src/lib/util.mjs DELETED
@@ -1,48 +0,0 @@
1
- export async function fetcher(url, config = {}, options = {}) {
2
- const { token, rawRes } = config;
3
- if (token) {
4
- options.headers = {
5
- ...(options.headers || {}),
6
- Authorization: `Bearer ${token}`,
7
- };
8
- }
9
- const res = await fetch(url, options);
10
- console.log("fetch", url, options);
11
- if (!res.ok) {
12
- throw new Error(res.statusText);
13
- }
14
- if (rawRes) {
15
- return res;
16
- }
17
- return await res.json();
18
- }
19
-
20
- const apis = {
21
- production: "https://datamanager.entrecode.de/",
22
- stage: "https://datamanager.cachena.entrecode.de/",
23
- };
24
-
25
- export function apiURL(route, env = "stage") {
26
- const base = apis[env];
27
- if (!base) {
28
- throw new Error(
29
- `env "${env}" not found. Try one of ${Object.keys(apis).join(", ")}`
30
- );
31
- }
32
- return base + route;
33
- }
34
-
35
- export function query(params, sort = true) {
36
- return Object.entries(params)
37
- .sort((a, b) => a[0].localeCompare(b[0]))
38
- .map(([key, value]) => `${key}=${value}`)
39
- .join("&");
40
- }
41
-
42
- export function expect(obj) {
43
- Object.entries(obj).forEach(([key, value]) => {
44
- if (value === undefined) {
45
- throw new Error(`expected ${key} to be set!`);
46
- }
47
- });
48
- }
package/src/storage.mjs DELETED
@@ -1,70 +0,0 @@
1
- import { persistentMap } from "@nanostores/persistent";
2
- import {
3
- loginPublic,
4
- logoutPublic,
5
- loginEc,
6
- logoutEc,
7
- } from "./lib/actions.mjs";
8
-
9
- // maybe switch to cookie: https://github.com/nanostores/persistent#persistent-engines
10
- export const auth = persistentMap(
11
- "fdk.auth",
12
- {},
13
- {
14
- encode: JSON.stringify,
15
- decode: JSON.parse,
16
- }
17
- );
18
-
19
- export function getPublicAuth(config) {
20
- let { dmShortID } = config;
21
- if (!dmShortID) {
22
- return;
23
- }
24
- return auth.get()?.[dmShortID];
25
- }
26
-
27
- export function getEcAuth(config) {
28
- let { env } = config;
29
- if (!env) {
30
- return;
31
- }
32
- return auth.get()?.[env];
33
- }
34
-
35
- export function getAuth(config) {
36
- return getEcAuth(config) || getPublicAuth(config);
37
- }
38
-
39
- export function hasAnyToken(config) {
40
- return !!getEcAuth(config)?.token || !!getPublicAuth(config)?.token;
41
- }
42
-
43
- export function hasEcToken(config) {
44
- return !!getEcAuth(config)?.token;
45
- }
46
- export function hasPublicToken(config) {
47
- return !!getPublicAuth(config)?.token;
48
- }
49
-
50
- export async function loginPublicStored(config) {
51
- const res = await loginPublic(config);
52
- auth.setKey(config.dmShortID, { token: res.token });
53
- return res;
54
- }
55
- export async function loginEcStored(config) {
56
- const res = await loginEc(config);
57
- auth.setKey(config.env, { token: res.token });
58
- return res;
59
- }
60
-
61
- export async function logoutPublicStored(config) {
62
- const res = await logoutPublic(config);
63
- auth.setKey(config.dmShortID, {});
64
- return res;
65
- }
66
- export async function logoutEcStored(config) {
67
- const res = await logoutEc(config);
68
- auth.setKey(config.env, { token: res.token });
69
- return res;
70
- }
package/vite.config.js DELETED
@@ -1,19 +0,0 @@
1
- import { defineConfig } from "vite";
2
- import { dependencies } from "./package.json";
3
- import { resolve } from "path";
4
-
5
- // https://vitejs.dev/config/
6
- export default defineConfig({
7
- plugins: [],
8
- build: {
9
- lib: {
10
- entry: resolve(__dirname, "src", "index.mjs"),
11
- formats: ["es", "cjs"],
12
- fileName: (ext) => ({ es: "index.mjs", cjs: "index.cjs" }[ext]),
13
- },
14
- rollupOptions: {
15
- external: [...Object.keys(dependencies)],
16
- },
17
- target: "esnext",
18
- },
19
- });