@iprokit/service
Version:
Powering distributed systems with simplicity and speed.
144 lines (143 loc) • 3.49 kB
TypeScript
/**
* @iProKit/Service
* Copyright (c) 2019-2025 Rutvik Katuri / iProTechs
* SPDX-License-Identifier: Apache-2.0
*/
/**
* `RFI` stands for Remote Function Identifier.
* It is represented as a case-sensitive string.
*
* Example: `SYNC:Hero.get#ONE=A&TWO=B`
*
* An RFI consists of a mode, operation, and parameters:
* - Mode: `SYNC`
* - Operation: `Hero.get`
* - Parameters: `ONE=A&TWO=B`
*/
export default class RFI implements IRFI {
readonly mode: Mode;
readonly operation: string;
readonly parameters: Parameters;
/**
* Creates an instance of `RFI`.
*
* @param mode mode of the remote function.
* @param operation operation of the remote function.
* @param parameters optional parameters of the remote function.
*/
constructor(mode: Mode, operation: string, parameters?: Parameters);
/**
* Gets a parameter value.
*
* @param key parameter key.
*/
get<K extends keyof Parameters>(key: K): Parameters[K];
/**
* Returns `true` if the parameter exists, `false` otherwise.
*
* @param key parameter key.
*/
has(key: keyof Parameters): boolean;
/**
* Sets a parameter.
*
* @param key parameter key.
* @param value parameter value.
*/
set<K extends keyof Parameters>(key: K, value: Parameters[K]): this;
/**
* Removes a parameter.
*
* @param key parameter key.
*/
delete(key: keyof Parameters): this;
/**
* Returns an array of parameter keys.
*/
keys(): string[];
/**
* Returns an array of parameter values.
*/
values(): (string | undefined)[];
/**
* Returns an array of key-value pairs of parameters.
*/
entries(): [string, string | undefined][];
/**
* Returns the number of parameters.
*/
get size(): number;
/**
* Returns the stringified version of the `RFI`.
*/
stringify(): string;
/**
* Returns the objectified version of an `RFI`.
*
* @param rfi stringified version of an `RFI`.
*/
static objectify(rfi: string): RFI;
/**
* Delimiter for mode, denoted by `:`.
*
* @example `mode:operation`
*/
static readonly MODE_DELIMITER = ":";
/**
* Delimiter for operation, denoted by `#`.
*
* @example `operation#parameters`
*/
static readonly OPERATION_DELIMITER = "#";
/**
* Delimiter for parameters, denoted by `&`.
*
* @example `param1¶m2`
*/
static readonly PARAMETERS_DELIMITER = "&";
/**
* Delimiter for a key-value pair, denoted by `=`.
*
* @example `key=value`
*/
static readonly PARAMETER_DELIMITER = "=";
}
/**
* Interface for `RFI`.
*/
export interface IRFI {
/**
* Mode of the remote function.
*/
mode: string;
/**
* Operation of the remote function.
*/
operation: string;
/**
* Parameters of the remote function.
*/
parameters?: Parameters;
}
/**
* Mode definitions for an `RFI`.
*/
export type Mode = 'SUBSCRIBE' | 'BROADCAST' | 'REPLY' | 'CONDUCTOR' | 'OMNI';
/**
* Parameters associated with an `RFI`.
*/
export interface Parameters {
[key: string]: string | undefined;
/**
* Unique identifier of the client.
*/
CID?: string;
/**
* Unique identifier of the server.
*/
SID?: string;
/**
* Status of the function execution.
*/
STATUS?: 'OK' | 'ERROR';
}