[Tables] Fix issue handling empty nextRowKey (#20916)
* Fix issue handling undefined nextRowKey * Address PR comments * Add changelog * remove export * Address comments
This commit is contained in:
Родитель
dccf667a3a
Коммит
26d973dba9
|
@ -8,6 +8,7 @@
|
|||
|
||||
### Bugs Fixed
|
||||
|
||||
- Fix issue when the Service returns an empty nextRowKey. [#20916](https://github.com/Azure/azure-sdk-for-js/pull/20916).
|
||||
- Fix issue with `getStatistics()` operation consistently failing and added test. [#20398](https://github.com/Azure/azure-sdk-for-js/pull/20398)
|
||||
|
||||
### Other Changes
|
||||
|
|
|
@ -3,28 +3,29 @@
|
|||
|
||||
import { base64Decode, base64Encode } from "./bufferSerializer";
|
||||
|
||||
export interface ContinuationToken {
|
||||
interface ContinuationToken {
|
||||
nextPartitionKey: string;
|
||||
nextRowKey: string;
|
||||
nextRowKey?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the nextPartitionKey and nextRowKey into a single continuation token
|
||||
*/
|
||||
export function encodeContinuationToken(
|
||||
nextPartitionKey: string = "",
|
||||
nextRowKey: string = ""
|
||||
nextPartitionKey?: string,
|
||||
nextRowKey?: string
|
||||
): string | undefined {
|
||||
if (!nextPartitionKey && !nextRowKey) {
|
||||
if (!nextPartitionKey) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const continuationToken = JSON.stringify({
|
||||
const continuationToken = {
|
||||
nextPartitionKey,
|
||||
nextRowKey,
|
||||
});
|
||||
// Only add nextRowKey if the value is not null, undefined or empty string.
|
||||
...(nextRowKey && { nextRowKey }),
|
||||
};
|
||||
|
||||
return base64Encode(continuationToken);
|
||||
return base64Encode(JSON.stringify(continuationToken));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
import {
|
||||
decodeContinuationToken,
|
||||
encodeContinuationToken,
|
||||
} from "../../src/utils/continuationToken";
|
||||
|
||||
import { assert } from "chai";
|
||||
|
||||
describe("continuation token utils", () => {
|
||||
it("should encode nextPartitionKey and nextRowKey", () => {
|
||||
const encoded = encodeContinuationToken("foo", "bar");
|
||||
assert.equal(encoded, "eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIiwibmV4dFJvd0tleSI6ImJhciJ9");
|
||||
});
|
||||
|
||||
it("should not encode nextRowKey if it is empty string", () => {
|
||||
const encoded = encodeContinuationToken("foo", "");
|
||||
assert.deepEqual(decodeContinuationToken(encoded!), { nextPartitionKey: "foo" });
|
||||
});
|
||||
|
||||
it("should encode nextPartitionKey and undefined nextRowKey", () => {
|
||||
const encoded = encodeContinuationToken("foo");
|
||||
assert.equal(encoded, "eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIn0=");
|
||||
});
|
||||
|
||||
it("should return undefined if nextPartitionKey and nextRowKey are empty", () => {
|
||||
const encoded = encodeContinuationToken();
|
||||
assert.equal(encoded, undefined);
|
||||
});
|
||||
|
||||
it("should decode nextPartitionKey and nextRowKey", () => {
|
||||
const decoded = decodeContinuationToken(
|
||||
"eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIiwibmV4dFJvd0tleSI6ImJhciJ9"
|
||||
);
|
||||
assert.deepEqual(decoded, { nextPartitionKey: "foo", nextRowKey: "bar" });
|
||||
});
|
||||
|
||||
it("should decode nextPartitionKey and undefined nextRowKey", () => {
|
||||
const decoded = decodeContinuationToken("eyJuZXh0UGFydGl0aW9uS2V5IjoiZm9vIn0=");
|
||||
assert.deepEqual(decoded, { nextPartitionKey: "foo" } as any);
|
||||
});
|
||||
});
|
Загрузка…
Ссылка в новой задаче