ec.fdk 0.4.3 → 0.4.5

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 CHANGED
@@ -17,7 +17,7 @@ npm i ec.fdk
17
17
  3. commit + push
18
18
  4. run `pnpm publish`
19
19
 
20
- # API
20
+ ## API
21
21
 
22
22
  Start by calling `sdk` with your environment (`stage` | `live`), then method chain your way to success:
23
23
 
@@ -50,8 +50,113 @@ await muffin.createEntry({ name: "new muffin" });
50
50
  await muffin.editEntrySafe(items[2].id, { _modified: items[2]._modified, name: "safePut!" });
51
51
  ```
52
52
 
53
+
54
+ ## migration from ec.sdk
55
+
56
+ ec.fdk won't change / decorate data returned from ec APIs. For example, an entry returned from the datamanager will be returned as is.
57
+ Advantages:
58
+
59
+ - The network tab shows what goes into the frontend
60
+ - Resources have the same shape everywhere
61
+ - Resources are serializable
62
+
63
+ ### Entry save
64
+
65
+ Instead of mutating an EntryResource and calling `.save()`, we now pass the new `value` directly:
66
+
67
+ ```js
68
+ // this does not exist anymore:
69
+ await entry.save(); // <- DONT
70
+ // use this to update an entry:
71
+ await editEntryObject(entry, value); // <- DO
72
+ // alternatively:
73
+ await fdk.env(env).dm(dmShortID).model(model).updateEntry(entryID, value);
74
+ // or:
75
+ await editEntry({ env, dmShortID, model, entryID, value });
76
+ ```
77
+
78
+ ### Entry delete
79
+
80
+ Similar to save:
81
+
82
+ ```js
83
+ // this does not exist anymore:
84
+ await entry.del(); // <- DONT
85
+ // use this to delete an entry:
86
+ await deleteEntryObject(entry); // <- DO
87
+ // alternatively:
88
+ await fdk.dm("shortID").model("model").deleteEntry("entryID");
89
+ ```
90
+
91
+ ### Entry Asset Fields
92
+
93
+ In ec.fdk, entry asset fields are plain ids:
94
+
95
+ ```js
96
+ // assuming "photo" is an asset field:
97
+ entry.photo; // <-- this used to be an AssetResource. Now it's a plain id string.
98
+ // use this to get the embedded AssetResource:
99
+ getEntryAsset("photo", entry); // (no request goes out)
100
+ ```
101
+
102
+ ### Entry Date Fields
103
+
104
+ ```js
105
+ // assuming "lastSeen" is a datetime field:
106
+ entry.lastSeen; // <-- this used to be an instance of Date. Now it's a date ISO string
107
+ // use this to get a Date instance:
108
+ new Date(entry.lastSeen);
109
+ ```
110
+
111
+ ### Entry List
112
+
113
+ ```js
114
+ // ec.sdk
115
+ const api = new PublicAPI(shortID, env, true);
116
+ const entryList = await api.entryList(model);
117
+ const items = entryList.getAllItems();
118
+ const first = entryList.getFirstItem();
119
+ // ec.fdk
120
+ const api = sdk(env).dm(shortID);
121
+ const entryList = await api.entryList(model);
122
+ const items = entryList.items; // <------- change
123
+ const first = entryList.items[0]; // <------- change
124
+ // or in one line:
125
+ const entryList = await sdk(env).dm(shortID).entryList(model);
126
+ ```
127
+
128
+ ### Entry List Filter Options
129
+
130
+ By default, the second param of ec.fdk `entryList` will just convert the object to url params:
131
+
132
+ ```js
133
+ const entryList = await sdk("stage")
134
+ .dm("83cc6374")
135
+ .entryList({ createdTo: "2021-01-18T09:13:47.605Z" });
136
+ /*
137
+ https://datamanager.cachena.entrecode.de/api/83cc6374/muffin?
138
+ _list=true&
139
+ createdTo=2021-01-18T09:13:47.605Z&
140
+ page=1&
141
+ size=50
142
+ */
143
+ ```
144
+
145
+ Read more in the [entrecode filtering doc](https://doc.entrecode.de/api-basics/#filtering)
146
+
147
+ There is some syntax sugar you can use to get the same behavior as [ec.sdk filterOptions](https://entrecode.github.io/ec.sdk/#filteroptions):
148
+
149
+ ```js
150
+ const entryList = await sdk("stage")
151
+ .dm("83cc6374")
152
+ .entryList(filterOptions({ created: { to: "2021-01-18T09:13:47.605Z" } }));
153
+ ```
154
+
155
+
53
156
  Now what follows is the autogenerated doc from source:
54
157
 
158
+ ## API Reference
159
+
55
160
  {{#module name="api"}}
56
161
  {{>members}}
57
162
  {{/module}}
package/README.md CHANGED
@@ -17,7 +17,7 @@ npm i ec.fdk
17
17
  3. commit + push
18
18
  4. run `pnpm publish`
19
19
 
20
- # API
20
+ ## API
21
21
 
22
22
  Start by calling `sdk` with your environment (`stage` | `live`), then method chain your way to success:
23
23
 
@@ -50,8 +50,113 @@ await muffin.createEntry({ name: "new muffin" });
50
50
  await muffin.editEntrySafe(items[2].id, { _modified: items[2]._modified, name: "safePut!" });
51
51
  ```
52
52
 
53
+
54
+ ## migration from ec.sdk
55
+
56
+ ec.fdk won't change / decorate data returned from ec APIs. For example, an entry returned from the datamanager will be returned as is.
57
+ Advantages:
58
+
59
+ - The network tab shows what goes into the frontend
60
+ - Resources have the same shape everywhere
61
+ - Resources are serializable
62
+
63
+ ### Entry save
64
+
65
+ Instead of mutating an EntryResource and calling `.save()`, we now pass the new `value` directly:
66
+
67
+ ```js
68
+ // this does not exist anymore:
69
+ await entry.save(); // <- DONT
70
+ // use this to update an entry:
71
+ await editEntryObject(entry, value); // <- DO
72
+ // alternatively:
73
+ await fdk.env(env).dm(dmShortID).model(model).updateEntry(entryID, value);
74
+ // or:
75
+ await editEntry({ env, dmShortID, model, entryID, value });
76
+ ```
77
+
78
+ ### Entry delete
79
+
80
+ Similar to save:
81
+
82
+ ```js
83
+ // this does not exist anymore:
84
+ await entry.del(); // <- DONT
85
+ // use this to delete an entry:
86
+ await deleteEntryObject(entry); // <- DO
87
+ // alternatively:
88
+ await fdk.dm("shortID").model("model").deleteEntry("entryID");
89
+ ```
90
+
91
+ ### Entry Asset Fields
92
+
93
+ In ec.fdk, entry asset fields are plain ids:
94
+
95
+ ```js
96
+ // assuming "photo" is an asset field:
97
+ entry.photo; // <-- this used to be an AssetResource. Now it's a plain id string.
98
+ // use this to get the embedded AssetResource:
99
+ getEntryAsset("photo", entry); // (no request goes out)
100
+ ```
101
+
102
+ ### Entry Date Fields
103
+
104
+ ```js
105
+ // assuming "lastSeen" is a datetime field:
106
+ entry.lastSeen; // <-- this used to be an instance of Date. Now it's a date ISO string
107
+ // use this to get a Date instance:
108
+ new Date(entry.lastSeen);
109
+ ```
110
+
111
+ ### Entry List
112
+
113
+ ```js
114
+ // ec.sdk
115
+ const api = new PublicAPI(shortID, env, true);
116
+ const entryList = await api.entryList(model);
117
+ const items = entryList.getAllItems();
118
+ const first = entryList.getFirstItem();
119
+ // ec.fdk
120
+ const api = sdk(env).dm(shortID);
121
+ const entryList = await api.entryList(model);
122
+ const items = entryList.items; // <------- change
123
+ const first = entryList.items[0]; // <------- change
124
+ // or in one line:
125
+ const entryList = await sdk(env).dm(shortID).entryList(model);
126
+ ```
127
+
128
+ ### Entry List Filter Options
129
+
130
+ By default, the second param of ec.fdk `entryList` will just convert the object to url params:
131
+
132
+ ```js
133
+ const entryList = await sdk("stage")
134
+ .dm("83cc6374")
135
+ .entryList({ createdTo: "2021-01-18T09:13:47.605Z" });
136
+ /*
137
+ https://datamanager.cachena.entrecode.de/api/83cc6374/muffin?
138
+ _list=true&
139
+ createdTo=2021-01-18T09:13:47.605Z&
140
+ page=1&
141
+ size=50
142
+ */
143
+ ```
144
+
145
+ Read more in the [entrecode filtering doc](https://doc.entrecode.de/api-basics/#filtering)
146
+
147
+ There is some syntax sugar you can use to get the same behavior as [ec.sdk filterOptions](https://entrecode.github.io/ec.sdk/#filteroptions):
148
+
149
+ ```js
150
+ const entryList = await sdk("stage")
151
+ .dm("83cc6374")
152
+ .entryList(filterOptions({ created: { to: "2021-01-18T09:13:47.605Z" } }));
153
+ ```
154
+
155
+
53
156
  Now what follows is the autogenerated doc from source:
54
157
 
158
+ ## API Reference
159
+
55
160
  <a name="module_api.Sdk"></a>
56
161
 
57
162
  ### api.Sdk
@@ -70,7 +175,7 @@ Now what follows is the autogenerated doc from source:
70
175
  * [.getAsset(assetID)](#module_api.Sdk+getAsset) ⇒ [<code>Promise.&lt;AssetResource&gt;</code>](#AssetResource)
71
176
  * [.createEntry(value)](#module_api.Sdk+createEntry) ⇒ [<code>Promise.&lt;EntryResource&gt;</code>](#EntryResource)
72
177
  * [.editEntry(entryID, value)](#module_api.Sdk+editEntry) ⇒ [<code>Promise.&lt;EntryResource&gt;</code>](#EntryResource)
73
- * [.deleteEntry(entryID)](#module_api.Sdk+deleteEntry) ⇒ <code>void</code>
178
+ * [.deleteEntry(entryID)](#module_api.Sdk+deleteEntry) ⇒ <code>Promise.&lt;void&gt;</code>
74
179
  * [.resourceList([options])](#module_api.Sdk+resourceList) ⇒ [<code>Promise.&lt;ResourceList&gt;</code>](#ResourceList)
75
180
  * [.raw([options], [fetchOptions])](#module_api.Sdk+raw) ⇒ <code>Promise.&lt;any&gt;</code>
76
181
  * [.model(model)](#module_api.Sdk+model) ⇒
@@ -281,7 +386,7 @@ const entry = await sdk("stage").dm("83cc6374").model("muffin").editEntry("1gOtz
281
386
  ```
282
387
  <a name="module_api.Sdk+deleteEntry"></a>
283
388
 
284
- #### sdk.deleteEntry(entryID) ⇒ <code>void</code>
389
+ #### sdk.deleteEntry(entryID) ⇒ <code>Promise.&lt;void&gt;</code>
285
390
  <p>Deletes an entry. Expects <code>dmShortID</code> / <code>model</code> to be set.
286
391
  If model DELETE is not public, you also need to provide a <code>token</code>.</p>
287
392
 
package/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";var ct=Object.defineProperty,at=Object.defineProperties;var ut=Object.getOwnPropertyDescriptors;var S=Object.getOwnPropertySymbols;var lt=Object.prototype.hasOwnProperty,dt=Object.prototype.propertyIsEnumerable;var B=(n,t,e)=>t in n?ct(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,o=(n,t)=>{for(var e in t||(t={}))lt.call(t,e)&&B(n,e,t[e]);if(S)for(var e of S(t))dt.call(t,e)&&B(n,e,t[e]);return n},y=(n,t)=>at(n,ut(t));var c=(n,t,e)=>new Promise((s,r)=>{var l=a=>{try{d(e.next(a))}catch(u){r(u)}},i=a=>{try{d(e.throw(a))}catch(u){r(u)}},d=a=>a.done?s(a.value):Promise.resolve(a.value).then(l,i);d((e=e.apply(n,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function g(s){return c(this,arguments,function*(n,t={},e={}){var d;const{token:r,rawRes:l}=t;r&&(e.headers=y(o({},e.headers||{}),{Authorization:`Bearer ${r}`}));const i=yield fetch(n,e);if(!i.ok){if((d=i.headers.get("content-type"))!=null&&d.includes("application/json")){const a=yield i.json(),u=`${a.title}
2
- ${a.detail}
3
- ${a.verbose}`;throw new Error(u)}throw new Error(`unexpected fetch error: ${i.statusText}`)}return l?i:yield i.json()})}const D={datamanager:{live:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"},accounts:{live:"https://accounts.entrecode.de/",stage:"https://accounts.cachena.entrecode.de/"},appserver:{live:"https://appserver.entrecode.de/",stage:"https://appserver.cachena.entrecode.de/"}};function p(n,t="stage",e="datamanager"){const s=D[e];if(!s)throw new Error(`subdomain "${e}" not found. Try one of ${Object.keys(D).join(", ")}`);const r=s[t];if(!r)throw new Error(`env "${t}" not found. Try one of ${Object.keys(s[t]).join(", ")}`);return r+n}function w(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 x={stage:"https://accounts.cachena.entrecode.de/",live:"https://accounts.entrecode.de/"};function K(n){return c(this,null,function*(){let{env:t,dmShortID:e,email:s,password:r}=n;h({env:t,dmShortID:e,email:s,password:r});const l=p(`api/${e}/_auth/login?clientID=rest`,t);return yield g(l,{},{method:"POST",body:JSON.stringify({email:s,password:r}),headers:{"Content-Type":"application/json"}})})}function N(n){return c(this,null,function*(){let{env:t,email:e,password:s}=n;h({env:t,email:e,password:s});const r=`${x[t]}auth/login?clientID=rest`;return yield g(r,{},{method:"POST",body:JSON.stringify({email:e,password:s}),headers:{"Content-Type":"application/json"}})})}function z(n){return c(this,null,function*(){let{dmShortID:t,env:e,token:s}=n;h({dmShortID:t,env:e,token:s});const r=p(`api/${t}/_auth/logout?clientID=rest&token=${s}`,e);return yield g(r,{dmShortID:t,rawRes:!0},{method:"POST"})})}function C(n){return c(this,null,function*(){let{env:t,token:e}=n;h({env:t,token:e});const s=`${x[t]}auth/logout?clientID=rest`;return yield g(s,{rawRes:!0,token:e},{method:"POST"})})}function F({dmShortID:n}){return h({dmShortID:n}),n}function J({env:n}){return h({env:n}),n}let ht=["created","creator","id","modified","private","_created","_creator","_embedded","_entryTitle","_id","_links","_modelTitle","_modelTitleField","_modified"];function R(n){let t={};for(let e in n)ht.includes(e)||(t[e]=n[e]);return t}function gt(n){return JSON.parse(JSON.stringify(n))}function v(n){return c(this,null,function*(){let{env:t,dmShortID:e}=n;h({env:t,dmShortID:e});const s=p(`api/${e}`,t);return g(s,n)})}function U(n){return c(this,null,function*(){let{env:t,dmShortID:e,model:s,options:r={}}=n;h({env:t,dmShortID:e,model:s}),r=o({size:50,page:1,_list:!0},r);const l=w(r),i=p(`api/${e}/${s}?${l}`,t),{count:d,total:a,_embedded:u}=yield g(i,n);let f=u?u[`${e}:${s}`]:[];return f=Array.isArray(f)?f:[f],{count:d,total:a,items:f}})}function I({env:n,dmShortID:t,model:e,entryID:s,token:r}){h({env:n,dmShortID:t,model:e,entryID:s});const l=w({_id:s}),i=p(`api/${t}/${e}?${l}`,n);return g(i,{dmShortID:t,token:r})}function M(l){return c(this,arguments,function*({env:n,dmShortID:t,model:e,value:s,token:r}){h({env:n,dmShortID:t,model:e,value:s});const i=p(`api/${t}/${e}`,n);return yield g(i,{env:n,dmShortID:t,token:r},{method:"POST",body:JSON.stringify(s),headers:{"Content-Type":"application/json"}})})}function V(d){return c(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,value:r,token:l,safePut:i=!1}){h({env:n,dmShortID:t,model:e,entryID:s,value:r});const a={"Content-Type":"application/json"};if(i){if(!("_modified"in r))throw new Error("expected _modified to be set!");a["If-Unmodified-Since"]=new Date(r._modified).toUTCString()}const u=p(`api/${t}/${e}?_id=${s}`,n);return r=gt(r),r=R(r),yield g(u,{token:l},{method:"PUT",headers:a,body:JSON.stringify(r)})})}function G(l){return c(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,token:r}){h({env:n,dmShortID:t,model:e,entryID:s});const i=p(`api/${t}/${e}?_id=${s}`,n);yield g(i,{token:r,rawRes:!0},{method:"DELETE",headers:{"Content-Type":"application/json"}})})}function H(r){return c(this,arguments,function*({env:n,dmShortID:t,model:e,withMetadata:s}){var f,$,O,L,j;h({env:n,dmShortID:t,model:e});const l=p(`api/schema/${t}/${e}`,n),i=yield g(l),d=(f=i==null?void 0:i.allOf)==null?void 0:f[1];if(typeof d.properties!="object")throw new Error(`getSchema: ${l} returned unexpected format: ${JSON.stringify(i)}`);const{properties:a}=d,u=R(a);for(let A in u){let m=u[A];if(m.required=d.required.includes(A),($=u[A])!=null&&$.oneOf&&((O=u[A])==null||delete O.oneOf),(L=m.title)!=null&&L.includes("<")&&((j=m.title)!=null&&j.includes(">"))){const b=m.title.split("<")[1].slice(0,-1),ot=m.title.split("<")[0];m.type=ot,b.includes(":")?m.resource=b.split(":")[1]:m.resource=b}else["asset","entry","assets","entries"].includes(m.title)?(m.type=m.title,m.resource=null):m.type=m.title;delete u[A].title}if(s){const A=a._modelTitle.title,m=a._modelTitleField.title;return{properties:u,meta:{modelTitleField:m,modelTitle:A}}}return u})}function Q(n){return Object.entries(n).map(([t,e])=>typeof e!="object"?{[t]:String(e)}:o(o(o(o(o(o(o({},e.sort&&{sort:Array.isArray(e)?e.join(","):e}),e.search&&{[t+"~"]:e.search}),e.notNull&&{[t+"!"]:""}),e.null&&{[t]:""}),e.any&&{[t]:e.any.join(",")}),e.from&&{[t+"From"]:e.from}),e.to&&{[t+"To"]:e.to})).reduce((t,e)=>o(o({},t),e),{})}function P(n){return n._links.collection.href.split("/").slice(-2)[0]}function W(n,t){const e=P(t);return t._embedded[`${e}:${t._modelTitle}/${n}/asset`]}function X(l){return c(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:s,token:r}){h({env:n,dmShortID:t,assetGroup:e,assetID:s});const i=w({assetID:s}),d=p(`a/${t}/${e}?${i}`,n);return(yield g(d,{dmShortID:t,token:r}))._embedded["ec:dm-asset"]})}function Y(n){return c(this,null,function*(){let{env:t,dmShortID:e,assetGroup:s,token:r,options:l={}}=n;h({env:t,dmShortID:e,assetGroup:s}),l=o({size:50,page:1,_list:!0},l);const i=w(l),d=p(`a/${e}/${s}?${i}`,t),{count:a,total:u,_embedded:f}=yield g(d,{dmShortID:e,token:r});let $=f?f["ec:dm-asset"]:[];return $=Array.isArray($)?$:[$],{count:a,total:u,items:$}})}function Z(d){return c(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,token:s,file:r,name:l,options:i}){h({env:n,dmShortID:t,assetGroup:e,file:r});const a=p(`a/${t}/${e}`,n),u=new FormData;return u.append("file",r,l),i&&Object.keys(i).forEach($=>{u.append($,i[$])}),(yield g(a,{token:s},{method:"POST",body:u}))._embedded["ec:dm-asset"]})}function tt(l){return c(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:s,token:r}){h({env:n,dmShortID:t,assetGroup:e,assetID:s});const i=p(`a/${t}/${e}/${s}`,n);yield g(i,{token:r,rawRes:!0},{method:"DELETE"})})}function et(n){return c(this,null,function*(){let{env:t,dmID:e,token:s}=n;h({env:t,dmID:e});const r=p(`?dataManagerID=${e}`,t);return g(r,{token:s})})}function nt(n){return c(this,null,function*(){let{env:t,options:e={}}=n;h({env:t}),e=o({size:25,page:1,_list:!0},e);const s=w(e),r=p(`?${s}`,t),{count:l,total:i,_embedded:d}=yield g(r,n);let a=d?d["ec:datamanager"]:[];return a=Array.isArray(a)?a:[a],{count:l,total:i,items:a}})}function st(n){return c(this,null,function*(){let{env:t,dmID:e,options:s={}}=n;h({env:t,dmID:e}),s=o({size:25,dataManagerID:e,page:1,_list:!0},s);const r=w(s),l=p(`model?${r}`,t),{count:i,total:d,_embedded:a}=yield g(l,n);let u=a?a["ec:model"]:[];return u=Array.isArray(u)?u:[u],{count:i,total:d,items:u}})}function rt(n){return c(this,null,function*(){let{env:t,resource:e,options:s={},subdomain:r="datamanager"}=n;h({env:t,subdomain:r,resource:e}),s=o({size:25,page:1,_list:!0},s);const l=w(s),i=p(`${e}?${l}`,t,r),{count:d,total:a,_embedded:u}=yield g(i,n);let f=u?u[Object.keys(u)[0]]:[];return f=Array.isArray(f)?f:[f],{count:d,total:a,items:f}})}function it(e){return c(this,arguments,function*(n,t={}){let{env:s,route:r,options:l={},subdomain:i="datamanager"}=n;h({env:s,subdomain:i,route:r}),l=o({},l);const d=w(l),a=p(`${r}?${d}`,s,i);return g(a,n,t)})}const E=Object.freeze(Object.defineProperty({__proto__:null,assetList:Y,createAsset:Z,createEntry:M,deleteAsset:tt,deleteEntry:G,dmList:nt,editEntry:V,entryList:U,getAsset:X,getDatamanager:et,getEcAuthKey:J,getEntry:I,getEntryAsset:W,getEntryShortID:P,getPublicAuthKey:F,getSchema:H,loginEc:N,loginPublic:K,logoutEc:C,logoutPublic:z,modelList:st,publicApi:v,raw:it,resourceList:rt,sdkOptions:Q},Symbol.toStringTag,{value:"Module"})),{entryList:ft,getEntry:yt,getAsset:pt,assetList:mt,createAsset:$t,deleteAsset:wt,createEntry:At,editEntry:q,deleteEntry:Et,getSchema:kt,loginPublic:bt,loginEc:Tt,logoutEc:_t,logoutPublic:Pt,getEcAuthKey:T,getPublicAuthKey:_,dmList:Ot,modelList:Lt,publicApi:jt,getDatamanager:St,resourceList:Bt,raw:Dt}=E;function qt(n){const{action:t}=n;if(h({action:t}),!E[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys(E).join(", ")}`);return E[t](n)}class k{constructor(t){this.config=t}set(t){return new k(o(o({},this.config),t))}entries(){return c(this,arguments,function*(t={}){const e=yield this.getBestToken();return ft(y(o({},this.config),{options:t,token:e}))})}entryList(t){return c(this,null,function*(){return this.entries(t)})}getEntry(t){return c(this,null,function*(){const e=yield this.getBestToken();return yt(y(o({},this.config),{entryID:t,token:e}))})}editEntrySafe(t,e){return c(this,null,function*(){const s=yield this.getBestToken();return q(y(o({},this.config),{entryID:t,token:s,value:e,safePut:!0}))})}getSchema(){return c(this,null,function*(){return kt(this.config)})}assets(t){return c(this,null,function*(){const e=yield this.getBestToken();return mt(y(o({},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,options:s}={}){const r=yield this.getBestToken();return $t(y(o({},this.config),{file:t,name:e,options:s,token:r}))})}deleteAsset(t){return c(this,null,function*(){const e=yield this.getBestToken();return wt(y(o({},this.config),{token:e,assetID:t}))})}getAsset(t){return c(this,null,function*(){const e=yield this.getBestToken();return pt(y(o({},this.config),{assetID:t,token:e}))})}createEntry(t){return c(this,null,function*(){const e=yield this.getBestToken();return At(y(o({},this.config),{token:e,value:t}))})}editEntry(t,e){return c(this,null,function*(){const s=yield this.getBestToken();return q(y(o({},this.config),{entryID:t,token:s,value:e}))})}deleteEntry(t){return c(this,null,function*(){const e=yield this.getBestToken();return Et(y(o({},this.config),{token:e,entryID:t}))})}resourceList(t){return c(this,null,function*(){const e=yield this.getBestToken();return Bt(y(o({},this.config),{options:t,token:e}))})}raw(t,e){return c(this,null,function*(){const s=yield this.getBestToken();return Dt(y(o({},this.config),{options:t,token:s}),e)})}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 Tt(o(o({},this.config),t)).then(this.setAuth(T(this.config)))}loginPublic(t){return bt(o(o({},this.config),t)).then(this.setAuth(_(this.config)))}logoutPublic(){const t=this.getPublicToken();return console.log("token",t),Pt(y(o({},this.config),{token:t})).then(this.unsetAuth(_(this.config)))}logoutEc(){const t=this.getEcToken();return console.log("token",t),_t(y(o({},this.config),{token:t})).then(this.unsetAuth(T(this.config)))}getPublicToken(){return this.config.token||this.getAuth(_(this.config))}getEcToken(){return this.config.token||this.getAuth(T(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})}dmID(t){return this.set({dmID:t})}dm(t){return this.dmShortID(t)}assetGroup(t){return this.set({assetGroup:t})}assetgroup(t){return this.assetGroup(t)}subdomain(t){return this.set({subdomain:t})}resource(t){return this.set({resource:t})}route(t){return this.set({route:t})}publicApi(){return jt(this.config)}getDatamanager(t){return c(this,null,function*(){const e=yield this.getBestToken();return St(y(o({},this.config),{dmID:t,token:e}))})}dmList(){return c(this,arguments,function*(t={}){const e=yield this.getBestToken();return Ot(y(o({},this.config),{options:t,token:e}))})}modelList(){return c(this,arguments,function*(t={}){const e=yield this.getBestToken();return Lt(y(o({},this.config),{options:t,token:e}))})}}const xt=n=>new k({env:n});exports.Sdk=k;exports.act=qt;exports.apiURL=p;exports.assetList=Y;exports.createAsset=Z;exports.createEntry=M;exports.deleteAsset=tt;exports.deleteEntry=G;exports.dmList=nt;exports.editEntry=V;exports.entryList=U;exports.expect=h;exports.fetcher=g;exports.getAsset=X;exports.getDatamanager=et;exports.getEcAuthKey=J;exports.getEntry=I;exports.getEntryAsset=W;exports.getEntryShortID=P;exports.getPublicAuthKey=F;exports.getSchema=H;exports.loginEc=N;exports.loginPublic=K;exports.logoutEc=C;exports.logoutPublic=z;exports.modelList=st;exports.publicApi=v;exports.query=w;exports.raw=it;exports.resourceList=rt;exports.sdk=xt;exports.sdkOptions=Q;
1
+ "use strict";var ht=Object.defineProperty,gt=Object.defineProperties;var ft=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var yt=Object.prototype.hasOwnProperty,pt=Object.prototype.propertyIsEnumerable;var C=(n,t,e)=>t in n?ht(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e,o=(n,t)=>{for(var e in t||(t={}))yt.call(t,e)&&C(n,e,t[e]);if(x)for(var e of x(t))pt.call(t,e)&&C(n,e,t[e]);return n},f=(n,t)=>gt(n,ft(t));var a=(n,t,e)=>new Promise((s,r)=>{var u=c=>{try{d(e.next(c))}catch(l){r(l)}},i=c=>{try{d(e.throw(c))}catch(l){r(l)}},d=c=>c.done?s(c.value):Promise.resolve(c.value).then(u,i);d((e=e.apply(n,t)).next())});Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function g(s){return a(this,arguments,function*(n,t={},e={}){var d;const{token:r,rawRes:u}=t;r&&(e.headers=f(o({},e.headers||{}),{Authorization:`Bearer ${r}`}));const i=yield fetch(n,e);if(!i.ok){if((d=i.headers.get("content-type"))!=null&&d.includes("application/json")){const c=yield i.json(),l=`${c.title}
2
+ ${c.detail}
3
+ ${c.verbose}`;throw new Error(l)}throw new Error(`unexpected fetch error: ${i.statusText}`)}return u?i:yield i.json()})}const b={datamanager:{live:"https://datamanager.entrecode.de/",stage:"https://datamanager.cachena.entrecode.de/"},accounts:{live:"https://accounts.entrecode.de/",stage:"https://accounts.cachena.entrecode.de/"},appserver:{live:"https://appserver.entrecode.de/",stage:"https://appserver.cachena.entrecode.de/"}};function p(n,t="stage",e="datamanager"){const s=b[e];if(!s)throw new Error(`subdomain "${e}" not found. Try one of ${Object.keys(b).join(", ")}`);const r=s[t];if(!r)throw new Error(`env "${t}" not found. Try one of ${Object.keys(s[t]).join(", ")}`);return r+n}function w(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 N={stage:"https://accounts.cachena.entrecode.de/",live:"https://accounts.entrecode.de/"};function z(n){return a(this,null,function*(){let{env:t,dmShortID:e,email:s,password:r}=n;h({env:t,dmShortID:e,email:s,password:r});const u=p(`api/${e}/_auth/login?clientID=rest`,t);return yield g(u,{},{method:"POST",body:JSON.stringify({email:s,password:r}),headers:{"Content-Type":"application/json"}})})}function F(n){return a(this,null,function*(){let{env:t,email:e,password:s}=n;h({env:t,email:e,password:s});const r=`${N[t]}auth/login?clientID=rest`;return yield g(r,{},{method:"POST",body:JSON.stringify({email:e,password:s}),headers:{"Content-Type":"application/json"}})})}function J(n){return a(this,null,function*(){let{dmShortID:t,env:e,token:s}=n;h({dmShortID:t,env:e,token:s});const r=p(`api/${t}/_auth/logout?clientID=rest&token=${s}`,e);return yield g(r,{dmShortID:t,rawRes:!0},{method:"POST"})})}function R(n){return a(this,null,function*(){let{env:t,token:e}=n;h({env:t,token:e});const s=`${N[t]}auth/logout?clientID=rest`;return yield g(s,{rawRes:!0,token:e},{method:"POST"})})}function U({dmShortID:n}){return h({dmShortID:n}),n}function I({env:n}){return h({env:n}),n}let mt=["created","creator","id","modified","private","_created","_creator","_embedded","_entryTitle","_id","_links","_modelTitle","_modelTitleField","_modified"];function M(n){let t={};for(let e in n)mt.includes(e)||(t[e]=n[e]);return t}function $t(n){return JSON.parse(JSON.stringify(n))}function V(n){return a(this,null,function*(){let{env:t,dmShortID:e}=n;h({env:t,dmShortID:e});const s=p(`api/${e}`,t);return g(s,n)})}function G(n){return a(this,null,function*(){let{env:t,dmShortID:e,model:s,options:r={}}=n;h({env:t,dmShortID:e,model:s}),r=o({size:50,page:1,_list:!0},r);const u=w(r),i=p(`api/${e}/${s}?${u}`,t),{count:d,total:c,_embedded:l}=yield g(i,n);let y=l?l[`${e}:${s}`]:[];return y=Array.isArray(y)?y:[y],{count:d,total:c,items:y}})}function H({env:n,dmShortID:t,model:e,entryID:s,token:r}){h({env:n,dmShortID:t,model:e,entryID:s});const u=w({_id:s}),i=p(`api/${t}/${e}?${u}`,n);return g(i,{dmShortID:t,token:r})}function Q(u){return a(this,arguments,function*({env:n,dmShortID:t,model:e,value:s,token:r}){h({env:n,dmShortID:t,model:e,value:s});const i=p(`api/${t}/${e}`,n);return yield g(i,{env:n,dmShortID:t,token:r},{method:"POST",body:JSON.stringify(s),headers:{"Content-Type":"application/json"}})})}function P(d){return a(this,arguments,function*({env:n,dmShortID:t,model:e,entryID:s,value:r,token:u,safePut:i=!1}){h({env:n,dmShortID:t,model:e,entryID:s,value:r});const c={"Content-Type":"application/json"};if(i){if(!("_modified"in r))throw new Error("expected _modified to be set!");c["If-Unmodified-Since"]=new Date(r._modified).toUTCString()}const l=p(`api/${t}/${e}?_id=${s}`,n);return r=$t(r),r=M(r),yield g(l,{token:u},{method:"PUT",headers:c,body:JSON.stringify(r)})})}function L({env:n,dmShortID:t,model:e,entryID:s,token:r}){h({env:n,dmShortID:t,model:e,entryID:s});const u=p(`api/${t}/${e}?_id=${s}`,n);return g(u,{token:r,rawRes:!0},{method:"DELETE",headers:{"Content-Type":"application/json"}})}function W(r){return a(this,arguments,function*({env:n,dmShortID:t,model:e,withMetadata:s}){var y,$,D,q,v;h({env:n,dmShortID:t,model:e});const u=p(`api/schema/${t}/${e}`,n),i=yield g(u),d=(y=i==null?void 0:i.allOf)==null?void 0:y[1];if(typeof d.properties!="object")throw new Error(`getSchema: ${u} returned unexpected format: ${JSON.stringify(i)}`);const{properties:c}=d,l=M(c);for(let E in l){let m=l[E];if(m.required=d.required.includes(E),($=l[E])!=null&&$.oneOf&&((D=l[E])==null||delete D.oneOf),(q=m.title)!=null&&q.includes("<")&&((v=m.title)!=null&&v.includes(">"))){const _=m.title.split("<")[1].slice(0,-1),dt=m.title.split("<")[0];m.type=dt,_.includes(":")?m.resource=_.split(":")[1]:m.resource=_}else["asset","entry","assets","entries"].includes(m.title)?(m.type=m.title,m.resource=null):m.type=m.title;delete l[E].title}if(s){const E=c._modelTitle.title,m=c._modelTitleField.title;return{properties:l,meta:{modelTitleField:m,modelTitle:E}}}return l})}function S(n){return Object.entries(n).map(([t,e])=>typeof e!="object"?{[t]:String(e)}:o(o(o(o(o(o(o({},e.sort&&{sort:Array.isArray(e)?e.join(","):e}),e.search&&{[t+"~"]:e.search}),e.notNull&&{[t+"!"]:""}),e.null&&{[t]:""}),e.any&&{[t]:e.any.join(",")}),e.from&&{[t+"From"]:e.from}),e.to&&{[t+"To"]:e.to})).reduce((t,e)=>o(o({},t),e),{})}const X=S;function k(n){return n._links.collection.href.split("/").slice(-2)[0]}function B(n){const t=n._links.collection.href.split("api/")[0];return Object.keys(b.datamanager).find(s=>b.datamanager[s]===t)}const Y=n=>{const t=k(n),e=B(n),s=n._modelTitle,r=n.id;return{dmShortID:t,env:e,model:s,entryID:r}},Z=n=>L(Y(n)),tt=(n,t)=>P(f(o({},Y(n)),{value:t}));function et(n,t){const e=k(t);return t._embedded[`${e}:${t._modelTitle}/${n}/asset`]}function nt(u){return a(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:s,token:r}){h({env:n,dmShortID:t,assetGroup:e,assetID:s});const i=w({assetID:s}),d=p(`a/${t}/${e}?${i}`,n);return(yield g(d,{dmShortID:t,token:r}))._embedded["ec:dm-asset"]})}function st(n){return a(this,null,function*(){let{env:t,dmShortID:e,assetGroup:s,token:r,options:u={}}=n;h({env:t,dmShortID:e,assetGroup:s}),u=o({size:50,page:1,_list:!0},u);const i=w(u),d=p(`a/${e}/${s}?${i}`,t),{count:c,total:l,_embedded:y}=yield g(d,{dmShortID:e,token:r});let $=y?y["ec:dm-asset"]:[];return $=Array.isArray($)?$:[$],{count:c,total:l,items:$}})}function rt(d){return a(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,token:s,file:r,name:u,options:i}){h({env:n,dmShortID:t,assetGroup:e,file:r});const c=p(`a/${t}/${e}`,n),l=new FormData;return l.append("file",r,u),i&&Object.keys(i).forEach($=>{l.append($,i[$])}),(yield g(c,{token:s},{method:"POST",body:l}))._embedded["ec:dm-asset"]})}function it(u){return a(this,arguments,function*({env:n,dmShortID:t,assetGroup:e,assetID:s,token:r}){h({env:n,dmShortID:t,assetGroup:e,assetID:s});const i=p(`a/${t}/${e}/${s}`,n);yield g(i,{token:r,rawRes:!0},{method:"DELETE"})})}function ot(n){return a(this,null,function*(){let{env:t,dmID:e,token:s}=n;h({env:t,dmID:e});const r=p(`?dataManagerID=${e}`,t);return g(r,{token:s})})}function ct(n){return a(this,null,function*(){let{env:t,options:e={}}=n;h({env:t}),e=o({size:25,page:1,_list:!0},e);const s=w(e),r=p(`?${s}`,t),{count:u,total:i,_embedded:d}=yield g(r,n);let c=d?d["ec:datamanager"]:[];return c=Array.isArray(c)?c:[c],{count:u,total:i,items:c}})}function at(n){return a(this,null,function*(){let{env:t,dmID:e,options:s={}}=n;h({env:t,dmID:e}),s=o({size:25,dataManagerID:e,page:1,_list:!0},s);const r=w(s),u=p(`model?${r}`,t),{count:i,total:d,_embedded:c}=yield g(u,n);let l=c?c["ec:model"]:[];return l=Array.isArray(l)?l:[l],{count:i,total:d,items:l}})}function ut(n){return a(this,null,function*(){let{env:t,resource:e,options:s={},subdomain:r="datamanager"}=n;h({env:t,subdomain:r,resource:e}),s=o({size:25,page:1,_list:!0},s);const u=w(s),i=p(`${e}?${u}`,t,r),{count:d,total:c,_embedded:l}=yield g(i,n);let y=l?l[Object.keys(l)[0]]:[];return y=Array.isArray(y)?y:[y],{count:d,total:c,items:y}})}function lt(e){return a(this,arguments,function*(n,t={}){let{env:s,route:r,options:u={},subdomain:i="datamanager"}=n;h({env:s,subdomain:i,route:r}),u=o({},u);const d=w(u),c=p(`${r}?${d}`,s,i);return g(c,n,t)})}const A=Object.freeze(Object.defineProperty({__proto__:null,assetList:st,createAsset:rt,createEntry:Q,deleteAsset:it,deleteEntry:L,deleteEntryObject:Z,dmList:ct,editEntry:P,editEntryObject:tt,entryList:G,filterOptions:X,getAsset:nt,getDatamanager:ot,getEcAuthKey:I,getEntry:H,getEntryAsset:et,getEntryEnv:B,getEntryShortID:k,getPublicAuthKey:U,getSchema:W,loginEc:F,loginPublic:z,logoutEc:R,logoutPublic:J,modelList:at,publicApi:V,raw:lt,resourceList:ut,sdkOptions:S},Symbol.toStringTag,{value:"Module"})),{entryList:wt,getEntry:Et,getAsset:bt,assetList:At,createAsset:kt,deleteAsset:Tt,createEntry:_t,editEntry:K,deleteEntry:Ot,getSchema:jt,loginPublic:Pt,loginEc:Lt,logoutEc:St,logoutPublic:Bt,getEcAuthKey:O,getPublicAuthKey:j,dmList:Dt,modelList:qt,publicApi:vt,getDatamanager:xt,resourceList:Ct,raw:Kt}=A;function Nt(n){const{action:t}=n;if(h({action:t}),!A[t])throw new Error(`"${t}" does not exist! try one of ${Object.keys(A).join(", ")}`);return A[t](n)}class T{constructor(t){this.config=t}set(t){return new T(o(o({},this.config),t))}entries(){return a(this,arguments,function*(t={}){const e=yield this.getBestToken();return wt(f(o({},this.config),{options:t,token:e}))})}entryList(t){return a(this,null,function*(){return this.entries(t)})}getEntry(t){return a(this,null,function*(){const e=yield this.getBestToken();return Et(f(o({},this.config),{entryID:t,token:e}))})}editEntrySafe(t,e){return a(this,null,function*(){const s=yield this.getBestToken();return K(f(o({},this.config),{entryID:t,token:s,value:e,safePut:!0}))})}getSchema(){return a(this,null,function*(){return jt(this.config)})}assets(t){return a(this,null,function*(){const e=yield this.getBestToken();return At(f(o({},this.config),{options:t,token:e}))})}assetList(t){return a(this,null,function*(){return this.assets(t)})}createAsset(){return a(this,arguments,function*({file:t,name:e,options:s}={}){const r=yield this.getBestToken();return kt(f(o({},this.config),{file:t,name:e,options:s,token:r}))})}deleteAsset(t){return a(this,null,function*(){const e=yield this.getBestToken();return Tt(f(o({},this.config),{token:e,assetID:t}))})}getAsset(t){return a(this,null,function*(){const e=yield this.getBestToken();return bt(f(o({},this.config),{assetID:t,token:e}))})}createEntry(t){return a(this,null,function*(){const e=yield this.getBestToken();return _t(f(o({},this.config),{token:e,value:t}))})}editEntry(t,e){return a(this,null,function*(){const s=yield this.getBestToken();return K(f(o({},this.config),{entryID:t,token:s,value:e}))})}deleteEntry(t){return a(this,null,function*(){const e=yield this.getBestToken();return Ot(f(o({},this.config),{token:e,entryID:t}))})}resourceList(t){return a(this,null,function*(){const e=yield this.getBestToken();return Ct(f(o({},this.config),{options:t,token:e}))})}raw(t,e){return a(this,null,function*(){const s=yield this.getBestToken();return Kt(f(o({},this.config),{options:t,token:s}),e)})}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 Lt(o(o({},this.config),t)).then(this.setAuth(O(this.config)))}loginPublic(t){return Pt(o(o({},this.config),t)).then(this.setAuth(j(this.config)))}logoutPublic(){const t=this.getPublicToken();return console.log("token",t),Bt(f(o({},this.config),{token:t})).then(this.unsetAuth(j(this.config)))}logoutEc(){const t=this.getEcToken();return console.log("token",t),St(f(o({},this.config),{token:t})).then(this.unsetAuth(O(this.config)))}getPublicToken(){return this.config.token||this.getAuth(j(this.config))}getEcToken(){return this.config.token||this.getAuth(O(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})}dmID(t){return this.set({dmID:t})}dm(t){return this.dmShortID(t)}assetGroup(t){return this.set({assetGroup:t})}assetgroup(t){return this.assetGroup(t)}subdomain(t){return this.set({subdomain:t})}resource(t){return this.set({resource:t})}route(t){return this.set({route:t})}publicApi(){return vt(this.config)}getDatamanager(t){return a(this,null,function*(){const e=yield this.getBestToken();return xt(f(o({},this.config),{dmID:t,token:e}))})}dmList(){return a(this,arguments,function*(t={}){const e=yield this.getBestToken();return Dt(f(o({},this.config),{options:t,token:e}))})}modelList(){return a(this,arguments,function*(t={}){const e=yield this.getBestToken();return qt(f(o({},this.config),{options:t,token:e}))})}}const zt=n=>new T({env:n});exports.Sdk=T;exports.act=Nt;exports.apiURL=p;exports.apis=b;exports.assetList=st;exports.createAsset=rt;exports.createEntry=Q;exports.deleteAsset=it;exports.deleteEntry=L;exports.deleteEntryObject=Z;exports.dmList=ct;exports.editEntry=P;exports.editEntryObject=tt;exports.entryList=G;exports.expect=h;exports.fetcher=g;exports.filterOptions=X;exports.getAsset=nt;exports.getDatamanager=ot;exports.getEcAuthKey=I;exports.getEntry=H;exports.getEntryAsset=et;exports.getEntryEnv=B;exports.getEntryShortID=k;exports.getPublicAuthKey=U;exports.getSchema=W;exports.loginEc=F;exports.loginPublic=z;exports.logoutEc=R;exports.logoutPublic=J;exports.modelList=at;exports.publicApi=V;exports.query=w;exports.raw=lt;exports.resourceList=ut;exports.sdk=zt;exports.sdkOptions=S;