easy-api.ts
Version:
A powerful library to create your own API with ease.
101 lines (100 loc) • 3.01 kB
TypeScript
import { CompiledFunction } from "../internal/CompiledFunction";
import { Data } from "./Data";
/**
* Represents a function parameter type.
*/
export declare enum ParamType {
/**
* A data type that can represent whole numbers larger than those representable by the Number type.
*/
BigInt = 0,
/**
* A data type that can be either true or false.
*/
Boolean = 1,
/**
* A data type that represents color values. This can be in formats like hex, RGB, HSL, etc.
*/
Color = 2,
/**
* A data type that represents a date and time.
* Any value that can be passed to JavaScript's `new Date()` is accepted.
*/
Date = 3,
/**
* A data type that represents data in JavaScript Object Notation (JSON) format.
*/
JSON = 4,
/**
* An object that is similar to an array. Any object with indexed properties.
*/
List = 5,
/**
* A valid numeric expression.
*/
Number = 6,
/**
* A data type that represents a sequence of characters.
*/
String = 7,
/**
* A data type that represents a time of day or a duration.
* Should be a valid millisecond number or valid string parseable to time.
*/
Time = 8,
/**
* A data type that can be any.
*/
Unknown = 9
}
/**
* Represents a function parameter definition.
*/
export interface ParameterDefinition<Type extends ParamType = ParamType, Required extends boolean = boolean, Rest extends boolean = boolean> {
/**
* The name for this parameter.
*/
name: string;
/**
* A short description about what this parameter does.
*/
description: string;
/**
* The type for this parameter.
*/
type: Type;
/**
* Whether this parameter is required.
*/
required: Required;
/**
* Whether this parameter is rest.
*/
rest: Rest;
/**
* Parameter default value, if any.
*/
defaultValue: string | null;
/**
* Possible strict allowed values.
*/
allowedValues?: string[];
}
export type AsRest<T, B extends boolean = boolean> = B extends true ? T[] : T;
export type CompileParam<T extends ParamType> = T extends ParamType.BigInt ? bigint : T extends ParamType.Boolean ? boolean : T extends ParamType.Color ? string : T extends ParamType.Date ? Date : T extends ParamType.JSON ? Record<any, any> : T extends ParamType.List ? any[] : T extends ParamType.Number ? number : T extends ParamType.String ? string : T extends ParamType.Time ? ParamType.Time : ParamType.Unknown;
/**
* Represents an easy-api.ts native function.
*/
export declare class APIFunction {
name: string;
description: string;
usage: string;
parameters: ParameterDefinition[] | readonly ParameterDefinition[];
returns: ParamType;
compile: boolean;
aliases?: string[];
run(this: CompiledFunction, d: Data, args: any[]): Promise<any>;
parent?: APIFunction;
example?: string;
init(): void;
}