@ciwergrp/nuxid 1.7.2 → 1.9.0
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/dist/module.json
CHANGED
|
@@ -20,10 +20,23 @@ export interface CursorFetchOptions<TRequest extends NitroFetchRequest = NitroFe
|
|
|
20
20
|
cursorParam?: string;
|
|
21
21
|
query?: Record<string, any>;
|
|
22
22
|
}
|
|
23
|
+
export interface FetchError<TData = any> {
|
|
24
|
+
statusCode: number | undefined;
|
|
25
|
+
statusMessage: string | undefined;
|
|
26
|
+
_data: TData | undefined;
|
|
27
|
+
}
|
|
23
28
|
export declare function useCursorHttp<T extends Record<string, any> = any, TResponse extends APIResponseCursor<T> = APIResponseCursor<T>, TRequest extends NitroFetchRequest = NitroFetchRequest>(url: TRequest, options?: CursorFetchOptions<TRequest>): Promise<{
|
|
24
29
|
data: import("vue").Ref<TResponse | undefined, TResponse | undefined>;
|
|
25
30
|
loading: Readonly<import("vue").Ref<boolean, boolean>>;
|
|
26
|
-
error: Readonly<import("vue").Ref<
|
|
31
|
+
error: Readonly<import("vue").Ref<{
|
|
32
|
+
readonly statusCode: number | undefined;
|
|
33
|
+
readonly statusMessage: string | undefined;
|
|
34
|
+
readonly _data: any;
|
|
35
|
+
} | null, {
|
|
36
|
+
readonly statusCode: number | undefined;
|
|
37
|
+
readonly statusMessage: string | undefined;
|
|
38
|
+
readonly _data: any;
|
|
39
|
+
} | null>>;
|
|
27
40
|
hasNextPage: Readonly<import("vue").Ref<boolean, boolean>>;
|
|
28
41
|
isLoadMoreTriggered: Readonly<import("vue").Ref<boolean, boolean>>;
|
|
29
42
|
loadMore: () => Promise<void>;
|
|
@@ -36,6 +36,20 @@ export async function useCursorHttp(url, options) {
|
|
|
36
36
|
}
|
|
37
37
|
return unwrapped;
|
|
38
38
|
}
|
|
39
|
+
function normalizeFetchError(e) {
|
|
40
|
+
if (e && typeof e === "object") {
|
|
41
|
+
const cast = e;
|
|
42
|
+
return {
|
|
43
|
+
statusCode: typeof cast.statusCode === "number" ? cast.statusCode : void 0,
|
|
44
|
+
statusMessage: typeof cast.statusMessage === "string" ? cast.statusMessage : cast.message ?? void 0,
|
|
45
|
+
_data: cast.data
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
if (typeof e === "string") {
|
|
49
|
+
return { statusCode: void 0, statusMessage: e, _data: void 0 };
|
|
50
|
+
}
|
|
51
|
+
return { statusCode: void 0, statusMessage: "Unknown error", _data: void 0 };
|
|
52
|
+
}
|
|
39
53
|
const {
|
|
40
54
|
lazy = false,
|
|
41
55
|
immediate = true,
|
|
@@ -102,7 +116,7 @@ export async function useCursorHttp(url, options) {
|
|
|
102
116
|
nextCursor.value = normalizeCursorValue(response.meta?.[cursorKey]);
|
|
103
117
|
hasNextPage.value = response.meta?.[hasMoreKey] ?? false;
|
|
104
118
|
} catch (e) {
|
|
105
|
-
error.value = e;
|
|
119
|
+
error.value = normalizeFetchError(e);
|
|
106
120
|
} finally {
|
|
107
121
|
loading.value = false;
|
|
108
122
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const createSameAsValidator = (otherField,
|
|
1
|
+
const createSameAsValidator = (otherField, getFormState, message) => {
|
|
2
2
|
return (_rule, value, callback) => {
|
|
3
|
-
const otherValue = getNestedValue(
|
|
3
|
+
const otherValue = getNestedValue(getFormState(), otherField);
|
|
4
4
|
if (value !== otherValue) {
|
|
5
5
|
callback(new Error(message));
|
|
6
6
|
} else {
|
|
@@ -206,9 +206,9 @@ const createIntegerValidator = (message) => {
|
|
|
206
206
|
}
|
|
207
207
|
};
|
|
208
208
|
};
|
|
209
|
-
const createComparisonValidator = (otherField,
|
|
209
|
+
const createComparisonValidator = (otherField, getFormState, message, compare) => {
|
|
210
210
|
return (_rule, value, callback) => {
|
|
211
|
-
const otherValue = getNestedValue(
|
|
211
|
+
const otherValue = getNestedValue(getFormState(), otherField);
|
|
212
212
|
if (value && otherValue && !compare(value, otherValue)) {
|
|
213
213
|
callback(new Error(message));
|
|
214
214
|
} else {
|
|
@@ -331,10 +331,14 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
331
331
|
if (authorize && !authorize()) {
|
|
332
332
|
throw new Error("This action is unauthorized.");
|
|
333
333
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
334
|
+
const getValidationData = () => {
|
|
335
|
+
const nextData = { ...formState };
|
|
336
|
+
if (prepareForValidation) {
|
|
337
|
+
return prepareForValidation(nextData);
|
|
338
|
+
}
|
|
339
|
+
return nextData;
|
|
340
|
+
};
|
|
341
|
+
const dataToValidate = getValidationData();
|
|
338
342
|
const rules = {};
|
|
339
343
|
const getMessage = (fieldName, rule, defaultMessage) => {
|
|
340
344
|
const customMessageKey = `${fieldName}.${rule}`;
|
|
@@ -397,7 +401,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
397
401
|
const expectedValues = paramValues.slice(1);
|
|
398
402
|
fieldRules.push({
|
|
399
403
|
validator: (_rule, value, callback) => {
|
|
400
|
-
const otherValue = getNestedValue(
|
|
404
|
+
const otherValue = getNestedValue(getValidationData(), otherField);
|
|
401
405
|
const shouldRequire = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
|
|
402
406
|
if (!shouldRequire) {
|
|
403
407
|
return callback();
|
|
@@ -428,7 +432,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
428
432
|
case "filled":
|
|
429
433
|
fieldRules.push({
|
|
430
434
|
validator: (_rule, value, callback) => {
|
|
431
|
-
if (!hasNestedKey(
|
|
435
|
+
if (!hasNestedKey(getValidationData(), fieldName)) {
|
|
432
436
|
return callback();
|
|
433
437
|
}
|
|
434
438
|
if (!isFilledValue(value)) {
|
|
@@ -442,7 +446,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
442
446
|
case "present":
|
|
443
447
|
fieldRules.push({
|
|
444
448
|
validator: (_rule, _value, callback) => {
|
|
445
|
-
if (!hasNestedKey(
|
|
449
|
+
if (!hasNestedKey(getValidationData(), fieldName)) {
|
|
446
450
|
return callback(new Error(getMessage(fieldName, "present", `${attributeName} must be present`)));
|
|
447
451
|
}
|
|
448
452
|
callback();
|
|
@@ -458,7 +462,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
458
462
|
const expectedValues = paramValues.slice(1);
|
|
459
463
|
fieldRules.push({
|
|
460
464
|
validator: (_rule, value, callback) => {
|
|
461
|
-
const otherValue = getNestedValue(
|
|
465
|
+
const otherValue = getNestedValue(getValidationData(), otherField);
|
|
462
466
|
const shouldRequire = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
|
|
463
467
|
if (!shouldRequire) {
|
|
464
468
|
return callback();
|
|
@@ -480,7 +484,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
480
484
|
const expectedValues = paramValues.slice(1);
|
|
481
485
|
fieldRules.push({
|
|
482
486
|
validator: (_rule, value, callback) => {
|
|
483
|
-
const otherValue = getNestedValue(
|
|
487
|
+
const otherValue = getNestedValue(getValidationData(), otherField);
|
|
484
488
|
const isExcluded = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
|
|
485
489
|
if (isExcluded) {
|
|
486
490
|
return callback();
|
|
@@ -501,7 +505,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
501
505
|
const otherFields = paramValues;
|
|
502
506
|
fieldRules.push({
|
|
503
507
|
validator: (_rule, value, callback) => {
|
|
504
|
-
if (!hasAnyFilled(
|
|
508
|
+
if (!hasAnyFilled(getValidationData(), otherFields)) {
|
|
505
509
|
return callback();
|
|
506
510
|
}
|
|
507
511
|
if (!isFilledValue(value)) {
|
|
@@ -520,7 +524,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
520
524
|
const otherFields = paramValues;
|
|
521
525
|
fieldRules.push({
|
|
522
526
|
validator: (_rule, value, callback) => {
|
|
523
|
-
if (!hasAllFilled(
|
|
527
|
+
if (!hasAllFilled(getValidationData(), otherFields)) {
|
|
524
528
|
return callback();
|
|
525
529
|
}
|
|
526
530
|
if (!isFilledValue(value)) {
|
|
@@ -539,7 +543,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
539
543
|
const otherFields = paramValues;
|
|
540
544
|
fieldRules.push({
|
|
541
545
|
validator: (_rule, value, callback) => {
|
|
542
|
-
if (hasAnyFilled(
|
|
546
|
+
if (hasAnyFilled(getValidationData(), otherFields)) {
|
|
543
547
|
return callback();
|
|
544
548
|
}
|
|
545
549
|
if (!isFilledValue(value)) {
|
|
@@ -558,7 +562,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
558
562
|
const otherFields = paramValues;
|
|
559
563
|
fieldRules.push({
|
|
560
564
|
validator: (_rule, value, callback) => {
|
|
561
|
-
if (hasAllFilled(
|
|
565
|
+
if (hasAllFilled(getValidationData(), otherFields)) {
|
|
562
566
|
return callback();
|
|
563
567
|
}
|
|
564
568
|
if (!isFilledValue(value)) {
|
|
@@ -589,7 +593,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
589
593
|
const expectedValues = paramValues.slice(1);
|
|
590
594
|
fieldRules.push({
|
|
591
595
|
validator: (_rule, value, callback) => {
|
|
592
|
-
const otherValue = getNestedValue(
|
|
596
|
+
const otherValue = getNestedValue(getValidationData(), otherField);
|
|
593
597
|
const shouldProhibit = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
|
|
594
598
|
if (!shouldProhibit) {
|
|
595
599
|
return callback();
|
|
@@ -611,7 +615,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
611
615
|
const expectedValues = paramValues.slice(1);
|
|
612
616
|
fieldRules.push({
|
|
613
617
|
validator: (_rule, value, callback) => {
|
|
614
|
-
const otherValue = getNestedValue(
|
|
618
|
+
const otherValue = getNestedValue(getValidationData(), otherField);
|
|
615
619
|
const isAllowed = expectedValues.some((expected) => normalizeComparisonValue(otherValue, expected));
|
|
616
620
|
if (isAllowed) {
|
|
617
621
|
return callback();
|
|
@@ -635,7 +639,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
635
639
|
if (!isFilledValue(value)) {
|
|
636
640
|
return callback();
|
|
637
641
|
}
|
|
638
|
-
const hasAny = otherFields.some((field) => isFilledValue(getNestedValue(
|
|
642
|
+
const hasAny = otherFields.some((field) => isFilledValue(getNestedValue(getValidationData(), field)));
|
|
639
643
|
if (hasAny) {
|
|
640
644
|
return callback(new Error(getMessage(fieldName, "prohibits", `${attributeName} prohibits ${otherFields.join(", ")}`)));
|
|
641
645
|
}
|
|
@@ -749,7 +753,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
749
753
|
}
|
|
750
754
|
const sameOtherAttr = attributes[firstParam] || firstParam;
|
|
751
755
|
fieldRules.push({
|
|
752
|
-
validator: createSameAsValidator(firstParam,
|
|
756
|
+
validator: createSameAsValidator(firstParam, getValidationData, getMessage(fieldName, "same", `${attributeName} must match ${sameOtherAttr}`)),
|
|
753
757
|
trigger: pickTrigger(fieldName, "blur")
|
|
754
758
|
});
|
|
755
759
|
break;
|
|
@@ -760,7 +764,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
760
764
|
}
|
|
761
765
|
const diffOtherAttr = attributes[firstParam] || firstParam;
|
|
762
766
|
fieldRules.push({
|
|
763
|
-
validator: createComparisonValidator(firstParam,
|
|
767
|
+
validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "different", `${attributeName} must be different from ${diffOtherAttr}`), (a, b) => a !== b),
|
|
764
768
|
trigger: pickTrigger(fieldName, "blur")
|
|
765
769
|
});
|
|
766
770
|
break;
|
|
@@ -896,7 +900,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
896
900
|
if (isEmptyValue(value)) {
|
|
897
901
|
return callback();
|
|
898
902
|
}
|
|
899
|
-
const haystack = getNestedValue(
|
|
903
|
+
const haystack = getNestedValue(getValidationData(), otherField);
|
|
900
904
|
if (!Array.isArray(haystack)) {
|
|
901
905
|
return callback(new Error(getMessage(fieldName, "in_array", `${attributeName} must be one of the values in ${otherField}`)));
|
|
902
906
|
}
|
|
@@ -1153,7 +1157,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1153
1157
|
if (isEmptyValue(value)) {
|
|
1154
1158
|
return callback();
|
|
1155
1159
|
}
|
|
1156
|
-
const confirmationValue = getNestedValue(
|
|
1160
|
+
const confirmationValue = getNestedValue(getValidationData(), confirmationField);
|
|
1157
1161
|
if (value !== confirmationValue) {
|
|
1158
1162
|
return callback(new Error(getMessage(fieldName, "confirmed", `${attributeName} confirmation does not match`)));
|
|
1159
1163
|
}
|
|
@@ -1210,7 +1214,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1210
1214
|
}
|
|
1211
1215
|
const gtOtherAttr = attributes[firstParam] || firstParam;
|
|
1212
1216
|
fieldRules.push({
|
|
1213
|
-
validator: createComparisonValidator(firstParam,
|
|
1217
|
+
validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "gt", `${attributeName} must be greater than ${gtOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x > y)),
|
|
1214
1218
|
trigger: pickTrigger(fieldName, "blur")
|
|
1215
1219
|
});
|
|
1216
1220
|
break;
|
|
@@ -1221,7 +1225,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1221
1225
|
}
|
|
1222
1226
|
const gteOtherAttr = attributes[firstParam] || firstParam;
|
|
1223
1227
|
fieldRules.push({
|
|
1224
|
-
validator: createComparisonValidator(firstParam,
|
|
1228
|
+
validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "gte", `${attributeName} must be greater than or equal to ${gteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x >= y)),
|
|
1225
1229
|
trigger: "blur"
|
|
1226
1230
|
});
|
|
1227
1231
|
break;
|
|
@@ -1232,7 +1236,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1232
1236
|
}
|
|
1233
1237
|
const ltOtherAttr = attributes[firstParam] || firstParam;
|
|
1234
1238
|
fieldRules.push({
|
|
1235
|
-
validator: createComparisonValidator(firstParam,
|
|
1239
|
+
validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "lt", `${attributeName} must be less than ${ltOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x < y)),
|
|
1236
1240
|
trigger: "blur"
|
|
1237
1241
|
});
|
|
1238
1242
|
break;
|
|
@@ -1243,7 +1247,7 @@ export const createValidationRules = (definitions, formState, options = {}) => {
|
|
|
1243
1247
|
}
|
|
1244
1248
|
const lteOtherAttr = attributes[firstParam] || firstParam;
|
|
1245
1249
|
fieldRules.push({
|
|
1246
|
-
validator: createComparisonValidator(firstParam,
|
|
1250
|
+
validator: createComparisonValidator(firstParam, getValidationData, getMessage(fieldName, "lte", `${attributeName} must be less than or equal to ${lteOtherAttr}`), (a, b) => compareFieldValues(a, b, (x, y) => x <= y)),
|
|
1247
1251
|
trigger: "blur"
|
|
1248
1252
|
});
|
|
1249
1253
|
break;
|