Skip to content

Commit

Permalink
Accept no ref
Browse files Browse the repository at this point in the history
  • Loading branch information
nihalbhatnagar committed Jan 31, 2025
1 parent 7e84ede commit b0dc2b8
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .changeset/forty-seahorses-matter.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"@osdk/api": patch
---

Exposes $cloneAndUpdate from OSDK Object instances
Exposes $clone from OSDK Object instances
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
"\\.\\./.*?/src",
"\\.\\./.*?/build",
"\\./index.js"
]
],
"typescript.preferGoToSourceDefinition": true
}
2 changes: 1 addition & 1 deletion etc/api.report.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ export namespace Osdk {
> = OsdkBase<Q> & Pick<CompileTimeMetadata<Q>["props"], GetPropsKeys<Q, P>> & {
readonly $link: Q extends { linksType?: any } ? Q["linksType"] : Q extends ObjectTypeDefinition ? OsdkObjectLinksObject<Q> : never;
readonly $as: <NEW_Q extends ValidToFrom<Q>>(type: NEW_Q | string) => Osdk.Instance<NEW_Q, OPTIONS, ConvertProps<Q, NEW_Q, P>>;
readonly $cloneAndUpdate: (updatedObject: Osdk.Instance<Q, any, any> | { [K in PropertyKeys<Q>]? : CompileTimeMetadata<Q>["props"][K] }) => Osdk.Instance<Q, OPTIONS, P>;
readonly $clone: (updatedObject?: Osdk.Instance<Q, any, any> | { [K in PropertyKeys<Q>]? : CompileTimeMetadata<Q>["props"][K] }) => Osdk.Instance<Q, OPTIONS, P>;
} & (IsNever<OPTIONS> extends true ? {} : IsAny<OPTIONS> extends true ? {} : "$rid" extends OPTIONS ? { readonly $rid: string } : {});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/OsdkObjectFrom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export namespace Osdk {
>;

readonly $clone: (
updatedObject:
updatedObject?:
| Osdk.Instance<Q, any, any>
| {
[K in PropertyKeys<Q>]?: CompileTimeMetadata<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,15 @@ const basePropDefs = {
"$clone": {
value: function(
this: InternalOsdkInstance & ObjectHolder<any>,
update: Record<string, any>,
update: Record<string, any> | undefined,
) {
const rawObj = this[UnderlyingOsdkObject];
const def = this[ObjectDefRef];

if (!update) {
return createOsdkObject(this[ClientRef], def, rawObj);
}

if (
def.primaryKeyApiName in update
&& rawObj[def.primaryKeyApiName] !== update[def.primaryKeyApiName]
Expand Down
14 changes: 14 additions & 0 deletions packages/client/src/object/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ describe("OsdkObject", () => {
`);
});

it("is able to clone with nothing passed in", async () => {
expect(employee.$clone()).toMatchObject({
"$apiName": "Employee",
"$objectType": "Employee",
"$primaryKey": 50031,
"$title": "Jane Doe",
"class": "Blue",
"employeeId": 50031,
"fullName": "Jane Doe",
"office": "SEA",
"startDate": "2012-02-12",
});
});

it("throws when merging objects with different primary keys", async () => {
expect(() =>
employee.$clone({
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e.sandbox.todoapp/src/useTodos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export function useTodos() {

type OsdkPropsOnly<T extends ObjectTypeDefinition> = Omit<
Osdk<T>,
"$as" | "$link" | "$cloneAndUpdate"
"$as" | "$link" | "$clone"
>;

export interface SimpleTodo extends OsdkPropsOnly<MyOsdk.Todo> {}
Expand Down

0 comments on commit b0dc2b8

Please sign in to comment.