@matreshka/bff 0.30.0 → 0.30.2
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.
|
@@ -24,11 +24,9 @@ export type ContextClientUpdatesForbiddenSchema = z.ZodObject<Record<string, nev
|
|
|
24
24
|
export declare const contextClientUpdatesForbiddenSchema: ContextClientUpdatesForbiddenSchema;
|
|
25
25
|
type StripNullish<T> = Exclude<T, null | undefined>;
|
|
26
26
|
type ArrayElement<T> = T extends readonly (infer E)[] ? E : never;
|
|
27
|
-
/** `true`, если output допускает null/undefined как значение, а поле `T` — нет. */
|
|
28
|
-
type NullishValueMismatch<TField, IField> = (null extends IField ? (null extends TField ? never : true) : never) | (undefined extends IField ? undefined extends TField ? never : true : never);
|
|
29
27
|
type FieldExtraSchemaKeysOne<TField, IField> = [StripNullish<IField>] extends [
|
|
30
|
-
|
|
31
|
-
] ? [StripNullish<TField>] extends [
|
|
28
|
+
readonly unknown[]
|
|
29
|
+
] ? [StripNullish<TField>] extends [readonly unknown[]] ? ExtraSchemaKeys<ArrayElement<StripNullish<TField>>, ArrayElement<StripNullish<IField>>> : never : [StripNullish<IField>] extends [JsonObject] ? [StripNullish<TField>] extends [JsonObject] ? ExtraSchemaKeys<StripNullish<TField>, StripNullish<IField>> : never : never;
|
|
32
30
|
/** Лишние ключи внутри поля (union, optional/nullable, массивы объектов). */
|
|
33
31
|
type FieldExtraSchemaKeys<TField, IField> = IField extends any ? FieldExtraSchemaKeysOne<TField, IField> : never;
|
|
34
32
|
/** Лишние ключи для одной пары object-веток (коррелированные поля). `true` — есть лишние ключи. */
|
|
@@ -42,30 +40,17 @@ type SomeExtraSchemaKeysBranchOk<T, Input extends JsonObject> = T extends JsonOb
|
|
|
42
40
|
* Union в `Input` и `T` проверяется по отдельным веткам. `true` — есть лишние ключи.
|
|
43
41
|
*/
|
|
44
42
|
type ExtraSchemaKeys<T, Input> = Input extends JsonObject ? SomeExtraSchemaKeysBranchOk<T, Input> extends true ? never : true : never;
|
|
45
|
-
type
|
|
46
|
-
type
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
type FieldSchemaCompatible<TField, IField> = IField extends any ? FieldSchemaCompatibleOne<TField, IField> : never;
|
|
51
|
-
/** Совместимость для одной пары object-веток. `true` — несовместимость. */
|
|
52
|
-
type SchemaCompatibleObjectExact<T extends JsonObject, Input extends JsonObject> = Exclude<keyof Input, keyof T> extends never ? {
|
|
53
|
-
[K in keyof Input & keyof T]: FieldSchemaCompatible<T[K], Input[K]>;
|
|
54
|
-
}[keyof Input & keyof T] extends never ? never : true : true;
|
|
55
|
-
type SomeSchemaCompatibleBranchOk<T, Input extends JsonObject> = T extends JsonObject ? SchemaCompatibleObjectExact<T, Input> extends never ? true : never : never;
|
|
56
|
-
/**
|
|
57
|
-
* Типы полей shape схемы совместимы с `T` (рекурсивно; для клиентской схемы — {@link z.output}).
|
|
58
|
-
* Подмножество ключей на любой глубине; union — по веткам. `true` — несовместимость.
|
|
59
|
-
*/
|
|
60
|
-
type SchemaCompatible<T, Input> = Input extends JsonObject ? SomeSchemaCompatibleBranchOk<T, Input> extends true ? never : true : never;
|
|
43
|
+
type SchemaInputKeysSubset<T extends JsonObject, S extends z.ZodObject> = Exclude<keyof z.input<S>, keyof T> extends never ? true : false;
|
|
44
|
+
type DeepPartial<T> = T extends readonly (infer E)[] ? readonly DeepPartial<E>[] : T extends object ? {
|
|
45
|
+
[K in keyof T]?: DeepPartial<T[K]>;
|
|
46
|
+
} : T;
|
|
47
|
+
type SchemaOutputCompatible<T extends JsonObject, S extends z.ZodObject> = z.output<S> extends DeepPartial<T> ? true : false;
|
|
61
48
|
/**
|
|
62
49
|
* Zod-схема для {@link createContext}: без лишних ключей во {@link z.input} и {@link z.output}
|
|
63
50
|
* ({@link ExtraSchemaKeys}, union по веткам); {@link z.output} совместим с `T`
|
|
64
51
|
* ({@link SchemaCompatible}, вложенные подмножества). В контекст попадает output после `parse()`.
|
|
65
52
|
*/
|
|
66
|
-
export type ContextSchema<T extends JsonObject, S extends z.ZodObject> =
|
|
67
|
-
S
|
|
68
|
-
] extends [ContextClientUpdatesForbiddenSchema] ? S : ExtraSchemaKeys<T, z.input<S>> extends never ? ExtraSchemaKeys<T, z.output<S>> extends never ? SchemaCompatible<T, z.output<S>> extends never ? S : never : never : never;
|
|
53
|
+
export type ContextSchema<T extends JsonObject, S extends z.ZodObject> = SchemaOutputCompatible<T, S> extends false ? never : SchemaInputKeysSubset<T, S> extends false ? never : ExtraSchemaKeys<T, z.input<S>> extends never ? ExtraSchemaKeys<T, z.output<S>> extends never ? S : never : never;
|
|
69
54
|
/** Конфиг {@link createContext} без клиентской Zod-схемы. */
|
|
70
55
|
export type ContextInitWithoutSchema<T extends JsonObject> = {
|
|
71
56
|
data: () => Promise<T>;
|
|
@@ -103,7 +88,6 @@ export type ContextSchemaFromInit<C extends ContextInitShape> = C extends {
|
|
|
103
88
|
} ? C["schema"] : undefined;
|
|
104
89
|
/** Экземпляр {@link Context} по форме конфига {@link createContext}. */
|
|
105
90
|
export type ContextFromInit<C extends ContextInitShape> = Context<ContextDataFromInit<C>>;
|
|
106
|
-
type ValidatedContextInit<C extends ContextInitShape> = ContextInitConfig<ContextDataFromInit<C>, ContextSchemaFromInit<C> extends z.ZodObject ? ContextSchemaFromInit<C> : undefined>;
|
|
107
91
|
/**
|
|
108
92
|
* Контекст данных, связанных с клиентом.
|
|
109
93
|
*
|
|
@@ -287,9 +271,17 @@ export type AnyContext = Context<any>;
|
|
|
287
271
|
* Создаёт контекст данных BFF: хранилище состояния с синхронизацией клиент ↔ сервер.
|
|
288
272
|
*
|
|
289
273
|
* Тип данных выводится из `data`, тип Zod-схемы — из `schema`. Связь проверяется через
|
|
290
|
-
* {@link ContextSchema}.
|
|
274
|
+
* {@link ContextSchema}.
|
|
291
275
|
*
|
|
292
276
|
* Если в начальном `data` нет части полей (например опциональных), укажите тип на колбэке:
|
|
293
277
|
* `data: async (): Promise<MyT> => ({ … })`.
|
|
294
278
|
*/
|
|
295
|
-
|
|
279
|
+
type ContextWithSchemaInit = {
|
|
280
|
+
data: () => Promise<unknown>;
|
|
281
|
+
schema: z.ZodObject;
|
|
282
|
+
preload?: boolean;
|
|
283
|
+
persistent?: boolean;
|
|
284
|
+
};
|
|
285
|
+
type ContextDataForSchema<C extends ContextWithSchemaInit> = Awaited<ReturnType<C["data"]>> extends infer T ? T extends JsonObject ? T : never : never;
|
|
286
|
+
export declare function createContext<const C extends ContextWithSchemaInit>(config: C, ...validation: ContextSchema<ContextDataForSchema<C>, NoInfer<C["schema"]>> extends never ? [schemaMismatch: never] : []): Context<ContextDataForSchema<C>>;
|
|
287
|
+
export declare function createContext<const T extends JsonObject>(config: ContextInitWithoutSchema<T>): Context<T>;
|
|
@@ -573,15 +573,6 @@ class Context {
|
|
|
573
573
|
}
|
|
574
574
|
}
|
|
575
575
|
exports.Context = Context;
|
|
576
|
-
|
|
577
|
-
* Создаёт контекст данных BFF: хранилище состояния с синхронизацией клиент ↔ сервер.
|
|
578
|
-
*
|
|
579
|
-
* Тип данных выводится из `data`, тип Zod-схемы — из `schema`. Связь проверяется через
|
|
580
|
-
* {@link ContextSchema}. Generic-параметры у вызова не задаются.
|
|
581
|
-
*
|
|
582
|
-
* Если в начальном `data` нет части полей (например опциональных), укажите тип на колбэке:
|
|
583
|
-
* `data: async (): Promise<MyT> => ({ … })`.
|
|
584
|
-
*/
|
|
585
|
-
function createContext(config) {
|
|
576
|
+
function createContext(config, ..._validation) {
|
|
586
577
|
return new Context(config);
|
|
587
578
|
}
|
package/package.json
CHANGED