@akanjs/signal 0.0.45 → 0.0.47
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.js +2 -2312
- package/package.json +1 -3
- package/src/baseFetch.js +34 -0
- package/src/client.js +164 -0
- package/src/doc.js +101 -0
- package/src/gql.js +850 -0
- package/src/immerify.js +35 -0
- package/src/index.js +30 -0
- package/src/signalDecorators.js +520 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akanjs/signal",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.47",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,10 +16,8 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@urql/core": "^5.1.0",
|
|
19
|
-
"dayjs": "^1.11.13",
|
|
20
19
|
"immer": "^10.1.1",
|
|
21
20
|
"next": "^15.3.2",
|
|
22
|
-
"pluralize": "^8.0.0",
|
|
23
21
|
"reflect-metadata": "^0.2.2",
|
|
24
22
|
"socket.io-client": "^4.8.1"
|
|
25
23
|
}
|
package/src/baseFetch.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var baseFetch_exports = {};
|
|
19
|
+
__export(baseFetch_exports, {
|
|
20
|
+
baseFetch: () => baseFetch
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(baseFetch_exports);
|
|
23
|
+
var import_client = require("./client");
|
|
24
|
+
const nativeFetch = fetch;
|
|
25
|
+
const baseFetch = Object.assign(nativeFetch, {
|
|
26
|
+
client: import_client.client,
|
|
27
|
+
clone: function(option = {}) {
|
|
28
|
+
return {
|
|
29
|
+
...this,
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
31
|
+
client: this.client.clone(option)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
});
|
package/src/client.js
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var client_exports = {};
|
|
19
|
+
__export(client_exports, {
|
|
20
|
+
client: () => client
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(client_exports);
|
|
23
|
+
var import_base = require("@akanjs/base");
|
|
24
|
+
var import_common = require("@akanjs/common");
|
|
25
|
+
var import_core = require("@urql/core");
|
|
26
|
+
var import_socket = require("socket.io-client");
|
|
27
|
+
class SocketIo {
|
|
28
|
+
socket;
|
|
29
|
+
roomSubscribeMap = /* @__PURE__ */ new Map();
|
|
30
|
+
constructor(uri) {
|
|
31
|
+
this.socket = (0, import_socket.io)(uri, { transports: ["websocket"] });
|
|
32
|
+
this.socket.on("connect", () => {
|
|
33
|
+
this.roomSubscribeMap.forEach((option) => {
|
|
34
|
+
this.socket.emit(option.key, { ...option.message, __subscribe__: true });
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
on(event, callback) {
|
|
39
|
+
this.socket.on(event, callback);
|
|
40
|
+
}
|
|
41
|
+
removeListener(event, callback) {
|
|
42
|
+
this.socket.removeListener(event, callback);
|
|
43
|
+
}
|
|
44
|
+
removeAllListeners() {
|
|
45
|
+
this.socket.removeAllListeners();
|
|
46
|
+
}
|
|
47
|
+
hasListeners(event) {
|
|
48
|
+
return this.socket.hasListeners(event);
|
|
49
|
+
}
|
|
50
|
+
emit(key, data) {
|
|
51
|
+
this.socket.emit(key, data);
|
|
52
|
+
}
|
|
53
|
+
subscribe(option) {
|
|
54
|
+
if (!this.roomSubscribeMap.has(option.roomId)) {
|
|
55
|
+
this.roomSubscribeMap.set(option.roomId, option);
|
|
56
|
+
this.socket.emit(option.key, { ...option.message, __subscribe__: true });
|
|
57
|
+
}
|
|
58
|
+
this.socket.on(option.roomId, option.handleEvent);
|
|
59
|
+
}
|
|
60
|
+
unsubscribe(roomId, handleEvent) {
|
|
61
|
+
this.socket.removeListener(roomId, handleEvent);
|
|
62
|
+
const option = this.roomSubscribeMap.get(roomId);
|
|
63
|
+
if (this.hasListeners(roomId) || !option)
|
|
64
|
+
return;
|
|
65
|
+
this.roomSubscribeMap.delete(roomId);
|
|
66
|
+
this.socket.emit(option.key, { ...option.message, __subscribe__: false });
|
|
67
|
+
}
|
|
68
|
+
disconnect() {
|
|
69
|
+
this.socket.disconnect();
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
class Client {
|
|
74
|
+
static globalIoMap = /* @__PURE__ */ new Map();
|
|
75
|
+
static tokenStore = /* @__PURE__ */ new Map();
|
|
76
|
+
async waitUntilWebSocketConnected(ws = import_base.baseClientEnv.serverWsUri) {
|
|
77
|
+
if (import_base.baseClientEnv.side === "server")
|
|
78
|
+
return true;
|
|
79
|
+
while (!this.getIo(ws).socket.connected) {
|
|
80
|
+
import_common.Logger.verbose("waiting for websocket to initialize...");
|
|
81
|
+
await (0, import_common.sleep)(300);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
isInitialized = false;
|
|
85
|
+
uri = import_base.baseClientEnv.serverGraphqlUri;
|
|
86
|
+
ws = import_base.baseClientEnv.serverWsUri;
|
|
87
|
+
udp = null;
|
|
88
|
+
gql = (0, import_core.createClient)({ url: this.uri, fetch, exchanges: [import_core.cacheExchange, import_core.fetchExchange] });
|
|
89
|
+
jwt = null;
|
|
90
|
+
async getJwt() {
|
|
91
|
+
const isNextServer = import_base.baseClientEnv.side === "server" && import_base.baseEnv.operationType === "client";
|
|
92
|
+
if (isNextServer) {
|
|
93
|
+
const nextHeaders = require("next/headers");
|
|
94
|
+
return (await nextHeaders.cookies?.())?.get("jwt")?.value ?? (await nextHeaders.headers?.())?.get("jwt") ?? this.jwt ?? null;
|
|
95
|
+
} else
|
|
96
|
+
return Client.tokenStore.get(this) ?? null;
|
|
97
|
+
}
|
|
98
|
+
io = null;
|
|
99
|
+
init(data = {}) {
|
|
100
|
+
Object.assign(this, data);
|
|
101
|
+
this.setLink(data.uri);
|
|
102
|
+
this.setIo(data.ws);
|
|
103
|
+
this.isInitialized = true;
|
|
104
|
+
}
|
|
105
|
+
setIo(ws = import_base.baseClientEnv.serverWsUri) {
|
|
106
|
+
this.ws = ws;
|
|
107
|
+
const existingIo = Client.globalIoMap.get(ws);
|
|
108
|
+
if (existingIo) {
|
|
109
|
+
this.io = existingIo;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
this.io = new SocketIo(ws);
|
|
113
|
+
Client.globalIoMap.set(ws, this.io);
|
|
114
|
+
}
|
|
115
|
+
getIo(ws = import_base.baseClientEnv.serverWsUri) {
|
|
116
|
+
const existingIo = Client.globalIoMap.get(ws);
|
|
117
|
+
if (existingIo)
|
|
118
|
+
return existingIo;
|
|
119
|
+
const io2 = new SocketIo(ws);
|
|
120
|
+
Client.globalIoMap.set(ws, io2);
|
|
121
|
+
return io2;
|
|
122
|
+
}
|
|
123
|
+
setLink(uri = import_base.baseClientEnv.serverGraphqlUri) {
|
|
124
|
+
this.uri = uri;
|
|
125
|
+
this.gql = (0, import_core.createClient)({
|
|
126
|
+
url: this.uri,
|
|
127
|
+
fetch,
|
|
128
|
+
exchanges: [import_core.cacheExchange, import_core.fetchExchange],
|
|
129
|
+
// requestPolicy: "network-only",
|
|
130
|
+
fetchOptions: () => {
|
|
131
|
+
return {
|
|
132
|
+
headers: {
|
|
133
|
+
"apollo-require-preflight": "true",
|
|
134
|
+
...this.jwt ? { authorization: `Bearer ${this.jwt}` } : {}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
setJwt(jwt) {
|
|
141
|
+
Client.tokenStore.set(this, jwt);
|
|
142
|
+
}
|
|
143
|
+
reset() {
|
|
144
|
+
this.io?.disconnect();
|
|
145
|
+
this.io = null;
|
|
146
|
+
this.jwt = null;
|
|
147
|
+
}
|
|
148
|
+
clone(data = {}) {
|
|
149
|
+
const newClient = new Client();
|
|
150
|
+
newClient.init({ ...this, ...data });
|
|
151
|
+
if (data.jwt)
|
|
152
|
+
Client.tokenStore.set(newClient, data.jwt);
|
|
153
|
+
return newClient;
|
|
154
|
+
}
|
|
155
|
+
terminate() {
|
|
156
|
+
this.reset();
|
|
157
|
+
Client.globalIoMap.forEach((io2) => io2.disconnect());
|
|
158
|
+
this.isInitialized = false;
|
|
159
|
+
}
|
|
160
|
+
setUdp(udp) {
|
|
161
|
+
this.udp = udp;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const client = new Client();
|
package/src/doc.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var doc_exports = {};
|
|
19
|
+
__export(doc_exports, {
|
|
20
|
+
getExampleData: () => getExampleData,
|
|
21
|
+
makeRequestExample: () => makeRequestExample,
|
|
22
|
+
makeResponseExample: () => makeResponseExample
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(doc_exports);
|
|
25
|
+
var import_base = require("@akanjs/base");
|
|
26
|
+
var import_constant = require("@akanjs/constant");
|
|
27
|
+
var import_signalDecorators = require("./signalDecorators");
|
|
28
|
+
class ResponseExampleStorage {
|
|
29
|
+
}
|
|
30
|
+
const getPredefinedRequestExample = (modelRef) => {
|
|
31
|
+
return Reflect.getMetadata(modelRef, ResponseExampleStorage.prototype);
|
|
32
|
+
};
|
|
33
|
+
const getPredefinedResponseExample = (modelRef) => {
|
|
34
|
+
return Reflect.getMetadata(modelRef, ResponseExampleStorage.prototype);
|
|
35
|
+
};
|
|
36
|
+
const getResponseExample = (ref) => {
|
|
37
|
+
const [modelRef, arrDepth] = (0, import_base.getNonArrayModel)(ref);
|
|
38
|
+
const existing = getPredefinedRequestExample(modelRef);
|
|
39
|
+
if (existing)
|
|
40
|
+
return existing;
|
|
41
|
+
const isScalar = (0, import_base.isGqlScalar)(modelRef);
|
|
42
|
+
if (isScalar)
|
|
43
|
+
return (0, import_base.arraiedModel)((0, import_constant.getScalarExample)(modelRef), arrDepth);
|
|
44
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(modelRef);
|
|
45
|
+
const example = {};
|
|
46
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
47
|
+
if (fieldMeta.example)
|
|
48
|
+
example[fieldMeta.key] = fieldMeta.example;
|
|
49
|
+
else if (fieldMeta.enum)
|
|
50
|
+
example[fieldMeta.key] = (0, import_base.arraiedModel)(fieldMeta.enum.values[0], fieldMeta.arrDepth);
|
|
51
|
+
else
|
|
52
|
+
example[fieldMeta.key] = getResponseExample(fieldMeta.modelRef);
|
|
53
|
+
});
|
|
54
|
+
const result = (0, import_base.arraiedModel)(example, arrDepth);
|
|
55
|
+
Reflect.defineMetadata(ref, result, ResponseExampleStorage.prototype);
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
class RequestExampleStorage {
|
|
59
|
+
}
|
|
60
|
+
const getRequestExample = (ref) => {
|
|
61
|
+
const existing = getPredefinedRequestExample(ref);
|
|
62
|
+
if (existing)
|
|
63
|
+
return existing;
|
|
64
|
+
const fieldMetas = (0, import_constant.getFieldMetas)(ref);
|
|
65
|
+
const example = {};
|
|
66
|
+
const isScalar = (0, import_base.isGqlScalar)(ref);
|
|
67
|
+
if (isScalar)
|
|
68
|
+
return (0, import_constant.getScalarExample)(ref);
|
|
69
|
+
else {
|
|
70
|
+
fieldMetas.forEach((fieldMeta) => {
|
|
71
|
+
if (!fieldMeta.isScalar && fieldMeta.isClass)
|
|
72
|
+
example[fieldMeta.key] = "ObjectID";
|
|
73
|
+
else
|
|
74
|
+
example[fieldMeta.key] = fieldMeta.example ?? fieldMeta.enum ? (0, import_base.arraiedModel)(fieldMeta.example ?? (fieldMeta.enum?.values)[0], fieldMeta.optArrDepth) : (0, import_base.arraiedModel)(getRequestExample(fieldMeta.modelRef), fieldMeta.arrDepth);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
Reflect.defineMetadata(ref, example, RequestExampleStorage.prototype);
|
|
78
|
+
return example;
|
|
79
|
+
};
|
|
80
|
+
const makeRequestExample = (sigRef, key) => {
|
|
81
|
+
const [argMetas] = (0, import_signalDecorators.getArgMetas)(sigRef, key);
|
|
82
|
+
return getExampleData(argMetas);
|
|
83
|
+
};
|
|
84
|
+
const getExampleData = (argMetas, signalType = "graphql") => Object.fromEntries(
|
|
85
|
+
argMetas.filter((argMeta) => argMeta.type !== "Upload").map((argMeta) => {
|
|
86
|
+
const [argRef, argArrDepth] = (0, import_base.getNonArrayModel)(argMeta.returns());
|
|
87
|
+
const example = argMeta.argsOption.example ?? getRequestExample(argRef);
|
|
88
|
+
return [
|
|
89
|
+
argMeta.name,
|
|
90
|
+
(0, import_base.arraiedModel)(
|
|
91
|
+
signalType === "restapi" && argRef.prototype === import_base.JSON.prototype ? JSON.stringify(example, null, 2) : example,
|
|
92
|
+
argArrDepth
|
|
93
|
+
)
|
|
94
|
+
];
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
const makeResponseExample = (sigRef, key) => {
|
|
98
|
+
const gqlMeta = (0, import_signalDecorators.getGqlMeta)(sigRef, key);
|
|
99
|
+
const example = getResponseExample(gqlMeta.returns());
|
|
100
|
+
return example;
|
|
101
|
+
};
|