From d496d03aeb52c621f7f324b5120a145e1066aa01 Mon Sep 17 00:00:00 2001 From: Eric Anderson Date: Wed, 4 Dec 2024 18:10:02 -0500 Subject: [PATCH] Cleanup getDolloarLink --- .../createOsdkObject.ts | 6 +- .../convertWireToOsdkObjects/getDollarLink.ts | 98 +++++++------------ 2 files changed, 37 insertions(+), 67 deletions(-) diff --git a/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts b/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts index d18fb69f5..d585f292d 100644 --- a/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts +++ b/packages/client/src/object/convertWireToOsdkObjects/createOsdkObject.ts @@ -56,11 +56,7 @@ const basePropDefs = { }, "$link": { get: function(this: InternalOsdkInstance & ObjectHolder) { - return get$link({ - [UnderlyingOsdkObject]: this as any, - [ObjectDefRef]: this[ObjectDefRef], - [ClientRef]: this[ClientRef], - } as ObjectHolder); + return get$link(this); }, }, }; diff --git a/packages/client/src/object/convertWireToOsdkObjects/getDollarLink.ts b/packages/client/src/object/convertWireToOsdkObjects/getDollarLink.ts index 70edc29e3..c20f15184 100644 --- a/packages/client/src/object/convertWireToOsdkObjects/getDollarLink.ts +++ b/packages/client/src/object/convertWireToOsdkObjects/getDollarLink.ts @@ -34,67 +34,41 @@ import type { ObjectHolder } from "./ObjectHolder.js"; export function get$link( holder: ObjectHolder, ): OsdkObjectLinksObject { - return new Proxy(holder, DollarLinkProxyHandler) as - & ObjectHolder - & OsdkObjectLinksObject; -} - -const DollarLinkProxyHandler: ProxyHandler> = { - get(target: ObjectHolder, p: string | symbol) { - const { - [ObjectDefRef]: objDef, - [ClientRef]: client, - [UnderlyingOsdkObject]: rawObj, - } = target; - const linkDef = objDef.links[p as string]; - if (linkDef == null) { - return; - } - const objectSet = - (client.objectSetFactory(objDef, client) as ObjectSet) - .where({ - [objDef.primaryKeyApiName]: rawObj.$primaryKey, - } as WhereClause) - .pivotTo(p as string); + const client = holder[ClientRef]; + const objDef = holder[ObjectDefRef]; + const rawObj = holder[UnderlyingOsdkObject]; + return Object.freeze(Object.fromEntries( + Object.keys(objDef.links).map( + (linkName) => { + const linkDef = objDef.links[linkName as keyof typeof objDef.links]; + const objectSet = + (client.objectSetFactory(objDef, client) as ObjectSet) + .where({ + [objDef.primaryKeyApiName]: rawObj.$primaryKey, + } as WhereClause) + .pivotTo(linkName); - if (!linkDef.multiplicity) { - return { - fetchOne: >(options?: A) => - fetchSingle( - client, - objDef, - options ?? {}, - getWireObjectSet(objectSet), - ), - fetchOneWithErrors: >(options?: A) => - fetchSingleWithErrors( - client, - objDef, - options ?? {}, - getWireObjectSet(objectSet), - ), - }; - } else { - return objectSet; - } - }, + const value = !linkDef.multiplicity + ? { + fetchOne: >(options?: A) => + fetchSingle( + client, + objDef, + options ?? {}, + getWireObjectSet(objectSet), + ), + fetchOneWithErrors: >(options?: A) => + fetchSingleWithErrors( + client, + objDef, + options ?? {}, + getWireObjectSet(objectSet), + ), + } + : objectSet; - ownKeys( - target: ObjectHolder, - ): ArrayLike | string> { - return [...Object.keys(target[ObjectDefRef].links)]; - }, - - getOwnPropertyDescriptor( - target: ObjectHolder, - p: string | symbol, - ): PropertyDescriptor | undefined { - if (target[ObjectDefRef].links[p as any]) { - return { - enumerable: true, - configurable: true, // fixme - writable: false, - }; - } - }, -}; + return [linkName, value]; + }, + ), + )); +}