@akanjs/base 0.0.54 → 0.0.55
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/index.mjs +1 -0
- package/package.json +7 -1
- package/src/base.mjs +159 -0
- package/src/baseEnv.mjs +65 -0
- package/src/index.mjs +4 -0
- package/src/scalar.mjs +105 -0
- package/src/types.mjs +0 -0
package/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akanjs/base",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.55",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,5 +16,11 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"dayjs": "^1.11.13"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"require": "./index.js",
|
|
23
|
+
"import": "./index.mjs"
|
|
24
|
+
}
|
|
19
25
|
}
|
|
20
26
|
}
|
package/src/base.mjs
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
class Enum {
|
|
2
|
+
constructor(values) {
|
|
3
|
+
this.values = values;
|
|
4
|
+
this.valueMap = new Map(values.map((value, idx) => [value, idx]));
|
|
5
|
+
}
|
|
6
|
+
value;
|
|
7
|
+
// for type
|
|
8
|
+
valueMap;
|
|
9
|
+
has(value) {
|
|
10
|
+
return this.valueMap.has(value);
|
|
11
|
+
}
|
|
12
|
+
indexOf(value) {
|
|
13
|
+
const idx = this.valueMap.get(value);
|
|
14
|
+
if (idx === void 0)
|
|
15
|
+
throw new Error(`Value ${value} is not in enum`);
|
|
16
|
+
return idx;
|
|
17
|
+
}
|
|
18
|
+
find(callback) {
|
|
19
|
+
const val = this.values.find(callback);
|
|
20
|
+
if (val === void 0)
|
|
21
|
+
throw new Error(`Value not found in enum`);
|
|
22
|
+
return val;
|
|
23
|
+
}
|
|
24
|
+
findIndex(callback) {
|
|
25
|
+
const idx = this.values.findIndex(callback);
|
|
26
|
+
if (idx === -1)
|
|
27
|
+
throw new Error(`Value not found in enum`);
|
|
28
|
+
return idx;
|
|
29
|
+
}
|
|
30
|
+
filter(callback) {
|
|
31
|
+
return this.values.filter(callback);
|
|
32
|
+
}
|
|
33
|
+
map(callback) {
|
|
34
|
+
return this.values.map(callback);
|
|
35
|
+
}
|
|
36
|
+
forEach(callback) {
|
|
37
|
+
this.values.forEach(callback);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const enumOf = (values) => new Enum(values);
|
|
41
|
+
class DataList {
|
|
42
|
+
// [immerable] = true;
|
|
43
|
+
#idMap;
|
|
44
|
+
length;
|
|
45
|
+
values;
|
|
46
|
+
constructor(data = []) {
|
|
47
|
+
this.values = Array.isArray(data) ? [...data] : [...data.values];
|
|
48
|
+
this.#idMap = new Map(this.values.map((value, idx) => [value.id, idx]));
|
|
49
|
+
this.length = this.values.length;
|
|
50
|
+
}
|
|
51
|
+
indexOf(id) {
|
|
52
|
+
const idx = this.#idMap.get(id);
|
|
53
|
+
if (idx === void 0)
|
|
54
|
+
throw new Error(`Value ${id} is not in list`);
|
|
55
|
+
return idx;
|
|
56
|
+
}
|
|
57
|
+
set(value) {
|
|
58
|
+
const idx = this.#idMap.get(value.id);
|
|
59
|
+
if (idx !== void 0)
|
|
60
|
+
this.values = [...this.values.slice(0, idx), value, ...this.values.slice(idx + 1)];
|
|
61
|
+
else {
|
|
62
|
+
this.#idMap.set(value.id, this.length);
|
|
63
|
+
this.values = [...this.values, value];
|
|
64
|
+
this.length++;
|
|
65
|
+
}
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
delete(id) {
|
|
69
|
+
const idx = this.#idMap.get(id);
|
|
70
|
+
if (idx === void 0)
|
|
71
|
+
return this;
|
|
72
|
+
this.#idMap.delete(id);
|
|
73
|
+
this.values.splice(idx, 1);
|
|
74
|
+
this.values.slice(idx).forEach((value, i) => this.#idMap.set(value.id, i + idx));
|
|
75
|
+
this.length--;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
get(id) {
|
|
79
|
+
const idx = this.#idMap.get(id);
|
|
80
|
+
if (idx === void 0)
|
|
81
|
+
return void 0;
|
|
82
|
+
return this.values[idx];
|
|
83
|
+
}
|
|
84
|
+
at(idx) {
|
|
85
|
+
return this.values.at(idx);
|
|
86
|
+
}
|
|
87
|
+
pickAt(idx) {
|
|
88
|
+
const value = this.values.at(idx);
|
|
89
|
+
if (value === void 0)
|
|
90
|
+
throw new Error(`Value at ${idx} is undefined`);
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
pick(id) {
|
|
94
|
+
return this.values[this.indexOf(id)];
|
|
95
|
+
}
|
|
96
|
+
has(id) {
|
|
97
|
+
return this.#idMap.has(id);
|
|
98
|
+
}
|
|
99
|
+
find(fn) {
|
|
100
|
+
const val = this.values.find(fn);
|
|
101
|
+
return val;
|
|
102
|
+
}
|
|
103
|
+
findIndex(fn) {
|
|
104
|
+
const val = this.values.findIndex(fn);
|
|
105
|
+
return val;
|
|
106
|
+
}
|
|
107
|
+
some(fn) {
|
|
108
|
+
return this.values.some(fn);
|
|
109
|
+
}
|
|
110
|
+
every(fn) {
|
|
111
|
+
return this.values.every(fn);
|
|
112
|
+
}
|
|
113
|
+
forEach(fn) {
|
|
114
|
+
this.values.forEach(fn);
|
|
115
|
+
}
|
|
116
|
+
map(fn) {
|
|
117
|
+
return this.values.map(fn);
|
|
118
|
+
}
|
|
119
|
+
flatMap(fn) {
|
|
120
|
+
return this.values.flatMap(fn);
|
|
121
|
+
}
|
|
122
|
+
sort(fn) {
|
|
123
|
+
return new DataList(this.values.sort(fn));
|
|
124
|
+
}
|
|
125
|
+
filter(fn) {
|
|
126
|
+
return new DataList(this.values.filter(fn));
|
|
127
|
+
}
|
|
128
|
+
reduce(fn, initialValue) {
|
|
129
|
+
return this.values.reduce(fn, initialValue);
|
|
130
|
+
}
|
|
131
|
+
slice(start, end = this.length) {
|
|
132
|
+
return new DataList(this.values.slice(start, end));
|
|
133
|
+
}
|
|
134
|
+
save() {
|
|
135
|
+
return new DataList(this);
|
|
136
|
+
}
|
|
137
|
+
[Symbol.iterator]() {
|
|
138
|
+
return this.values[Symbol.iterator]();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const version = "0.9.0";
|
|
142
|
+
const logo = `
|
|
143
|
+
_ _ _
|
|
144
|
+
/ \\ | | ____ _ _ __ (_)___
|
|
145
|
+
/ _ \\ | |/ / _' | '_ \\ | / __|
|
|
146
|
+
/ ___ \\| < (_| | | | |_ | \\__ \\
|
|
147
|
+
/_/ \\_\\_|\\_\\__,_|_| |_(_)/ |___/
|
|
148
|
+
|__/ ver ${version}
|
|
149
|
+
? See more details on docs https://www.akanjs.com/docs
|
|
150
|
+
\u2605 Star Akanjs on GitHub https://github.com/aka-bassman/akanjs
|
|
151
|
+
|
|
152
|
+
`;
|
|
153
|
+
export {
|
|
154
|
+
DataList,
|
|
155
|
+
Enum,
|
|
156
|
+
enumOf,
|
|
157
|
+
logo,
|
|
158
|
+
version
|
|
159
|
+
};
|
package/src/baseEnv.mjs
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//! Nextjs는 환경변수를 build time에 그냥 하드코딩으로 값을 넣어버림. operationMode같은것들 잘 동작안할 수 있음. 추후 수정 필요.
|
|
2
|
+
const appName = process.env.NEXT_PUBLIC_APP_NAME ?? "unknown";
|
|
3
|
+
const repoName = process.env.NEXT_PUBLIC_REPO_NAME ?? "unknown";
|
|
4
|
+
const serveDomain = process.env.NEXT_PUBLIC_SERVE_DOMAIN ?? "unknown";
|
|
5
|
+
if (appName === "unknown")
|
|
6
|
+
throw new Error("environment variable NEXT_PUBLIC_APP_NAME is required");
|
|
7
|
+
if (repoName === "unknown")
|
|
8
|
+
throw new Error("environment variable NEXT_PUBLIC_REPO_NAME is required");
|
|
9
|
+
if (serveDomain === "unknown")
|
|
10
|
+
throw new Error("environment variable NEXT_PUBLIC_SERVE_DOMAIN is required");
|
|
11
|
+
const environment = process.env.NEXT_PUBLIC_ENV ?? "debug";
|
|
12
|
+
const operationType = typeof window !== "undefined" ? "client" : process.env.NEXT_RUNTIME ? "client" : "server";
|
|
13
|
+
const operationMode = process.env.NEXT_PUBLIC_OPERATION_MODE ?? "cloud";
|
|
14
|
+
const networkType = process.env.NEXT_PUBLIC_NETWORK_TYPE ?? (environment === "main" ? "mainnet" : environment === "develop" ? "testnet" : "debugnet");
|
|
15
|
+
const tunnelUsername = process.env.SSU_TUNNEL_USERNAME ?? "root";
|
|
16
|
+
const tunnelPassword = process.env.SSU_TUNNEL_PASSWORD ?? repoName;
|
|
17
|
+
const baseEnv = {
|
|
18
|
+
repoName,
|
|
19
|
+
serveDomain,
|
|
20
|
+
appName,
|
|
21
|
+
environment,
|
|
22
|
+
operationType,
|
|
23
|
+
operationMode,
|
|
24
|
+
networkType,
|
|
25
|
+
tunnelUsername,
|
|
26
|
+
tunnelPassword
|
|
27
|
+
};
|
|
28
|
+
const side = typeof window === "undefined" ? "server" : "client";
|
|
29
|
+
const renderMode = process.env.RENDER_ENV ?? "ssr";
|
|
30
|
+
const clientHost = process.env.NEXT_PUBLIC_CLIENT_HOST ?? (operationMode === "local" || side === "server" ? "localhost" : window.location.hostname);
|
|
31
|
+
const clientPort = parseInt(
|
|
32
|
+
process.env.NEXT_PUBLIC_CLIENT_PORT ?? (operationMode === "local" ? renderMode === "ssr" ? "4200" : "4201" : "443")
|
|
33
|
+
);
|
|
34
|
+
const clientHttpProtocol = side === "client" ? window.location.protocol : clientHost === "localhost" ? "http:" : "https:";
|
|
35
|
+
const clientHttpUri = `${clientHttpProtocol}//${clientHost}${clientPort === 443 ? "" : `:${clientPort}`}`;
|
|
36
|
+
const serverHost = process.env.SERVER_HOST ?? (operationMode === "local" ? typeof window === "undefined" ? "localhost" : window.location.host.split(":")[0] : renderMode === "csr" ? `${appName}-${environment}.${serveDomain}` : side === "client" ? window.location.host.split(":")[0] : operationMode === "cloud" ? `backend-svc.${appName}-${environment}.svc.cluster.local` : "localhost");
|
|
37
|
+
const serverPort = parseInt(
|
|
38
|
+
process.env.SERVER_PORT ?? (operationMode === "local" || side === "server" ? "8080" : "443")
|
|
39
|
+
);
|
|
40
|
+
const serverHttpProtocol = side === "client" ? window.location.protocol : "http:";
|
|
41
|
+
const serverHttpUri = `${serverHttpProtocol}//${serverHost}${serverPort === 443 ? "" : `:${serverPort}`}/backend`;
|
|
42
|
+
const serverGraphqlUri = `${serverHttpUri}/graphql`;
|
|
43
|
+
const serverWsProtocol = serverHttpProtocol === "http:" ? "ws:" : "wss:";
|
|
44
|
+
const serverWsUri = `${serverWsProtocol}//${serverHost}${serverPort === 443 ? "" : `:${serverPort}`}`;
|
|
45
|
+
const baseClientEnv = {
|
|
46
|
+
...baseEnv,
|
|
47
|
+
side,
|
|
48
|
+
renderMode,
|
|
49
|
+
websocket: true,
|
|
50
|
+
clientHost,
|
|
51
|
+
clientPort,
|
|
52
|
+
clientHttpProtocol,
|
|
53
|
+
clientHttpUri,
|
|
54
|
+
serverHost,
|
|
55
|
+
serverPort,
|
|
56
|
+
serverHttpProtocol,
|
|
57
|
+
serverHttpUri,
|
|
58
|
+
serverGraphqlUri,
|
|
59
|
+
serverWsProtocol,
|
|
60
|
+
serverWsUri
|
|
61
|
+
};
|
|
62
|
+
export {
|
|
63
|
+
baseClientEnv,
|
|
64
|
+
baseEnv
|
|
65
|
+
};
|
package/src/index.mjs
ADDED
package/src/scalar.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import dayjsLib, { Dayjs } from "dayjs";
|
|
2
|
+
const dayjs = dayjsLib;
|
|
3
|
+
class BaseObject {
|
|
4
|
+
id;
|
|
5
|
+
createdAt;
|
|
6
|
+
updatedAt;
|
|
7
|
+
removedAt;
|
|
8
|
+
}
|
|
9
|
+
class Int {
|
|
10
|
+
__Scalar__;
|
|
11
|
+
}
|
|
12
|
+
class Upload {
|
|
13
|
+
__Scalar__;
|
|
14
|
+
filename;
|
|
15
|
+
mimetype;
|
|
16
|
+
encoding;
|
|
17
|
+
createReadStream;
|
|
18
|
+
}
|
|
19
|
+
class Float {
|
|
20
|
+
__Scalar__;
|
|
21
|
+
}
|
|
22
|
+
class ID {
|
|
23
|
+
__Scalar__;
|
|
24
|
+
}
|
|
25
|
+
class JSON {
|
|
26
|
+
__Scalar__;
|
|
27
|
+
}
|
|
28
|
+
const getNonArrayModel = (arraiedModel2) => {
|
|
29
|
+
let arrDepth = 0;
|
|
30
|
+
let target = arraiedModel2;
|
|
31
|
+
while (Array.isArray(target)) {
|
|
32
|
+
target = target[0];
|
|
33
|
+
arrDepth++;
|
|
34
|
+
}
|
|
35
|
+
return [target, arrDepth];
|
|
36
|
+
};
|
|
37
|
+
const arraiedModel = (modelRef, arrDepth = 0) => {
|
|
38
|
+
let target = modelRef;
|
|
39
|
+
for (let i = 0; i < arrDepth; i++)
|
|
40
|
+
target = [target];
|
|
41
|
+
return target;
|
|
42
|
+
};
|
|
43
|
+
const applyFnToArrayObjects = (arraiedData, fn) => {
|
|
44
|
+
if (Array.isArray(arraiedData))
|
|
45
|
+
return arraiedData.map((data) => applyFnToArrayObjects(data, fn));
|
|
46
|
+
return fn(arraiedData);
|
|
47
|
+
};
|
|
48
|
+
const gqlScalars = [String, Boolean, Date, ID, Int, Float, Upload, JSON, Map];
|
|
49
|
+
const gqlScalarNames = ["ID", "Int", "Float", "String", "Boolean", "Date", "Upload", "JSON", "Map"];
|
|
50
|
+
const scalarSet = /* @__PURE__ */ new Set([String, Boolean, Date, ID, Int, Float, Upload, JSON, Map]);
|
|
51
|
+
const scalarNameMap = /* @__PURE__ */ new Map([
|
|
52
|
+
[ID, "ID"],
|
|
53
|
+
[Int, "Int"],
|
|
54
|
+
[Float, "Float"],
|
|
55
|
+
[String, "String"],
|
|
56
|
+
[Boolean, "Boolean"],
|
|
57
|
+
[Date, "Date"],
|
|
58
|
+
[Upload, "Upload"],
|
|
59
|
+
[JSON, "JSON"],
|
|
60
|
+
[Map, "Map"]
|
|
61
|
+
]);
|
|
62
|
+
const scalarArgMap = /* @__PURE__ */ new Map([
|
|
63
|
+
[ID, null],
|
|
64
|
+
[String, ""],
|
|
65
|
+
[Boolean, false],
|
|
66
|
+
[Date, dayjs(/* @__PURE__ */ new Date(-1))],
|
|
67
|
+
[Int, 0],
|
|
68
|
+
[Float, 0],
|
|
69
|
+
[JSON, {}],
|
|
70
|
+
[Map, {}]
|
|
71
|
+
]);
|
|
72
|
+
const scalarDefaultMap = /* @__PURE__ */ new Map([
|
|
73
|
+
[ID, null],
|
|
74
|
+
[String, ""],
|
|
75
|
+
[Boolean, false],
|
|
76
|
+
[Date, dayjs(/* @__PURE__ */ new Date(-1))],
|
|
77
|
+
[Int, 0],
|
|
78
|
+
[Float, 0],
|
|
79
|
+
[JSON, {}]
|
|
80
|
+
]);
|
|
81
|
+
const isGqlClass = (modelRef) => !scalarSet.has(modelRef);
|
|
82
|
+
const isGqlScalar = (modelRef) => scalarSet.has(modelRef);
|
|
83
|
+
const isGqlMap = (modelRef) => modelRef === Map;
|
|
84
|
+
export {
|
|
85
|
+
BaseObject,
|
|
86
|
+
Dayjs,
|
|
87
|
+
Float,
|
|
88
|
+
ID,
|
|
89
|
+
Int,
|
|
90
|
+
JSON,
|
|
91
|
+
Upload,
|
|
92
|
+
applyFnToArrayObjects,
|
|
93
|
+
arraiedModel,
|
|
94
|
+
dayjs,
|
|
95
|
+
getNonArrayModel,
|
|
96
|
+
gqlScalarNames,
|
|
97
|
+
gqlScalars,
|
|
98
|
+
isGqlClass,
|
|
99
|
+
isGqlMap,
|
|
100
|
+
isGqlScalar,
|
|
101
|
+
scalarArgMap,
|
|
102
|
+
scalarDefaultMap,
|
|
103
|
+
scalarNameMap,
|
|
104
|
+
scalarSet
|
|
105
|
+
};
|
package/src/types.mjs
ADDED
|
File without changes
|