ec.fdk 0.1.0 → 0.1.1
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/package.json +1 -1
- package/src/index.mjs +0 -4
- package/src/lib/README.md +0 -7
- package/src/lib/actions.mjs +0 -3
- package/src/lib/api.mjs +0 -137
- package/src/lib/assets.mjs +0 -21
- package/src/lib/auth.mjs +0 -77
- package/src/lib/entries.mjs +0 -54
- package/src/lib/util.mjs +0 -48
- package/src/storage.mjs +0 -70
- package/vite.config.js +0 -19
package/package.json
CHANGED
package/src/index.mjs
DELETED
package/src/lib/README.md
DELETED
package/src/lib/actions.mjs
DELETED
package/src/lib/api.mjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import * as actions from "./actions.mjs";
|
|
2
|
-
import { expect } from "./util.mjs";
|
|
3
|
-
export * from "./util.mjs";
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
entryList,
|
|
7
|
-
getEntry,
|
|
8
|
-
getAsset,
|
|
9
|
-
assetList,
|
|
10
|
-
createEntry,
|
|
11
|
-
editEntry,
|
|
12
|
-
loginPublic,
|
|
13
|
-
loginEc,
|
|
14
|
-
logoutEc,
|
|
15
|
-
logoutPublic,
|
|
16
|
-
} = actions;
|
|
17
|
-
|
|
18
|
-
export function act(config) {
|
|
19
|
-
const { action } = config;
|
|
20
|
-
expect({ action });
|
|
21
|
-
if (!actions[action]) {
|
|
22
|
-
throw new Error(
|
|
23
|
-
`"${action}" does not exist! try one of ${Object.keys(actions).join(
|
|
24
|
-
", "
|
|
25
|
-
)}`
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
return actions[action](config);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
class Sdk {
|
|
32
|
-
constructor(config) {
|
|
33
|
-
this.config = config;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
set(obj) {
|
|
37
|
-
// "copy on write"
|
|
38
|
-
return new Sdk({ ...this.config, ...obj });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
handle(handlers) {
|
|
42
|
-
const keys = Object.keys(this.config);
|
|
43
|
-
const [_, handle] =
|
|
44
|
-
Object.entries(handlers).find(([k, v]) => keys.includes(k)) || [];
|
|
45
|
-
if (!handle) {
|
|
46
|
-
throw new Error(
|
|
47
|
-
`you need to first set ${Object.keys(handlers).join(" | ")}`
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
return handle(this.config);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
///
|
|
54
|
-
|
|
55
|
-
entries(options) {
|
|
56
|
-
return entryList({ ...this.config, options });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
assets(options) {
|
|
60
|
-
return assetList({ ...this.config, options });
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
get() {
|
|
64
|
-
return this.handle({
|
|
65
|
-
assetID: getAsset,
|
|
66
|
-
entryID: getEntry,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
del() {}
|
|
71
|
-
|
|
72
|
-
createEntry(value) {
|
|
73
|
-
return createEntry({ ...this.config, value });
|
|
74
|
-
}
|
|
75
|
-
editEntry(value) {
|
|
76
|
-
return editEntry({ ...this.config, value });
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
create(value) {
|
|
80
|
-
return this.set({ value }).handle({
|
|
81
|
-
//assetID: createAsset,
|
|
82
|
-
model: createEntry,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
edit(value) {
|
|
87
|
-
return this.set({ value }).handle({
|
|
88
|
-
//assetID: createAsset,
|
|
89
|
-
entryID: editEntry,
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
//
|
|
94
|
-
|
|
95
|
-
loginPublic(config) {
|
|
96
|
-
//
|
|
97
|
-
return loginPublic({ ...this.config, ...config });
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
logoutPublic() {
|
|
101
|
-
return logoutPublic(this.config);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
loginEc(config) {
|
|
105
|
-
return loginEc({ ...this.config, ...config });
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
logoutEc() {
|
|
109
|
-
return logoutEc(this.config);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const addSetter = (...args) => {
|
|
114
|
-
const [main] = args;
|
|
115
|
-
|
|
116
|
-
args.forEach((alias) => {
|
|
117
|
-
Sdk.prototype[alias] = function (value) {
|
|
118
|
-
return this.set({ [main]: value });
|
|
119
|
-
};
|
|
120
|
-
});
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const addAlias = (a, b) => {
|
|
124
|
-
Sdk.prototype[a] = Sdk.prototype[b];
|
|
125
|
-
};
|
|
126
|
-
// define setters with aliases
|
|
127
|
-
addSetter("dmShortID", "dm", "dmshortid");
|
|
128
|
-
addSetter("model");
|
|
129
|
-
addSetter("token");
|
|
130
|
-
addSetter("entryID", "entry");
|
|
131
|
-
addSetter("assetGroup");
|
|
132
|
-
addSetter("assetID", "asset");
|
|
133
|
-
addAlias("assetgroup", "assetGroup");
|
|
134
|
-
addAlias("entryList", "entries");
|
|
135
|
-
addAlias("assetList", "assets");
|
|
136
|
-
|
|
137
|
-
export const sdk = (env) => new Sdk({ env });
|
package/src/lib/assets.mjs
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { expect, query, apiURL, fetcher } from "./util.mjs";
|
|
2
|
-
|
|
3
|
-
export async function getAsset({ env, dmShortID, assetGroup, assetID, token }) {
|
|
4
|
-
expect({ env, dmShortID, assetGroup, assetID });
|
|
5
|
-
const q = query({ assetID: assetID });
|
|
6
|
-
const url = apiURL(`a/${dmShortID}/${assetGroup}?${q}`, env);
|
|
7
|
-
const list = await fetcher(url, { dmShortID, token });
|
|
8
|
-
return list._embedded["ec:dm-asset"];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function assetList(config) {
|
|
12
|
-
let { env, dmShortID, assetGroup, token, options = {} } = config;
|
|
13
|
-
expect({ env, dmShortID, assetGroup });
|
|
14
|
-
options = { size: 50, page: 1, _list: true, ...options };
|
|
15
|
-
// name~ = search
|
|
16
|
-
const q = query(options);
|
|
17
|
-
const url = apiURL(`a/${dmShortID}/${assetGroup}?${q}`, env);
|
|
18
|
-
const { count, total, _embedded } = await fetcher(url, { dmShortID, token });
|
|
19
|
-
const items = _embedded ? _embedded[`ec:dm-asset`] : [];
|
|
20
|
-
return { count, total, items };
|
|
21
|
-
}
|
package/src/lib/auth.mjs
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { expect, apiURL, fetcher } from "./util.mjs";
|
|
2
|
-
|
|
3
|
-
const accountServer = {
|
|
4
|
-
stage: "https://accounts.cachena.entrecode.de/",
|
|
5
|
-
production: "https://accounts.cachena.entrecode.de/",
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
export async function loginPublic(config) {
|
|
9
|
-
let { env, dmShortID, email, password } = config;
|
|
10
|
-
expect({ env, dmShortID, email, password });
|
|
11
|
-
// TODO: check if already logged in?
|
|
12
|
-
const url = apiURL(`api/${dmShortID}/_auth/login?clientID=rest`, env);
|
|
13
|
-
const res = await fetcher(
|
|
14
|
-
url,
|
|
15
|
-
{},
|
|
16
|
-
{
|
|
17
|
-
method: "POST",
|
|
18
|
-
body: JSON.stringify({ email, password }),
|
|
19
|
-
headers: {
|
|
20
|
-
"Content-Type": "application/json",
|
|
21
|
-
},
|
|
22
|
-
}
|
|
23
|
-
);
|
|
24
|
-
return res;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export async function loginEc(config) {
|
|
28
|
-
let { env, email, password } = config;
|
|
29
|
-
expect({ env, email, password });
|
|
30
|
-
const url = `${accountServer[env]}auth/login?clientID=rest`;
|
|
31
|
-
const res = await fetcher(
|
|
32
|
-
url,
|
|
33
|
-
{},
|
|
34
|
-
{
|
|
35
|
-
method: "POST",
|
|
36
|
-
body: JSON.stringify({ email, password }),
|
|
37
|
-
headers: {
|
|
38
|
-
"Content-Type": "application/json",
|
|
39
|
-
},
|
|
40
|
-
}
|
|
41
|
-
);
|
|
42
|
-
return res;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function logoutPublic(config) {
|
|
46
|
-
let { dmShortID, env } = config;
|
|
47
|
-
expect({ dmShortID, env, token });
|
|
48
|
-
const url = apiURL(
|
|
49
|
-
`api/${dmShortID}/_auth/logout?clientID=rest&token=${token}`,
|
|
50
|
-
env
|
|
51
|
-
);
|
|
52
|
-
const res = await fetcher(
|
|
53
|
-
url,
|
|
54
|
-
{ dmShortID, rawRes: true },
|
|
55
|
-
{
|
|
56
|
-
method: "POST",
|
|
57
|
-
}
|
|
58
|
-
);
|
|
59
|
-
return res;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export async function logoutEc(config) {
|
|
63
|
-
let { env, token } = config;
|
|
64
|
-
expect({ env });
|
|
65
|
-
const url = `${accountServer[env]}auth/logout?clientID=rest`;
|
|
66
|
-
const res = await fetcher(
|
|
67
|
-
url,
|
|
68
|
-
{
|
|
69
|
-
rawRes: true,
|
|
70
|
-
token,
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
method: "POST",
|
|
74
|
-
}
|
|
75
|
-
);
|
|
76
|
-
return res;
|
|
77
|
-
}
|
package/src/lib/entries.mjs
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { expect, query, apiURL, fetcher } from "./util.mjs";
|
|
2
|
-
|
|
3
|
-
export async function entryList(config) {
|
|
4
|
-
let { env, dmShortID, model, options = {} } = config;
|
|
5
|
-
expect({ env, dmShortID, model });
|
|
6
|
-
options = { size: 50, page: 1, _list: true, ...options };
|
|
7
|
-
// name~ = search
|
|
8
|
-
const q = query(options);
|
|
9
|
-
const url = apiURL(`api/${dmShortID}/${model}?${q}`, env);
|
|
10
|
-
const { count, total, _embedded } = await fetcher(url, config);
|
|
11
|
-
|
|
12
|
-
const items = _embedded ? _embedded[`${dmShortID}:${model}`] : [];
|
|
13
|
-
return { count, total, items };
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function getEntry({ env, dmShortID, model, entryID, token }) {
|
|
17
|
-
expect({ env, dmShortID, model, entryID });
|
|
18
|
-
const q = query({ _id: entryID });
|
|
19
|
-
const url = apiURL(`api/${dmShortID}/${model}?${q}`, env);
|
|
20
|
-
return fetcher(url, { dmShortID, token });
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function createEntry({ env, dmShortID, model, value, token }) {
|
|
24
|
-
expect({ env, dmShortID, model, value });
|
|
25
|
-
console.log("create entry", dmShortID, model, value);
|
|
26
|
-
const url = apiURL(`api/${dmShortID}/${model}`, env);
|
|
27
|
-
console.log("url", url);
|
|
28
|
-
const res = await fetcher(
|
|
29
|
-
url,
|
|
30
|
-
{ env, dmShortID, token },
|
|
31
|
-
{
|
|
32
|
-
method: "POST",
|
|
33
|
-
body: JSON.stringify(value),
|
|
34
|
-
headers: {
|
|
35
|
-
"Content-Type": "application/json",
|
|
36
|
-
},
|
|
37
|
-
}
|
|
38
|
-
);
|
|
39
|
-
return res;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export async function editEntry({
|
|
43
|
-
env,
|
|
44
|
-
dmShortID,
|
|
45
|
-
model,
|
|
46
|
-
entryID,
|
|
47
|
-
value,
|
|
48
|
-
token,
|
|
49
|
-
}) {
|
|
50
|
-
expect({ env, dmShortID, model, entryID, value });
|
|
51
|
-
console.log("edit entry", dmShortID, model, entryID, value);
|
|
52
|
-
return;
|
|
53
|
-
// https://datamanager.cachena.entrecode.de/api/83cc6374/
|
|
54
|
-
}
|
package/src/lib/util.mjs
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
export async function fetcher(url, config = {}, options = {}) {
|
|
2
|
-
const { token, rawRes } = config;
|
|
3
|
-
if (token) {
|
|
4
|
-
options.headers = {
|
|
5
|
-
...(options.headers || {}),
|
|
6
|
-
Authorization: `Bearer ${token}`,
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
const res = await fetch(url, options);
|
|
10
|
-
console.log("fetch", url, options);
|
|
11
|
-
if (!res.ok) {
|
|
12
|
-
throw new Error(res.statusText);
|
|
13
|
-
}
|
|
14
|
-
if (rawRes) {
|
|
15
|
-
return res;
|
|
16
|
-
}
|
|
17
|
-
return await res.json();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const apis = {
|
|
21
|
-
production: "https://datamanager.entrecode.de/",
|
|
22
|
-
stage: "https://datamanager.cachena.entrecode.de/",
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export function apiURL(route, env = "stage") {
|
|
26
|
-
const base = apis[env];
|
|
27
|
-
if (!base) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`env "${env}" not found. Try one of ${Object.keys(apis).join(", ")}`
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
return base + route;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function query(params, sort = true) {
|
|
36
|
-
return Object.entries(params)
|
|
37
|
-
.sort((a, b) => a[0].localeCompare(b[0]))
|
|
38
|
-
.map(([key, value]) => `${key}=${value}`)
|
|
39
|
-
.join("&");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function expect(obj) {
|
|
43
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
44
|
-
if (value === undefined) {
|
|
45
|
-
throw new Error(`expected ${key} to be set!`);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
package/src/storage.mjs
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { persistentMap } from "@nanostores/persistent";
|
|
2
|
-
import {
|
|
3
|
-
loginPublic,
|
|
4
|
-
logoutPublic,
|
|
5
|
-
loginEc,
|
|
6
|
-
logoutEc,
|
|
7
|
-
} from "./lib/actions.mjs";
|
|
8
|
-
|
|
9
|
-
// maybe switch to cookie: https://github.com/nanostores/persistent#persistent-engines
|
|
10
|
-
export const auth = persistentMap(
|
|
11
|
-
"fdk.auth",
|
|
12
|
-
{},
|
|
13
|
-
{
|
|
14
|
-
encode: JSON.stringify,
|
|
15
|
-
decode: JSON.parse,
|
|
16
|
-
}
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
export function getPublicAuth(config) {
|
|
20
|
-
let { dmShortID } = config;
|
|
21
|
-
if (!dmShortID) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
return auth.get()?.[dmShortID];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function getEcAuth(config) {
|
|
28
|
-
let { env } = config;
|
|
29
|
-
if (!env) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
return auth.get()?.[env];
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function getAuth(config) {
|
|
36
|
-
return getEcAuth(config) || getPublicAuth(config);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function hasAnyToken(config) {
|
|
40
|
-
return !!getEcAuth(config)?.token || !!getPublicAuth(config)?.token;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export function hasEcToken(config) {
|
|
44
|
-
return !!getEcAuth(config)?.token;
|
|
45
|
-
}
|
|
46
|
-
export function hasPublicToken(config) {
|
|
47
|
-
return !!getPublicAuth(config)?.token;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export async function loginPublicStored(config) {
|
|
51
|
-
const res = await loginPublic(config);
|
|
52
|
-
auth.setKey(config.dmShortID, { token: res.token });
|
|
53
|
-
return res;
|
|
54
|
-
}
|
|
55
|
-
export async function loginEcStored(config) {
|
|
56
|
-
const res = await loginEc(config);
|
|
57
|
-
auth.setKey(config.env, { token: res.token });
|
|
58
|
-
return res;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export async function logoutPublicStored(config) {
|
|
62
|
-
const res = await logoutPublic(config);
|
|
63
|
-
auth.setKey(config.dmShortID, {});
|
|
64
|
-
return res;
|
|
65
|
-
}
|
|
66
|
-
export async function logoutEcStored(config) {
|
|
67
|
-
const res = await logoutEc(config);
|
|
68
|
-
auth.setKey(config.env, { token: res.token });
|
|
69
|
-
return res;
|
|
70
|
-
}
|
package/vite.config.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "vite";
|
|
2
|
-
import { dependencies } from "./package.json";
|
|
3
|
-
import { resolve } from "path";
|
|
4
|
-
|
|
5
|
-
// https://vitejs.dev/config/
|
|
6
|
-
export default defineConfig({
|
|
7
|
-
plugins: [],
|
|
8
|
-
build: {
|
|
9
|
-
lib: {
|
|
10
|
-
entry: resolve(__dirname, "src", "index.mjs"),
|
|
11
|
-
formats: ["es", "cjs"],
|
|
12
|
-
fileName: (ext) => ({ es: "index.mjs", cjs: "index.cjs" }[ext]),
|
|
13
|
-
},
|
|
14
|
-
rollupOptions: {
|
|
15
|
-
external: [...Object.keys(dependencies)],
|
|
16
|
-
},
|
|
17
|
-
target: "esnext",
|
|
18
|
-
},
|
|
19
|
-
});
|