Skip to content

Commit

Permalink
Update Item batch operation method signature (Azure#23652)
Browse files Browse the repository at this point in the history
* update method signature

* update api documentation

* update data type

* change formatting

* update operation response check

* update any type to unknown type

Co-authored-by: Will Temple <[email protected]>

* add operation response import

Co-authored-by: Manik Khandelwal <[email protected]>
Co-authored-by: Will Temple <[email protected]>
  • Loading branch information
3 people authored Nov 10, 2022
1 parent dffef55 commit 70250cc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sdk/cosmosdb/cosmos/review/cosmos.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ export class ItemResponse<T extends ItemDefinition> extends ResourceResponse<T &
// @public
export class Items {
constructor(container: Container, clientContext: ClientContext);
batch(operations: OperationInput[], partitionKey?: string, options?: RequestOptions): Promise<Response_2<any>>;
batch(operations: OperationInput[], partitionKey?: string, options?: RequestOptions): Promise<Response_2<OperationResponse[]>>;
bulk(operations: OperationInput[], bulkOptions?: BulkOptions, options?: RequestOptions): Promise<OperationResponse[]>;
changeFeed(partitionKey: string | number | boolean, changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>;
changeFeed(changeFeedOptions?: ChangeFeedOptions): ChangeFeedIterator<any>;
Expand Down
4 changes: 2 additions & 2 deletions sdk/cosmosdb/cosmos/src/client/Item/Items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ export class Items {
operations: OperationInput[],
partitionKey: string = "[{}]",
options?: RequestOptions
): Promise<Response<any>> {
): Promise<Response<OperationResponse[]>> {
operations.map((operation) => decorateBatchOperation(operation, options));

const path = getPathFromLink(this.container.url, ResourceType.item);
Expand All @@ -510,7 +510,7 @@ export class Items {
throw new Error("Cannot run batch request with more than 100 operations per partition");
}
try {
const response = await this.clientContext.batch({
const response: Response<OperationResponse[]> = await this.clientContext.batch({
body: operations,
partitionKey,
path,
Expand Down
19 changes: 18 additions & 1 deletion sdk/cosmosdb/cosmos/test/public/functional/item.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
// Licensed under the MIT license.
import assert from "assert";
import { Suite } from "mocha";
import { Container, CosmosClient, PatchOperation, PatchOperationType } from "../../../src";
import {
Container,
CosmosClient,
OperationResponse,
PatchOperation,
PatchOperationType,
} from "../../../src";
import { ItemDefinition } from "../../../src";
import {
bulkDeleteItems,
Expand Down Expand Up @@ -688,6 +694,7 @@ describe("bulk/batch item operations", function () {
];

const response = await container.items.batch(operations, "A");
assert(isOperationResponse(response.result[0]));
assert.strictEqual(response.result[0].statusCode, 201);
assert.strictEqual(response.result[1].statusCode, 201);
assert.strictEqual(response.result[2].statusCode, 200);
Expand All @@ -711,7 +718,17 @@ describe("bulk/batch item operations", function () {
assert.strictEqual(deleteResponse.result[1].statusCode, 404);
const { resource: readItem } = await container.item(otherItemId).read();
assert.strictEqual(readItem, undefined);
assert(isOperationResponse(deleteResponse.result[0]));
});

function isOperationResponse(object: unknown): object is OperationResponse {
return (
typeof object === "object" &&
object !== null &&
Object.prototype.hasOwnProperty.call(object, "statusCode") &&
Object.prototype.hasOwnProperty.call(object, "requestCharge")
);
}
});
});
describe("patch operations", function () {
Expand Down

0 comments on commit 70250cc

Please sign in to comment.