@akanjs/signal 0.9.47 → 0.9.49

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.
@@ -1,116 +1,64 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __decorateClass = (decorators, target, key, kind) => {
4
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
6
- if (decorator = decorators[i])
7
- result = (kind ? decorator(target, key, result) : decorator(result)) || result;
8
- if (kind && result)
9
- __defProp(target, key, result);
10
- return result;
11
- };
12
- var __decorateParam = (index, decorator) => (target, key) => decorator(target, key, index);
13
1
  import { JSON } from "@akanjs/base";
14
- import { Srvs } from "@akanjs/constant";
15
- import {
16
- Arg,
17
- emit,
18
- LogSignal,
19
- Message,
20
- Mutation,
21
- Pubsub,
22
- Query,
23
- resolve,
24
- Schedule,
25
- Signal,
26
- subscribe
27
- } from "./signalDecorators";
28
- let BaseSignal = class extends LogSignal(Srvs) {
29
- publishPing() {
2
+ import { getAllDictionary, getDictionary } from "@akanjs/dictionary";
3
+ import { endpoint, internal, mergeSignals } from "./signalDecorators";
4
+ import { signalInfo } from "./signalInfo";
5
+ const srvBase = { refName: "base" };
6
+ class BaseInternal extends internal(srvBase, ({ interval }) => ({
7
+ publishPing: interval(3e3).exec(function() {
30
8
  this.baseService.publishPing();
31
- }
32
- ping() {
33
- return resolve("ping");
34
- }
35
- pingBody(data) {
36
- return resolve(global.JSON.stringify(data));
37
- }
38
- pingParam(id) {
39
- return resolve(id);
40
- }
41
- pingQuery(id) {
42
- return resolve(id);
43
- }
44
- pingEvery() {
45
- return resolve("pingEvery");
46
- }
47
- pingUser() {
48
- return resolve("pingUser");
49
- }
50
- pingAdmin() {
51
- return resolve("pingAdmin");
52
- }
53
- async cleanup() {
9
+ })
10
+ })) {
11
+ }
12
+ class BaseEndpoint extends endpoint(srvBase, ({ query, mutation, message, pubsub }) => ({
13
+ ping: query(String, { cache: 3e3 }).exec(function() {
14
+ return "ping";
15
+ }),
16
+ pingBody: query(String, { cache: 1e4 }).body("data", String).exec(function() {
17
+ return "pingBody";
18
+ }),
19
+ pingParam: query(String, { cache: 1e4 }).param("id", String).exec(function() {
20
+ return "pingParam";
21
+ }),
22
+ pingQuery: query(String, { nullable: true }).search("id", String).exec(function(id) {
23
+ return id;
24
+ }),
25
+ pingEvery: query(String).exec(function() {
26
+ return "pingEvery";
27
+ }),
28
+ pingUser: query(String).exec(function() {
29
+ return "pingUser";
30
+ }),
31
+ pingAdmin: query(String).exec(function() {
32
+ return "pingAdmin";
33
+ }),
34
+ getDictionary: query(JSON).param("lang", String).exec(function(lang) {
35
+ const dictionary = getDictionary(lang);
36
+ return dictionary;
37
+ }),
38
+ getAllDictionary: query(JSON).exec(function() {
39
+ const dictionary = getAllDictionary();
40
+ return dictionary;
41
+ }),
42
+ cleanup: mutation(Boolean).exec(async function() {
54
43
  if (process.env.NODE_ENV !== "test")
55
44
  throw new Error("cleanup is only available in test environment");
56
45
  await this.baseService.cleanup();
57
- return resolve(true);
58
- }
59
- wsPing(data) {
60
- return emit(data);
61
- }
62
- pubsubPing() {
63
- return subscribe();
64
- }
65
- getDictionary(lang) {
66
- const dictionary = this.baseService.getDictionary(lang);
67
- return resolve(dictionary);
68
- }
69
- };
70
- __decorateClass([
71
- Schedule.Interval(3e3)
72
- ], BaseSignal.prototype, "publishPing", 1);
73
- __decorateClass([
74
- Query.Public(() => String, { cache: 3e3 })
75
- ], BaseSignal.prototype, "ping", 1);
76
- __decorateClass([
77
- Query.Public(() => String, { cache: 1e4 }),
78
- __decorateParam(0, Arg.Body("data", () => JSON))
79
- ], BaseSignal.prototype, "pingBody", 1);
80
- __decorateClass([
81
- Query.Public(() => String, { cache: 1e4 }),
82
- __decorateParam(0, Arg.Param("id", () => String))
83
- ], BaseSignal.prototype, "pingParam", 1);
84
- __decorateClass([
85
- Query.Public(() => String),
86
- __decorateParam(0, Arg.Query("id", () => String))
87
- ], BaseSignal.prototype, "pingQuery", 1);
88
- __decorateClass([
89
- Query.Every(() => String)
90
- ], BaseSignal.prototype, "pingEvery", 1);
91
- __decorateClass([
92
- Query.User(() => String)
93
- ], BaseSignal.prototype, "pingUser", 1);
94
- __decorateClass([
95
- Query.Admin(() => String)
96
- ], BaseSignal.prototype, "pingAdmin", 1);
97
- __decorateClass([
98
- Mutation.Public(() => Boolean)
99
- ], BaseSignal.prototype, "cleanup", 1);
100
- __decorateClass([
101
- Message.Public(() => String),
102
- __decorateParam(0, Arg.Msg("data", () => String))
103
- ], BaseSignal.prototype, "wsPing", 1);
104
- __decorateClass([
105
- Pubsub.Public(() => String)
106
- ], BaseSignal.prototype, "pubsubPing", 1);
107
- __decorateClass([
108
- Query.Public(() => JSON),
109
- __decorateParam(0, Arg.Param("lang", () => String))
110
- ], BaseSignal.prototype, "getDictionary", 1);
111
- BaseSignal = __decorateClass([
112
- Signal({ name: "Base" })
113
- ], BaseSignal);
46
+ return true;
47
+ }),
48
+ wsPing: message(String).exec(function() {
49
+ return "wsPing";
50
+ }),
51
+ pubsubPing: pubsub(String).exec(function() {
52
+ }),
53
+ getSignals: query(JSON).exec(function() {
54
+ return signalInfo.serializedSignals;
55
+ })
56
+ })) {
57
+ }
58
+ class BaseSignal extends mergeSignals(BaseEndpoint, BaseInternal) {
59
+ }
114
60
  export {
61
+ BaseEndpoint,
62
+ BaseInternal,
115
63
  BaseSignal
116
64
  };
@@ -1,6 +1,5 @@
1
1
  import { client } from "./client";
2
- const nativeFetch = fetch;
3
- const baseFetch = Object.assign(nativeFetch, {
2
+ const baseFetch = Object.assign(global.fetch, {
4
3
  client,
5
4
  clone: function(option = {}) {
6
5
  return {
package/esm/src/fetch.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { BaseSignal } from "./base.signal";
2
2
  import { baseFetch } from "./baseFetch";
3
3
  import { fetchOf, makeFetch } from "./gql";
4
+ import { signalInfo } from "./signalInfo";
5
+ signalInfo.registerSignals(BaseSignal);
4
6
  const fetch = makeFetch(baseFetch, {
5
7
  ...fetchOf(BaseSignal)
6
8
  });
@@ -0,0 +1,345 @@
1
+ import { DataList, dayjs, isGqlScalar } from "@akanjs/base";
2
+ import { capitalize, Logger, lowerlize } from "@akanjs/common";
3
+ import { constantInfo, makeCrystalize, serializeArg } from "@akanjs/constant";
4
+ import { mutate, query, setGqlOnStorage } from "./gql";
5
+ import { getGqlStr, graphql } from "./graphql";
6
+ const serviceFetchOf = (signal) => {
7
+ const fetch = {};
8
+ Object.entries(signal.endpoint).forEach(([key, endpoint]) => {
9
+ const returnRef = constantInfo.getModelRef(endpoint.returns.refName, endpoint.returns.modelType);
10
+ const isScalar = isGqlScalar(returnRef);
11
+ if (endpoint.type === "message") {
12
+ const emitEvent = function(...args) {
13
+ const fetchPolicy = args[endpoint.args.length] ?? { crystalize: true };
14
+ if (!this.client.io && !fetchPolicy.url) {
15
+ Logger.warn(`${key} emit suppressed - socket is not connected`);
16
+ return;
17
+ }
18
+ const message = Object.fromEntries(
19
+ endpoint.args.map((arg, idx) => {
20
+ const argRef = constantInfo.getModelRef(arg.refName, arg.modelType);
21
+ return [arg.name, serializeArg(argRef, arg.arrDepth, args[idx], arg.argsOption) ?? null];
22
+ })
23
+ );
24
+ if (fetchPolicy.transport === "udp") {
25
+ if (!this.client.udp)
26
+ throw new Error("UDP is not set");
27
+ const uri = fetchPolicy.url ?? "udpout:localhost:4000";
28
+ const [host, port] = uri.split(":").slice(1);
29
+ this.client.udp.send(JSON.stringify(message), parseInt(port), host);
30
+ Logger.debug(`udp emit: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
31
+ return;
32
+ } else {
33
+ const io = this.client.getIo(fetchPolicy.url);
34
+ void this.client.waitUntilWebSocketConnected(fetchPolicy.url).then(() => {
35
+ io.emit(key, message);
36
+ Logger.debug(`socket emit: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
37
+ });
38
+ }
39
+ };
40
+ const listenEvent = function(handleEvent, fetchPolicy = {}) {
41
+ const crystalize = (data) => {
42
+ if (isScalar) {
43
+ if (returnRef.prototype === Date.prototype)
44
+ return dayjs(data);
45
+ else
46
+ return data;
47
+ } else if (Array.isArray(data))
48
+ return data.map((d) => crystalize(d));
49
+ else
50
+ return makeCrystalize(returnRef)(data);
51
+ };
52
+ const handle = (data) => {
53
+ Logger.debug(`socket listened: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
54
+ handleEvent(crystalize(data));
55
+ };
56
+ const io = this.client.getIo(fetchPolicy.url);
57
+ this.client.waitUntilWebSocketConnected(fetchPolicy.url).then(() => {
58
+ io.removeListener(key, handle);
59
+ io.on(key, handle);
60
+ Logger.debug(`socket listen start: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
61
+ });
62
+ return async () => {
63
+ await this.client.waitUntilWebSocketConnected(fetchPolicy.url);
64
+ Logger.debug(`socket listen end: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
65
+ io.removeListener(key, handle);
66
+ };
67
+ };
68
+ fetch[key] = emitEvent;
69
+ fetch[`listen${capitalize(key)}`] = listenEvent;
70
+ } else if (endpoint.type === "pubsub") {
71
+ const makeRoomId = (gqlKey, argValues) => `${gqlKey}-${argValues.join("-")}`;
72
+ const crystalize = (data) => {
73
+ if (isScalar) {
74
+ if (returnRef.prototype === Date.prototype)
75
+ return dayjs(data);
76
+ else
77
+ return data;
78
+ } else if (Array.isArray(data))
79
+ return data.map((d) => crystalize(d));
80
+ else
81
+ return makeCrystalize(returnRef)(data);
82
+ };
83
+ const subscribeEvent = function(...args) {
84
+ const onData = args[endpoint.args.length];
85
+ const fetchPolicy = args[endpoint.args.length + 1] ?? { crystalize: true };
86
+ const message = Object.fromEntries(
87
+ endpoint.args.map((arg, idx) => {
88
+ const argRef = constantInfo.getModelRef(arg.refName, arg.modelType);
89
+ return [arg.name, serializeArg(argRef, arg.arrDepth, args[idx], arg.argsOption) ?? null];
90
+ })
91
+ );
92
+ const handleEvent = (data) => {
93
+ if (data.__subscribe__)
94
+ return;
95
+ onData(crystalize(data));
96
+ };
97
+ const roomId = makeRoomId(
98
+ key,
99
+ endpoint.args.map((arg) => message[arg.name])
100
+ );
101
+ const io = this.client.getIo(fetchPolicy.url);
102
+ void this.client.waitUntilWebSocketConnected(fetchPolicy.url).then(() => {
103
+ Logger.debug(`socket subscribe start: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
104
+ io.subscribe({ key, roomId, message, handleEvent });
105
+ });
106
+ return async () => {
107
+ //! 앱에서 다른 앱 넘어갈 때 언마운트 되버리면서 subscribe가 끊기는 일이 있음.
108
+ await this.client.waitUntilWebSocketConnected(fetchPolicy.url);
109
+ Logger.debug(`socket unsubscribe: ${key}: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
110
+ io.unsubscribe(roomId, handleEvent);
111
+ };
112
+ };
113
+ fetch[`subscribe${capitalize(key)}`] = subscribeEvent;
114
+ } else if (["query", "mutation"].includes(endpoint.type)) {
115
+ const name = endpoint.signalOption?.name ?? key;
116
+ const makeReq = ({ resolve }) => async function(...args) {
117
+ Logger.debug(`fetch: ${key} start: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
118
+ const now = Date.now();
119
+ const lightenedReturnRef = isScalar || endpoint.returns.arrDepth === 0 ? returnRef : endpoint.returns.modelType === "full" ? constantInfo.getDatabase(endpoint.returns.refName).light : returnRef;
120
+ const fetchPolicy = args[endpoint.args.length] ?? { crystalize: true };
121
+ const partial = fetchPolicy.partial ?? endpoint.signalOption?.partial;
122
+ const crystalize = (data) => {
123
+ if (fetchPolicy.crystalize === false)
124
+ return data;
125
+ if (isScalar) {
126
+ if (lightenedReturnRef.prototype === Date.prototype)
127
+ return dayjs(data);
128
+ else
129
+ return data;
130
+ } else if (Array.isArray(data))
131
+ return data.map((d) => crystalize(d));
132
+ else
133
+ return makeCrystalize(lightenedReturnRef, { partial })(data);
134
+ };
135
+ try {
136
+ const res = (await (endpoint.type === "query" ? query : mutate)(
137
+ this.client,
138
+ graphql(getGqlStr(returnRef, key, endpoint, lightenedReturnRef, partial)),
139
+ Object.fromEntries(
140
+ endpoint.args.map((arg, idx) => {
141
+ const argRef = constantInfo.getModelRef(arg.refName, arg.modelType);
142
+ return [arg.name, serializeArg(argRef, arg.arrDepth, args[idx], arg.argsOption) ?? null];
143
+ })
144
+ ),
145
+ fetchPolicy
146
+ ))[name];
147
+ const data = resolve ? crystalize(res) : res;
148
+ Logger.debug(`fetch: ${key} end: ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")} ${Date.now() - now}ms`);
149
+ return data;
150
+ } catch (e) {
151
+ Logger.error(`fetch: ${key} error: ${e}`);
152
+ throw e;
153
+ }
154
+ };
155
+ const reqFn = makeReq({ resolve: true });
156
+ const reqFnWithoutResolve = makeReq({ resolve: false });
157
+ fetch[name] = async function(...args) {
158
+ return await reqFn.apply(this, args);
159
+ };
160
+ fetch[`_${name}`] = async function(...args) {
161
+ return await reqFnWithoutResolve.apply(this, args);
162
+ };
163
+ } else
164
+ throw new Error(`Invalid endpoint type: ${endpoint.type}`);
165
+ });
166
+ return fetch;
167
+ };
168
+ const databaseFetchOf = (signal, option = {}) => {
169
+ const cnst = constantInfo.getDatabase(signal.refName);
170
+ const [fieldName, className] = [lowerlize(signal.refName), capitalize(signal.refName)];
171
+ const names = {
172
+ refName: signal.refName,
173
+ model: fieldName,
174
+ Model: className,
175
+ _model: `_${fieldName}`,
176
+ lightModel: `light${className}`,
177
+ _lightModel: `_light${className}`,
178
+ defaultModel: `default${className}`,
179
+ defaultModelInsight: `default${className}Insight`,
180
+ mergeModel: `merge${className}`,
181
+ viewModel: `view${className}`,
182
+ getModelView: `get${className}View`,
183
+ modelView: `${fieldName}View`,
184
+ modelViewAt: `${fieldName}ViewAt`,
185
+ editModel: `edit${className}`,
186
+ getModelEdit: `get${className}Edit`,
187
+ modelEdit: `${fieldName}Edit`,
188
+ listModel: `list${className}`,
189
+ modelList: `${fieldName}List`,
190
+ modelObjList: `${fieldName}ObjList`,
191
+ modelInsight: `${fieldName}Insight`,
192
+ modelObjInsight: `${fieldName}ObjInsight`,
193
+ updateModel: `update${className}`,
194
+ modelObj: `${fieldName}Obj`,
195
+ _modelList: `_${fieldName}List`,
196
+ modelInit: `${fieldName}Init`,
197
+ pageOfModel: `pageOf${className}`,
198
+ lastPageOfModel: `lastPageOf${className}`,
199
+ limitOfModel: `limitOf${className}`,
200
+ queryArgsOfModel: `queryArgsOf${className}`,
201
+ sortOfModel: `sortOf${className}`,
202
+ modelInitAt: `${fieldName}InitAt`,
203
+ initModel: `init${className}`,
204
+ getModelInit: `get${className}Init`,
205
+ addModelFiles: `add${className}Files`,
206
+ addFiles: `addFiles`
207
+ };
208
+ const fetch = serviceFetchOf(signal);
209
+ const util = {
210
+ [names.addModelFiles]: async (files, id, option2) => {
211
+ const metas = Array.from(files).map((file) => ({ lastModifiedAt: new Date(file.lastModified), size: file.size }));
212
+ //! will not work properly
213
+ return await global.builtFetch[names.addFiles](
214
+ files,
215
+ metas,
216
+ names.model,
217
+ id,
218
+ option2
219
+ );
220
+ },
221
+ [names.mergeModel]: async (modelOrId, data, option2) => {
222
+ const model = typeof modelOrId === "string" ? await global.builtFetch[names._model](modelOrId) : modelOrId;
223
+ const input = cnst.purify({ ...model, ...data });
224
+ if (!input)
225
+ throw new Error("Error");
226
+ return await global.builtFetch[names.updateModel](model.id, input, option2);
227
+ },
228
+ [names.viewModel]: async (id, option2) => {
229
+ const modelObj = await global.builtFetch[names._model](id, option2);
230
+ return {
231
+ [names.model]: cnst.crystalize(modelObj),
232
+ [names.modelView]: {
233
+ refName: names.model,
234
+ [names.modelObj]: modelObj,
235
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
236
+ }
237
+ };
238
+ },
239
+ [names.getModelView]: async (id, option2) => {
240
+ const modelView = await global.builtFetch[names._model](id, option2);
241
+ return {
242
+ refName: names.model,
243
+ [names.modelObj]: modelView,
244
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
245
+ };
246
+ },
247
+ [names.editModel]: async (id, option2) => {
248
+ const modelObj = await global.builtFetch[names._model](id, option2);
249
+ return {
250
+ [names.model]: cnst.crystalize(modelObj),
251
+ [names.modelEdit]: {
252
+ refName: names.model,
253
+ [names.modelObj]: modelObj,
254
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
255
+ }
256
+ };
257
+ },
258
+ [names.getModelEdit]: async (id, option2) => {
259
+ const modelEdit = await global.builtFetch[names.editModel](id, option2);
260
+ return modelEdit[names.modelEdit];
261
+ }
262
+ };
263
+ const sliceUtil = Object.fromEntries(
264
+ signal.slices.reduce((acc, { sliceName, argLength, defaultArgs }) => {
265
+ const namesOfSlice = {
266
+ modelList: sliceName.replace(names.model, names.modelList),
267
+ // modelListInSelf
268
+ modelInsight: sliceName.replace(names.model, names.modelInsight),
269
+ // modelInsightInSelf
270
+ modelInit: sliceName.replace(names.model, names.modelInit),
271
+ // modelInitInSelf
272
+ initModel: sliceName.replace(names.model, names.initModel),
273
+ // initModelInSelf
274
+ getModelInit: sliceName.replace(names.model, names.getModelInit)
275
+ // getModelInitInSelf
276
+ };
277
+ const getInitFn = async (...args) => {
278
+ const queryArgLength = Math.min(args.length, argLength);
279
+ const queryArgs = [
280
+ ...new Array(queryArgLength).fill(null).map((_, i) => args[i]),
281
+ ...queryArgLength < argLength ? new Array(argLength - queryArgLength).fill(null).map((_, i) => defaultArgs[i + queryArgLength] ?? null) : []
282
+ ];
283
+ const fetchInitOption = args[argLength] ?? {};
284
+ const { page = 1, limit = 20, sort = "latest", insight } = fetchInitOption;
285
+ const skip = (page - 1) * limit;
286
+ const [modelObjList, modelObjInsight] = await Promise.all([
287
+ global.builtFetch[`_${namesOfSlice.modelList}`](
288
+ ...queryArgs,
289
+ skip,
290
+ limit,
291
+ sort,
292
+ fetchInitOption
293
+ ),
294
+ global.builtFetch[`_${namesOfSlice.modelInsight}`](
295
+ ...queryArgs,
296
+ fetchInitOption
297
+ )
298
+ ]);
299
+ const count = modelObjInsight.count;
300
+ return {
301
+ // Client Component용
302
+ refName: names.model,
303
+ sliceName,
304
+ argLength,
305
+ [names.modelObjList]: modelObjList,
306
+ [names.modelObjInsight]: modelObjInsight,
307
+ [names.pageOfModel]: page,
308
+ [names.lastPageOfModel]: Math.max(Math.floor((count - 1) / limit) + 1, 1),
309
+ [names.limitOfModel]: limit,
310
+ [names.queryArgsOfModel]: JSON.parse(JSON.stringify(queryArgs)),
311
+ [names.sortOfModel]: sort,
312
+ [names.modelInitAt]: /* @__PURE__ */ new Date()
313
+ };
314
+ };
315
+ const initFn = async function(...args) {
316
+ const modelInit = await getInitFn(...args);
317
+ const modelObjList = modelInit[names.modelObjList];
318
+ const modelObjInsight = modelInit[names.modelObjInsight];
319
+ const modelList = new DataList(
320
+ modelObjList.map((modelObj) => cnst.lightCrystalize(modelObj))
321
+ );
322
+ const modelInsight = cnst.crystalizeInsight(modelObjInsight);
323
+ return {
324
+ [namesOfSlice.modelList]: modelList,
325
+ // Server Component용
326
+ [namesOfSlice.modelInsight]: modelInsight,
327
+ // Server Component용
328
+ [namesOfSlice.modelInit]: modelInit
329
+ };
330
+ };
331
+ return [...acc, [namesOfSlice.getModelInit, getInitFn], [namesOfSlice.initModel, initFn]];
332
+ }, [])
333
+ );
334
+ const modelGql = Object.assign(fetch, {
335
+ ...util,
336
+ ...sliceUtil
337
+ // slices: [...overwriteSlices, ...sigMeta.slices],
338
+ });
339
+ setGqlOnStorage(signal.refName, modelGql);
340
+ return modelGql;
341
+ };
342
+ export {
343
+ databaseFetchOf,
344
+ serviceFetchOf
345
+ };