@akanjs/signal 0.0.4

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/src/gql.js ADDED
@@ -0,0 +1,860 @@
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 gql_exports = {};
19
+ __export(gql_exports, {
20
+ CrystalizeStorage: () => CrystalizeStorage,
21
+ DefaultStorage: () => DefaultStorage,
22
+ FragmentStorage: () => FragmentStorage,
23
+ GqlStorage: () => GqlStorage,
24
+ PurifyStorage: () => PurifyStorage,
25
+ deserializeArg: () => deserializeArg,
26
+ fetchOf: () => fetchOf,
27
+ getGqlOnStorage: () => getGqlOnStorage,
28
+ getGqlStr: () => getGqlStr,
29
+ gqlOf: () => gqlOf,
30
+ makeCrystalize: () => makeCrystalize,
31
+ makeDefault: () => makeDefault,
32
+ makeFetch: () => makeFetch,
33
+ makeFragment: () => makeFragment,
34
+ makePurify: () => makePurify,
35
+ scalarUtilOf: () => scalarUtilOf,
36
+ serializeArg: () => serializeArg
37
+ });
38
+ module.exports = __toCommonJS(gql_exports);
39
+ var import_base = require("@akanjs/base");
40
+ var import_common = require("@akanjs/common");
41
+ var import_constant = require("@akanjs/constant");
42
+ var import__ = require(".");
43
+ var import_signalDecorators = require("./signalDecorators");
44
+ function graphql(literals, ...args) {
45
+ if (typeof literals === "string")
46
+ literals = [literals];
47
+ let result = literals[0];
48
+ args.forEach((arg, i) => {
49
+ if (arg && arg.kind === "Document")
50
+ result += arg.loc.source.body;
51
+ else
52
+ result += arg;
53
+ result += literals[i + 1];
54
+ });
55
+ return result;
56
+ }
57
+ class GqlStorage {
58
+ }
59
+ class FragmentStorage {
60
+ }
61
+ class PurifyStorage {
62
+ }
63
+ class DefaultStorage {
64
+ }
65
+ class CrystalizeStorage {
66
+ }
67
+ const scalarUtilOf = (name, target) => {
68
+ const refName = (0, import_constant.getClassMeta)(target).refName;
69
+ const [fieldName, className] = [(0, import_common.lowerlize)(refName), (0, import_common.capitalize)(refName)];
70
+ const graphQL = {
71
+ refName,
72
+ [className]: target,
73
+ [`default${className}`]: (0, import__.immerify)(target, Object.assign(new target(), makeDefault(target))),
74
+ [`purify${className}`]: makePurify(target),
75
+ [`crystalize${className}`]: makeCrystalize(target),
76
+ [`${fieldName}Fragment`]: makeFragment(target)
77
+ };
78
+ return graphQL;
79
+ };
80
+ const getGqlOnStorage = (refName) => {
81
+ const modelGql = Reflect.getMetadata(refName, GqlStorage.prototype);
82
+ if (!modelGql)
83
+ throw new Error("Gql is not defined");
84
+ return modelGql;
85
+ };
86
+ const setGqlOnStorage = (refName, modelGql) => {
87
+ Reflect.defineMetadata(refName, modelGql, GqlStorage.prototype);
88
+ };
89
+ const gqlOf = (constant, sigRef, option = {}) => {
90
+ const refName = constant.refName;
91
+ const [fieldName, className] = [(0, import_common.lowerlize)(refName), (0, import_common.capitalize)(refName)];
92
+ const sigMeta = (0, import_signalDecorators.getSigMeta)(sigRef);
93
+ const names = {
94
+ refName,
95
+ model: fieldName,
96
+ Model: className,
97
+ _model: `_${fieldName}`,
98
+ lightModel: `light${className}`,
99
+ _lightModel: `_light${className}`,
100
+ purifyModel: `purify${className}`,
101
+ crystalizeModel: `crystalize${className}`,
102
+ lightCrystalizeModel: `lightCrystalize${className}`,
103
+ crystalizeModelInsight: `crystalize${className}Insight`,
104
+ defaultModel: `default${className}`,
105
+ defaultModelInsight: `default${className}Insight`,
106
+ mergeModel: `merge${className}`,
107
+ viewModel: `view${className}`,
108
+ getModelView: `get${className}View`,
109
+ modelView: `${fieldName}View`,
110
+ modelViewAt: `${fieldName}ViewAt`,
111
+ editModel: `edit${className}`,
112
+ getModelEdit: `get${className}Edit`,
113
+ modelEdit: `${fieldName}Edit`,
114
+ listModel: `list${className}`,
115
+ modelList: `${fieldName}List`,
116
+ modelObjList: `${fieldName}ObjList`,
117
+ modelInsight: `${fieldName}Insight`,
118
+ modelObjInsight: `${fieldName}ObjInsight`,
119
+ updateModel: `update${className}`,
120
+ modelObj: `${fieldName}Obj`,
121
+ _modelList: `_${fieldName}List`,
122
+ modelInit: `${fieldName}Init`,
123
+ pageOfModel: `pageOf${className}`,
124
+ lastPageOfModel: `lastPageOf${className}`,
125
+ limitOfModel: `limitOf${className}`,
126
+ queryArgsOfModel: `queryArgsOf${className}`,
127
+ sortOfModel: `sortOf${className}`,
128
+ modelInitAt: `${fieldName}InitAt`,
129
+ initModel: `init${className}`,
130
+ getModelInit: `get${className}Init`,
131
+ addModelFiles: `add${className}Files`
132
+ };
133
+ const base = {
134
+ refName,
135
+ [names.purifyModel]: makePurify(constant.Input, option),
136
+ [names.crystalizeModel]: makeCrystalize(constant.Full, option),
137
+ [names.lightCrystalizeModel]: makeCrystalize(constant.Light, option),
138
+ [names.crystalizeModelInsight]: makeCrystalize(constant.Insight, option),
139
+ [names.defaultModel]: (0, import__.immerify)(
140
+ constant.Full,
141
+ Object.assign(new constant.Full(), makeDefault(constant.Full, option))
142
+ ),
143
+ [names.defaultModelInsight]: Object.assign(
144
+ new constant.Insight(),
145
+ makeDefault(constant.Insight, option)
146
+ )
147
+ };
148
+ const gql = Object.assign(option.overwrite ?? { client: import__.client }, fetchOf(sigRef));
149
+ const util = {
150
+ [names.addModelFiles]: async (files, id, option2) => {
151
+ const fileGql = getGqlOnStorage("file");
152
+ const metas = Array.from(files).map((file) => ({ lastModifiedAt: new Date(file.lastModified), size: file.size }));
153
+ return await fileGql.addFiles(
154
+ files,
155
+ metas,
156
+ names.model,
157
+ id,
158
+ option2
159
+ );
160
+ },
161
+ [names.mergeModel]: async (modelOrId, data, option2) => {
162
+ const model = typeof modelOrId === "string" ? await gql[names._model](modelOrId) : modelOrId;
163
+ const input = base[names.purifyModel]({ ...model, ...data });
164
+ if (!input)
165
+ throw new Error("Error");
166
+ return await gql[names.updateModel](model.id, input, option2);
167
+ },
168
+ [names.viewModel]: async (id, option2) => {
169
+ const modelObj = await gql[names._model](id, option2);
170
+ return {
171
+ [names.model]: base[names.crystalizeModel](modelObj),
172
+ [names.modelView]: {
173
+ refName: names.model,
174
+ [names.modelObj]: modelObj,
175
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
176
+ }
177
+ };
178
+ },
179
+ [names.getModelView]: async (id, option2) => {
180
+ const modelView = await gql[names._model](id, option2);
181
+ return {
182
+ refName: names.model,
183
+ [names.modelObj]: modelView,
184
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
185
+ };
186
+ },
187
+ [names.editModel]: async (id, option2) => {
188
+ const modelObj = await gql[names._model](id, option2);
189
+ return {
190
+ [names.model]: base[names.crystalizeModel](modelObj),
191
+ [names.modelEdit]: {
192
+ refName: names.model,
193
+ [names.modelObj]: modelObj,
194
+ [names.modelViewAt]: /* @__PURE__ */ new Date()
195
+ }
196
+ };
197
+ },
198
+ [names.getModelEdit]: async (id, option2) => {
199
+ const modelEdit = await gql[names.editModel](id, option2);
200
+ return modelEdit[names.modelEdit];
201
+ }
202
+ };
203
+ const sliceUtil = Object.fromEntries(
204
+ sigMeta.slices.reduce((acc, { sliceName, argLength, defaultArgs }) => {
205
+ const namesOfSlice = {
206
+ modelList: sliceName.replace(names.model, names.modelList),
207
+ // modelListInSelf
208
+ modelInsight: sliceName.replace(names.model, names.modelInsight),
209
+ // modelInsightInSelf
210
+ modelInit: sliceName.replace(names.model, names.modelInit),
211
+ // modelInitInSelf
212
+ initModel: sliceName.replace(names.model, names.initModel),
213
+ // initModelInSelf
214
+ getModelInit: sliceName.replace(names.model, names.getModelInit)
215
+ // getModelInitInSelf
216
+ };
217
+ const getInitFn = async (...args) => {
218
+ const queryArgLength = Math.min(args.length, argLength);
219
+ const queryArgs = [
220
+ ...new Array(queryArgLength).fill(null).map((_, i) => args[i]),
221
+ ...queryArgLength < argLength ? new Array(argLength - queryArgLength).fill(null).map((_, i) => defaultArgs[i + queryArgLength] ?? null) : []
222
+ ];
223
+ const fetchInitOption = args[argLength] ?? {};
224
+ const { page = 1, limit = 20, sort = "latest", insight } = fetchInitOption;
225
+ const skip = (page - 1) * limit;
226
+ const [modelObjList, modelObjInsight] = await Promise.all([
227
+ gql[`_${namesOfSlice.modelList}`](
228
+ ...queryArgs,
229
+ skip,
230
+ limit,
231
+ sort,
232
+ fetchInitOption
233
+ ),
234
+ gql[`_${namesOfSlice.modelInsight}`](...queryArgs, fetchInitOption)
235
+ ]);
236
+ const count = modelObjInsight.count;
237
+ return {
238
+ // Client Component용
239
+ refName: names.model,
240
+ sliceName,
241
+ argLength,
242
+ [names.modelObjList]: modelObjList,
243
+ [names.modelObjInsight]: modelObjInsight,
244
+ [names.pageOfModel]: page,
245
+ [names.lastPageOfModel]: Math.max(Math.floor((count - 1) / limit) + 1, 1),
246
+ [names.limitOfModel]: limit,
247
+ [names.queryArgsOfModel]: JSON.parse(JSON.stringify(queryArgs)),
248
+ [names.sortOfModel]: sort,
249
+ [names.modelInitAt]: /* @__PURE__ */ new Date()
250
+ };
251
+ };
252
+ const initFn = async (...args) => {
253
+ const modelInit = await getInitFn(...args);
254
+ const modelObjList = modelInit[names.modelObjList];
255
+ const modelObjInsight = modelInit[names.modelObjInsight];
256
+ const modelList = new import_base.DataList(
257
+ modelObjList.map((modelObj) => base[names.lightCrystalizeModel](modelObj))
258
+ );
259
+ const modelInsight = base[names.crystalizeModelInsight](modelObjInsight);
260
+ return {
261
+ [namesOfSlice.modelList]: modelList,
262
+ // Server Component용
263
+ [namesOfSlice.modelInsight]: modelInsight,
264
+ // Server Component용
265
+ [namesOfSlice.modelInit]: modelInit
266
+ };
267
+ };
268
+ return [...acc, [namesOfSlice.getModelInit, getInitFn], [namesOfSlice.initModel, initFn]];
269
+ }, [])
270
+ );
271
+ const overwriteSlices = option.overwrite ? option.overwrite.slices.filter(
272
+ (slice) => !sigMeta.slices.some((s) => s.sliceName === slice.sliceName)
273
+ ) : [];
274
+ const modelGql = Object.assign(option.overwrite ?? {}, {
275
+ ...gql,
276
+ ...base,
277
+ ...util,
278
+ ...sliceUtil,
279
+ slices: [...overwriteSlices, ...sigMeta.slices]
280
+ });
281
+ setGqlOnStorage(refName, modelGql);
282
+ return modelGql;
283
+ };
284
+ const getPredefinedDefault = (refName) => {
285
+ const defaultData = Reflect.getMetadata(refName, DefaultStorage.prototype);
286
+ return defaultData;
287
+ };
288
+ const setPredefinedDefault = (refName, defaultData) => {
289
+ Reflect.defineMetadata(refName, defaultData, DefaultStorage.prototype);
290
+ };
291
+ const makeDefault = (target, option = {}) => {
292
+ const classMeta = (0, import_constant.getClassMeta)(target);
293
+ const predefinedDefault = getPredefinedDefault(classMeta.refName);
294
+ if (predefinedDefault && !option.overwrite)
295
+ return predefinedDefault;
296
+ if (option.isChild && classMeta.type !== "scalar")
297
+ return null;
298
+ const metadatas = (0, import_constant.getFieldMetas)(target);
299
+ const result = {};
300
+ for (const metadata of metadatas) {
301
+ if (metadata.fieldType === "hidden")
302
+ result[metadata.key] = null;
303
+ else if (metadata.default) {
304
+ if (typeof metadata.default === "function")
305
+ result[metadata.key] = metadata.default();
306
+ else if (metadata.default instanceof import_base.Enum)
307
+ result[metadata.key] = [...metadata.default.values];
308
+ else
309
+ result[metadata.key] = metadata.default;
310
+ } else if (metadata.isArray)
311
+ result[metadata.key] = [];
312
+ else if (metadata.nullable)
313
+ result[metadata.key] = null;
314
+ else if (metadata.isClass)
315
+ result[metadata.key] = metadata.isScalar ? makeDefault(metadata.modelRef) : null;
316
+ else
317
+ result[metadata.key] = import_base.scalarDefaultMap.get(metadata.modelRef);
318
+ }
319
+ setPredefinedDefault(classMeta.refName, result);
320
+ return result;
321
+ };
322
+ const query = async (fetchClient, query2, variables = {}, option = {}) => {
323
+ const jwt = option.url ? null : await fetchClient.getJwt();
324
+ const { data, error } = await fetchClient.gql.query(query2, variables, {
325
+ fetch,
326
+ url: option.url ?? fetchClient.uri,
327
+ requestPolicy: typeof option.cache === "string" ? option.cache : option.cache === true ? "cache-first" : "network-only",
328
+ fetchOptions: {
329
+ ...typeof option.cache === "number" ? { next: { revalidate: option.cache } } : option.cache === true ? { cache: "force-cache" } : { cache: "no-store" },
330
+ headers: {
331
+ "apollo-require-preflight": "true",
332
+ ...jwt ? { authorization: `Bearer ${jwt}` } : {},
333
+ ...option.token ? { authorization: `Bearer ${option.token}` } : {}
334
+ }
335
+ }
336
+ }).toPromise();
337
+ if (!data) {
338
+ const content = error?.graphQLErrors[0]?.message ?? "Unknown Error";
339
+ if (option.onError) {
340
+ option.onError(content);
341
+ return;
342
+ } else
343
+ throw new Error(content);
344
+ }
345
+ return data;
346
+ };
347
+ const mutate = async (fetchClient, mutation, variables = {}, option = {}) => {
348
+ const jwt = option.url ? null : await fetchClient.getJwt();
349
+ const { data, error } = await fetchClient.gql.mutation(mutation, variables, {
350
+ fetch,
351
+ url: option.url ?? fetchClient.uri,
352
+ requestPolicy: "network-only",
353
+ fetchOptions: {
354
+ cache: "no-store",
355
+ headers: {
356
+ "apollo-require-preflight": "true",
357
+ ...jwt ? { authorization: `Bearer ${jwt}` } : {},
358
+ ...option.token ? { authorization: `Bearer ${option.token}` } : {}
359
+ }
360
+ }
361
+ }).toPromise();
362
+ if (!data) {
363
+ const content = error?.graphQLErrors[0]?.message ?? "Unknown Error";
364
+ if (option.onError) {
365
+ option.onError(content);
366
+ return;
367
+ } else
368
+ throw new Error(content);
369
+ }
370
+ return data;
371
+ };
372
+ const scalarPurifyMap = /* @__PURE__ */ new Map([
373
+ [Date, (value) => (0, import_base.dayjs)(value).toDate()],
374
+ [String, (value) => value],
375
+ [import_base.ID, (value) => value],
376
+ [Boolean, (value) => value],
377
+ [import_base.Int, (value) => value],
378
+ [import_base.Float, (value) => value],
379
+ [import_base.JSON, (value) => value]
380
+ ]);
381
+ const getPurifyFn = (modelRef) => {
382
+ const [valueRef] = (0, import_base.getNonArrayModel)(modelRef);
383
+ return scalarPurifyMap.get(valueRef) ?? ((value) => value);
384
+ };
385
+ const purify = (metadata, value, self) => {
386
+ if (metadata.nullable && (value === null || value === void 0 || typeof value === "number" && isNaN(value) || typeof value === "string" && !value.length))
387
+ return null;
388
+ if (metadata.isArray) {
389
+ if (!Array.isArray(value))
390
+ throw new Error(`Invalid Array Value in ${metadata.key} for value ${value}`);
391
+ if (metadata.minlength && value.length < metadata.minlength)
392
+ throw new Error(`Invalid Array Length (Min) in ${metadata.key} for value ${value}`);
393
+ else if (metadata.maxlength && value.length > metadata.maxlength)
394
+ throw new Error(`Invalid Array Length (Max) in ${metadata.key} for value ${value}`);
395
+ else if (metadata.optArrDepth === 0 && metadata.validate && !metadata.validate(value, self))
396
+ throw new Error(`Invalid Array Value (Failed to pass validation) in ${metadata.key} for value ${value}`);
397
+ return value.map((v) => purify({ ...metadata, isArray: false }, v, v));
398
+ }
399
+ if (metadata.isMap && metadata.of) {
400
+ const purifyFn2 = getPurifyFn(metadata.of);
401
+ return Object.fromEntries(
402
+ [...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, purifyFn2)])
403
+ );
404
+ }
405
+ if (metadata.isClass)
406
+ return makePurify(metadata.modelRef)(value, true);
407
+ if (metadata.name === "Date" && (0, import_base.dayjs)(value).isBefore((0, import_base.dayjs)(/* @__PURE__ */ new Date("0000"))))
408
+ throw new Error(`Invalid Date Value (Default) in ${metadata.key} for value ${value}`);
409
+ if (["String", "ID"].includes(metadata.name) && (value === "" || !value))
410
+ throw new Error(`Invalid String Value (Default) in ${metadata.key} for value ${value}`);
411
+ if (metadata.validate && !metadata.validate(value, self))
412
+ throw new Error(`Invalid Value (Failed to pass validation) / ${value} in ${metadata.key}`);
413
+ if (!metadata.nullable && !value && value !== 0 && value !== false)
414
+ throw new Error(`Invalid Value (Nullable) in ${metadata.key} for value ${value}`);
415
+ const purifyFn = getPurifyFn(metadata.modelRef);
416
+ return purifyFn(value);
417
+ };
418
+ const getPredefinedPurifyFn = (refName) => {
419
+ const purify2 = Reflect.getMetadata(refName, PurifyStorage.prototype);
420
+ return purify2;
421
+ };
422
+ const setPredefinedPurifyFn = (refName, purify2) => {
423
+ Reflect.defineMetadata(refName, purify2, PurifyStorage.prototype);
424
+ };
425
+ const makePurify = (target, option = {}) => {
426
+ const classMeta = (0, import_constant.getClassMeta)(target);
427
+ const purifyFn = getPredefinedPurifyFn(classMeta.refName);
428
+ if (purifyFn && !option.overwrite)
429
+ return purifyFn;
430
+ const metadatas = (0, import_constant.getFieldMetas)(target);
431
+ const fn = (self, isChild) => {
432
+ try {
433
+ if (isChild && classMeta.type !== "scalar") {
434
+ const id = self.id;
435
+ if (!id)
436
+ throw new Error(`Invalid Value (No ID) for id ${classMeta.refName}`);
437
+ return id;
438
+ }
439
+ const result = {};
440
+ for (const metadata of metadatas) {
441
+ const value = self[metadata.key];
442
+ result[metadata.key] = purify(metadata, value, self);
443
+ }
444
+ return result;
445
+ } catch (err) {
446
+ if (isChild)
447
+ throw new Error(err);
448
+ import_common.Logger.debug(err);
449
+ return null;
450
+ }
451
+ };
452
+ setPredefinedPurifyFn(classMeta.refName, fn);
453
+ return fn;
454
+ };
455
+ const scalarCrystalizeMap = /* @__PURE__ */ new Map([
456
+ [Date, (value) => (0, import_base.dayjs)(value)],
457
+ [String, (value) => value],
458
+ [import_base.ID, (value) => value],
459
+ [Boolean, (value) => value],
460
+ [import_base.Int, (value) => value],
461
+ [import_base.Float, (value) => value],
462
+ [import_base.JSON, (value) => value]
463
+ ]);
464
+ const crystalize = (metadata, value) => {
465
+ if (value === void 0 || value === null)
466
+ return value;
467
+ if (metadata.isArray && Array.isArray(value))
468
+ return value.map((v) => crystalize({ ...metadata, isArray: false }, v));
469
+ if (metadata.isMap) {
470
+ const [valueRef] = (0, import_base.getNonArrayModel)(metadata.of);
471
+ const crystalizeValue = scalarCrystalizeMap.get(valueRef) ?? ((value2) => value2);
472
+ return new Map(
473
+ Object.entries(value).map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, crystalizeValue)])
474
+ );
475
+ }
476
+ if (metadata.isClass)
477
+ return makeCrystalize(metadata.modelRef)(value, true);
478
+ if (metadata.name === "Date")
479
+ return (0, import_base.dayjs)(value);
480
+ return (scalarCrystalizeMap.get(metadata.modelRef) ?? ((value2) => value2))(value);
481
+ };
482
+ const getPredefinedCrystalizeFn = (refName) => {
483
+ const crystalize2 = Reflect.getMetadata(refName, CrystalizeStorage.prototype);
484
+ return crystalize2;
485
+ };
486
+ const setPredefinedCrystalizeFn = (refName, crystalize2) => {
487
+ Reflect.defineMetadata(refName, crystalize2, CrystalizeStorage.prototype);
488
+ };
489
+ const makeCrystalize = (target, option = {}) => {
490
+ const classMeta = (0, import_constant.getClassMeta)(target);
491
+ const crystalizeFn = getPredefinedCrystalizeFn(classMeta.refName);
492
+ if (crystalizeFn && !option.overwrite && !option.partial?.length)
493
+ return crystalizeFn;
494
+ const fieldMetaMap = (0, import_constant.getFieldMetaMap)(target);
495
+ const fieldKeys = option.partial?.length ? classMeta.type === "scalar" ? option.partial : ["id", ...option.partial, "updatedAt"] : [...fieldMetaMap.keys()];
496
+ const metadatas = fieldKeys.map((key) => fieldMetaMap.get(key));
497
+ const fn = (self, isChild) => {
498
+ try {
499
+ const result = Object.assign(new target(), self);
500
+ for (const metadata of metadatas.filter((m) => !!self[m.key])) {
501
+ if (metadata.fieldType === "hidden")
502
+ continue;
503
+ result[metadata.key] = crystalize(metadata, self[metadata.key]);
504
+ }
505
+ return result;
506
+ } catch (err) {
507
+ if (isChild)
508
+ throw new Error(err);
509
+ return null;
510
+ }
511
+ };
512
+ if (!option.partial?.length)
513
+ setPredefinedCrystalizeFn(classMeta.refName, fn);
514
+ return fn;
515
+ };
516
+ const fragmentize = (target, fragMap = /* @__PURE__ */ new Map(), partial) => {
517
+ const classMeta = (0, import_constant.getClassMeta)(target);
518
+ const metadatas = (0, import_constant.getFieldMetas)(target);
519
+ const selectKeys = partial ? ["id", ...partial, "updatedAt"] : metadatas.map((metadata) => metadata.key);
520
+ const selectKeySet = new Set(selectKeys);
521
+ const fragment = `fragment ${(0, import_common.lowerlize)(classMeta.refName)}Fragment on ${(0, import_common.capitalize)(
522
+ classMeta.type === "light" ? classMeta.refName.slice(5) : classMeta.refName
523
+ )} {
524
+ ` + metadatas.filter((metadata) => metadata.fieldType !== "hidden" && selectKeySet.has(metadata.key)).map((metadata) => {
525
+ return metadata.isClass ? ` ${metadata.key} {
526
+ ...${(0, import_common.lowerlize)(metadata.name)}Fragment
527
+ }` : ` ${metadata.key}`;
528
+ }).join(`
529
+ `) + `
530
+ }`;
531
+ fragMap.set(classMeta.refName, fragment);
532
+ metadatas.filter((metadata) => metadata.fieldType !== "hidden" && selectKeySet.has(metadata.key) && metadata.isClass).forEach((metadata) => fragmentize(metadata.modelRef, fragMap));
533
+ return fragMap;
534
+ };
535
+ const getPredefinedFragment = (refName) => {
536
+ const fragment = Reflect.getMetadata(refName, FragmentStorage.prototype);
537
+ return fragment;
538
+ };
539
+ const setPredefinedFragment = (refName, fragment) => {
540
+ Reflect.defineMetadata(refName, fragment, FragmentStorage.prototype);
541
+ };
542
+ const makeFragment = (target, option = {}) => {
543
+ const classMeta = (0, import_constant.getClassMeta)(target);
544
+ const fragment = getPredefinedFragment(classMeta.refName);
545
+ if (fragment && !option.overwrite && !option.excludeSelf && !option.partial?.length)
546
+ return fragment;
547
+ const fragMap = new Map(fragmentize(target, /* @__PURE__ */ new Map(), option.partial));
548
+ if (option.excludeSelf)
549
+ fragMap.delete(classMeta.refName);
550
+ const gqlStr = [...fragMap.values()].join("\n");
551
+ if (!option.excludeSelf)
552
+ setPredefinedFragment(classMeta.refName, gqlStr);
553
+ return gqlStr;
554
+ };
555
+ const getGqlStr = (modelRef, gqlMeta, argMetas, returnRef, partial) => {
556
+ const isScalar = (0, import_base.isGqlScalar)(modelRef);
557
+ const argStr = makeArgStr(argMetas);
558
+ const argAssignStr = makeArgAssignStr(argMetas);
559
+ const returnStr = makeReturnStr(returnRef, partial);
560
+ const gqlStr = `${isScalar ? "" : makeFragment(returnRef, { excludeSelf: !!partial?.length, partial })}
561
+ ${(0, import_common.lowerlize)(gqlMeta.type) + " " + gqlMeta.key + argStr}{
562
+ ${gqlMeta.key}${argAssignStr}${returnStr}
563
+ }
564
+ `;
565
+ return gqlStr;
566
+ };
567
+ const scalarSerializeMap = /* @__PURE__ */ new Map([
568
+ [Date, (value) => (0, import_base.dayjs)(value).toDate()],
569
+ [String, (value) => value],
570
+ [import_base.ID, (value) => value],
571
+ [Boolean, (value) => value],
572
+ [import_base.Int, (value) => value],
573
+ [import_base.Float, (value) => value],
574
+ [import_base.JSON, (value) => value]
575
+ ]);
576
+ const getSerializeFn = (inputRef) => {
577
+ const serializeFn = scalarSerializeMap.get(inputRef);
578
+ if (!serializeFn)
579
+ return (value) => value;
580
+ else
581
+ return serializeFn;
582
+ };
583
+ const serializeInput = (value, inputRef, arrDepth) => {
584
+ if (arrDepth && Array.isArray(value))
585
+ return value.map((v) => serializeInput(v, inputRef, arrDepth - 1));
586
+ else if (inputRef.prototype === Map.prototype) {
587
+ const [valueRef] = (0, import_base.getNonArrayModel)(inputRef);
588
+ const serializeFn = getSerializeFn(valueRef);
589
+ return Object.fromEntries(
590
+ [...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, serializeFn)])
591
+ );
592
+ } else if ((0, import_base.isGqlScalar)(inputRef)) {
593
+ const serializeFn = getSerializeFn(inputRef);
594
+ return serializeFn(value);
595
+ }
596
+ const classMeta = (0, import_constant.getClassMeta)(inputRef);
597
+ if (classMeta.type !== "scalar")
598
+ return value;
599
+ else
600
+ return Object.fromEntries(
601
+ (0, import_constant.getFieldMetas)(inputRef).map((fieldMeta) => [
602
+ fieldMeta.key,
603
+ serializeInput(value[fieldMeta.key], fieldMeta.modelRef, fieldMeta.arrDepth)
604
+ ])
605
+ );
606
+ };
607
+ const serializeArg = (argMeta, value) => {
608
+ const [returnRef, arrDepth] = (0, import_base.getNonArrayModel)(argMeta.returns());
609
+ if (argMeta.argsOption.nullable && (value === null || value === void 0))
610
+ return null;
611
+ else if (!argMeta.argsOption.nullable && (value === null || value === void 0))
612
+ throw new Error(`Invalid Value (Nullable) in ${argMeta.name} for value ${value}`);
613
+ return serializeInput(value, returnRef, arrDepth);
614
+ };
615
+ const scalarDeserializeMap = /* @__PURE__ */ new Map([
616
+ [Date, (value) => (0, import_base.dayjs)(value)],
617
+ [String, (value) => value],
618
+ [import_base.ID, (value) => value],
619
+ [Boolean, (value) => value],
620
+ [import_base.Int, (value) => value],
621
+ [import_base.Float, (value) => value],
622
+ [import_base.JSON, (value) => value]
623
+ ]);
624
+ const getDeserializeFn = (inputRef) => {
625
+ const deserializeFn = scalarDeserializeMap.get(inputRef);
626
+ if (!deserializeFn)
627
+ return (value) => value;
628
+ return deserializeFn;
629
+ };
630
+ const deserializeInput = (value, inputRef, arrDepth) => {
631
+ if (arrDepth && Array.isArray(value))
632
+ return value.map((v) => deserializeInput(v, inputRef, arrDepth - 1));
633
+ else if (inputRef.prototype === Map.prototype) {
634
+ const [valueRef] = (0, import_base.getNonArrayModel)(inputRef);
635
+ const deserializeFn = getDeserializeFn(valueRef);
636
+ return Object.fromEntries(
637
+ [...value.entries()].map(([key, val]) => [key, (0, import_base.applyFnToArrayObjects)(val, deserializeFn)])
638
+ );
639
+ } else if ((0, import_base.isGqlScalar)(inputRef)) {
640
+ const deserializeFn = getDeserializeFn(inputRef);
641
+ return deserializeFn(value);
642
+ }
643
+ const classMeta = (0, import_constant.getClassMeta)(inputRef);
644
+ if (classMeta.type !== "scalar")
645
+ return value;
646
+ else
647
+ return Object.fromEntries(
648
+ (0, import_constant.getFieldMetas)(inputRef).map((fieldMeta) => [
649
+ fieldMeta.key,
650
+ deserializeInput(value[fieldMeta.key], fieldMeta.modelRef, fieldMeta.arrDepth)
651
+ ])
652
+ );
653
+ };
654
+ const deserializeArg = (argMeta, value) => {
655
+ const [returnRef, arrDepth] = (0, import_base.getNonArrayModel)(argMeta.returns());
656
+ if (argMeta.argsOption.nullable && (value === null || value === void 0))
657
+ return null;
658
+ else if (!argMeta.argsOption.nullable && (value === null || value === void 0))
659
+ throw new Error(`Invalid Value (Nullable) in ${argMeta.name} for value ${value}`);
660
+ return deserializeInput(value, returnRef, arrDepth);
661
+ };
662
+ const fetchOf = (sigRef) => {
663
+ const gqls = {};
664
+ const gqlMetas = (0, import_signalDecorators.getGqlMetas)(sigRef);
665
+ gqlMetas.filter((gqlMeta) => !gqlMeta.signalOption.default).forEach((gqlMeta) => {
666
+ if (gqlMeta.type === "Message") {
667
+ const [returnRef, arrDepth] = (0, import_base.getNonArrayModel)(gqlMeta.returns());
668
+ const [argMetas] = (0, import_signalDecorators.getArgMetas)(sigRef, gqlMeta.signalOption.name ?? gqlMeta.key);
669
+ const isScalar = (0, import_base.isGqlScalar)(returnRef);
670
+ const emitEvent = function(...args) {
671
+ const fetchPolicy = args[argMetas.length] ?? { crystalize: true };
672
+ if (!this.client.io && !fetchPolicy.url) {
673
+ import_common.Logger.warn(`${gqlMeta.key} emit suppressed - socket is not connected`);
674
+ return;
675
+ }
676
+ const message = Object.fromEntries(
677
+ argMetas.map((argMeta) => [argMeta.name, serializeArg(argMeta, args[argMeta.idx]) ?? null])
678
+ );
679
+ const io = this.client.getIo(fetchPolicy.url);
680
+ void this.client.waitUntilWebSocketConnected(fetchPolicy.url).then(() => {
681
+ io.emit(gqlMeta.key, message);
682
+ import_common.Logger.debug(`socket emit: ${gqlMeta.key}: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
683
+ });
684
+ };
685
+ const listenEvent = function(handleEvent, fetchPolicy = {}) {
686
+ const crystalize2 = (data) => {
687
+ if (isScalar) {
688
+ if (returnRef.prototype === Date.prototype)
689
+ return (0, import_base.dayjs)(data);
690
+ else
691
+ return data;
692
+ } else if (Array.isArray(data))
693
+ return data.map((d) => crystalize2(d));
694
+ else
695
+ return makeCrystalize(returnRef)(data);
696
+ };
697
+ const handle = (data) => {
698
+ import_common.Logger.debug(`socket listened: ${gqlMeta.key}: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
699
+ handleEvent(crystalize2(data));
700
+ };
701
+ const io = this.client.getIo(fetchPolicy.url);
702
+ this.client.waitUntilWebSocketConnected(fetchPolicy.url).then(() => {
703
+ io.removeListener(gqlMeta.key, handle);
704
+ io.on(gqlMeta.key, handle);
705
+ import_common.Logger.debug(`socket listen start: ${gqlMeta.key}: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
706
+ });
707
+ return async () => {
708
+ await this.client.waitUntilWebSocketConnected(fetchPolicy.url);
709
+ import_common.Logger.debug(`socket listen end: ${gqlMeta.key}: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
710
+ io.removeListener(gqlMeta.key, handle);
711
+ };
712
+ };
713
+ gqls[gqlMeta.key] = emitEvent;
714
+ gqls[`listen${(0, import_common.capitalize)(gqlMeta.key)}`] = listenEvent;
715
+ } else if (gqlMeta.type === "Pubsub") {
716
+ const [returnRef] = (0, import_base.getNonArrayModel)(gqlMeta.returns());
717
+ const [argMetas] = (0, import_signalDecorators.getArgMetas)(sigRef, gqlMeta.signalOption.name ?? gqlMeta.key);
718
+ const isScalar = (0, import_base.isGqlScalar)(returnRef);
719
+ const makeRoomId = (gqlKey, argValues) => `${gqlKey}-${argValues.join("-")}`;
720
+ const crystalize2 = (data) => {
721
+ if (isScalar) {
722
+ if (returnRef.prototype === Date.prototype)
723
+ return (0, import_base.dayjs)(data);
724
+ else
725
+ return data;
726
+ } else if (Array.isArray(data))
727
+ return data.map((d) => crystalize2(d));
728
+ else
729
+ return makeCrystalize(returnRef)(data);
730
+ };
731
+ const subscribeEvent = function(...args) {
732
+ const onData = args[argMetas.length];
733
+ const fetchPolicy = args[argMetas.length + 1] ?? { crystalize: true };
734
+ const message = Object.fromEntries(
735
+ argMetas.map((argMeta) => [argMeta.name, serializeArg(argMeta, args[argMeta.idx]) ?? null])
736
+ );
737
+ const handleEvent = (data) => {
738
+ if (data.__subscribe__)
739
+ return;
740
+ onData(crystalize2(data));
741
+ };
742
+ const roomId = makeRoomId(
743
+ gqlMeta.key,
744
+ argMetas.map((argMeta) => message[argMeta.name])
745
+ );
746
+ const io = this.client.getIo(fetchPolicy.url);
747
+ void this.client.waitUntilWebSocketConnected(fetchPolicy.url).then(() => {
748
+ import_common.Logger.debug(`socket subscribe start: ${gqlMeta.key}: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
749
+ io.subscribe({ key: gqlMeta.key, roomId, message, handleEvent });
750
+ });
751
+ return async () => {
752
+ //! 앱에서 다른 앱 넘어갈 때 언마운트 되버리면서 subscribe가 끊기는 일이 있음.
753
+ await this.client.waitUntilWebSocketConnected(fetchPolicy.url);
754
+ import_common.Logger.debug(`socket unsubscribe: ${gqlMeta.key}: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
755
+ io.unsubscribe(roomId, handleEvent);
756
+ };
757
+ };
758
+ gqls[`subscribe${(0, import_common.capitalize)(gqlMeta.key)}`] = subscribeEvent;
759
+ } else if (gqlMeta.type === "Query" || gqlMeta.type === "Mutation") {
760
+ const name = gqlMeta.signalOption.name ?? gqlMeta.key;
761
+ const makeReq = ({ resolve }) => async function(...args) {
762
+ import_common.Logger.debug(`fetch: ${gqlMeta.key} start: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
763
+ const now = Date.now();
764
+ const [argMetas] = (0, import_signalDecorators.getArgMetas)(sigRef, gqlMeta.signalOption.name ?? gqlMeta.key);
765
+ const [modelRef, arrDepth] = (0, import_base.getNonArrayModel)(gqlMeta.returns());
766
+ const isScalar = (0, import_base.isGqlScalar)(modelRef);
767
+ const returnRef = isScalar || !arrDepth ? modelRef : (0, import_constant.getLightModelRef)(modelRef);
768
+ const fetchPolicy = args[argMetas.length] ?? { crystalize: true };
769
+ const partial = fetchPolicy.partial ?? gqlMeta.signalOption.partial;
770
+ const crystalize2 = (data) => {
771
+ if (fetchPolicy.crystalize === false)
772
+ return data;
773
+ if (isScalar) {
774
+ if (returnRef.prototype === Date.prototype)
775
+ return (0, import_base.dayjs)(data);
776
+ else
777
+ return data;
778
+ } else if (Array.isArray(data))
779
+ return data.map((d) => crystalize2(d));
780
+ else
781
+ return makeCrystalize(returnRef, { partial })(data);
782
+ };
783
+ try {
784
+ const res = (await (gqlMeta.type === "Query" ? query : mutate)(
785
+ this.client,
786
+ graphql(getGqlStr(modelRef, gqlMeta, argMetas, returnRef, partial)),
787
+ Object.fromEntries(
788
+ argMetas.map((argMeta) => [argMeta.name, serializeArg(argMeta, args[argMeta.idx]) ?? null])
789
+ ),
790
+ fetchPolicy
791
+ ))[name];
792
+ const data = resolve ? crystalize2(res) : res;
793
+ import_common.Logger.debug(
794
+ `fetch: ${gqlMeta.key} end: ${(0, import_base.dayjs)().format("YYYY-MM-DD HH:mm:ss.SSS")} ${Date.now() - now}ms`
795
+ );
796
+ return data;
797
+ } catch (e) {
798
+ import_common.Logger.error(`fetch: ${gqlMeta.key} error: ${e}`);
799
+ throw e;
800
+ }
801
+ };
802
+ gqls[name] = makeReq({ resolve: true });
803
+ gqls[`_${name}`] = makeReq({ resolve: false });
804
+ }
805
+ });
806
+ return gqls;
807
+ };
808
+ const makeFetch = (fetch1, fetch2, fetch3, fetch4, fetch5, fetch6, fetch7, fetch8, fetch9, fetch10) => {
809
+ return Object.assign(fetch1, fetch2, fetch3, fetch4, fetch5, fetch6, fetch7, fetch8, fetch9, fetch10);
810
+ };
811
+ const makeArgStr = (argMetas) => {
812
+ return argMetas.length ? `(${argMetas.map((argMeta) => {
813
+ const [argRef, arrDepth] = (0, import_base.getNonArrayModel)(argMeta.returns());
814
+ const argRefType = (0, import_base.isGqlScalar)(argRef) ? "gqlScalar" : (0, import_constant.getClassMeta)(argRef).type === "scalar" ? "scalar" : "model";
815
+ const gqlTypeStr = "[".repeat(arrDepth) + ((0, import_constant.getGqlTypeStr)(argRef) + (argRefType === "scalar" ? "Input" : "")) + "!]".repeat(arrDepth);
816
+ return `$${argMeta.name}: ` + gqlTypeStr + (argMeta.argsOption.nullable ? "" : "!");
817
+ }).join(", ")})` : "";
818
+ };
819
+ const makeArgAssignStr = (argMetas) => {
820
+ return argMetas.length ? `(${argMetas.map((argMeta) => `${argMeta.name}: $${argMeta.name}`).join(", ")})` : "";
821
+ };
822
+ const makeReturnStr = (returnRef, partial) => {
823
+ const isScalar = (0, import_base.isGqlScalar)(returnRef);
824
+ if (isScalar)
825
+ return "";
826
+ const classMeta = (0, import_constant.getClassMeta)(returnRef);
827
+ if (!partial?.length)
828
+ return ` {
829
+ ...${(0, import_common.lowerlize)(classMeta.refName)}Fragment
830
+ }`;
831
+ const targetKeys = classMeta.type === "scalar" ? partial : [.../* @__PURE__ */ new Set(["id", ...partial, "updatedAt"])];
832
+ const fieldMetaMap = (0, import_constant.getFieldMetaMap)(returnRef);
833
+ return ` {
834
+ ${targetKeys.map((key) => fieldMetaMap.get(key)).filter((metadata) => metadata && metadata.fieldType !== "hidden").map(
835
+ (fieldMeta) => fieldMeta.isClass ? ` ${fieldMeta.key} {
836
+ ...${(0, import_common.lowerlize)(fieldMeta.name)}Fragment
837
+ }` : ` ${fieldMeta.key}`
838
+ ).join("\n")}
839
+ }`;
840
+ };
841
+ // Annotate the CommonJS export names for ESM import in node:
842
+ 0 && (module.exports = {
843
+ CrystalizeStorage,
844
+ DefaultStorage,
845
+ FragmentStorage,
846
+ GqlStorage,
847
+ PurifyStorage,
848
+ deserializeArg,
849
+ fetchOf,
850
+ getGqlOnStorage,
851
+ getGqlStr,
852
+ gqlOf,
853
+ makeCrystalize,
854
+ makeDefault,
855
+ makeFetch,
856
+ makeFragment,
857
+ makePurify,
858
+ scalarUtilOf,
859
+ serializeArg
860
+ });