ec.fdk 0.2.2 → 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 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 B({dmShortID:n}){return h({dmShortID:n}),n}function S({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:S,getEntry:q,getPublicAuthKey:B,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:k,getPublicAuthKey:w}=$;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 A{constructor(t){this.config=t}set(t){return new A(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(k(this.config)))}loginPublic(t){return tt(c(c({},this.config),t)).then(this.setAuth(w(this.config)))}logoutPublic(){const t=this.getPublicToken();return console.log("token",t),st(d(c({},this.config),{token:t})).then(this.unsetAuth(w(this.config)))}logoutEc(){const t=this.getEcToken();return console.log("token",t),nt(d(c({},this.config),{token:t})).then(this.unsetAuth(k(this.config)))}getPublicToken(){return this.config.token||this.getAuth(w(this.config))}getEcToken(){return this.config.token||this.getAuth(k(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 A({env:n});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=S;exports.getEntry=q;exports.getPublicAuthKey=B;exports.loginEc=j;exports.loginPublic=O;exports.logoutEc=L;exports.logoutPublic=_;exports.query=p;exports.sdk=rt;
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;
package/dist/index.mjs CHANGED
@@ -566,6 +566,7 @@ class A {
566
566
  }
567
567
  const it = (n) => new A({ env: n });
568
568
  export {
569
+ A as Sdk,
569
570
  rt as act,
570
571
  y as apiURL,
571
572
  H as assetList,
@@ -1,7 +1,9 @@
1
1
  export function act(config: any): any;
2
2
  export * from "./util.mjs";
3
- export function sdk(env: any): Sdk;
4
- declare class Sdk {
3
+ /**
4
+ * SDK
5
+ */
6
+ export class Sdk {
5
7
  constructor(config: any);
6
8
  config: any;
7
9
  set(obj: any): Sdk;
@@ -179,4 +181,5 @@ declare class Sdk {
179
181
  */
180
182
  assetgroup(assetGroup: string): Sdk;
181
183
  }
184
+ export function sdk(env: any): Sdk;
182
185
  //# sourceMappingURL=api.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.mts","sourceRoot":"","sources":["../../src/lib/api.mjs"],"names":[],"mappings":"AAqBA,sCAWC;;AAyRM,mCAAqC;AAvR5C;IACE,yBAEC;IADC,YAAoB;IAGtB,mBAGC;IAED;;;;;;;;;;;;OAYG;IACH,kBATW,MAAM,eACJ;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAW7D;IACD;eAZsB,MAAM;eAAS,MAAM;eAAS,MAAM,EAAE;OAc3D;IACD;;;;;;;;OAQG;IACH,kBALW,MAAM,GACJ,MAAM,CAOlB;IACD;;;;;;;;;;;;OAYG;IACH,iBATW,MAAM,eACJ;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAW7D;IACD;eAZsB,MAAM;eAAS,MAAM;eAAS,MAAM,EAAE;OAc3D;IACD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,6BAjBW,MAAM,eACJ;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAmB7D;IACD;;;;;;;;OAQG;IACH,kBALW,MAAM,GACJ,MAAM,CAOlB;IAED;;;;;;;;OAQG;IACH,mBALW,MAAM,GACJ,MAAM,CAOlB;IACD;;;;;;;;;OASG;IACH,mBANW,MAAM,SACN,MAAM,GACJ,MAAM,CAOlB;IACD;;;;;;;;OAQG;IACH,qBALW,MAAM,GACJ,MAAM,CAOlB;IAID,mCAGC;IAED,sCASC;IACD,wCAUC;IACD,uBAMC;IAED,mCAIC;IACD,uCAKC;IAED,6BAMC;IAED,yBAMC;IAED,sBAEC;IACD,kBAEC;IACD,0BAEC;IACD,sBAEC;IACD,uBAEC;IACD,oBAMC;IACD;;;;OAIG;IACH,aAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,aAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,qBAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,cAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,uBAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,uBAHW,MAAM,OAKhB;CACF"}
1
+ {"version":3,"file":"api.d.mts","sourceRoot":"","sources":["../../src/lib/api.mjs"],"names":[],"mappings":"AAwBA,sCAWC;;AAED;;GAEG;AACH;IACE,yBAEC;IADC,YAAoB;IAGtB,mBAGC;IAED;;;;;;;;;;;;OAYG;IACH,kBATW,MAAM,eACJ;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAW7D;IACD;eAZsB,MAAM;eAAS,MAAM;eAAS,MAAM,EAAE;OAc3D;IACD;;;;;;;;OAQG;IACH,kBALW,MAAM,GACJ,MAAM,CAOlB;IACD;;;;;;;;;;;;OAYG;IACH,iBATW,MAAM,eACJ;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAW7D;IACD;eAZsB,MAAM;eAAS,MAAM;eAAS,MAAM,EAAE;OAc3D;IACD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,6BAjBW,MAAM,eACJ;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAmB7D;IACD;;;;;;;;OAQG;IACH,kBALW,MAAM,GACJ,MAAM,CAOlB;IAED;;;;;;;;OAQG;IACH,mBALW,MAAM,GACJ,MAAM,CAOlB;IACD;;;;;;;;;OASG;IACH,mBANW,MAAM,SACN,MAAM,GACJ,MAAM,CAOlB;IACD;;;;;;;;OAQG;IACH,qBALW,MAAM,GACJ,MAAM,CAOlB;IAID,mCAGC;IAED,sCASC;IACD,wCAUC;IACD,uBAMC;IAED,mCAIC;IACD,uCAKC;IAED,6BAMC;IAED,yBAMC;IAED,sBAEC;IACD,kBAEC;IACD,0BAEC;IACD,sBAEC;IACD,uBAEC;IACD,oBAMC;IACD;;;;OAIG;IACH,aAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,aAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,qBAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,cAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,uBAHW,MAAM,OAKhB;IACD;;;;OAIG;IACH,uBAHW,MAAM,OAKhB;CACF;AAEM,mCAAqC"}
@@ -0,0 +1,11 @@
1
+ {
2
+ "source": {
3
+ "includePattern": ".+\\.(js(doc|x)?|mjs)$",
4
+ "excludePattern": "node_modules|dist"
5
+ },
6
+ "plugins": ["plugins/markdown"],
7
+ "opts": {
8
+ "destination": "./out/",
9
+ "recurse": true
10
+ }
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ec.fdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "ec frontend sdk",
5
5
  "main": "dist/index.cjs",
6
6
  "directories": {
@@ -21,14 +21,18 @@
21
21
  "homepage": "https://github.com/felixroos/ec.fdk#readme",
22
22
  "dependencies": {
23
23
  "@nanostores/persistent": "^0.9.1",
24
+ "jsdoc-json": "^2.0.2",
24
25
  "nanostores": "^0.9.3"
25
26
  },
26
27
  "devDependencies": {
28
+ "jsdoc": "^4.0.2",
29
+ "jsdoc-to-markdown": "^8.0.0",
27
30
  "typescript": "^5.3.3",
28
31
  "vite": "^4.4.9"
29
32
  },
30
33
  "scripts": {
31
- "build": "vite build && tsc"
34
+ "build": "vite build && tsc",
35
+ "readme": "jsdoc2md -c ./jsdoc.config.json src/lib/api.mjs --template README.hbs > README.md"
32
36
  },
33
37
  "module": "dist/index.mjs",
34
38
  "types": "dist/index.d.mts"
package/tsconfig.json DELETED
@@ -1,21 +0,0 @@
1
- {
2
- // Change this to match your project
3
- "include": ["src/**/*"],
4
- "compilerOptions": {
5
- // Tells TypeScript to read JS files, as
6
- // normally they are ignored as source files
7
- "allowJs": true,
8
- // Generate d.ts files
9
- "declaration": true,
10
- // This compiler run should
11
- // only output d.ts files
12
- "emitDeclarationOnly": true,
13
- // Types should go into this directory.
14
- // Removing this would place the .d.ts files
15
- // next to the .js files
16
- "outDir": "dist",
17
- // go to js file when using IDE functions like
18
- // "Go to Definition" in VSCode
19
- "declarationMap": true
20
- }
21
- }