Skip to content

Commit

Permalink
Cleanup getDolloarLink
Browse files Browse the repository at this point in the history
  • Loading branch information
ericanderson committed Dec 5, 2024
1 parent ecfb384 commit 9a7b268
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ const basePropDefs = {
},
"$link": {
get: function(this: InternalOsdkInstance & ObjectHolder<any>) {
return get$link({
[UnderlyingOsdkObject]: this as any,
[ObjectDefRef]: this[ObjectDefRef],
[ClientRef]: this[ClientRef],
} as ObjectHolder<any>);
return get$link(this);
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,67 +34,41 @@ import type { ObjectHolder } from "./ObjectHolder.js";
export function get$link(
holder: ObjectHolder<any>,
): OsdkObjectLinksObject<any> {
return new Proxy(holder, DollarLinkProxyHandler) as
& ObjectHolder<any>
& OsdkObjectLinksObject<any>;
}

const DollarLinkProxyHandler: ProxyHandler<ObjectHolder<any>> = {
get(target: ObjectHolder<any>, 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<any>)
.where({
[objDef.primaryKeyApiName]: rawObj.$primaryKey,
} as WhereClause<ObjectTypeDefinition>)
.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<any>)
.where({
[objDef.primaryKeyApiName]: rawObj.$primaryKey,
} as WhereClause<ObjectTypeDefinition>)
.pivotTo(linkName);

if (!linkDef.multiplicity) {
return {
fetchOne: <A extends SelectArg<any>>(options?: A) =>
fetchSingle(
client,
objDef,
options ?? {},
getWireObjectSet(objectSet),
),
fetchOneWithErrors: <A extends SelectArg<any>>(options?: A) =>
fetchSingleWithErrors(
client,
objDef,
options ?? {},
getWireObjectSet(objectSet),
),
};
} else {
return objectSet;
}
},
const value = !linkDef.multiplicity
? {
fetchOne: <A extends SelectArg<any>>(options?: A) =>
fetchSingle(
client,
objDef,
options ?? {},
getWireObjectSet(objectSet),
),
fetchOneWithErrors: <A extends SelectArg<any>>(options?: A) =>
fetchSingleWithErrors(
client,
objDef,
options ?? {},
getWireObjectSet(objectSet),
),
}
: objectSet;

ownKeys(
target: ObjectHolder<any>,
): ArrayLike<keyof ObjectHolder<any> | string> {
return [...Object.keys(target[ObjectDefRef].links)];
},

getOwnPropertyDescriptor(
target: ObjectHolder<any>,
p: string | symbol,
): PropertyDescriptor | undefined {
if (target[ObjectDefRef].links[p as any]) {
return {
enumerable: true,
configurable: true, // fixme
writable: false,
};
}
},
};
return [linkName, value];
},
),
));
}

0 comments on commit 9a7b268

Please sign in to comment.