/* * Copyright (C) 2023-2025 Yomitan Authors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * This type describes the structure of an API surface. * It is effectively just an object containing a list of items which describe a basic API functionality. */ type ApiSurface = { [name: string]: ApiDescriptor; }; /** * This type describes the structure of a single API function. */ type ApiDescriptor = { /** The parameters for the function. If there are no parameters, `void` should be used. */ params: void | {[name: string]: unknown}; /** The return type for the function. */ return: unknown; }; /** * This type represents a mapping of an entire API surface to its handlers. */ type ApiHandlerSurface = { [name in ApiNames]: ApiHandler; }; /** * This type represents a single API map initializer. * Type safety is enforced by ensuring that the name and handler signature are valid. */ type ApiMapInitItem> = [ name: TName, handler: ApiHandler, ]; /** * This type represents a union of all API map initializers for a given surface. */ type ApiMapInitItemAny = {[key in ApiNames]: ApiMapInitItem}[ApiNames]; /** Base type for extra params, which is just a generic array. */ type ApiTExtraParams = unknown[]; /** Default type for extra params, which is an empty array. */ type ApiExtraParamsDefault = []; /** Type alias for the params member of a descriptor. */ export type ApiParams = TDescriptor['params']; /** Type alias for a single param of a descriptor. */ export type ApiParam> = ApiParams[TParamName]; /** Type alias for the union of parameter names in a descriptor. */ export type ApiParamNames = keyof ApiParams; /** Type alias for a tuple of parameter types for a descriptor. */ export type ApiOrderedParams[]> = { [index in keyof TParamNames]: ApiParams[TParamNames[index]]; }; /** Type alias for the return member of a descriptor. */ export type ApiReturn = TDescriptor['return']; /** A type representing a synchronous handler. */ export type ApiHandlerSync = (params: ApiParams, ...extraParams: TExtraParams) => ApiReturn; /** A type representing an asynchronous handler. */ export type ApiHandlerAsync = (params: ApiParams, ...extraParams: TExtraParams) => Promise>; /** A type representing a generic handler. */ export type ApiHandler = (params: ApiParams, ...extraParams: TExtraParams) => ApiReturn | Promise>; /** A union of all of the handlers for a given surface. */ export type ApiHandlerAny = ApiHandlerSurface[ApiNames]; /** A union of all of the names for a given surface. */ export type ApiNames = keyof TSurface; /** A mapping of names to the corresponding handler function. */ export type ApiMap = Map, ApiHandlerAny>; /** The initialization array structure for populating an API map. */ export type ApiMapInit = ApiMapInitItemAny[]; /** The type for a public API function, using a parameters object. */ export type ApiFunction> = ( params: ApiParams, ) => Promise>; /** The type for a public API function, using ordered parameters. */ export type ApiFunctionOrdered, TParamNames extends ApiParamNames[]> = ( ...params: ApiOrderedParams, ) => Promise>; /** Type alias for a union of all params types. */ export type ApiParamsAny = {[name in ApiNames]: ApiParams}[ApiNames]; /** Type alias for a union of all return types. */ export type ApiReturnAny = {[name in ApiNames]: ApiReturn}[ApiNames];