@azure-net/kit 0.8.8 → 0.9.1
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/core/delivery/injectableDependencies/ErrorHandler.d.ts +12 -12
- package/dist/core/delivery/injectableDependencies/ErrorHandler.js +15 -23
- package/dist/core/delivery/schema/Schema.d.ts +32 -20
- package/dist/core/delivery/schema/Schema.js +105 -60
- package/dist/core/delivery/schema/rules/index.d.ts +2 -0
- package/dist/core/delivery/schema/rules/index.js +2 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/shared/middleware/Middleware.d.ts +4 -2
- package/dist/core/shared/middleware/Middleware.js +9 -9
- package/dist/{svelte → core/svelte}/ActiveForm/ActiveForm.svelte.d.ts +0 -4
- package/dist/{svelte → core/svelte}/ActiveForm/ActiveForm.svelte.js +1 -1
- package/dist/core/ui/index.d.ts +1 -0
- package/dist/core/ui/index.js +1 -0
- package/dist/core/ui/mask/Mask.d.ts +13 -0
- package/dist/core/ui/mask/Mask.js +110 -0
- package/dist/core/ui/mask/index.d.ts +1 -0
- package/dist/core/ui/mask/index.js +1 -0
- package/package.json +4 -12
- package/dist/core/shared/request/BaseRequest.d.ts +0 -41
- package/dist/core/shared/request/BaseRequest.js +0 -72
- package/dist/core/shared/request/index.d.ts +0 -1
- package/dist/core/shared/request/index.js +0 -1
- package/dist/core/shared/request/rules/Rules.d.ts +0 -62
- package/dist/core/shared/request/rules/Rules.js +0 -288
- package/dist/core/shared/request/rules/index.d.ts +0 -1
- package/dist/core/shared/request/rules/index.js +0 -1
- package/dist/core/shared/request/rules/messages/En.d.ts +0 -2
- package/dist/core/shared/request/rules/messages/En.js +0 -40
- package/dist/core/shared/request/rules/messages/Ru.d.ts +0 -2
- package/dist/core/shared/request/rules/messages/Ru.js +0 -40
- package/dist/core/shared/request/rules/messages/WithI18n.d.ts +0 -2
- package/dist/core/shared/request/rules/messages/WithI18n.js +0 -40
- package/dist/core/shared/request/rules/messages/index.d.ts +0 -3
- package/dist/core/shared/request/rules/messages/index.js +0 -3
- package/dist/core/shared/request/rules/messages/types.d.ts +0 -41
- package/dist/core/shared/request/rules/messages/types.js +0 -1
- /package/dist/{svelte → core/svelte}/ActiveForm/index.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/ActiveForm/index.js +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/AsyncSignal.svelte.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/AsyncSignal.svelte.js +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/index.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/AsyncSignal/index.js +0 -0
- /package/dist/{svelte → core/svelte}/index.d.ts +0 -0
- /package/dist/{svelte → core/svelte}/index.js +0 -0
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { BaseRequest, type RequestErrors } from '../../shared/request/index.js';
|
|
2
1
|
import { HttpServiceError } from '../../infra/httpService/index.js';
|
|
3
|
-
|
|
2
|
+
import { SchemaFail, type RequestErrors } from '../schema/index.js';
|
|
3
|
+
export type AppErrorType = 'http' | 'app' | 'schema' | 'abort';
|
|
4
4
|
export interface AppError<T = unknown, D = never> {
|
|
5
5
|
type: AppErrorType;
|
|
6
6
|
message: string;
|
|
7
7
|
fields?: RequestErrors<T>;
|
|
8
8
|
status?: number;
|
|
9
|
-
original?: HttpServiceError<T> |
|
|
9
|
+
original?: HttpServiceError<T> | SchemaFail<T> | Error;
|
|
10
10
|
custom?: D;
|
|
11
11
|
}
|
|
12
|
-
export type ErrorType<T = unknown> = Error |
|
|
13
|
-
export declare const
|
|
14
|
-
export declare const
|
|
15
|
-
export declare const
|
|
16
|
-
export declare const createErrorParser: <Custom = unknown>(
|
|
17
|
-
parseBaseError: typeof
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}) =>
|
|
12
|
+
export type ErrorType<T = unknown> = Error | HttpServiceError<T> | SchemaFail<T>;
|
|
13
|
+
export declare const baseParseHttpError: <T = unknown, D = never>(error: HttpServiceError<T>) => AppError<T, D>;
|
|
14
|
+
export declare const baseParseSchemaError: <SchemaData = unknown, D = never>(error: SchemaFail<SchemaData>) => AppError<SchemaData, D>;
|
|
15
|
+
export declare const baseParseBaseError: <D = never>(error: Error) => AppError<never, D>;
|
|
16
|
+
export declare const createErrorParser: <Custom = unknown>(parsers?: {
|
|
17
|
+
parseBaseError: typeof baseParseBaseError;
|
|
18
|
+
parseHttpError: typeof baseParseHttpError;
|
|
19
|
+
parseSchemaError: typeof baseParseSchemaError;
|
|
20
|
+
}) => (<T>(error: ErrorType<T>) => AppError<T, Custom>);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BaseRequest } from '../../shared/request/index.js';
|
|
2
1
|
import { HttpServiceError } from '../../infra/httpService/index.js';
|
|
3
|
-
|
|
2
|
+
import { SchemaFail } from '../schema/index.js';
|
|
3
|
+
export const baseParseHttpError = (error) => {
|
|
4
4
|
return {
|
|
5
5
|
type: 'http',
|
|
6
6
|
message: error.message ?? 'unexpected error',
|
|
@@ -8,40 +8,32 @@ export const parseHttpError = (error) => {
|
|
|
8
8
|
original: error
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
|
-
export const
|
|
11
|
+
export const baseParseSchemaError = (error) => {
|
|
12
12
|
return {
|
|
13
|
-
type: '
|
|
14
|
-
message: 'validation error',
|
|
13
|
+
type: 'schema',
|
|
14
|
+
message: 'schema validation error',
|
|
15
15
|
status: 422,
|
|
16
16
|
fields: error.getErrors(),
|
|
17
17
|
original: error
|
|
18
18
|
};
|
|
19
19
|
};
|
|
20
|
-
export const
|
|
20
|
+
export const baseParseBaseError = (error) => {
|
|
21
21
|
return {
|
|
22
22
|
type: 'app',
|
|
23
23
|
message: error.message,
|
|
24
24
|
original: error
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
|
-
export const createErrorParser = (
|
|
27
|
+
export const createErrorParser = (parsers) => {
|
|
28
28
|
return (error) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
switch (true) {
|
|
38
|
-
case error instanceof HttpServiceError:
|
|
39
|
-
return parseHttpError(error);
|
|
40
|
-
case error instanceof BaseRequest:
|
|
41
|
-
return parseRequestError(error);
|
|
42
|
-
default:
|
|
43
|
-
return parseBaseError(error);
|
|
44
|
-
}
|
|
29
|
+
const { parseBaseError = baseParseBaseError, parseHttpError = baseParseHttpError, parseSchemaError = baseParseSchemaError } = parsers ?? {};
|
|
30
|
+
switch (true) {
|
|
31
|
+
case error instanceof HttpServiceError:
|
|
32
|
+
return parseHttpError(error);
|
|
33
|
+
case error instanceof SchemaFail:
|
|
34
|
+
return parseSchemaError(error);
|
|
35
|
+
default:
|
|
36
|
+
return parseBaseError(error);
|
|
45
37
|
}
|
|
46
38
|
};
|
|
47
39
|
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
type DeepKeys<
|
|
2
|
-
[K in keyof
|
|
3
|
-
}[keyof
|
|
4
|
-
export type ValidationParams<
|
|
5
|
-
val:
|
|
6
|
-
listValues?: Partial<
|
|
7
|
-
key?:
|
|
1
|
+
type DeepKeys<SchemaData> = SchemaData extends object ? {
|
|
2
|
+
[K in keyof SchemaData & string]: SchemaData[K] extends object ? K | `${K}.${DeepKeys<SchemaData[K]>}` : K;
|
|
3
|
+
}[keyof SchemaData & string] : never;
|
|
4
|
+
export type ValidationParams<SchemaData = unknown, CurrentValue = unknown, CurrentKey = string> = {
|
|
5
|
+
val: CurrentValue;
|
|
6
|
+
listValues?: Partial<SchemaData>;
|
|
7
|
+
key?: CurrentKey;
|
|
8
8
|
};
|
|
9
9
|
export type ValidationMessage = string | {
|
|
10
10
|
key: string;
|
|
@@ -15,19 +15,31 @@ export interface ValidationErrorsMap {
|
|
|
15
15
|
}
|
|
16
16
|
export type ValidationResult = ValidationMessage | ValidationErrorsMap | ValidationErrorsMap[] | undefined;
|
|
17
17
|
export type ValidationRuleResult<CurrentValue, ListValues = unknown, CurrentKey = string> = (params: ValidationParams<CurrentValue, ListValues, CurrentKey>) => ValidationResult;
|
|
18
|
-
type ValidationErrors<
|
|
19
|
-
[K in keyof
|
|
18
|
+
type ValidationErrors<Errors> = Errors extends ReadonlyArray<infer U> ? ValidationErrors<U>[] : Errors extends object ? {
|
|
19
|
+
[K in keyof Errors]?: ValidationErrors<Errors[K]>;
|
|
20
20
|
} : ValidationMessage;
|
|
21
|
-
export type RequestErrors<
|
|
22
|
-
export type RequestRules<
|
|
23
|
-
export declare
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
export type RequestErrors<SchemaData> = Partial<ValidationErrors<SchemaData>>;
|
|
22
|
+
export type RequestRules<SchemaData> = Partial<Record<DeepKeys<SchemaData>, ValidationRuleResult<SchemaData>[]>>;
|
|
23
|
+
export declare class SchemaFail<SchemaData> {
|
|
24
|
+
private _errors;
|
|
25
|
+
constructor(_errors: RequestErrors<SchemaData>);
|
|
26
|
+
getErrors(): Partial<ValidationErrors<SchemaData>>;
|
|
27
|
+
}
|
|
28
|
+
interface SchemaBuilder<SchemaData, Rules = unknown, TransformResult = SchemaData, CustomMethods = unknown> {
|
|
29
|
+
rules(callback: (factory: Rules) => RequestRules<SchemaData>): SchemaBuilder<SchemaData, Rules, TransformResult, CustomMethods>;
|
|
30
|
+
transform<T>(callback: (data: SchemaData) => T): SchemaBuilder<SchemaData, Rules, T, CustomMethods>;
|
|
31
|
+
with<M extends Record<string, unknown>>(callback: () => M): SchemaBuilder<SchemaData, Rules, TransformResult, M>;
|
|
32
|
+
create(): Schema<SchemaData, TransformResult, CustomMethods>;
|
|
33
|
+
}
|
|
34
|
+
interface SchemaInstance<TransformResult> {
|
|
35
|
+
json(): TransformResult;
|
|
36
|
+
formData(): FormData;
|
|
37
|
+
validated(): boolean;
|
|
38
|
+
}
|
|
39
|
+
type Schema<SchemaData, TransformResult, CustomMethods> = CustomMethods & {
|
|
40
|
+
from(data: Partial<SchemaData>): SchemaInstance<TransformResult>;
|
|
41
|
+
getSchemaError(e: unknown): RequestErrors<SchemaData>;
|
|
32
42
|
};
|
|
43
|
+
export declare const createSchemaFactory: <Rules extends Record<string, unknown>>(rules: Rules) => <SchemaData>() => SchemaBuilder<SchemaData, Rules>;
|
|
44
|
+
export declare const schema: <SchemaData>() => SchemaBuilder<SchemaData>;
|
|
33
45
|
export {};
|
|
@@ -1,13 +1,46 @@
|
|
|
1
|
-
import { FormDataUtil } from 'azure-net-tools';
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { FormDataUtil, ObjectUtil } from 'azure-net-tools';
|
|
2
|
+
export class SchemaFail {
|
|
3
|
+
_errors;
|
|
4
|
+
constructor(_errors) {
|
|
5
|
+
this._errors = _errors;
|
|
6
|
+
}
|
|
7
|
+
getErrors() {
|
|
8
|
+
return this._errors;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class SchemaBuilderImpl {
|
|
12
|
+
_rulesFactory;
|
|
13
|
+
_rules;
|
|
14
|
+
_transform;
|
|
15
|
+
_customMethods;
|
|
16
|
+
constructor(_rulesFactory = {}) {
|
|
17
|
+
this._rulesFactory = _rulesFactory;
|
|
18
|
+
}
|
|
19
|
+
rules(callback) {
|
|
20
|
+
this._rules = callback;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
transform(callback) {
|
|
24
|
+
this._transform = callback;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
with(callback) {
|
|
28
|
+
this._customMethods = callback;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
create() {
|
|
32
|
+
const rules = this._rules;
|
|
33
|
+
const transform = this._transform || ((data) => data);
|
|
34
|
+
const customMethods = this._customMethods ? this._customMethods() : {};
|
|
35
|
+
const rulesFactory = this._rulesFactory;
|
|
36
|
+
const prepare = (dataToPrepare) => {
|
|
37
|
+
if (dataToPrepare instanceof FormData) {
|
|
38
|
+
return FormDataUtil.toObject(dataToPrepare);
|
|
39
|
+
}
|
|
40
|
+
return ObjectUtil.deepClone(dataToPrepare);
|
|
41
|
+
};
|
|
42
|
+
const getByPath = (path, data) => {
|
|
43
|
+
return path.split('.').reduce((o, k) => (o && typeof o === 'object' ? o[k] : undefined), data);
|
|
11
44
|
};
|
|
12
45
|
const setByPath = (obj, path, value) => {
|
|
13
46
|
const keys = path.split('.');
|
|
@@ -21,58 +54,70 @@ export const createSchemaFactory = (rulesList) => {
|
|
|
21
54
|
}
|
|
22
55
|
current[keys[(keys.length - 1)]] = value;
|
|
23
56
|
};
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
};
|
|
30
|
-
const validate = () => {
|
|
31
|
-
_isValid = true;
|
|
32
|
-
_errors = {};
|
|
33
|
-
if (typeof schemaData !== 'object') {
|
|
34
|
-
throw Error('Data to validate is not an object');
|
|
35
|
-
}
|
|
36
|
-
for (const key in definedSchema) {
|
|
37
|
-
const fieldRules = definedSchema[key] ?? [];
|
|
38
|
-
const value = getByPath(key);
|
|
39
|
-
for (const rule of fieldRules) {
|
|
40
|
-
const failMessage = rule({
|
|
41
|
-
val: value,
|
|
42
|
-
listValues: schemaData,
|
|
43
|
-
key: key
|
|
44
|
-
});
|
|
45
|
-
if (failMessage) {
|
|
46
|
-
setByPath(_errors, key, failMessage);
|
|
47
|
-
_isValid = false;
|
|
48
|
-
break;
|
|
49
|
-
}
|
|
57
|
+
const schema = {
|
|
58
|
+
...customMethods,
|
|
59
|
+
getSchemaError(e) {
|
|
60
|
+
if (e instanceof SchemaFail) {
|
|
61
|
+
return e.getErrors();
|
|
50
62
|
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
63
|
+
},
|
|
64
|
+
from(data) {
|
|
65
|
+
const _preparedData = prepare(data);
|
|
66
|
+
let _errors = {};
|
|
67
|
+
let _isValid = true;
|
|
68
|
+
if (typeof _preparedData !== 'object') {
|
|
69
|
+
throw Error('Data to validate is not an object');
|
|
70
|
+
}
|
|
71
|
+
const validated = () => {
|
|
72
|
+
if (!rules)
|
|
73
|
+
return true;
|
|
74
|
+
_isValid = true;
|
|
75
|
+
_errors = {};
|
|
76
|
+
const definedSchema = rules(rulesFactory);
|
|
77
|
+
for (const key in definedSchema) {
|
|
78
|
+
const fieldRules = definedSchema[key] ?? [];
|
|
79
|
+
const value = getByPath(key, _preparedData);
|
|
80
|
+
for (const rule of fieldRules) {
|
|
81
|
+
const failMessage = rule({
|
|
82
|
+
val: value,
|
|
83
|
+
listValues: _preparedData,
|
|
84
|
+
key: key
|
|
85
|
+
});
|
|
86
|
+
if (failMessage) {
|
|
87
|
+
setByPath(_errors, key, failMessage);
|
|
88
|
+
_isValid = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (!_isValid) {
|
|
94
|
+
throw new SchemaFail(_errors);
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
};
|
|
98
|
+
const json = () => {
|
|
99
|
+
validated();
|
|
100
|
+
return transform(_preparedData);
|
|
101
|
+
};
|
|
102
|
+
const formData = () => {
|
|
103
|
+
validated();
|
|
104
|
+
return FormDataUtil.fromObject(transform(_preparedData));
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
json,
|
|
108
|
+
formData,
|
|
109
|
+
validated
|
|
110
|
+
};
|
|
75
111
|
}
|
|
76
112
|
};
|
|
113
|
+
return schema;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export const createSchemaFactory = (rules) => {
|
|
117
|
+
return () => {
|
|
118
|
+
return new SchemaBuilderImpl(rules);
|
|
77
119
|
};
|
|
78
120
|
};
|
|
121
|
+
export const schema = () => {
|
|
122
|
+
return new SchemaBuilderImpl();
|
|
123
|
+
};
|
package/dist/core/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './delivery/index.js';
|
|
2
|
-
export * from './shared/request/index.js';
|
|
3
2
|
export * from './shared/middleware/index.js';
|
|
4
3
|
export * from './shared/classMirror/index.js';
|
|
5
4
|
export * from './shared/cookie/index.js';
|
|
6
5
|
export * from './shared/boundaryProvider/index.js';
|
|
7
6
|
export * from './ui/index.js';
|
|
7
|
+
export * from './svelte/index.js';
|
package/dist/core/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from './delivery/index.js';
|
|
2
|
-
export * from './shared/request/index.js';
|
|
3
2
|
export * from './shared/middleware/index.js';
|
|
4
3
|
export * from './shared/classMirror/index.js';
|
|
5
4
|
export * from './shared/cookie/index.js';
|
|
6
5
|
export * from './shared/boundaryProvider/index.js';
|
|
7
6
|
export * from './ui/index.js';
|
|
7
|
+
export * from './svelte/index.js';
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { error, type Page, type RequestEvent } from '@sveltejs/kit';
|
|
2
2
|
import { UniversalCookie } from '../cookie/index.js';
|
|
3
|
+
type RedirectStatus = 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308;
|
|
3
4
|
export type IMiddleware = (middlewareData: {
|
|
4
5
|
to: RequestEvent['url'] | Page['url'];
|
|
5
6
|
from?: RequestEvent['url'] | Page['url'];
|
|
6
7
|
error: typeof error;
|
|
7
|
-
next: (location?: string | URL, status?:
|
|
8
|
+
next: (location?: string | URL, status?: RedirectStatus) => void;
|
|
8
9
|
isServer: boolean;
|
|
9
|
-
cookies: UniversalCookie;
|
|
10
|
+
cookies: typeof UniversalCookie;
|
|
10
11
|
event?: RequestEvent;
|
|
11
12
|
page: Page;
|
|
12
13
|
}) => Promise<void> | void;
|
|
@@ -14,3 +15,4 @@ export declare const createMiddlewareManager: (middlewares: IMiddleware[]) => {
|
|
|
14
15
|
serverMiddleware: () => Promise<void>;
|
|
15
16
|
clientMiddleware: () => void;
|
|
16
17
|
};
|
|
18
|
+
export {};
|
|
@@ -4,16 +4,16 @@ import { beforeNavigate, goto } from '$app/navigation';
|
|
|
4
4
|
import { page } from '$app/state';
|
|
5
5
|
import { EnvironmentUtil } from 'azure-net-tools';
|
|
6
6
|
import { UniversalCookie } from '../cookie/index.js';
|
|
7
|
-
const universalRedirect = (location, status = 301, navigation) => {
|
|
8
|
-
if (EnvironmentUtil.isBrowser && navigation) {
|
|
9
|
-
navigation?.cancel();
|
|
10
|
-
return goto(location);
|
|
11
|
-
}
|
|
12
|
-
else {
|
|
13
|
-
return redirect(status, location);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
7
|
export const createMiddlewareManager = (middlewares) => {
|
|
8
|
+
const universalRedirect = (location, status = 301, navigation) => {
|
|
9
|
+
if (EnvironmentUtil.isBrowser && navigation) {
|
|
10
|
+
navigation?.cancel();
|
|
11
|
+
return goto(location);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return redirect(status, location);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
17
|
const executeMiddlewares = async (navigation) => {
|
|
18
18
|
let event;
|
|
19
19
|
let from = undefined;
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import { type RequestErrors, BaseRequest } from '../../core/index.js';
|
|
2
1
|
export interface FormConfig<T> {
|
|
3
2
|
onSubmit: (data: T) => Promise<void> | void;
|
|
4
3
|
initialData?: T;
|
|
5
4
|
validation?: {
|
|
6
|
-
schema?: new (data: Partial<T>) => BaseRequest<T, T>;
|
|
7
5
|
validateOnChange?: boolean;
|
|
8
6
|
validateOnBlur?: boolean;
|
|
9
7
|
};
|
|
10
8
|
}
|
|
11
9
|
export interface ActiveForm<T> {
|
|
12
10
|
data: Partial<T>;
|
|
13
|
-
errors: RequestErrors<T>;
|
|
14
11
|
touched: Partial<Record<keyof T, boolean>>;
|
|
15
12
|
pending: boolean;
|
|
16
13
|
dirty: boolean;
|
|
@@ -19,6 +16,5 @@ export interface ActiveForm<T> {
|
|
|
19
16
|
validate: (field?: keyof T) => void;
|
|
20
17
|
reset: () => void;
|
|
21
18
|
setField: <K extends keyof T>(field: K, value: T[K]) => void;
|
|
22
|
-
setErrors: (errors: RequestErrors<T>) => void;
|
|
23
19
|
touchField: (field: keyof T) => void;
|
|
24
20
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
|
2
2
|
// export function createActiveForm<T extends object>(config: FormConfig<T>): ActiveForm<T> {
|
|
3
3
|
// const initial: Partial<T> = config.initialData ?? {};
|
|
4
4
|
// let data = $state<Partial<T>>(structuredClone(initial));
|
package/dist/core/ui/index.d.ts
CHANGED
package/dist/core/ui/index.js
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type TokenSettings = {
|
|
2
|
+
pattern?: RegExp;
|
|
3
|
+
escape?: boolean;
|
|
4
|
+
transform?: (v: string) => string;
|
|
5
|
+
};
|
|
6
|
+
export type Tokens = Record<string, TokenSettings>;
|
|
7
|
+
export declare class Mask {
|
|
8
|
+
static tokens: Tokens;
|
|
9
|
+
static dynamicMask(maskIt: typeof Mask.maskIt, masks: string[], tokens: Tokens): (value: string, masked?: boolean) => string;
|
|
10
|
+
static maskIt(value: string, mask: string | undefined, masked: boolean | undefined, tokens: Tokens): string;
|
|
11
|
+
static masker(value: string, mask: string | undefined, masked: boolean | undefined, tokens: Tokens): string;
|
|
12
|
+
}
|
|
13
|
+
export declare const masked: (el: HTMLInputElement, mask?: string, tokens?: Tokens) => void;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
export class Mask {
|
|
2
|
+
static tokens = {
|
|
3
|
+
X: { pattern: /[0-9a-zA-Z]/ },
|
|
4
|
+
S: { pattern: /[a-zA-Z]/ },
|
|
5
|
+
A: { pattern: /[a-zA-Z]/, transform: (v) => v.toLocaleUpperCase() },
|
|
6
|
+
a: { pattern: /[a-zA-Z]/, transform: (v) => v.toLocaleLowerCase() },
|
|
7
|
+
'#': { pattern: /\d/ },
|
|
8
|
+
'!': { escape: true }
|
|
9
|
+
};
|
|
10
|
+
static dynamicMask(maskIt, masks, tokens) {
|
|
11
|
+
masks = masks.sort((a, b) => a.length - b.length);
|
|
12
|
+
return function (value, masked = true) {
|
|
13
|
+
let i = 0;
|
|
14
|
+
while (i < masks.length) {
|
|
15
|
+
const currentMask = masks[i];
|
|
16
|
+
i++;
|
|
17
|
+
const nextMask = masks[i];
|
|
18
|
+
if (!(nextMask && maskIt(value, nextMask, true, tokens).length > currentMask.length)) {
|
|
19
|
+
return maskIt(value, currentMask, masked, tokens);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return '';
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
static maskIt(value, mask = undefined, masked = true, tokens) {
|
|
26
|
+
value = value || '';
|
|
27
|
+
mask = mask || '';
|
|
28
|
+
let iMask = 0;
|
|
29
|
+
let iValue = 0;
|
|
30
|
+
let output = '';
|
|
31
|
+
while (iMask < mask.length && iValue < value.length) {
|
|
32
|
+
let cMask = mask[iMask];
|
|
33
|
+
const masker = tokens[cMask];
|
|
34
|
+
const cValue = value[iValue];
|
|
35
|
+
if (masker && !masker.escape) {
|
|
36
|
+
if (masker.pattern?.test(cValue)) {
|
|
37
|
+
output += masker.transform ? masker.transform(cValue) : cValue;
|
|
38
|
+
iMask++;
|
|
39
|
+
}
|
|
40
|
+
iValue++;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (masker && masker.escape) {
|
|
44
|
+
iMask++;
|
|
45
|
+
cMask = mask[iMask];
|
|
46
|
+
}
|
|
47
|
+
if (masked)
|
|
48
|
+
output += cMask;
|
|
49
|
+
if (cValue === cMask)
|
|
50
|
+
iValue++;
|
|
51
|
+
iMask++;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
let restOutput = '';
|
|
55
|
+
while (iMask < mask.length && masked) {
|
|
56
|
+
const cMask = mask[iMask];
|
|
57
|
+
if (tokens[cMask]) {
|
|
58
|
+
restOutput = '';
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
restOutput += cMask;
|
|
62
|
+
iMask++;
|
|
63
|
+
}
|
|
64
|
+
return output + restOutput;
|
|
65
|
+
}
|
|
66
|
+
static masker(value, mask = undefined, masked = true, tokens) {
|
|
67
|
+
return Array.isArray(mask) ? this.dynamicMask(Mask.maskIt, mask, tokens)(value, masked) : this.maskIt(value, mask, masked, tokens);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export const masked = (el, mask, tokens) => {
|
|
71
|
+
const config = {
|
|
72
|
+
mask,
|
|
73
|
+
tokens: tokens ? { ...Mask.tokens, ...tokens } : Mask.tokens
|
|
74
|
+
};
|
|
75
|
+
if (el.tagName.toLocaleUpperCase() !== 'INPUT') {
|
|
76
|
+
const els = el.getElementsByTagName('input');
|
|
77
|
+
if (els.length !== 1) {
|
|
78
|
+
throw new Error('mask requires 1 input, found ' + els.length);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
el = els[0];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (config.mask) {
|
|
85
|
+
el.oninput = function (evt) {
|
|
86
|
+
if (!evt.isTrusted)
|
|
87
|
+
return;
|
|
88
|
+
let position = el.selectionEnd;
|
|
89
|
+
if (position) {
|
|
90
|
+
const digit = el.value[position - 1];
|
|
91
|
+
el.value = Mask.masker(el.value, config.mask, true, config.tokens);
|
|
92
|
+
while (position < el.value.length && el.value.charAt(position - 1) !== digit) {
|
|
93
|
+
position++;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
if (el === document.activeElement) {
|
|
97
|
+
el.setSelectionRange(position, position);
|
|
98
|
+
setTimeout(function () {
|
|
99
|
+
el.setSelectionRange(position, position);
|
|
100
|
+
}, 0);
|
|
101
|
+
}
|
|
102
|
+
el.dispatchEvent(new Event('input'));
|
|
103
|
+
};
|
|
104
|
+
const newDisplay = Mask.masker(el.value, config.mask, true, config.tokens);
|
|
105
|
+
if (newDisplay !== el.value) {
|
|
106
|
+
el.value = newDisplay;
|
|
107
|
+
el.dispatchEvent(new Event('input'));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Mask.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './Mask.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure-net/kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"dist",
|
|
6
6
|
"!dist/**/*.test.*",
|
|
@@ -21,17 +21,9 @@
|
|
|
21
21
|
"types": "./dist/core/infra/index.d.ts",
|
|
22
22
|
"svelte": "./dist/core/infra/index.js"
|
|
23
23
|
},
|
|
24
|
-
"./
|
|
25
|
-
"types": "./dist/
|
|
26
|
-
"svelte": "./dist/
|
|
27
|
-
},
|
|
28
|
-
"./request/rules": {
|
|
29
|
-
"types": "./dist/core/shared/request/rules/index.d.ts",
|
|
30
|
-
"svelte": "./dist/core/shared/request/rules/index.js"
|
|
31
|
-
},
|
|
32
|
-
"./request/messages": {
|
|
33
|
-
"types": "./dist/core/shared/request/rules/messages/index.d.ts",
|
|
34
|
-
"svelte": "./dist/core/shared/request/rules/messages/index.js"
|
|
24
|
+
"./schema": {
|
|
25
|
+
"types": "./dist/core/delivery/schema/rules/index.d.ts",
|
|
26
|
+
"svelte": "./dist/core/delivery/schema/rules/index.js"
|
|
35
27
|
},
|
|
36
28
|
"./edges": {
|
|
37
29
|
"types": "./dist/edges/index.d.ts",
|