Skip to content

Commit a3ca988

Browse files
authored
chore(http): Replacing method "createHTTPMetadataQueryParam" (#480)
chore(http): Replacing method "createHTTPMetadataQueryParam" with createHTTPQueryParam in every http implementations Signed-off-by: SoTrx <[email protected]>
1 parent d999efa commit a3ca988

File tree

3 files changed

+3
-70
lines changed

3 files changed

+3
-70
lines changed

src/implementation/Client/HTTPClient/pubsub.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ import HTTPClient from "./HTTPClient";
1515
import IClientPubSub from "../../../interfaces/Client/IClientPubSub";
1616
import { Logger } from "../../../logger/Logger";
1717
import { KeyValueType } from "../../../types/KeyValue.type";
18-
import {
19-
createHTTPMetadataQueryParam,
20-
getBulkPublishEntries,
21-
getBulkPublishResponse,
22-
} from "../../../utils/Client.util";
18+
import { createHTTPQueryParam, getBulkPublishEntries, getBulkPublishResponse } from "../../../utils/Client.util";
2319
import { THTTPExecuteParams } from "../../../types/http/THTTPExecuteParams.type";
2420
import { PubSubBulkPublishResponse } from "../../../types/pubsub/PubSubBulkPublishResponse.type";
2521
import { PubSubBulkPublishMessage } from "../../../types/pubsub/PubSubBulkPublishMessage.type";
@@ -43,7 +39,7 @@ export default class HTTPClientPubSub implements IClientPubSub {
4339
data: object | string,
4440
options: PubSubPublishOptions = {},
4541
): Promise<PubSubPublishResponseType> {
46-
const queryParams = createHTTPMetadataQueryParam(options.metadata);
42+
const queryParams = createHTTPQueryParam({ data: options.metadata, type: "metadata" });
4743

4844
// Set content type if provided.
4945
// If not, HTTPClient will infer it from the data.
@@ -72,7 +68,7 @@ export default class HTTPClientPubSub implements IClientPubSub {
7268
messages: PubSubBulkPublishMessage[],
7369
metadata?: KeyValueType | undefined,
7470
): Promise<PubSubBulkPublishResponse> {
75-
const queryParams = createHTTPMetadataQueryParam(metadata);
71+
const queryParams = createHTTPQueryParam({ data: metadata, type: "metadata" });
7672
const params: THTTPExecuteParams = {
7773
method: "POST",
7874
headers: {

src/utils/Client.util.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,6 @@ export function addMetadataToMap(map: Map<string, string>, metadata: KeyValueTyp
4242
}
4343
}
4444

45-
/**
46-
* Converts a KeyValueType to a HTTP query parameters.
47-
* The query parameters are separated by "&", and the key value pair is separated by "=".
48-
* Each metadata key is prefixed with "metadata.".
49-
*
50-
* Example, if the metadata is { "key1": "value1", "key2": "value2" }, the query parameter will be:
51-
* "metadata.key1=value1&metadata.key2=value2"
52-
*
53-
* Note, the returned value does not contain the "?" prefix.
54-
*
55-
* @param metadata key value pair of metadata
56-
* @returns HTTP query parameter string
57-
*/
58-
export function createHTTPMetadataQueryParam(metadata: KeyValueType = {}): string {
59-
let queryParam = "";
60-
for (const [key, value] of Object.entries(metadata)) {
61-
queryParam += "&" + "metadata." + encodeURIComponent(key) + "=" + encodeURIComponent(value);
62-
}
63-
// strip the first "&" if it exists
64-
queryParam = queryParam.substring(1);
65-
return queryParam;
66-
}
67-
6845
/**
6946
* Converts one or multiple sets of data to a querystring
7047
* Each set of data contains a set of KeyValue Pair

test/unit/utils/Client.util.test.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ limitations under the License.
1414
import { ConfigurationItem } from "../../../src/proto/dapr/proto/common/v1/common_pb";
1515
import {
1616
addMetadataToMap,
17-
createHTTPMetadataQueryParam,
1817
createConfigurationType,
1918
getContentType,
2019
getBulkPublishEntries,
@@ -55,45 +54,6 @@ describe("Client.util", () => {
5554
expect(m.entries()).toEqual(new Map<string, string>([]).entries());
5655
});
5756
});
58-
59-
describe("createHTTPMetadataQueryParam", () => {
60-
it("converts a KeyValueType to a HTTP query parameters", () => {
61-
const metadata = {
62-
key1: "value1",
63-
key2: "value2",
64-
};
65-
const queryParam = createHTTPMetadataQueryParam(metadata);
66-
expect(queryParam).toEqual("metadata.key1=value1&metadata.key2=value2");
67-
});
68-
69-
it("converts a KeyValueType to a HTTP query parameters with empty metadata", () => {
70-
const metadata = {};
71-
const queryParam = createHTTPMetadataQueryParam(metadata);
72-
expect(queryParam).toEqual("");
73-
});
74-
75-
it("converts a KeyValueType to a HTTP query parameters with no metadata", () => {
76-
const queryParam = createHTTPMetadataQueryParam();
77-
expect(queryParam).toEqual("");
78-
});
79-
80-
it("converts a KeyValueType to a HTTP query parameters with undefined metadata", () => {
81-
const queryParam = createHTTPMetadataQueryParam(undefined);
82-
expect(queryParam).toEqual("");
83-
});
84-
85-
it("encodes the query parameters", () => {
86-
const metadata = {
87-
"key&with=special!ch#r#cters": "value1&value2",
88-
key00: "value3 value4",
89-
};
90-
const queryParam = createHTTPMetadataQueryParam(metadata);
91-
expect(queryParam).toEqual(
92-
"metadata.key%26with%3Dspecial!ch%23r%23cters=value1%26value2&metadata.key00=value3%20value4",
93-
);
94-
});
95-
});
96-
9757
describe("createHTTPQueryParam", () => {
9858
it("converts a KeyValueType to a HTTP query parameters", () => {
9959
const metadata = {

0 commit comments

Comments
 (0)