ec.fdk 0.2.1 → 0.2.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/README.hbs ADDED
@@ -0,0 +1,47 @@
1
+ # ec.fdk
2
+
3
+ Lightweight SDK for entrecode APIs.
4
+ Currently supports only most common PublicAPI functions.
5
+
6
+ ## Install
7
+
8
+ ```sh
9
+ npm i ec.fdk
10
+ ```
11
+
12
+ # API
13
+
14
+ Start by calling `sdk` with your environment (`stage` | `production`), then method chain your way to success:
15
+
16
+ ```js
17
+ import { sdk } from "ec.fdk";
18
+
19
+ sdk("stage") // choose stage environment
20
+ .dm("83cc6374") // select datamanager via short id
21
+ .model("muffin") // select model muffin
22
+ .entries() // load entry list
23
+ .then(list => {
24
+ console.log(list);
25
+ })
26
+ ```
27
+
28
+ You can also reuse parts of the chain with variables:
29
+
30
+ ```js
31
+ // we want to do stuff with model muffin here
32
+ const muffin = sdk("stage").dm("83cc6374").model("muffin");
33
+ // load entry list
34
+ const { items } = await muffin.entries();
35
+ // edit first entry
36
+ await muffin.editEntry(items[0].id, { name: "edit!" });
37
+ // delete second entry
38
+ await muffin.deleteEntry(items[1].id);
39
+ // create a new muffin
40
+ await muffin.createEntry({ name: "new muffin" });
41
+ ```
42
+
43
+ Now what follows is the autogenerated doc from source:
44
+
45
+ {{#module name="api"}}
46
+ {{>members}}
47
+ {{/module}}
package/README.md CHANGED
@@ -1,3 +1,294 @@
1
1
  # ec.fdk
2
2
 
3
- pssst
3
+ Lightweight SDK for entrecode APIs.
4
+ Currently supports only most common PublicAPI functions.
5
+
6
+ ## Install
7
+
8
+ ```sh
9
+ npm i ec.fdk
10
+ ```
11
+
12
+ # API
13
+
14
+ Start by calling `sdk` with your environment (`stage` | `production`), then method chain your way to success:
15
+
16
+ ```js
17
+ import { sdk } from "ec.fdk";
18
+
19
+ sdk("stage") // choose stage environment
20
+ .dm("83cc6374") // select datamanager via short id
21
+ .model("muffin") // select model muffin
22
+ .entries() // load entry list
23
+ .then(list => {
24
+ console.log(list);
25
+ })
26
+ ```
27
+
28
+ You can also reuse parts of the chain with variables:
29
+
30
+ ```js
31
+ // we want to do stuff with model muffin here
32
+ const muffin = sdk("stage").dm("83cc6374").model("muffin");
33
+ // load entry list
34
+ const { items } = await muffin.entries();
35
+ // edit first entry
36
+ await muffin.editEntry(items[0].id, { name: "edit!" });
37
+ // delete second entry
38
+ await muffin.deleteEntry(items[1].id);
39
+ // create a new muffin
40
+ await muffin.createEntry({ name: "new muffin" });
41
+ ```
42
+
43
+ Now what follows is the autogenerated doc from source:
44
+
45
+ <a name="module_api.Sdk"></a>
46
+
47
+ ### api.Sdk
48
+ <p>SDK</p>
49
+
50
+ **Kind**: static class of [<code>api</code>](#module_api)
51
+
52
+ * [.Sdk](#module_api.Sdk)
53
+ * [.entries([options])](#module_api.Sdk+entries) ⇒ <code>Object</code>
54
+ * [.getEntry(entryID)](#module_api.Sdk+getEntry) ⇒ <code>object</code>
55
+ * [.assets([options])](#module_api.Sdk+assets) ⇒ <code>Object</code>
56
+ * [.createAsset([options])](#module_api.Sdk+createAsset) ⇒ <code>Object</code>
57
+ * [.getAsset(assetID)](#module_api.Sdk+getAsset) ⇒ <code>object</code>
58
+ * [.createEntry(value)](#module_api.Sdk+createEntry) ⇒ <code>object</code>
59
+ * [.editEntry(entryID, value)](#module_api.Sdk+editEntry) ⇒ <code>object</code>
60
+ * [.deleteEntry(entryID)](#module_api.Sdk+deleteEntry) ⇒ <code>object</code>
61
+ * [.model(model)](#module_api.Sdk+model) ⇒
62
+ * [.token(token)](#module_api.Sdk+token) ⇒
63
+ * [.dmShortID(dmShortID)](#module_api.Sdk+dmShortID) ⇒
64
+ * [.dm(dmShortID)](#module_api.Sdk+dm) ⇒
65
+ * [.assetGroup(assetGroup)](#module_api.Sdk+assetGroup) ⇒
66
+ * [.assetgroup(assetGroup)](#module_api.Sdk+assetgroup) ⇒
67
+
68
+ <a name="module_api.Sdk+entries"></a>
69
+
70
+ #### sdk.entries([options]) ⇒ <code>Object</code>
71
+ <p>Loads entry list. Expects <code>dmShortID</code> / <code>model</code> to be set.
72
+ If the model is not public, you also need to provide a <code>token</code>.</p>
73
+
74
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
75
+
76
+ | Param | Type | Description |
77
+ | --- | --- | --- |
78
+ | [options] | <code>object</code> | <p>options for entry list request.</p> |
79
+
80
+ **Example**
81
+ ```js
82
+ // public model
83
+ const muffins = await sdk("stage").dm("83cc6374").model("muffin").entries()
84
+ ```
85
+ **Example**
86
+ ```js
87
+ // non-public model
88
+ const secrets = await sdk("stage").token(token).dm("83cc6374").model("secret").entries()
89
+ ```
90
+ <a name="module_api.Sdk+getEntry"></a>
91
+
92
+ #### sdk.getEntry(entryID) ⇒ <code>object</code>
93
+ <p>Loads a single entry. Expects <code>dmShortID</code> / <code>model</code> to be set.
94
+ If the model is not public, you also need to provide a <code>token</code>.</p>
95
+
96
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
97
+
98
+ | Param | Type |
99
+ | --- | --- |
100
+ | entryID | <code>string</code> |
101
+
102
+ **Example**
103
+ ```js
104
+ const muffin = await sdk("stage").dm("83cc6374").model("muffin").getEntry("1gOtzWvrdq")
105
+ ```
106
+ <a name="module_api.Sdk+assets"></a>
107
+
108
+ #### sdk.assets([options]) ⇒ <code>Object</code>
109
+ <p>Loads asset list. Expects <code>dmShortID</code> / <code>assetGroup</code> to be set.
110
+ If the assetGroup is not public, you also need to provide a <code>token</code>.</p>
111
+
112
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
113
+
114
+ | Param | Type | Description |
115
+ | --- | --- | --- |
116
+ | [options] | <code>object</code> | <p>options for entry list request.</p> |
117
+
118
+ **Example**
119
+ ```js
120
+ // public assetGroup
121
+ const files = await sdk("stage").dm("83cc6374").assetGroup("avatars").assets()
122
+ ```
123
+ **Example**
124
+ ```js
125
+ // non-public assetGroup
126
+ const files = await sdk("stage").token(token).dm("83cc6374").assetGroup("avatars").assets()
127
+ ```
128
+ <a name="module_api.Sdk+createAsset"></a>
129
+
130
+ #### sdk.createAsset([options]) ⇒ <code>Object</code>
131
+ <p>Uploads an asset. Expects <code>dmShortID</code> / <code>assetGroup</code> / <code>file</code> to be set.
132
+ If the assetGroup is not public, you also need to provide a <code>token</code>.</p>
133
+
134
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
135
+
136
+ | Param | Type | Description |
137
+ | --- | --- | --- |
138
+ | [options] | <code>object</code> | <p>options for entry list request.</p> |
139
+
140
+ **Example**
141
+ ```js
142
+ // browser example
143
+ document.getElementById("file").addEventListener("input", async (e) => {
144
+ const [file] = e.target.files;
145
+ const asset = await ecadmin.assetgroup("test").createAsset({ file })
146
+ });
147
+ ```
148
+ **Example**
149
+ ```js
150
+ // node example
151
+ const buf = fs.readFileSync("venndiagram.png");
152
+ const file = new Blob([buf]);
153
+ const upload = await sdk("stage")
154
+ .dm("83cc6374")
155
+ .assetgroup("test")
156
+ .createAsset({ file, name: "venndiagram.png" });
157
+ ```
158
+ <a name="module_api.Sdk+getAsset"></a>
159
+
160
+ #### sdk.getAsset(assetID) ⇒ <code>object</code>
161
+ <p>Loads a single asset. Expects <code>dmShortID</code> / <code>assetGroup</code> to be set.
162
+ If the asset group is not public, you also need to provide a <code>token</code>.</p>
163
+
164
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
165
+
166
+ | Param | Type |
167
+ | --- | --- |
168
+ | assetID | <code>string</code> |
169
+
170
+ **Example**
171
+ ```js
172
+ const asset = await sdk("stage").dm("83cc6374").assetgroup("test").getAsset("tP-ZxpZZTGmbPnET-wArAQ")
173
+ ```
174
+ <a name="module_api.Sdk+createEntry"></a>
175
+
176
+ #### sdk.createEntry(value) ⇒ <code>object</code>
177
+ <p>Creates a new entry. Expects <code>dmShortID</code> / <code>model</code> to be set.
178
+ If model POST is not public, you also need to provide a <code>token</code>.</p>
179
+
180
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
181
+
182
+ | Param | Type | Description |
183
+ | --- | --- | --- |
184
+ | value | <code>object</code> | <p>values to set.</p> |
185
+
186
+ **Example**
187
+ ```js
188
+ const entry = await sdk("stage").dm("83cc6374").model("muffin").createEntry({ name: 'test' })
189
+ ```
190
+ <a name="module_api.Sdk+editEntry"></a>
191
+
192
+ #### sdk.editEntry(entryID, value) ⇒ <code>object</code>
193
+ <p>Edits an entry. Expects <code>dmShortID</code> / <code>model</code> to be set.
194
+ If model PUT is not public, you also need to provide a <code>token</code>.</p>
195
+
196
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
197
+
198
+ | Param | Type | Description |
199
+ | --- | --- | --- |
200
+ | entryID | <code>string</code> | <p>id of entry to edit</p> |
201
+ | value | <code>object</code> | <p>values to set. undefined fields are ignored</p> |
202
+
203
+ **Example**
204
+ ```js
205
+ const entry = await sdk("stage").dm("83cc6374").model("muffin").editEntry("1gOtzWvrdq", { name: "test" })
206
+ ```
207
+ <a name="module_api.Sdk+deleteEntry"></a>
208
+
209
+ #### sdk.deleteEntry(entryID) ⇒ <code>object</code>
210
+ <p>Deletes an entry. Expects <code>dmShortID</code> / <code>model</code> to be set.
211
+ If model DELETE is not public, you also need to provide a <code>token</code>.</p>
212
+
213
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
214
+
215
+ | Param | Type | Description |
216
+ | --- | --- | --- |
217
+ | entryID | <code>string</code> | <p>id of entry to delete</p> |
218
+
219
+ **Example**
220
+ ```js
221
+ await sdk("stage").dm("83cc6374").model("muffin").deleteEntry("1gOtzWvrdq")
222
+ ```
223
+ <a name="module_api.Sdk+model"></a>
224
+
225
+ #### sdk.model(model) ⇒
226
+ <p>Sets the given model to use</p>
227
+
228
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
229
+ **Returns**: <p>Sdk</p>
230
+
231
+ | Param | Type | Description |
232
+ | --- | --- | --- |
233
+ | model | <code>string</code> | <p>name of the model</p> |
234
+
235
+ <a name="module_api.Sdk+token"></a>
236
+
237
+ #### sdk.token(token) ⇒
238
+ <p>Sets the token to use in requests</p>
239
+
240
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
241
+ **Returns**: <p>Sdk</p>
242
+
243
+ | Param | Type |
244
+ | --- | --- |
245
+ | token | <code>string</code> |
246
+
247
+ <a name="module_api.Sdk+dmShortID"></a>
248
+
249
+ #### sdk.dmShortID(dmShortID) ⇒
250
+ <p>Sets the short ID of the datamanager to use</p>
251
+
252
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
253
+ **Returns**: <p>Sdk</p>
254
+
255
+ | Param | Type |
256
+ | --- | --- |
257
+ | dmShortID | <code>string</code> |
258
+
259
+ <a name="module_api.Sdk+dm"></a>
260
+
261
+ #### sdk.dm(dmShortID) ⇒
262
+ <p>Sets the short ID of the datamanager to use. Alias for <code>dmShortID</code></p>
263
+
264
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
265
+ **Returns**: <p>Sdk</p>
266
+
267
+ | Param | Type |
268
+ | --- | --- |
269
+ | dmShortID | <code>string</code> |
270
+
271
+ <a name="module_api.Sdk+assetGroup"></a>
272
+
273
+ #### sdk.assetGroup(assetGroup) ⇒
274
+ <p>Sets the name of the asset group to use.</p>
275
+
276
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
277
+ **Returns**: <p>Sdk</p>
278
+
279
+ | Param | Type | Description |
280
+ | --- | --- | --- |
281
+ | assetGroup | <code>string</code> | <p>name of the asset group</p> |
282
+
283
+ <a name="module_api.Sdk+assetgroup"></a>
284
+
285
+ #### sdk.assetgroup(assetGroup) ⇒
286
+ <p>Sets the name of the asset group to use. Alias for <code>assetGroup</code></p>
287
+
288
+ **Kind**: instance method of [<code>Sdk</code>](#module_api.Sdk)
289
+ **Returns**: <p>Sdk</p>
290
+
291
+ | Param | Type | Description |
292
+ | --- | --- | --- |
293
+ | assetGroup | <code>string</code> | <p>name of the asset group</p> |
294
+
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var M=Object.defineProperty,F=Object.defineProperties;var G=Object.getOwnPropertyDescriptors;var A=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,H=Object.prototype.propertyIsEnumerable;var T=(n,t,e)=>t in n?M(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,a=(n,t)=>{for(var e in t||(t={}))v.call(t,e)&&T(n,e,t[e]);if(A)for(var e of A(t))H.call(t,e)&&T(n,e,t[e]);return n},y=(n,t)=>F(n,G(t));var c=(n,t,e)=>new Promise((s,r)=>{var o=h=>{try{u(e.next(h))}catch(f){r(f)}},i=h=>{try{u(e.throw(h))}catch(f){r(f)}},u=h=>h.done?s(h.value):Promise.resolve(h.value).then(o,i);u((e=e.apply(n,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function g(s){return c(this,arguments,function*(n,t={},e={}){const{token:r,rawRes:o}=t;r&&(e.headers=y(a({},e.headers||{}),{Authorization:`Bearer ${r}`}));const i=yield fetch(n,e);if(!i.ok)throw new Error(i.statusText);return o?i:yield i.json()})}const P={production:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"};function d(n,t="stage"){const e=P[t];if(!e)throw new Error(`env "${t}" not found. Try one of ${Object.keys(P).join(", ")}`);return e+n}function p(n,t=!0){return Object.entries(n).sort((e,s)=>e[0].localeCompare(s[0])).map(([e,s])=>`${e}=${s}`).join("&")}function l(n){Object.entries(n).forEach(([t,e])=>{if(e===void 0)throw new Error(`expected ${t} to be set!`)})}const O={stage:"https://accounts.cachena.entrecode.de/",production:"https://accounts.cachena.entrecode.de/"};function _(n){return c(this,null,function*(){let{env:t,dmShortID:e,email:s,password:r}=n;l({env:t,dmShortID:e,email:s,password:r});const o=d(`api/${e}/_auth/login?clientID=rest`,t);return yield g(o,{},{method:"POST",body:JSON.stringify({email:s,password:r}),headers:{"Content-Type":"application/json"}})})}function L(n){return c(this,null,function*(){let{env:t,email:e,password:s}=n;l({env:t,email:e,password:s});const r=`${O[t]}auth/login?clientID=rest`;return yield g(r,{},{method:"POST",body:JSON.stringify({email:e,password:s}),headers:{"Content-Type":"application/json"}})})}function S(n){return c(this,null,function*(){let{dmShortID:t,env:e,token:s}=n;l({dmShortID:t,env:e,token:s});const r=d(`api/${t}/_auth/logout?clientID=rest&token=${s}`,e);return yield g(r,{dmShortID:t,rawRes:!0},{method:"POST"})})}function K(n){return c(this,null,function*(){let{env:t,token:e}=n;l({env:t,token:e});const s=`${O[t]}auth/logout?clientID=rest`;return yield g(s,{rawRes:!0,token:e},{method:"POST"})})}function D({dmShortID:n}){return l({dmShortID:n}),n}function q({env:n}){return l({env:n}),n}function B(n){return c(this,null,function*(){let{env:t,dmShortID:e,model:s,options:r={}}=n;l({env:t,dmShortID:e,model:s}),r=a({size:50,page:1,_list:!0},r);const o=p(r),i=d(`api/${e}/${s}?${o}`,t),{count:u,total:h,_embedded:f}=yield g(i,n),$=f?f[`${e}:${s}`]:[];return{count:u,total:h,items:$}})}function C({env:n,dmShortID:t,model:e,entryID:s,token:r}){l({env:n,dmShortID:t,model:e,entryID:s});const o=p({_id:s}),i=d(`api/${t}/${e}?${o}`,n);return g(i,{dmShortID:t,token:r})}function R(o){return c(this,arguments,function*({env:n,dmShortID:t,model:e,value:s,token:r}){l({env:n,dmShortID:t,model:e,value:s}),console.log("create entry",t,e,s);const i=d(`api/${t}/${e}`,n);return console.log("url",i),yield g(i,{env:n,dmShortID:t,token:r},{method:"POST",body:JSON.stringify(s),headers:{"Content-Type":"application/json"}})})}function x(i){return c(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,value:r,token:o}){l({env:n,dmShortID:t,model:e,entryID:s,value:r});const u=d(`api/${t}/${e}?_id=${s}`,n);return yield g(u,{token:o},{method:"PUT",body:JSON.stringify(r),headers:{"Content-Type":"application/json"}})})}function z(o){return c(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,token:r}){l({env:n,dmShortID:t,model:e,entryID:s});const i=d(`api/${t}/${e}?_id=${s}`,n);return yield g(i,{token:r,rawRes:!0},{method:"DELETE",headers:{"Content-Type":"application/json"}})})}function I(o){return c(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:s,token:r}){l({env:n,dmShortID:t,assetGroup:e,assetID:s});const i=p({assetID:s}),u=d(`a/${t}/${e}?${i}`,n);return(yield g(u,{dmShortID:t,token:r}))._embedded["ec:dm-asset"]})}function J(n){return c(this,null,function*(){let{env:t,dmShortID:e,assetGroup:s,token:r,options:o={}}=n;l({env:t,dmShortID:e,assetGroup:s}),o=a({size:50,page:1,_list:!0},o);const i=p(o),u=d(`a/${e}/${s}?${i}`,t),{count:h,total:f,_embedded:$}=yield g(u,{dmShortID:e,token:r}),U=$?$["ec:dm-asset"]:[];return{count:h,total:f,items:U}})}function N(i){return c(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,token:s,file:r,name:o}){l({env:n,dmShortID:t,assetGroup:e,file:r});const u=d(`a/${t}/${e}`,n),h=new FormData;return h.append("file",r,o),(yield g(u,{token:s},{method:"POST",body:h}))._embedded["ec:dm-asset"]})}const E=Object.freeze(Object.defineProperty({__proto__:null,assetList:J,createAsset:N,createEntry:R,deleteEntry:z,editEntry:x,entryList:B,getAsset:I,getEcAuthKey:q,getEntry:C,getPublicAuthKey:D,loginEc:L,loginPublic:_,logoutEc:K,logoutPublic:S},Symbol.toStringTag,{value:"Module"})),{entryList:Q,getEntry:V,getAsset:W,assetList:X,createAsset:Y,createEntry:m,editEntry:j,deleteEntry:Z,loginPublic:tt,loginEc:et,logoutEc:nt,logoutPublic:st,getEcAuthKey:b,getPublicAuthKey:k}=E;function rt(n){const{action:t}=n;if(l({action:t}),!E[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys(E).join(", ")}`);return E[t](n)}class w{constructor(t){this.config=t}set(t){return new w(a(a({},this.config),t))}handle(t){return c(this,null,function*(){const e=Object.keys(this.config),s=yield this.getBestToken(),[r,o]=Object.entries(t).find(([i,u])=>e.includes(i))||[];if(!o)throw new Error(`you need to first set ${Object.keys(t).join(" | ")}`);return o(y(a({},this.config),{token:s}))})}entries(t){return c(this,null,function*(){const e=yield this.getBestToken();return Q(y(a({},this.config),{options:t,token:e}))})}entryList(t){return c(this,null,function*(){return this.entries(t)})}assets(t){return c(this,null,function*(){const e=yield this.getBestToken();return X(y(a({},this.config),{options:t,token:e}))})}assetList(t){return c(this,null,function*(){return this.assets(t)})}createAsset(){return c(this,arguments,function*({file:t,name:e}={}){const s=yield this.getBestToken();return Y(y(a({},this.config),{file:t,name:e,token:s}))})}get(){return this.handle({assetID:W,entryID:V})}del(){return this.handle({entryID:Z})}createEntry(t){return m(y(a({},this.config),{value:t}))}editEntry(t){return j(y(a({},this.config),{value:t}))}create(t){return this.set({value:t}).handle({model:m})}edit(t){return this.set({value:t}).handle({entryID:j})}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:s}=this.config.authAdapter;return s(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:s}=this.config.authAdapter;return s(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 et(a(a({},this.config),t)).then(this.setAuth(b(this.config)))}loginPublic(t){return tt(a(a({},this.config),t)).then(this.setAuth(k(this.config)))}logoutPublic(){const t=this.getPublicToken();return console.log("token",t),st(y(a({},this.config),{token:t})).then(this.unsetAuth(k(this.config)))}logoutEc(){const t=this.getEcToken();return console.log("token",t),nt(y(a({},this.config),{token:t})).then(this.unsetAuth(b(this.config)))}getPublicToken(){return this.config.token||this.getAuth(k(this.config))}getEcToken(){return this.config.token||this.getAuth(b(this.config))}hasPublicToken(){return!!this.getPublicToken()}hasEcToken(){return!!this.getEcToken()}hasAnyToken(){return!!this.getEcToken()||!!this.getPublicToken()}getBestToken(){try{return this.getEcToken()||this.getPublicToken()}catch(t){return}}model(t){return this.set({model:t})}token(t){return this.set({token:t})}dmShortID(t){return this.set({dmShortID:t})}dm(t){return this.dmShortID(t)}entryID(t){return this.set({entryID:t})}entry(t){return this.entryID(t)}assetGroup(t){return this.set({assetGroup:t})}assetgroup(t){return this.assetGroup(t)}assetID(t){return this.set({assetID:t})}asset(t){return this.assetID(t)}}const ot=n=>new w({env:n});exports.act=rt;exports.apiURL=d;exports.assetList=J;exports.createAsset=N;exports.createEntry=R;exports.deleteEntry=z;exports.editEntry=x;exports.entryList=B;exports.expect=l;exports.fetcher=g;exports.getAsset=I;exports.getEcAuthKey=q;exports.getEntry=C;exports.getPublicAuthKey=D;exports.loginEc=L;exports.loginPublic=_;exports.logoutEc=K;exports.logoutPublic=S;exports.query=p;exports.sdk=ot;
1
+ "use strict";var M=Object.defineProperty,F=Object.defineProperties;var G=Object.getOwnPropertyDescriptors;var b=Object.getOwnPropertySymbols;var H=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable;var T=(n,t,e)=>t in n?M(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,c=(n,t)=>{for(var e in t||(t={}))H.call(t,e)&&T(n,e,t[e]);if(b)for(var e of b(t))Q.call(t,e)&&T(n,e,t[e]);return n},d=(n,t)=>F(n,G(t));var r=(n,t,e)=>new Promise((s,o)=>{var a=u=>{try{g(e.next(u))}catch(f){o(f)}},i=u=>{try{g(e.throw(u))}catch(f){o(f)}},g=u=>u.done?s(u.value):Promise.resolve(u.value).then(a,i);g((e=e.apply(n,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function l(s){return r(this,arguments,function*(n,t={},e={}){const{token:o,rawRes:a}=t;o&&(e.headers=d(c({},e.headers||{}),{Authorization:`Bearer ${o}`}));const i=yield fetch(n,e);if(!i.ok)throw new Error(i.statusText);return a?i:yield i.json()})}const P={production:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"};function y(n,t="stage"){const e=P[t];if(!e)throw new Error(`env "${t}" not found. Try one of ${Object.keys(P).join(", ")}`);return e+n}function p(n,t=!0){return Object.entries(n).sort((e,s)=>e[0].localeCompare(s[0])).map(([e,s])=>`${e}=${s}`).join("&")}function h(n){Object.entries(n).forEach(([t,e])=>{if(e===void 0)throw new Error(`expected ${t} to be set!`)})}const m={stage:"https://accounts.cachena.entrecode.de/",production:"https://accounts.cachena.entrecode.de/"};function O(n){return r(this,null,function*(){let{env:t,dmShortID:e,email:s,password:o}=n;h({env:t,dmShortID:e,email:s,password:o});const a=y(`api/${e}/_auth/login?clientID=rest`,t);return yield l(a,{},{method:"POST",body:JSON.stringify({email:s,password:o}),headers:{"Content-Type":"application/json"}})})}function j(n){return r(this,null,function*(){let{env:t,email:e,password:s}=n;h({env:t,email:e,password:s});const o=`${m[t]}auth/login?clientID=rest`;return yield l(o,{},{method:"POST",body:JSON.stringify({email:e,password:s}),headers:{"Content-Type":"application/json"}})})}function _(n){return r(this,null,function*(){let{dmShortID:t,env:e,token:s}=n;h({dmShortID:t,env:e,token:s});const o=y(`api/${t}/_auth/logout?clientID=rest&token=${s}`,e);return yield l(o,{dmShortID:t,rawRes:!0},{method:"POST"})})}function L(n){return r(this,null,function*(){let{env:t,token:e}=n;h({env:t,token:e});const s=`${m[t]}auth/logout?clientID=rest`;return yield l(s,{rawRes:!0,token:e},{method:"POST"})})}function S({dmShortID:n}){return h({dmShortID:n}),n}function B({env:n}){return h({env:n}),n}function K(n){return r(this,null,function*(){let{env:t,dmShortID:e,model:s,options:o={}}=n;h({env:t,dmShortID:e,model:s}),o=c({size:50,page:1,_list:!0},o);const a=p(o),i=y(`api/${e}/${s}?${a}`,t),{count:g,total:u,_embedded:f}=yield l(i,n),E=f?f[`${e}:${s}`]:[];return{count:g,total:u,items:E}})}function q({env:n,dmShortID:t,model:e,entryID:s,token:o}){h({env:n,dmShortID:t,model:e,entryID:s});const a=p({_id:s}),i=y(`api/${t}/${e}?${a}`,n);return l(i,{dmShortID:t,token:o})}function C(a){return r(this,arguments,function*({env:n,dmShortID:t,model:e,value:s,token:o}){h({env:n,dmShortID:t,model:e,value:s}),console.log("create entry",t,e,s);const i=y(`api/${t}/${e}`,n);return console.log("url",i),yield l(i,{env:n,dmShortID:t,token:o},{method:"POST",body:JSON.stringify(s),headers:{"Content-Type":"application/json"}})})}function R(i){return r(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,value:o,token:a}){h({env:n,dmShortID:t,model:e,entryID:s,value:o});const g=y(`api/${t}/${e}?_id=${s}`,n);return yield l(g,{token:a},{method:"PUT",body:JSON.stringify(o),headers:{"Content-Type":"application/json"}})})}function x(a){return r(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,token:o}){h({env:n,dmShortID:t,model:e,entryID:s});const i=y(`api/${t}/${e}?_id=${s}`,n);return yield l(i,{token:o,rawRes:!0},{method:"DELETE",headers:{"Content-Type":"application/json"}})})}function z(a){return r(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:s,token:o}){h({env:n,dmShortID:t,assetGroup:e,assetID:s});const i=p({assetID:s}),g=y(`a/${t}/${e}?${i}`,n);return(yield l(g,{dmShortID:t,token:o}))._embedded["ec:dm-asset"]})}function J(n){return r(this,null,function*(){let{env:t,dmShortID:e,assetGroup:s,token:o,options:a={}}=n;h({env:t,dmShortID:e,assetGroup:s}),a=c({size:50,page:1,_list:!0},a);const i=p(a),g=y(`a/${e}/${s}?${i}`,t),{count:u,total:f,_embedded:E}=yield l(g,{dmShortID:e,token:o}),U=E?E["ec:dm-asset"]:[];return{count:u,total:f,items:U}})}function N(i){return r(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,token:s,file:o,name:a}){h({env:n,dmShortID:t,assetGroup:e,file:o});const g=y(`a/${t}/${e}`,n),u=new FormData;return u.append("file",o,a),(yield l(g,{token:s},{method:"POST",body:u}))._embedded["ec:dm-asset"]})}const $=Object.freeze(Object.defineProperty({__proto__:null,assetList:J,createAsset:N,createEntry:C,deleteEntry:x,editEntry:R,entryList:K,getAsset:z,getEcAuthKey:B,getEntry:q,getPublicAuthKey:S,loginEc:j,loginPublic:O,logoutEc:L,logoutPublic:_},Symbol.toStringTag,{value:"Module"})),{entryList:V,getEntry:W,getAsset:X,assetList:Y,createAsset:Z,createEntry:v,editEntry:D,deleteEntry:I,loginPublic:tt,loginEc:et,logoutEc:nt,logoutPublic:st,getEcAuthKey:w,getPublicAuthKey:A}=$;function ot(n){const{action:t}=n;if(h({action:t}),!$[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys($).join(", ")}`);return $[t](n)}class k{constructor(t){this.config=t}set(t){return new k(c(c({},this.config),t))}entries(){return r(this,arguments,function*(t={}){const e=yield this.getBestToken();return V(d(c({},this.config),{options:t,token:e}))})}entryList(t){return r(this,null,function*(){return this.entries(t)})}getEntry(t){return r(this,null,function*(){const e=yield this.getBestToken();return W(d(c({},this.config),{entryID:t,token:e}))})}assets(t){return r(this,null,function*(){const e=yield this.getBestToken();return Y(d(c({},this.config),{options:t,token:e}))})}assetList(t){return r(this,null,function*(){return this.assets(t)})}createAsset(){return r(this,arguments,function*({file:t,name:e}={}){const s=yield this.getBestToken();return Z(d(c({},this.config),{file:t,name:e,token:s}))})}getAsset(t){return r(this,null,function*(){const e=yield this.getBestToken();return X(d(c({},this.config),{assetID:t,token:e}))})}createEntry(t){return r(this,null,function*(){const e=yield this.getBestToken();return v(d(c({},this.config),{token:e,value:t}))})}editEntry(t,e){return r(this,null,function*(){const s=yield this.getBestToken();return D(d(c({},this.config),{entryID:t,token:s,value:e}))})}deleteEntry(t){return r(this,null,function*(){const e=yield this.getBestToken();return I(d(c({},this.config),{token:e,entryID:t}))})}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:s}=this.config.authAdapter;return s(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:s}=this.config.authAdapter;return s(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 et(c(c({},this.config),t)).then(this.setAuth(w(this.config)))}loginPublic(t){return tt(c(c({},this.config),t)).then(this.setAuth(A(this.config)))}logoutPublic(){const t=this.getPublicToken();return console.log("token",t),st(d(c({},this.config),{token:t})).then(this.unsetAuth(A(this.config)))}logoutEc(){const t=this.getEcToken();return console.log("token",t),nt(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(){try{return this.getEcToken()||this.getPublicToken()}catch(t){return}}model(t){return this.set({model:t})}token(t){return this.set({token:t})}dmShortID(t){return this.set({dmShortID:t})}dm(t){return this.dmShortID(t)}assetGroup(t){return this.set({assetGroup:t})}assetgroup(t){return this.assetGroup(t)}}const rt=n=>new k({env:n});exports.Sdk=k;exports.act=ot;exports.apiURL=y;exports.assetList=J;exports.createAsset=N;exports.createEntry=C;exports.deleteEntry=x;exports.editEntry=R;exports.entryList=K;exports.expect=h;exports.fetcher=l;exports.getAsset=z;exports.getEcAuthKey=B;exports.getEntry=q;exports.getPublicAuthKey=S;exports.loginEc=j;exports.loginPublic=O;exports.logoutEc=L;exports.logoutPublic=_;exports.query=p;exports.sdk=rt;