/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import type { SqlExpression } from 'druid-query-toolkit'; import type { Host } from './host'; import type { UpdateEvent } from './host-store'; import type { Parameter, ParameterDefinitions, ParameterTypes } from './parameter'; type ExtractParameters
= P[Name] extends Parameter ? Name : never; // If the parameter has a default value, use that, otherwise include undefined type ParameterValueType
= P extends { default: infer V }
? V
: ParameterTypes[P['type']] | undefined;
export type ParametersToParams {
/**
* Parameters that this module accepts.
*/
parameters: P | undefined;
/**
* Order of parameters for display purposes.
*
* Omitting a parameter from this list _may_ cause it to be hidden from the UI,
* depending on the host implementation.
*/
parameterOrder: (keyof P)[] | undefined;
/**
* Creates a new instance of this module.
*/
(context: VisualModuleContext
>;
}
type ParameterizedModuleFactory = (
context: VisualModuleContext {
/**
* The visual module instance factory.
*/
module: ParameterizedModuleFactory ;
/**
* Parameters that this module accepts.
*/
parameters?: P & ParameterDefinitions;
/**
* Order of parameters for display purposes.
*
* Omitting a parameter from this list _may_ cause it to be hidden from the UI,
* depending on the host implementation.
*/
parameterOrder?: (keyof P)[];
}
/**
* Defines a new visual module.
*
* This is a convenience function for defining a module with strongly typed parameters
* inferred from the parameter definitions.
*/
export function typedVisualModule ({
module: factory,
parameters,
parameterOrder,
}: TypedVisualModuleOptions ): VisualModule {
if (typeof factory !== 'function') {
throw new Error('Expected "module" to be a function');
}
if (parameterOrder) {
const seenParams = new Set = context => {
return factory(context);
};
result.parameters = parameters;
result.parameterOrder = parameterOrder;
return result;
}