easy-api.ts
Version:
A powerful library to create your own API with ease.
192 lines (191 loc) • 5.53 kB
TypeScript
import { CompiledFunction } from "../internal/CompiledFunction";
import { FunctionManager } from "../managers/FunctionManager";
import { FastifyReply, FastifyRequest } from "fastify";
import { Interpreter } from "../core/Interpreter";
import { APIFunction } from "./APIFunction";
import { API } from "./API";
import { ReadStream } from "fs";
/**
* All options that belongs to the API itself.
*/
interface APIData {
/**
* Represents the API instance.
*/
server: API;
/**
* API incoming request.
*/
request: FastifyRequest | null;
/**
* API request reply.
*/
reply: FastifyReply | null;
/**
* API stream reply.
*/
stream: ReadStream | null;
}
export type ClassType = new (...args: any[]) => any;
export type ClassInstance<T> = T extends new (...args: any[]) => infer T ? T : never;
export declare class Data<P extends boolean = true> {
#private;
server: P extends true ? API : P extends false ? null : never;
break: boolean;
code: string | null;
comments: boolean;
functions: FunctionManager;
function: CompiledFunction;
interpreter: Interpreter;
parent: Data<P> | null;
req: P extends true ? FastifyRequest : P extends false ? null : never;
res: P extends true ? FastifyReply : P extends false ? null : never;
reverse: boolean;
startTime: number;
stream: P extends true ? ReadStream : P extends false ? null : never;
constructor(reverse: boolean, functions: Iterable<readonly [string, APIFunction]>, apiData: APIData | undefined, locale?: {
locale: Intl.LocalesArgument;
timeZone: string;
});
/**
* Deletes a variable from the cached data.
* @param name - Variable name.
* @returns {Data}
*/
deleteVar(name: string): this;
/**
* Deletes an internal variable from the cached data.
* @param name - Variable name.
* @returns {Data}
*/
deleteInternalVar(name: string): this;
/**
* Extend this parent data.
* @param vars - Variables to be added to this child data.
* @returns {Data}
*/
extend(vars?: Record<string, any>, internals?: Record<string, any>): Data<P>;
/**
* Check whether a internal variable is cached.
* @param name - Variable name.
* @returns {boolean}
*/
internalVarExists(name: string): boolean;
/**
* Overrides or add internal variables in this data.
* @param vars - Variables to override.
* @returns {Data}
*/
overrideInternals(internals: Record<string, any> | null): this;
/**
* Overrides or add variables in this data.
* @param vars - Variables to override.
* @returns {Data}
*/
overrideVars(vars: Record<string, any> | null): this;
/**
* Set a internal variable.
* @param name - Variable name.
* @param value - Variable value.
* @returns {Data}
*/
setInternalVar(name: string, value: any): this;
/**
* Set the date locale.
* @param tz - Locale to be set.
* @returns {Data}
*/
setLocale(locale: Intl.LocalesArgument): this;
/**
* Set the date timezone.
* @param tz - Timezone to be set.
* @returns {Data}
*/
setTimeZone(tz: string): this;
/**
* Set a variable in the cached data.
* @param name - Variable name.
* @param value - Variable value.
* @returns {Data}
*/
setVar(name: string, value: any): this;
/**
* Retrieves a internal variable.
* @param name - Variable name.
* @returns {T}
*/
getInternalVar<T>(name: string): T;
/**
* Retrieves a cached variable.
* @param name - Variable name.
* @returns {T}
*/
getVar<T>(name: string): T | null;
/**
* Set a parent data to this.
* @param parent - Parent data.
* @returns {Data}
*/
setParent(parent: Data<P>): this;
/**
* Set the code execution start time.
* @param time - Start time.
* @returns {Data}
*/
setStart(time: number): this;
/**
* Whether stop code execution.
* @param state - Stop state.
* @returns {Data}
*/
stopCode(state?: boolean): this;
/**
* Check whether a variable is cached.
* @param name - Variable name.
* @returns {boolean}
*/
varExists(name: string): boolean;
/**
* Check if the given class instance exists in the internal variables dataset.
* @param key - Variable name.
* @param type - Class type.
* @returns {boolean}
*/
hasInstance<K extends string, V extends ClassType>(key: K, type: V): this is this & {
[P in keyof {
bro: boolean;
} as K]: ClassInstance<V>;
};
/**
* Returns a class instance with typings.
* @param key - Class key.
* @param type - Class instance.
* @returns {unknown}
*/
getInstance<K extends string, T extends ClassType>(key: K, type: T): InstanceType<T> | null;
/**
* Returns the current date using local time.
*/
get date(): Date;
/**
* Whether the core has "rs" syntax enabled.
*/
get dots(): boolean;
/**
* Returns the system locale.
*/
get locale(): Intl.LocalesArgument;
/**
* Returns the system timezone.
*/
get timeZone(): string;
/**
* Returns the cached variables.
*/
get _(): Record<string, any>;
/**
* Returns the cached internal variables.
*/
get __(): Record<string, any>;
}
export {};