chore: small refactor and fixing some of the types

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-07-24 13:40:43 +02:00
parent 3fb421d1b0
commit e967332785
4 changed files with 122 additions and 76 deletions
+22 -29
View File
@@ -513,7 +513,7 @@ export function createExtension<
TInputs extends AnyExtensionInputMap,
TConfig,
TConfigSchema extends {
[key: string]: z.ZodType;
[key: string]: (zImpl: typeof z) => z.ZodType;
},
>(
options: CreateExtensionOptions<TOutput, TInputs, TConfig, TConfigSchema>,
@@ -525,7 +525,7 @@ export function createExtensionBlueprint<
TInputs extends AnyExtensionInputMap,
TOutput extends AnyExtensionDataMap,
TConfigSchema extends {
[key in string]: z.ZodType;
[key in string]: (zImpl: typeof z) => z.ZodType;
},
TDataRefs extends AnyExtensionDataMap = never,
>(
@@ -540,9 +540,11 @@ export function createExtensionBlueprint<
TParams,
TInputs,
TOutput,
{
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
},
string extends keyof TConfigSchema
? {}
: {
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
},
TDataRefs
>;
@@ -552,7 +554,7 @@ export interface CreateExtensionBlueprintOptions<
TInputs extends AnyExtensionInputMap,
TOutput extends AnyExtensionDataMap,
TConfigSchema extends {
[key in string]: z.ZodType;
[key in string]: (zImpl: typeof z) => z.ZodType;
},
TDataRefs extends AnyExtensionDataMap,
> {
@@ -563,9 +565,7 @@ export interface CreateExtensionBlueprintOptions<
};
// (undocumented)
config?: {
schema: {
[key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key];
};
schema: TConfigSchema;
};
// (undocumented)
dataRefs?: TDataRefs;
@@ -577,7 +577,7 @@ export interface CreateExtensionBlueprintOptions<
context: {
node: AppNode;
config: {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
},
@@ -628,7 +628,7 @@ export interface CreateExtensionOptions<
TInputs extends AnyExtensionInputMap,
TConfig,
TConfigSchema extends {
[key: string]: z.ZodType;
[key: string]: (zImpl: typeof z) => z.ZodType;
},
> {
// (undocumented)
@@ -638,9 +638,7 @@ export interface CreateExtensionOptions<
};
// (undocumented)
config?: {
schema: {
[key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key];
};
schema: TConfigSchema;
};
// @deprecated (undocumented)
configSchema?: PortableSchema<TConfig>;
@@ -650,7 +648,7 @@ export interface CreateExtensionOptions<
factory(context: {
node: AppNode;
config: TConfig & {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
}): Expand<ExtensionDataValues<TOutput>>;
@@ -967,7 +965,7 @@ export interface ExtensionBlueprint<
dataRefs: TDataRefs;
make<
TExtensionConfigSchema extends {
[key in string]: z.ZodType;
[key in string]: (zImpl: typeof z) => z.ZodType;
},
>(
args: {
@@ -981,12 +979,9 @@ export interface ExtensionBlueprint<
inputs?: TInputs;
output?: TOutput;
config?: {
schema: {
[key in keyof TExtensionConfigSchema]: (
zImpl: typeof z,
) => TExtensionConfigSchema[key];
} & {
[key in keyof TConfig]?: never;
schema: TExtensionConfigSchema & {
[KName in keyof TConfig]?: `Error: Config key '${KName &
string}' is already defined in parent schema`;
};
};
} & (
@@ -1001,11 +996,11 @@ export interface ExtensionBlueprint<
) => Expand<ExtensionDataValues<TOutput>>,
context: {
node: AppNode;
config: {
config: TConfig & {
[key in keyof TExtensionConfigSchema]: z.infer<
TExtensionConfigSchema[key]
ReturnType<TExtensionConfigSchema[key]>
>;
} & TConfig;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
},
): Expand<ExtensionDataValues<TOutput>>;
@@ -1017,7 +1012,7 @@ export interface ExtensionBlueprint<
): ExtensionDefinition<
{
[key in keyof TExtensionConfigSchema]: z.infer<
TExtensionConfigSchema[key]
ReturnType<TExtensionConfigSchema[key]>
>;
} & TConfig
>;
@@ -1175,9 +1170,7 @@ export const IconBundleBlueprint: ExtensionBlueprint<
{}
>;
},
{
[x: string]: any;
},
{},
{
icons: ConfigurableExtensionDataRef<
'core.icons',
@@ -82,7 +82,7 @@ export interface CreateExtensionOptions<
TOutput extends AnyExtensionDataMap,
TInputs extends AnyExtensionInputMap,
TConfig,
TConfigSchema extends { [key: string]: z.ZodType },
TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodType },
> {
kind?: string;
namespace?: string;
@@ -94,14 +94,12 @@ export interface CreateExtensionOptions<
/** @deprecated - use `config.schema` instead */
configSchema?: PortableSchema<TConfig>;
config?: {
schema: {
[key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key];
};
schema: TConfigSchema;
};
factory(context: {
node: AppNode;
config: TConfig & {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
}): Expand<ExtensionDataValues<TOutput>>;
@@ -154,7 +152,7 @@ export function createExtension<
TOutput extends AnyExtensionDataMap,
TInputs extends AnyExtensionInputMap,
TConfig,
TConfigSchema extends { [key: string]: z.ZodType },
TConfigSchema extends { [key: string]: (zImpl: typeof z) => z.ZodType },
>(
options: CreateExtensionOptions<TOutput, TInputs, TConfig, TConfigSchema>,
): ExtensionDefinition<TConfig> {
@@ -188,7 +186,7 @@ export function createExtension<
return options.factory({
inputs: inputs as Expand<ResolvedExtensionInputs<TInputs>>,
config: config as TConfig & {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
},
...rest,
});
@@ -225,4 +225,53 @@ describe('createExtensionBlueprint', () => {
expect('test').toBe('test');
});
it('should allow setting config when one was not already defined in the blueprint', () => {
const TestExtensionBlueprint = createExtensionBlueprint({
kind: 'test-extension',
attachTo: { id: 'test', input: 'default' },
output: {
element: coreExtensionData.reactElement,
},
factory(_, { config }) {
// @ts-expect-error
const b = config.something;
return {
element: <div />,
};
},
});
const extension = TestExtensionBlueprint.make({
name: 'my-extension',
params: {
text: 'Hello, world!',
},
config: {
schema: {
something: z => z.string(),
defaulted: z => z.string().optional().default('default'),
},
},
factory(origFactory, { config }) {
const b: string = config.something;
unused(b);
expect(config.something).toBe('something new!');
expect(config.defaulted).toBe('lolz');
return origFactory({});
},
});
expect.assertions(2);
createExtensionTester(extension, {
config: {
something: 'something new!',
defaulted: 'lolz',
},
}).render();
});
});
@@ -33,7 +33,7 @@ export interface CreateExtensionBlueprintOptions<
TParams,
TInputs extends AnyExtensionInputMap,
TOutput extends AnyExtensionDataMap,
TConfigSchema extends { [key in string]: z.ZodType },
TConfigSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType },
TDataRefs extends AnyExtensionDataMap,
> {
kind: string;
@@ -43,16 +43,14 @@ export interface CreateExtensionBlueprintOptions<
inputs?: TInputs;
output: TOutput;
config?: {
schema: {
[key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key];
};
schema: TConfigSchema;
};
factory(
params: TParams,
context: {
node: AppNode;
config: {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
},
@@ -79,7 +77,11 @@ export interface ExtensionBlueprint<
* You must either pass `params` directly, or define a `factory` that can
* optionally call the original factory with the same params.
*/
make<TExtensionConfigSchema extends { [key in string]: z.ZodType }>(
make<
TExtensionConfigSchema extends {
[key in string]: (zImpl: typeof z) => z.ZodType;
},
>(
args: {
namespace?: string;
name?: string;
@@ -88,12 +90,9 @@ export interface ExtensionBlueprint<
inputs?: TInputs;
output?: TOutput;
config?: {
schema: {
[key in keyof TExtensionConfigSchema]: (
zImpl: typeof z,
) => TExtensionConfigSchema[key];
} & {
[key in keyof TConfig]?: never;
schema: TExtensionConfigSchema & {
[KName in keyof TConfig]?: `Error: Config key '${KName &
string}' is already defined in parent schema`;
};
};
} & (
@@ -108,11 +107,11 @@ export interface ExtensionBlueprint<
) => Expand<ExtensionDataValues<TOutput>>,
context: {
node: AppNode;
config: {
config: TConfig & {
[key in keyof TExtensionConfigSchema]: z.infer<
TExtensionConfigSchema[key]
ReturnType<TExtensionConfigSchema[key]>
>;
} & TConfig;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
},
): Expand<ExtensionDataValues<TOutput>>;
@@ -124,7 +123,7 @@ export interface ExtensionBlueprint<
): ExtensionDefinition<
{
[key in keyof TExtensionConfigSchema]: z.infer<
TExtensionConfigSchema[key]
ReturnType<TExtensionConfigSchema[key]>
>;
} & TConfig
>;
@@ -137,7 +136,7 @@ class ExtensionBlueprintImpl<
TParams,
TInputs extends AnyExtensionInputMap,
TOutput extends AnyExtensionDataMap,
TConfigSchema extends { [key in string]: z.ZodType },
TConfigSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType },
TDataRefs extends AnyExtensionDataMap,
> {
constructor(
@@ -155,7 +154,9 @@ class ExtensionBlueprintImpl<
dataRefs: TDataRefs;
public make<
TExtensionConfigSchema extends { [key in string]: z.ZodType },
TExtensionConfigSchema extends {
[key in string]: (zImpl: typeof z) => z.ZodType;
},
>(args: {
namespace?: string;
name?: string;
@@ -165,18 +166,16 @@ class ExtensionBlueprintImpl<
output?: TOutput;
params?: TParams;
config?: {
schema: {
[key in keyof TExtensionConfigSchema]: (
zImpl: typeof z,
) => TExtensionConfigSchema[key];
};
schema: TExtensionConfigSchema;
};
factory?(
originalFactory: (
params: TParams,
context?: {
config?: {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<
ReturnType<TConfigSchema[key]>
>;
};
inputs?: Expand<ResolvedExtensionInputs<TInputs>>;
},
@@ -185,10 +184,10 @@ class ExtensionBlueprintImpl<
node: AppNode;
config: {
[key in keyof TExtensionConfigSchema]: z.infer<
TExtensionConfigSchema[key]
ReturnType<TExtensionConfigSchema[key]>
>;
} & {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
};
inputs: Expand<ResolvedExtensionInputs<TInputs>>;
},
@@ -196,23 +195,16 @@ class ExtensionBlueprintImpl<
}): ExtensionDefinition<
{
[key in keyof TExtensionConfigSchema]: z.infer<
TExtensionConfigSchema[key]
ReturnType<TExtensionConfigSchema[key]>
>;
} & {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
}
> {
const schema = {
...this.options.config?.schema,
...args.config?.schema,
} as {
[key in keyof TConfigSchema]: (zImpl: typeof z) => TConfigSchema[key];
} & {
[key in keyof TExtensionConfigSchema]: (
zImpl: typeof z,
) => TExtensionConfigSchema[key];
};
} as TConfigSchema & TExtensionConfigSchema;
return createExtension({
kind: this.options.kind,
namespace: args.namespace ?? this.options.namespace,
@@ -229,7 +221,9 @@ class ExtensionBlueprintImpl<
innerParams: TParams,
innerContext?: {
config?: {
[key in keyof TConfigSchema]: z.infer<TConfigSchema[key]>;
[key in keyof TConfigSchema]: z.infer<
ReturnType<TConfigSchema[key]>
>;
};
inputs?: Expand<ResolvedExtensionInputs<TInputs>>;
},
@@ -268,7 +262,7 @@ export function createExtensionBlueprint<
TParams,
TInputs extends AnyExtensionInputMap,
TOutput extends AnyExtensionDataMap,
TConfigSchema extends { [key in string]: z.ZodType },
TConfigSchema extends { [key in string]: (zImpl: typeof z) => z.ZodType },
TDataRefs extends AnyExtensionDataMap = never,
>(
options: CreateExtensionBlueprintOptions<
@@ -282,8 +276,20 @@ export function createExtensionBlueprint<
TParams,
TInputs,
TOutput,
{ [key in keyof TConfigSchema]: z.infer<TConfigSchema[key]> },
string extends keyof TConfigSchema
? {}
: { [key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>> },
TDataRefs
> {
return new ExtensionBlueprintImpl(options);
return new ExtensionBlueprintImpl(options) as ExtensionBlueprint<
TParams,
TInputs,
TOutput,
string extends keyof TConfigSchema
? {}
: {
[key in keyof TConfigSchema]: z.infer<ReturnType<TConfigSchema[key]>>;
},
TDataRefs
>;
}