ravendb
Version:
RavenDB client for Node.js
64 lines • 2.35 kB
JavaScript
import { RavenCommand } from "../../Http/RavenCommand.js";
import { StatusCodes } from "../../Http/StatusCode.js";
import { readToEnd } from "../../Utility/StreamUtil.js";
import { ObjectUtil } from "../../Utility/ObjectUtil.js";
import { HEADERS } from "../../Constants.js";
export class ConditionalGetDocumentsCommand extends RavenCommand {
_changeVector;
_id;
_conventions;
constructor(id, changeVector, conventions) {
super();
this._changeVector = changeVector;
this._id = id;
this._conventions = conventions;
}
createRequest(node) {
const uri = node.url + "/databases/" + node.database + "/docs?id=" + this._urlEncode(this._id);
return {
uri,
method: "GET",
headers: {
[HEADERS.IF_NONE_MATCH]: `"${this._changeVector}"`
}
};
}
async setResponseAsync(bodyStream, fromCache) {
if (!bodyStream) {
this.result = null;
return;
}
let body = null;
this.result =
await ConditionalGetDocumentsCommand.parseDocumentsResultResponseAsync(bodyStream, this._conventions, b => body = b);
return body;
}
static async parseDocumentsResultResponseAsync(bodyStream, conventions, bodyCallback) {
const body = await readToEnd(bodyStream);
bodyCallback?.(body);
const parsedJson = JSON.parse(body);
return ConditionalGetDocumentsCommand._mapToLocalObject(parsedJson, conventions);
}
static _mapToLocalObject(json, conventions) {
return {
results: json.Results.map(x => ObjectUtil.transformDocumentKeys(x, conventions)),
changeVector: json.ChangeVector
};
}
async processResponse(cache, response, bodyStream, url) {
if (response.status === StatusCodes.NotModified) {
return "Automatic";
}
const result = await super.processResponse(cache, response, bodyStream, url);
this.result.changeVector = response.headers.get("ETag");
return result;
}
/**
* Here we explicitly do _NOT_ want to have caching
* by the Request Executor, we want to manage it ourselves
*/
get isReadRequest() {
return false;
}
}
//# sourceMappingURL=ConditionalGetDocumentsCommand.js.map