arangojs
Version:
The official ArangoDB JavaScript driver.
116 lines • 3.72 kB
JavaScript
import * as util from "./lib/util.js";
/**
* Represents an arbitrary route relative to an ArangoDB database.
*/
export 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 });
}
}
//# sourceMappingURL=routes.js.map