Created wrapper proxies for DM and Contacts. Rest is WIP

This commit is contained in:
Noel Maróti
2026-11-07 13:44:30 +01:00
parent 3c553ceb71
commit 137976d2d7
2 changed files with 53 additions and 0 deletions

32
src/services/contact.ts Normal file
View File

@@ -0,0 +1,32 @@
import {Contact, ContactsApi} from "../generated";
const EXCLUDED_METHODS = ['getContacts', 'newContact'] as const;
type ExcludedMethods = typeof EXCLUDED_METHODS[number];
const excludeSet = new Set<string>(EXCLUDED_METHODS);
type OmitContactIdArg<T> = T extends (first: any, ...args: infer Rest) => infer R
? (...args: Rest) => R
: T;
type WrappedDMHandler<T> = {
[K in keyof T]: K extends ExcludedMethods
? T[K]
: T[K] extends (...args: any[]) => any
? OmitContactIdArg<T[K]>
: T[K];
};
function createContactWrapper<TFirst>(
instance: ContactsApi,
boundArg: TFirst
): WrappedDMHandler<ContactsApi> {
return new Proxy(instance, {
get(target, prop, receiver) {
const value = Reflect.get(target, prop, receiver);
if (typeof value === 'function' && !excludeSet.has(prop as string)) {
return (...args: any[]) => value.call(target, boundArg, ...args);
}
return value;
}
}) as unknown as WrappedDMHandler<ContactsApi>;
}

21
src/services/dm.ts Normal file
View File

@@ -0,0 +1,21 @@
import {DirectMessagingApi} from "../generated";
type OmitContactIdArg<T> = T extends (first: any, ...args: infer Rest) => infer R
? (...args: Rest) => R
: T;
type WrappedDMHandler<T> = {
[K in keyof T]: T[K] extends (...args: any[]) => any ? OmitContactIdArg<T[K]> : T[K];
};
export function createDMHandler<TFirst>(instance: DirectMessagingApi, contactId: TFirst): WrappedDMHandler<DirectMessagingApi> {
return new Proxy(instance, {
get(target, prop, receiver) {
const value = Reflect.get(target, prop, receiver);
if (typeof value === 'function') {
return (...args: any[]) => value.call(target, contactId, ...args);
}
return value;
}
}) as unknown as WrappedDMHandler<DirectMessagingApi>;
}