Skip to content

Commit 1575393

Browse files
committed
lint & couple comments addressed
1 parent 1ab8362 commit 1575393

6 files changed

Lines changed: 119 additions & 8 deletions

File tree

packages/scenes-client/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { SceneClient } from "@itwin/scenes-client";
1111

1212
const client = new SceneClient(
1313
getAccessToken: async () => "<itwin_platform_auth_token>",
14-
baseUrl: HOST_URL, // default prod: https://itwinscenes-eus.bentley.com
14+
baseUrl: HOST_URL, // default prod: https://api.bentley.com/scenes
1515
);
1616

1717
const scene = await client.getScene({

packages/scenes-client/src/api/sceneObjectApi.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DeleteObjectParams,
1818
DeleteObjectsParams,
1919
GetObjectsParams,
20+
PatchObjectParam,
2021
} from "../models/index";
2122
import { iteratePagedEndpoint, batched } from "../utilities";
2223
import { callApi, AuthArgs } from "./apiFetch";
@@ -72,12 +73,12 @@ export async function getObjects({
7273
iTwinId,
7374
top,
7475
skip,
75-
orderBy: kind,
76+
orderBy,
7677
getAccessToken,
7778
baseUrl,
7879
}: GetObjectsParams & AuthArgs): Promise<SceneObjectListResponse> {
7980
return callApi<SceneObjectListResponse>({
80-
endpoint: `/${sceneId}/objects?iTwinId=${iTwinId}&$top=${top}&$skip=${skip}&orderBy=${kind}`,
81+
endpoint: `/${sceneId}/objects?iTwinId=${iTwinId}&$top=${top}&$skip=${skip}&orderBy=${orderBy}`,
8182
getAccessToken,
8283
baseUrl,
8384
postProcess: async (response) => {
@@ -208,6 +209,54 @@ export async function postObjects({
208209
};
209210
}
210211

212+
/**
213+
* Updates a single scene object by its ID.
214+
* @param params - {@link PatchObjectParam}
215+
* @returns Updated scene object details.
216+
* @throws {ScenesApiError} If the API call fails or the response format is invalid.
217+
*/
218+
export async function patchObject({
219+
sceneId,
220+
iTwinId,
221+
objectId,
222+
object,
223+
getAccessToken,
224+
baseUrl,
225+
}: PatchObjectParam & AuthArgs): Promise<SceneObjectResponse> {
226+
return callApi<SceneObjectResponse>({
227+
endpoint: `/${sceneId}/objects/${objectId}?iTwinId=${iTwinId}`,
228+
getAccessToken,
229+
baseUrl,
230+
postProcess: async (response) => {
231+
const responseJson = await response.json();
232+
if (!response.ok) {
233+
const err = responseJson.error as ScenesErrorResponse;
234+
throw new ScenesApiError(err, response.status);
235+
}
236+
if (!isSceneObjectResponse(responseJson)) {
237+
throw new ScenesApiError(
238+
{
239+
code: "InvalidResponse", //@naron: this seemed repetitive
240+
message: "Error updating scene object: unexpected response format",
241+
},
242+
response.status,
243+
);
244+
}
245+
return responseJson;
246+
},
247+
fetchOptions: {
248+
method: "PATCH",
249+
body: JSON.stringify(object, (_, value) =>
250+
value === undefined ? null : value,
251+
),
252+
},
253+
additionalHeaders: {
254+
Accept: "application/vnd.bentley.itwin-platform.v1+json",
255+
"Content-Type": "application/json",
256+
},
257+
});
258+
}
259+
211260
/**
212261
* Updates one or multiple scene objects.
213262
* @param params - {@link PatchObjectsParams}

packages/scenes-client/src/client.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
patchObjects,
1717
getAllObjects,
1818
getObjects,
19+
patchObject,
1920
} from "./api/sceneObjectApi";
2021
import {
2122
SceneListResponse,
@@ -39,6 +40,7 @@ import {
3940
DeleteObjectsParams,
4041
GetScenesParams,
4142
GetObjectsParams,
43+
PatchObjectParam,
4244
} from "./models/index";
4345

4446
type AccessTokenFn = () => Promise<string>;
@@ -251,7 +253,7 @@ export class SceneClient {
251253
* Create one or multiple scene objects.
252254
* @param params.iTwinId – The iTwin’s unique identifier.
253255
* @param params.sceneId – The scene’s unique identifier.
254-
* @param params.objects – Array of SceneObjectCreateDTO to create.
256+
* @param params.objects – Array of {@link SceneObjectCreateDTO} to create.
255257
* @returns Created scene objects details in list.
256258
* @throws {ScenesApiError} If the API call fails or the response format is invalid.
257259
*/
@@ -267,11 +269,31 @@ export class SceneClient {
267269
});
268270
}
269271

272+
/**
273+
* Update a single scene object by its ID.
274+
* @param params.iTwinId – The iTwin’s unique identifier.
275+
* @param params.sceneId – The scene’s unique identifier.
276+
* @param params.objectId – The object’s unique identifier.
277+
* @param params.object – The {@link SceneObjectUpdateDTO} to update.
278+
* @returns Updated scene object details.
279+
* @throws {ScenesApiError} If the API call fails or the response format is invalid.
280+
*/
281+
async patchObject(params: PatchObjectParam): Promise<SceneObjectResponse> {
282+
return patchObject({
283+
iTwinId: params.iTwinId,
284+
sceneId: params.sceneId,
285+
objectId: params.objectId,
286+
object: params.object,
287+
getAccessToken: this.getAccessToken,
288+
baseUrl: this.baseUrl,
289+
});
290+
}
291+
270292
/**
271293
* Update one or multiple scene objects.
272294
* @param params.iTwinId – The iTwin’s unique identifier.
273295
* @param params.sceneId – The scene’s unique identifier.
274-
* @param params.objects – Array of SceneObjectUpdateById to update.
296+
* @param params.objects – Array of {@link SceneObjectUpdateById} to update.
275297
* @returns Updated scene objects details.
276298
* @throws {ScenesApiError} If the API call fails or the response format is invalid.
277299
*/

packages/scenes-client/src/models/apiParams.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
import { GetObjectsOptions } from "./object/GetObjectsOptions";
44
import { BulkSceneObjectCreateDTO } from "./object/sceneObjectCreate.dto";
5-
import { BulkSceneObjectUpdate } from "./object/sceneObjectUpdate.dto";
5+
import {
6+
BulkSceneObjectUpdate,
7+
SceneObjectUpdateDTO,
8+
} from "./object/sceneObjectUpdate.dto";
69
import { GetScenesOptions } from "./scene/GetScenesOptions";
710
import { SceneCreateDTO } from "./scene/sceneCreate.dto";
811
import { SceneUpdateDTO } from "./scene/sceneUpdate.dto";
@@ -22,6 +25,7 @@ export type GetObjectParams = ObjectParams;
2225
export type GetObjectsParams = SceneParams & Omit<GetObjectsOptions, "delayMs">;
2326
export type GetAllObjectsParams = SceneParams & GetObjectsOptions;
2427
export type PostObjectsParams = SceneParams & BulkSceneObjectCreateDTO;
28+
export type PatchObjectParam = ObjectParams & { object: SceneObjectUpdateDTO };
2529
export type PatchObjectsParams = SceneParams & BulkSceneObjectUpdate;
2630
export type DeleteObjectParams = ObjectParams;
2731
export type DeleteObjectsParams = SceneParams & { objectIds: string[] };

packages/scenes-client/tests/integration.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,28 @@ describe("Scenes Objects operations", () => {
180180
obj2 = repo.id;
181181
});
182182

183+
it("patch object", async () => {
184+
const p1 = await client.patchObject({
185+
iTwinId: ITWIN_ID,
186+
sceneId: SCENE_ID,
187+
objectId: obj1,
188+
object: { displayName: "Layer1Up" },
189+
});
190+
expect(p1.object.displayName).toBe("Layer1Up");
191+
});
192+
183193
it("patches objects", async () => {
184194
const p1 = await client.patchObjects({
185195
iTwinId: ITWIN_ID,
186196
sceneId: SCENE_ID,
187197
objects: [
188198
{
189199
id: obj1,
190-
displayName: "Layer1Up",
200+
displayName: "Layer1Up squared",
191201
},
192202
],
193203
});
194-
expect(p1.objects[0].displayName).toBe("Layer1Up");
204+
expect(p1.objects[0].displayName).toBe("Layer1Up squared");
195205

196206
const pAll = await client.patchObjects({
197207
iTwinId: ITWIN_ID,

packages/scenes-client/tests/unit.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,32 @@ describe("Scene Object Operations", () => {
249249
});
250250
});
251251

252+
it("patchObject()", async () => {
253+
fetchMock.mockImplementation(() =>
254+
createSuccessfulResponse(exampleSceneObjectResponse),
255+
);
256+
const client = new SceneClient(getAccessToken);
257+
await client.patchObject({
258+
iTwinId: "itw-1",
259+
sceneId: "scene-1",
260+
objectId: "object-1",
261+
object: {
262+
displayName: "UpdatedObject1",
263+
},
264+
});
265+
verifyFetch(fetchMock, {
266+
url: `${BASE_DOMAIN}/scene-1/objects/object-1?iTwinId=itw-1`,
267+
headers: {
268+
"Content-Type": "application/json",
269+
Accept: "application/vnd.bentley.itwin-platform.v1+json",
270+
},
271+
method: "PATCH",
272+
body: JSON.stringify({
273+
displayName: "UpdatedObject1",
274+
}),
275+
});
276+
});
277+
252278
it("patchObjects()", async () => {
253279
fetchMock.mockImplementation(() =>
254280
createSuccessfulResponse({ objects: [] }),

0 commit comments

Comments
 (0)