diff --git a/arango.ts b/arango.ts new file mode 100644 index 0000000..0030369 --- /dev/null +++ b/arango.ts @@ -0,0 +1,124 @@ +import { axiod } from "./deps.ts"; +import { Collection } from "./collection.ts"; +import { ArangoCursor, CursorOptions } from "./cursor.ts"; + +/** + * Base options required. + */ +interface ArangoOptionsBase { + uri: string; +} + +/** + * Basic Auth options. + */ +interface ArangoOptionsBasicAuth extends ArangoOptionsBase { + username: string; + password: string; +} + +/** + * JWT Auth options. + */ +interface ArangoOptionsJWT extends ArangoOptionsBase { + jwt: string; +} + +/** + * The ArangoDB client. + */ +export class Arango { + private ax: typeof axiod; + constructor( + private readonly uri: string, + private jwt: string, + ) { + this.ax = axiod.create({ + "baseURL": this.uri, + "headers": { + "Authorization": `bearer ${this.jwt}`, + }, + }); + } + /** + * Create a new Arango instance by first obtaining a valid JWT token. + * @param options Options required for Basic Auth. + * @returns A new Arango instance. + */ + public static async basicAuth( + options: ArangoOptionsBasicAuth, + ): Promise { + const res = await axiod({ + "baseURL": options.uri, + "url": "/_open/auth", + "method": "post", + "data": { username: options.username, password: options.password }, + }); + if (res.status != 200) { + throw new Error(`Bad response from server: ${res.statusText}`); + } + return new Arango(options.uri, res.data.jwt); + } + /** + * Create a new Arango instance using a previously obtained JWT token, validating it first. + * @param options Options required for JWT Auth. + * @returns A new Arango instance. + */ + public static async jwtAuth(options: ArangoOptionsJWT): Promise { + const res = await axiod({ + "baseURL": options.uri, + "url": "/_api/database/user", + headers: { + "Authorization": `bearer ${options.jwt}`, + }, + "method": "get", + }); + if (res.status != 200) { + throw new Error(`Bad response from server: ${res.statusText}`); + } + return new Arango(options.uri, options.jwt); + } + /** + * Create a new Cursor to execute a query. NOTE: Data returned by this cannot be turned into a Document object for direct modification. + * @param aql The query string to be executed. + * @param options Cursor options. + * @returns A new ArangoCursor that can be asynchronously iterated or collected. + */ + public query(aql: string, options?: CursorOptions): ArangoCursor { + return new ArangoCursor(this.ax, aql, options); + } + /** + * Get an existing collection by name. + * @param name The name of the collection to get. + * @returns A Collection instance. + */ + public async collection(name: string): Promise> { + const res = await this.ax.get(`/_api/collection/${name}`); + if (res.status != 200) throw new Error("Unable to find collection."); + return new Collection(this.ax, name); + } + /** + * + * @param name The name of the collection. + * @param edge Optional. Make this an edge collection. + * @returns The new collection as a Collection instance. + */ + public async createCollection( + name: string, + edge?: boolean, + ): Promise> { + const res = await this.ax.post("/_api/collection", { + name, + type: edge ? 3 : 2, + }); + switch (res.status) { + case 400: + case 404: + throw new Error( + `Unable to create collection: ${res.data.errorMessage}`, + ); + default: + return new Collection(this.ax, name); + } + } +} diff --git a/mod.ts b/mod.ts index 0030369..90c33b9 100644 --- a/mod.ts +++ b/mod.ts @@ -1,124 +1,5 @@ -import { axiod } from "./deps.ts"; -import { Collection } from "./collection.ts"; -import { ArangoCursor, CursorOptions } from "./cursor.ts"; - -/** - * Base options required. - */ -interface ArangoOptionsBase { - uri: string; -} - -/** - * Basic Auth options. - */ -interface ArangoOptionsBasicAuth extends ArangoOptionsBase { - username: string; - password: string; -} - -/** - * JWT Auth options. - */ -interface ArangoOptionsJWT extends ArangoOptionsBase { - jwt: string; -} - -/** - * The ArangoDB client. - */ -export class Arango { - private ax: typeof axiod; - constructor( - private readonly uri: string, - private jwt: string, - ) { - this.ax = axiod.create({ - "baseURL": this.uri, - "headers": { - "Authorization": `bearer ${this.jwt}`, - }, - }); - } - /** - * Create a new Arango instance by first obtaining a valid JWT token. - * @param options Options required for Basic Auth. - * @returns A new Arango instance. - */ - public static async basicAuth( - options: ArangoOptionsBasicAuth, - ): Promise { - const res = await axiod({ - "baseURL": options.uri, - "url": "/_open/auth", - "method": "post", - "data": { username: options.username, password: options.password }, - }); - if (res.status != 200) { - throw new Error(`Bad response from server: ${res.statusText}`); - } - return new Arango(options.uri, res.data.jwt); - } - /** - * Create a new Arango instance using a previously obtained JWT token, validating it first. - * @param options Options required for JWT Auth. - * @returns A new Arango instance. - */ - public static async jwtAuth(options: ArangoOptionsJWT): Promise { - const res = await axiod({ - "baseURL": options.uri, - "url": "/_api/database/user", - headers: { - "Authorization": `bearer ${options.jwt}`, - }, - "method": "get", - }); - if (res.status != 200) { - throw new Error(`Bad response from server: ${res.statusText}`); - } - return new Arango(options.uri, options.jwt); - } - /** - * Create a new Cursor to execute a query. NOTE: Data returned by this cannot be turned into a Document object for direct modification. - * @param aql The query string to be executed. - * @param options Cursor options. - * @returns A new ArangoCursor that can be asynchronously iterated or collected. - */ - public query(aql: string, options?: CursorOptions): ArangoCursor { - return new ArangoCursor(this.ax, aql, options); - } - /** - * Get an existing collection by name. - * @param name The name of the collection to get. - * @returns A Collection instance. - */ - public async collection(name: string): Promise> { - const res = await this.ax.get(`/_api/collection/${name}`); - if (res.status != 200) throw new Error("Unable to find collection."); - return new Collection(this.ax, name); - } - /** - * - * @param name The name of the collection. - * @param edge Optional. Make this an edge collection. - * @returns The new collection as a Collection instance. - */ - public async createCollection( - name: string, - edge?: boolean, - ): Promise> { - const res = await this.ax.post("/_api/collection", { - name, - type: edge ? 3 : 2, - }); - switch (res.status) { - case 400: - case 404: - throw new Error( - `Unable to create collection: ${res.data.errorMessage}`, - ); - default: - return new Collection(this.ax, name); - } - } -} +export { Arango } from "./arango.ts"; +export { ArangoCursor } from "./cursor.ts"; +export type { Cursor, CursorOptions } from "./cursor.ts"; +export { Collection } from "./collection.ts"; +export type { Document, DocumentBase, DocumentData } from "./collection.ts";