@azure/web-pubsub
Version:
Azure client library for Azure Web PubSub
75 lines • 2.54 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPayloadForMessage = getPayloadForMessage;
exports.odata = odata;
function isRequestBody(obj) {
return (typeof obj === "function" ||
(typeof obj === "object" &&
obj != null &&
(obj.constructor.name === "ArrayBuffer" ||
obj.constructor.name === "Blob" ||
ArrayBuffer.isView(obj))));
}
function getPayloadForMessage(message, options) {
if ((options === null || options === void 0 ? void 0 : options.contentType) === "text/plain") {
if (typeof message !== "string") {
throw new TypeError("Message must be a string.");
}
return { contentType: "text/plain", payload: message };
}
else if (isRequestBody(message)) {
return { contentType: "application/octet-stream", payload: message };
}
else {
return { contentType: "application/json", payload: JSON.stringify(message) };
}
}
function formatNullAndUndefined(input) {
if (input === null || input === undefined) {
return "null";
}
return input;
}
function escapeQuotesIfString(input, previous) {
let result = input;
if (typeof input === "string") {
result = input.replace(/'/g, "''");
// check if we need to escape this literal
if (!previous.trim().endsWith("'")) {
result = `'${result}'`;
}
}
return result;
}
/**
* Escapes an odata filter expression to avoid errors with quoting string literals.
* Example usage:
* ```ts snippet:ReadmeSampleOdata
* import { odata } from "@azure/web-pubsub";
*
* const userId = "vic's";
* const anonymous = null;
* const length = 3;
* const filter = odata`userId eq ${anonymous} or userId eq ${userId} or length(userId) > ${length}`;
* ```
* @param strings - Array of strings for the expression
* @param values - Array of values for the expression
*/
function odata(strings, ...values) {
const results = [];
for (let i = 0; i < strings.length; i++) {
results.push(strings[i]);
if (i < values.length) {
if (values[i] === null || values[i] === undefined) {
results.push(formatNullAndUndefined(values[i]));
}
else {
results.push(escapeQuotesIfString(values[i], strings[i]));
}
}
}
return results.join("");
}
//# sourceMappingURL=utils.js.map