ec.fdk 0.1.0

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # ec.fdk
2
+
3
+ pssst
package/dist/index.cjs ADDED
@@ -0,0 +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;
package/dist/index.mjs ADDED
@@ -0,0 +1,336 @@
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}`
7
+ });
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
+ }
13
+ const m = {
14
+ production: "https://datamanager.entrecode.de/",
15
+ stage: "https://datamanager.cachena.entrecode.de/"
16
+ };
17
+ function u(e, t = "stage") {
18
+ const n = m[t];
19
+ if (!n)
20
+ throw new Error(
21
+ `env "${t}" not found. Try one of ${Object.keys(m).join(", ")}`
22
+ );
23
+ return n + e;
24
+ }
25
+ function $(e, t = !0) {
26
+ return Object.entries(e).sort((n, o) => n[0].localeCompare(o[0])).map(([n, o]) => `${n}=${o}`).join("&");
27
+ }
28
+ function c(e) {
29
+ Object.entries(e).forEach(([t, n]) => {
30
+ if (n === void 0)
31
+ throw new Error(`expected ${t} to be set!`);
32
+ });
33
+ }
34
+ const S = {
35
+ stage: "https://accounts.cachena.entrecode.de/",
36
+ production: "https://accounts.cachena.entrecode.de/"
37
+ };
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"
50
+ }
51
+ }
52
+ );
53
+ }
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"
66
+ }
67
+ }
68
+ );
69
+ }
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
+ );
84
+ }
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
+ );
99
+ }
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 };
105
+ }
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 });
110
+ }
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"
122
+ }
123
+ }
124
+ );
125
+ }
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);
135
+ }
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"];
140
+ }
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 };
146
+ }
147
+ const p = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
148
+ __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: _
159
+ }, 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) {
172
+ const { action: t } = e;
173
+ if (c({ action: t }), !p[t])
174
+ throw new Error(
175
+ `"${t}" does not exist! try one of ${Object.keys(p).join(
176
+ ", "
177
+ )}`
178
+ );
179
+ return p[t](e);
180
+ }
181
+ class l {
182
+ constructor(t) {
183
+ this.config = t;
184
+ }
185
+ set(t) {
186
+ return new l({ ...this.config, ...t });
187
+ }
188
+ handle(t) {
189
+ const n = Object.keys(this.config), [o, s] = Object.entries(t).find(([r, i]) => n.includes(r)) || [];
190
+ if (!s)
191
+ throw new Error(
192
+ `you need to first set ${Object.keys(t).join(" | ")}`
193
+ );
194
+ return s(this.config);
195
+ }
196
+ ///
197
+ entries(t) {
198
+ return z({ ...this.config, options: t });
199
+ }
200
+ assets(t) {
201
+ return R({ ...this.config, options: t });
202
+ }
203
+ get() {
204
+ return this.handle({
205
+ assetID: K,
206
+ entryID: C
207
+ });
208
+ }
209
+ del() {
210
+ }
211
+ createEntry(t) {
212
+ return P({ ...this.config, value: t });
213
+ }
214
+ editEntry(t) {
215
+ return O({ ...this.config, value: t });
216
+ }
217
+ create(t) {
218
+ return this.set({ value: t }).handle({
219
+ //assetID: createAsset,
220
+ model: P
221
+ });
222
+ }
223
+ edit(t) {
224
+ return this.set({ value: t }).handle({
225
+ //assetID: createAsset,
226
+ entryID: O
227
+ });
228
+ }
229
+ //
230
+ loginPublic(t) {
231
+ return G({ ...this.config, ...t });
232
+ }
233
+ logoutPublic() {
234
+ return U(this.config);
235
+ }
236
+ loginEc(t) {
237
+ return M({ ...this.config, ...t });
238
+ }
239
+ logoutEc() {
240
+ return B(this.config);
241
+ }
242
+ }
243
+ const d = (...e) => {
244
+ const [t] = e;
245
+ e.forEach((n) => {
246
+ l.prototype[n] = function(o) {
247
+ return this.set({ [t]: o });
248
+ };
249
+ });
250
+ }, b = (e, t) => {
251
+ l.prototype[e] = l.prototype[t];
252
+ };
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(
263
+ "fdk.auth",
264
+ {},
265
+ {
266
+ encode: JSON.stringify,
267
+ decode: JSON.parse
268
+ }
269
+ );
270
+ function w(e) {
271
+ let { dmShortID: t } = e;
272
+ if (t)
273
+ return y.get()?.[t];
274
+ }
275
+ function k(e) {
276
+ let { env: t } = e;
277
+ if (t)
278
+ return y.get()?.[t];
279
+ }
280
+ function V(e) {
281
+ return k(e) || w(e);
282
+ }
283
+ function W(e) {
284
+ return !!k(e)?.token || !!w(e)?.token;
285
+ }
286
+ function X(e) {
287
+ return !!k(e)?.token;
288
+ }
289
+ function Y(e) {
290
+ return !!w(e)?.token;
291
+ }
292
+ async function Z(e) {
293
+ const t = await j(e);
294
+ return y.setKey(e.dmShortID, { token: t.token }), t;
295
+ }
296
+ async function tt(e) {
297
+ const t = await T(e);
298
+ return y.setKey(e.env, { token: t.token }), t;
299
+ }
300
+ async function et(e) {
301
+ const t = await _(e);
302
+ return y.setKey(e.dmShortID, {}), t;
303
+ }
304
+ async function nt(e) {
305
+ const t = await L(e);
306
+ return y.setKey(e.env, { token: t.token }), t;
307
+ }
308
+ 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
336
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "ec.fdk",
3
+ "version": "0.1.0",
4
+ "description": "ec frontend sdk",
5
+ "main": "dist/index.cjs",
6
+ "directories": {
7
+ "lib": "lib"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/felixroos/ec.fdk.git"
12
+ },
13
+ "keywords": [
14
+ "cool"
15
+ ],
16
+ "author": "Felix Roos <roos@entrecode.de>",
17
+ "license": "ISC",
18
+ "bugs": {
19
+ "url": "https://github.com/felixroos/ec.fdk/issues"
20
+ },
21
+ "homepage": "https://github.com/felixroos/ec.fdk#readme",
22
+ "dependencies": {
23
+ "@nanostores/persistent": "^0.9.1",
24
+ "nanostores": "^0.9.3"
25
+ },
26
+ "devDependencies": {
27
+ "vite": "^4.4.9"
28
+ },
29
+ "scripts": {
30
+ "build": "vite build"
31
+ },
32
+ "module": "dist/index.mjs"
33
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./lib/api.mjs";
2
+ export * from "./lib/util.mjs";
3
+ export * from "./lib/actions.mjs";
4
+ export * from "./storage.mjs";
@@ -0,0 +1,7 @@
1
+ # lib
2
+
3
+ everything in here is:
4
+
5
+ - stateless
6
+ - agnostic of environment (node, browser)
7
+ - pure functions
@@ -0,0 +1,3 @@
1
+ export * from "./auth.mjs";
2
+ export * from "./entries.mjs";
3
+ export * from "./assets.mjs";
@@ -0,0 +1,137 @@
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 });
@@ -0,0 +1,21 @@
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
+ }
@@ -0,0 +1,77 @@
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
+ }
@@ -0,0 +1,54 @@
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
+ }
@@ -0,0 +1,48 @@
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
+ }
@@ -0,0 +1,70 @@
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 ADDED
@@ -0,0 +1,19 @@
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
+ });