Skip to content

Commit

Permalink
Add fetchOneWithErrors (#250) (#258)
Browse files Browse the repository at this point in the history
* add fetchOneWithErrors

* add changeset

* Add fetchOne (#253)

* add fetchpage without errors

* add changeset
  • Loading branch information
ssanjay1 authored May 1, 2024
1 parent 7585d64 commit 212f651
Show file tree
Hide file tree
Showing 18 changed files with 475 additions and 57 deletions.
6 changes: 6 additions & 0 deletions .changeset/angry-trees-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@osdk/foundry-sdk-generator": patch
"@osdk/legacy-client": patch
---

Add fetchone, that is get replacement without result wrapper
6 changes: 6 additions & 0 deletions .changeset/slow-kids-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@osdk/foundry-sdk-generator": patch
"@osdk/legacy-client": patch
---

Deprecate get and add fetchonewitherrors, which functionally is the same
15 changes: 15 additions & 0 deletions packages/foundry-sdk-generator/src/__e2e_tests__/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ describe("test", () => {
expect(officeImpl.__rid).toBe(
"ri.phonograph2-objects.main.object.c0c0c0c0-c0c0-c0c0-c0c0-c0c0c0c0c0c0",
);
const officeResult2: Result<Office, GetObjectError> = await objectEdit
.fetchOneWithErrors();
const officeImpl2: Office = assertOkOrThrow(officeResult2);
expect(officeImpl2.__apiName).toBe("Office");
expect(officeImpl2.__primaryKey).toBe("NYC");
expect(officeImpl2.__rid).toBe(
"ri.phonograph2-objects.main.object.c0c0c0c0-c0c0-c0c0-c0c0-c0c0c0c0c0c0",
);

const officeNoErrors: Office = await objectEdit.fetchOne();
expect(officeNoErrors.__apiName).toBe("Office");
expect(officeNoErrors.__primaryKey).toBe("NYC");
expect(officeNoErrors.__rid).toBe(
"ri.phonograph2-objects.main.object.c0c0c0c0-c0c0-c0c0-c0c0-c0c0c0c0c0c0",
);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ describe("LoadObjects", () => {
expect(destructured.office).toEqual("SF");
});

it("Loads object which can be destructured with fetchOneWithErrors", async () => {
const result: Result<Employee, GetObjectError> = await client.ontology
.objects.Employee.fetchOneWithErrors(
stubData.employee1.__primaryKey,
);

const emp = assertOkOrThrow(result);
expect(emp.employeeId).toEqual(50030);
expect(emp.fullName).toEqual("John Doe");
expect(emp.office).toEqual("NYC");
expect(emp.startDate).toEqual(LocalDate.fromISOString("2019-01-01"));

const destructured: Employee = { ...emp, office: "SF" };
expect(destructured.fullName).toEqual("John Doe");
expect(destructured.office).toEqual("SF");
});

it("Loads object which can be destructured with fetchOne", async () => {
const emp: Employee = await client.ontology
.objects.Employee.fetchOne(
stubData.employee1.__primaryKey,
);

expect(emp.employeeId).toEqual(50030);
expect(emp.fullName).toEqual("John Doe");
expect(emp.office).toEqual("NYC");
expect(emp.startDate).toEqual(LocalDate.fromISOString("2019-01-01"));

const destructured: Employee = { ...emp, office: "SF" };
expect(destructured.fullName).toEqual("John Doe");
expect(destructured.office).toEqual("SF");
});

it("Loads object with specified properties", async () => {
const result = await client.ontology.objects.Employee.select(["fullName"])
.get(stubData.employee1.__primaryKey);
Expand All @@ -102,6 +135,40 @@ describe("LoadObjects", () => {
expect((emp as any).office).toBeUndefined();
});

it("Loads object with specified properties - fetchOneWithErrors", async () => {
const result = await client.ontology.objects.Employee.select(["fullName"])
.fetchOneWithErrors(stubData.employee1.__primaryKey);

const emp = assertOkOrThrow(result);

expectTypeOf(emp).toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
readonly $primaryKey: number;
readonly $apiName: "Employee";
}>();

expect(emp.fullName).toEqual("John Doe");
expect((emp as any).office).toBeUndefined();
});

it("Loads object with specified properties - fetchOne", async () => {
const emp = await client.ontology.objects.Employee.select(["fullName"])
.fetchOne(stubData.employee1.__primaryKey);

expectTypeOf(emp).toEqualTypeOf<{
readonly fullName: string | undefined;
readonly __primaryKey: number;
readonly __apiName: "Employee";
readonly $primaryKey: number;
readonly $apiName: "Employee";
}>();

expect(emp.fullName).toEqual("John Doe");
expect((emp as any).office).toBeUndefined();
});

it("Failure to load object", async () => {
const result: Result<Employee, GetObjectError> = await client.ontology
.objects.Employee.get(1234);
Expand All @@ -128,6 +195,32 @@ describe("LoadObjects", () => {
visitError(err, visitor);
});

it("Failure to load object - fetchOneWithErrors", async () => {
const result: Result<Employee, GetObjectError> = await client.ontology
.objects.Employee.fetchOneWithErrors(1234);
const err = assertErrOrThrow(result);

const visitor: ErrorVisitor<GetObjectError, void> = {
ObjectNotFound: error => {
expect(error).toMatchObject({
name: "ObjectNotFound",
errorType: "NOT_FOUND",
errorInstanceId: "errorInstanceId",
statusCode: 404,
primaryKey: {
employeeId: "1234",
},
objectType: "Employee",
});
},
default: error => {
throw new Error(`Unexpected error: ${error.errorName as string}`);
},
};

visitError(err, visitor);
});

it("Load TimeSeries firstPoint and lastPoint", async () => {
const result: Result<Employee, GetObjectError> = await client.ontology
.objects.Employee.get(
Expand Down
4 changes: 4 additions & 0 deletions packages/legacy-client/src/client/actions/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,15 @@ describe("Actions", () => {
"modified": [
{
"apiName": "Office",
"fetchOne": [Function],
"fetchOneWithErrors": [Function],
"get": [Function],
"primaryKey": "SEA",
},
{
"apiName": "Office",
"fetchOne": [Function],
"fetchOneWithErrors": [Function],
"get": [Function],
"primaryKey": "NYC",
},
Expand Down
13 changes: 12 additions & 1 deletion packages/legacy-client/src/client/baseTypes/ActionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
SyncApplyActionResponseV2,
} from "@osdk/gateway/types";
import type { ClientContext } from "@osdk/shared.net";
import { getObject } from "../../client/net/getObject";
import { getObject, getObjectWithoutErrors } from "../../client/net/getObject";
import type { GetObjectError } from "../errors";
import type { Result } from "../Result";
import type { OntologyObject } from "./OntologyObject";
Expand Down Expand Up @@ -66,7 +66,10 @@ export declare type ObjectEdit<T extends OntologyObject> = {
primaryKey: Extract<T, {
__apiName: K;
}>["__primaryKey"];
/** @deprecated use fetchOneWithErrors instead */
get: () => Promise<Result<T, GetObjectError>>;
fetchOneWithErrors: () => Promise<Result<T, GetObjectError>>;
fetchOne: () => Promise<T>;
};
}[T["$apiName"]];

Expand Down Expand Up @@ -228,13 +231,21 @@ function getEdits(
apiName: edit.objectType,
primaryKey: edit.primaryKey,
get: () => getObject(client, edit.objectType, edit.primaryKey),
fetchOneWithErrors: () =>
getObject(client, edit.objectType, edit.primaryKey),
fetchOne: () =>
getObjectWithoutErrors(client, edit.objectType, edit.primaryKey),
});
}
if (edit.type === "modifyObject") {
modified.push({
apiName: edit.objectType,
primaryKey: edit.primaryKey,
get: () => getObject(client, edit.objectType, edit.primaryKey),
fetchOneWithErrors: () =>
getObject(client, edit.objectType, edit.primaryKey),
fetchOne: () =>
getObjectWithoutErrors(client, edit.objectType, edit.primaryKey),
});
}
}
Expand Down
26 changes: 26 additions & 0 deletions packages/legacy-client/src/client/baseTypes/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,43 @@ import type { OntologyObject } from "./OntologyObject";
export interface SingleLink<T extends OntologyObject = OntologyObject> {
/**
* Loads the linked object
* @deprecated use fetchOneWithErrors instead
*/
get(): Promise<Result<T, GetLinkedObjectError>>;
/**
* Loads the linked object
*/
fetchOneWithErrors(): Promise<Result<T, GetLinkedObjectError>>;
/**
* Loads the linked object, without a result wrapper
*/
fetchOne(): Promise<T>;
}

export interface MultiLink<T extends OntologyObject = OntologyObject> {
/**
* Loads the linked object with the given primary key
*
* @param primaryKey
* @deprecated use fetchOneWithErrors instead
*/
get(primaryKey: T["__primaryKey"]): Promise<Result<T, GetLinkedObjectError>>;
/**
* Loads the linked object with the given primary key
*
* @param primaryKey
*/
fetchOneWithErrors(
primaryKey: T["__primaryKey"],
): Promise<Result<T, GetLinkedObjectError>>;
/**
* Loads the linked object with the given primary key, without a result wrapper
*
* @param primaryKey
*/
fetchOne(
primaryKey: T["__primaryKey"],
): Promise<T>;
/**
* Gets all the linked objects
* @deprecated use asyncIter instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type FilteredPropertiesTerminalOperationsWithGet<
T extends OntologyObject,
V extends Array<keyof T>,
> = FilteredPropertiesTerminalOperations<T, V> & {
/** @deprecated use fetchOneWithErrors */
get(
primaryKey: T["$primaryKey"],
): Promise<
Expand All @@ -104,4 +105,23 @@ export type FilteredPropertiesTerminalOperationsWithGet<
GetObjectError
>
>;
fetchOneWithErrors(
primaryKey: T["$primaryKey"],
): Promise<
Result<
Pick<
T,
V[number] | "$apiName" | "$primaryKey" | "__apiName" | "__primaryKey"
>,
GetObjectError
>
>;
fetchOne(
primaryKey: T["$primaryKey"],
): Promise<
Pick<
T,
V[number] | "$apiName" | "$primaryKey" | "__apiName" | "__primaryKey"
>
>;
};
9 changes: 9 additions & 0 deletions packages/legacy-client/src/client/interfaces/baseObjectSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ export type BaseObjectSetOperations<O extends OntologyObject> = {

properties: Properties<O>;

/** @deprecated use fetchOneWithErrors instead */
get(primaryKey: O["$primaryKey"]): Promise<Result<O, GetObjectError>>;

fetchOneWithErrors(
primaryKey: O["$primaryKey"],
): Promise<Result<O, GetObjectError>>;

fetchOne(
primaryKey: O["$primaryKey"],
): Promise<O>;

select<T extends keyof SelectableProperties<O>>(
properties: readonly T[],
): FilteredPropertiesTerminalOperationsWithGet<O, T[]>;
Expand Down
41 changes: 28 additions & 13 deletions packages/legacy-client/src/client/net/getLinkedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,14 @@ export function getLinkedObject<T extends OntologyObject>(
linkedObjectPrimaryKey: string,
): Promise<Result<T, GetLinkedObjectError>> {
return wrapResult(
async () => {
const object = await getLinkedObjectV2(
createOpenApiRequest(client.stack, client.fetch),
client.ontology.metadata.ontologyApiName,
async () =>
getLinkedObjectNoErrors(
client,
sourceApiName,
primaryKey,
linkTypeApiName,
linkedObjectPrimaryKey,
{
select: [],
},
);
return convertWireToOsdkObject(
client,
object as WireOntologyObjectV2<T["__apiName"]>,
) as unknown as T;
},
),
e =>
handleGetLinkedObjectError(
new GetLinkedObjectErrorHandler(),
Expand All @@ -62,3 +53,27 @@ export function getLinkedObject<T extends OntologyObject>(
),
);
}

export async function getLinkedObjectNoErrors<T extends OntologyObject>(
client: ClientContext<OntologyDefinition<T["__apiName"]>>,
sourceApiName: string,
primaryKey: any,
linkTypeApiName: string,
linkedObjectPrimaryKey: string,
): Promise<T> {
const object = await getLinkedObjectV2(
createOpenApiRequest(client.stack, client.fetch),
client.ontology.metadata.ontologyApiName,
sourceApiName,
primaryKey,
linkTypeApiName,
linkedObjectPrimaryKey,
{
select: [],
},
);
return convertWireToOsdkObject(
client,
object as WireOntologyObjectV2<T["__apiName"]>,
) as unknown as T;
}
Loading

0 comments on commit 212f651

Please sign in to comment.