@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
194 lines (193 loc) • 9.08 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var request_exports = {};
__export(request_exports, {
bodyFromData: () => bodyFromData,
getHeaders: () => getHeaders
});
module.exports = __toCommonJS(request_exports);
var import_auth = require("../auth.js");
var import_common = require("../common/index.js");
var import_index = require("./index.js");
var import_logger = require("../common/logger.js");
var import_ChangeFeed = require("../client/ChangeFeed/index.js");
function javaScriptFriendlyJSONStringify(s) {
return JSON.stringify(s).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
}
function bodyFromData(data) {
if (typeof data === "object") {
return javaScriptFriendlyJSONStringify(data);
}
return data;
}
const JsonContentType = "application/json";
async function getHeaders({
clientOptions,
defaultHeaders,
verb,
path,
resourceId,
resourceType,
options = {},
operationType,
partitionKeyRangeId,
useMultipleWriteLocations,
partitionKey
}) {
const headers = {
[import_common.Constants.HttpHeaders.ResponseContinuationTokenLimitInKB]: 1,
[import_common.Constants.HttpHeaders.EnableCrossPartitionQuery]: true,
...defaultHeaders
};
headers[import_common.Constants.HttpHeaders.SDKSupportedCapabilities] = import_common.SDKSupportedCapabilities.PartitionMerge;
if (useMultipleWriteLocations) {
headers[import_common.Constants.HttpHeaders.ALLOW_MULTIPLE_WRITES] = true;
}
if (options.continuationTokenLimitInKB) {
headers[import_common.Constants.HttpHeaders.ResponseContinuationTokenLimitInKB] = options.continuationTokenLimitInKB;
}
if (options.continuationToken) {
headers[import_common.Constants.HttpHeaders.Continuation] = options.continuationToken;
} else if (options.continuation) {
headers[import_common.Constants.HttpHeaders.Continuation] = options.continuation;
}
if (options.preTriggerInclude) {
headers[import_common.Constants.HttpHeaders.PreTriggerInclude] = options.preTriggerInclude.constructor === Array ? options.preTriggerInclude.join(",") : options.preTriggerInclude;
}
if (options.postTriggerInclude) {
headers[import_common.Constants.HttpHeaders.PostTriggerInclude] = options.postTriggerInclude.constructor === Array ? options.postTriggerInclude.join(",") : options.postTriggerInclude;
}
if (options.offerType) {
headers[import_common.Constants.HttpHeaders.OfferType] = options.offerType;
}
if (options.offerThroughput) {
headers[import_common.Constants.HttpHeaders.OfferThroughput] = options.offerThroughput;
}
if (options.maxItemCount) {
headers[import_common.Constants.HttpHeaders.PageSize] = options.maxItemCount;
}
if (options.accessCondition) {
if (options.accessCondition.type === "IfMatch") {
headers[import_common.Constants.HttpHeaders.IfMatch] = options.accessCondition.condition;
} else {
headers[import_common.Constants.HttpHeaders.IfNoneMatch] = options.accessCondition.condition;
}
}
if (options.useAllVersionsAndDeletesFeed) {
headers[import_common.Constants.HttpHeaders.A_IM] = import_ChangeFeed.ChangeFeedMode.AllVersionsAndDeletes;
headers[import_common.Constants.HttpHeaders.ChangeFeedWireFormatVersion] = import_common.Constants.AllVersionsAndDeletesChangeFeedWireFormatVersion;
}
if (options.useIncrementalFeed || options.useLatestVersionFeed) {
headers[import_common.Constants.HttpHeaders.A_IM] = import_ChangeFeed.ChangeFeedMode.LatestVersion;
}
if (options.indexingDirective) {
headers[import_common.Constants.HttpHeaders.IndexingDirective] = options.indexingDirective;
}
if (options.consistencyLevel) {
headers[import_common.Constants.HttpHeaders.ConsistencyLevel] = options.consistencyLevel;
}
if (options.priorityLevel) {
headers[import_common.Constants.HttpHeaders.PriorityLevel] = options.priorityLevel;
}
if (options.throughputBucket) {
headers[import_common.Constants.HttpHeaders.ThroughputBucket] = options.throughputBucket;
}
if (options.maxIntegratedCacheStalenessInMs && resourceType === import_common.ResourceType.item) {
if (typeof options.maxIntegratedCacheStalenessInMs === "number") {
headers[import_common.Constants.HttpHeaders.DedicatedGatewayPerRequestCacheStaleness] = options.maxIntegratedCacheStalenessInMs.toString();
} else {
import_logger.defaultLogger.error(
`RangeError: maxIntegratedCacheStalenessInMs "${options.maxIntegratedCacheStalenessInMs}" is not a valid parameter.`
);
headers[import_common.Constants.HttpHeaders.DedicatedGatewayPerRequestCacheStaleness] = "null";
}
}
if (options.bypassIntegratedCache) {
headers[import_common.Constants.HttpHeaders.DedicatedGatewayPerRequestBypassCache] = options.bypassIntegratedCache.toString();
}
if (options.resourceTokenExpirySeconds) {
headers[import_common.Constants.HttpHeaders.ResourceTokenExpiry] = options.resourceTokenExpirySeconds;
}
if (options.sessionToken) {
headers[import_common.Constants.HttpHeaders.SessionToken] = options.sessionToken;
}
if (options.enableScanInQuery) {
headers[import_common.Constants.HttpHeaders.EnableScanInQuery] = options.enableScanInQuery;
}
if (options.populateQuotaInfo) {
headers[import_common.Constants.HttpHeaders.PopulateQuotaInfo] = options.populateQuotaInfo;
}
if (options.populateQueryMetrics) {
headers[import_common.Constants.HttpHeaders.PopulateQueryMetrics] = options.populateQueryMetrics;
}
if (options.maxDegreeOfParallelism !== void 0 && options.maxDegreeOfParallelism !== 0 && options.maxDegreeOfParallelism !== 1) {
headers[import_common.Constants.HttpHeaders.ParallelizeCrossPartitionQuery] = true;
}
if (options.populateQuotaInfo) {
headers[import_common.Constants.HttpHeaders.PopulateQuotaInfo] = true;
}
if (partitionKey !== void 0 && !headers[import_common.Constants.HttpHeaders.PartitionKey]) {
headers[import_common.Constants.HttpHeaders.PartitionKey] = (0, import_common.jsonStringifyAndEscapeNonASCII)(partitionKey);
} else if (partitionKeyRangeId !== void 0) {
headers[import_common.Constants.HttpHeaders.PartitionKeyRangeID] = partitionKeyRangeId;
}
if (clientOptions.key || clientOptions.tokenProvider) {
headers[import_common.Constants.HttpHeaders.XDate] = (/* @__PURE__ */ new Date()).toUTCString();
}
if (verb === import_common.HTTPMethod.post || verb === import_common.HTTPMethod.put) {
if (!headers[import_common.Constants.HttpHeaders.ContentType]) {
headers[import_common.Constants.HttpHeaders.ContentType] = JsonContentType;
}
}
if (!headers[import_common.Constants.HttpHeaders.Accept]) {
headers[import_common.Constants.HttpHeaders.Accept] = JsonContentType;
}
if (options.enableScriptLogging) {
headers[import_common.Constants.HttpHeaders.EnableScriptLogging] = options.enableScriptLogging;
}
if (options.disableRUPerMinuteUsage) {
headers[import_common.Constants.HttpHeaders.DisableRUPerMinuteUsage] = true;
}
if (options.populateIndexMetrics) {
headers[import_common.Constants.HttpHeaders.PopulateIndexMetrics] = options.populateIndexMetrics;
}
if (clientOptions.clientEncryptionOptions) {
headers[import_common.Constants.HttpHeaders.IsClientEncryptedHeader] = true;
if (options.containerRid) {
headers[import_common.Constants.HttpHeaders.IntendedCollectionHeader] = options.containerRid;
}
}
if (clientOptions.key || clientOptions.resourceTokens || clientOptions.tokenProvider || clientOptions.permissionFeed) {
await (0, import_auth.setAuthorizationHeader)(clientOptions, verb, path, resourceId, resourceType, headers);
}
if (resourceType === import_common.ResourceType.item && Object.prototype.hasOwnProperty.call(options, "contentResponseOnWriteEnabled") && !options.contentResponseOnWriteEnabled) {
if (operationType === import_common.OperationType.Batch) {
headers[import_common.Constants.HttpHeaders.Prefer] = import_common.Constants.HttpHeaders.PreferReturnMinimal;
} else {
throw new import_index.ErrorResponse(
"Currently `contentResponseOnWriteEnabled` option is only supported for batch and bulk operations."
);
}
}
return headers;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
bodyFromData,
getHeaders
});