Skip to content

Commit 986fc1e

Browse files
feat: add PluginKVStore and PluginContext interfaces (#189)
Signed-off-by: Dylan Kilkenny <dylankilkenny95@gmail.com>
1 parent 21b6cd7 commit 986fc1e

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

custom-models/plugin-api.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,81 @@ export type Relayer = {
109109
signTransaction: (payload: SignTransactionRequest) => Promise<SignTransactionResponse>;
110110
};
111111

112+
export interface PluginKVStore {
113+
/**
114+
* Get and JSON-parse a value by key.
115+
* @typeParam T - Expected value type after JSON parse.
116+
* @param key - The key to retrieve.
117+
* @returns Resolves to the parsed value, or null if missing.
118+
*/
119+
get<T = unknown>(key: string): Promise<T | null>;
120+
121+
/**
122+
* Set a JSON-encoded value.
123+
* @param key - The key to set.
124+
* @param value - Serializable value; must not be undefined.
125+
* @param opts - Optional settings.
126+
* @param opts.ttlSec - Time-to-live in seconds; if > 0, sets expiry.
127+
* @returns True on success.
128+
* @throws Error if `value` is undefined or the key is invalid.
129+
*/
130+
set(key: string, value: unknown, opts?: { ttlSec?: number }): Promise<boolean>;
131+
132+
/**
133+
* Delete a key.
134+
* @param key - The key to remove.
135+
* @returns True if exactly one key was removed.
136+
*/
137+
del(key: string): Promise<boolean>;
138+
139+
/**
140+
* Check whether a key exists.
141+
* @param key - The key to check.
142+
* @returns True if the key exists.
143+
*/
144+
exists(key: string): Promise<boolean>;
145+
146+
/**
147+
* List keys in this namespace matching `pattern`.
148+
* @param pattern - Glob-like match pattern (default '*').
149+
* @param batch - SCAN COUNT per iteration (default 500).
150+
* @returns Array of bare keys (without the namespace prefix).
151+
*/
152+
listKeys(pattern?: string, batch?: number): Promise<string[]>;
153+
154+
/**
155+
* Remove all keys in this namespace.
156+
* @returns The number of keys deleted.
157+
*/
158+
clear(): Promise<number>;
159+
160+
/**
161+
* Execute `fn` under a distributed lock for `key`.
162+
* @typeParam T - The return type of `fn`.
163+
* @param key - The lock key.
164+
* @param fn - Async function to execute while holding the lock.
165+
* @param opts - Lock options.
166+
* @param opts.ttlSec - Lock expiry in seconds (default 30).
167+
* @param opts.onBusy - Behavior when the lock is busy: 'throw' or 'skip'.
168+
* @returns The result of `fn`, or null when skipped due to a busy lock.
169+
* @throws Error if the lock is busy and `onBusy` is 'throw'.
170+
*/
171+
withLock<T>(
172+
key: string,
173+
fn: () => Promise<T>,
174+
opts?: { ttlSec?: number; onBusy?: 'throw' | 'skip' },
175+
): Promise<T | null>;
176+
}
177+
178+
/**
179+
* Plugin context with KV always available for modern plugins.
180+
*/
181+
export interface PluginContext {
182+
api: PluginAPI;
183+
kv: PluginKVStore;
184+
params: any;
185+
}
186+
112187
export interface PluginAPI {
113188
useRelayer(relayerId: string): Relayer;
114189
transactionWait(transaction: SendTransactionResult, options?: TransactionWaitOptions): Promise<TransactionResponse>;

src/models/plugin-api.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,81 @@ export type Relayer = {
109109
signTransaction: (payload: SignTransactionRequest) => Promise<SignTransactionResponse>;
110110
};
111111

112+
export interface PluginKVStore {
113+
/**
114+
* Get and JSON-parse a value by key.
115+
* @typeParam T - Expected value type after JSON parse.
116+
* @param key - The key to retrieve.
117+
* @returns Resolves to the parsed value, or null if missing.
118+
*/
119+
get<T = unknown>(key: string): Promise<T | null>;
120+
121+
/**
122+
* Set a JSON-encoded value.
123+
* @param key - The key to set.
124+
* @param value - Serializable value; must not be undefined.
125+
* @param opts - Optional settings.
126+
* @param opts.ttlSec - Time-to-live in seconds; if > 0, sets expiry.
127+
* @returns True on success.
128+
* @throws Error if `value` is undefined or the key is invalid.
129+
*/
130+
set(key: string, value: unknown, opts?: { ttlSec?: number }): Promise<boolean>;
131+
132+
/**
133+
* Delete a key.
134+
* @param key - The key to remove.
135+
* @returns True if exactly one key was removed.
136+
*/
137+
del(key: string): Promise<boolean>;
138+
139+
/**
140+
* Check whether a key exists.
141+
* @param key - The key to check.
142+
* @returns True if the key exists.
143+
*/
144+
exists(key: string): Promise<boolean>;
145+
146+
/**
147+
* List keys in this namespace matching `pattern`.
148+
* @param pattern - Glob-like match pattern (default '*').
149+
* @param batch - SCAN COUNT per iteration (default 500).
150+
* @returns Array of bare keys (without the namespace prefix).
151+
*/
152+
listKeys(pattern?: string, batch?: number): Promise<string[]>;
153+
154+
/**
155+
* Remove all keys in this namespace.
156+
* @returns The number of keys deleted.
157+
*/
158+
clear(): Promise<number>;
159+
160+
/**
161+
* Execute `fn` under a distributed lock for `key`.
162+
* @typeParam T - The return type of `fn`.
163+
* @param key - The lock key.
164+
* @param fn - Async function to execute while holding the lock.
165+
* @param opts - Lock options.
166+
* @param opts.ttlSec - Lock expiry in seconds (default 30).
167+
* @param opts.onBusy - Behavior when the lock is busy: 'throw' or 'skip'.
168+
* @returns The result of `fn`, or null when skipped due to a busy lock.
169+
* @throws Error if the lock is busy and `onBusy` is 'throw'.
170+
*/
171+
withLock<T>(
172+
key: string,
173+
fn: () => Promise<T>,
174+
opts?: { ttlSec?: number; onBusy?: 'throw' | 'skip' },
175+
): Promise<T | null>;
176+
}
177+
178+
/**
179+
* Plugin context with KV always available for modern plugins.
180+
*/
181+
export interface PluginContext {
182+
api: PluginAPI;
183+
kv: PluginKVStore;
184+
params: any;
185+
}
186+
112187
export interface PluginAPI {
113188
useRelayer(relayerId: string): Relayer;
114189
transactionWait(transaction: SendTransactionResult, options?: TransactionWaitOptions): Promise<TransactionResponse>;

0 commit comments

Comments
 (0)