arangojs
Version:
The official ArangoDB JavaScript driver.
143 lines • 4.87 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Route = void 0;
const util = __importStar(require("./lib/util.js"));
/**
* Represents an arbitrary route relative to an ArangoDB database.
*/
class Route {
_db;
_pathname;
_headers;
/**
* @internal
*/
constructor(db, pathname = "", headers = {}) {
if (!pathname)
pathname = "";
else if (pathname.charAt(0) !== "/")
pathname = `/${pathname}`;
this._db = db;
this._pathname = pathname;
this._headers = headers instanceof Headers ? headers : new Headers(headers);
}
/**
* Database this route belongs to.
*/
get database() {
return this._db;
}
/**
* Path of this route.
*/
get pathname() {
return this._pathname;
}
/**
* Headers of this route.
*/
get headers() {
return this._headers;
}
/**
* Creates a new route relative to this route that inherits any of its default
* HTTP headers.
*
* @param pathname - Path relative to this route.
* @param headers - Additional headers that will be sent with each request.
*
* @example
* ```js
* const db = new Database();
* const foxx = db.route("/my-foxx-service");
* const users = foxx.route("/users");
* ```
*/
route(pathname, headers) {
return new Route(this._db, util.joinPath(this._pathname, pathname), util.mergeHeaders(this._headers, headers));
}
/**
* Performs an arbitrary HTTP request relative to this route and returns the
* server response.
*
* @param options - Options for performing the request.
*
* @example
* ```js
* const db = new Database();
* const foxx = db.route("/my-foxx-service");
* const res = await foxx.request({
* method: "POST",
* pathname: "/users",
* body: {
* username: "admin",
* password: "hunter2"
* }
* });
* ```
*/
request(options = {}) {
const { method = "GET", pathname, headers, ...opts } = options;
return this._db.request({
...opts,
method: method.toUpperCase(),
pathname: util.joinPath(this._pathname, pathname),
headers: util.mergeHeaders(this._headers, headers),
}, false);
}
delete(...args) {
const pathname = typeof args[0] === "string" ? args.shift() : undefined;
const [search, headers] = args;
return this.request({ method: "DELETE", pathname, search, headers });
}
get(...args) {
const pathname = typeof args[0] === "string" ? args.shift() : undefined;
const [search, headers] = args;
return this.request({ method: "GET", pathname, search, headers });
}
head(...args) {
const pathname = typeof args[0] === "string" ? args.shift() : undefined;
const [search, headers] = args;
return this.request({ method: "HEAD", pathname, search, headers });
}
patch(...args) {
const pathname = typeof args[0] === "string" ? args.shift() : undefined;
const [body, search, headers] = args;
return this.request({ method: "PATCH", pathname, body, search, headers });
}
post(...args) {
const pathname = typeof args[0] === "string" ? args.shift() : undefined;
const [body, search, headers] = args;
return this.request({ method: "POST", pathname, body, search, headers });
}
put(...args) {
const pathname = typeof args[0] === "string" ? args.shift() : undefined;
const [body, search, headers] = args;
return this.request({ method: "PUT", pathname, body, search, headers });
}
}
exports.Route = Route;
//# sourceMappingURL=routes.js.map
;