diff --git a/package.json b/package.json index 8b2e672..477c5b6 100644 --- a/package.json +++ b/package.json @@ -5,10 +5,6 @@ "main": "dist/index.js", "types": "dist/index.d.ts", "files": [ - "dist/client.d.ts", - "dist/client.d.ts.map", - "dist/client.js", - "dist/client.js.map", "dist/index.d.ts", "dist/index.d.ts.map", "dist/index.js", diff --git a/src/axios.ts b/src/axios.ts new file mode 100644 index 0000000..126471e --- /dev/null +++ b/src/axios.ts @@ -0,0 +1,230 @@ +import axios, { AxiosInstance, AxiosPromise, AxiosRequestConfig, AxiosResponse } from 'axios'; + +import { + CreateGraphOptionsSpec, + TableMetadata, + FileUploadOptionsSpec, + EdgesSpec, + EdgesOptionsSpec, + NodesSpec, + OffsetLimitSpec, + GraphSpec, + RowsSpec, + TablesOptionsSpec, + UserSpec, + WorkspacePermissionsSpec, +} from './index'; + +function fileToText(file: File): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = (e) => { + if (e.target === null || typeof e.target.result !== 'string') { + throw new Error(); + } + resolve(e.target.result); + }; + reader.onerror = (e) => { + reject(); + }; + + reader.readAsText(file); + }); +} + +export interface MultinetAxiosInstance extends AxiosInstance { + logout(): void; + userInfo(): AxiosPromise; + workspaces(): AxiosPromise; + getWorkspacePermissions(workspace: string): AxiosPromise; + setWorkspacePermissions(workspace: string, permissions: WorkspacePermissionsSpec): AxiosPromise; + searchUsers(query: string): AxiosPromise; + tables(workspace: string, options: TablesOptionsSpec): AxiosPromise; + table(workspace: string, table: string, options: OffsetLimitSpec): AxiosPromise; + graphs(workspace: string): AxiosPromise; + graph(workspace: string, graph: string): AxiosPromise; + nodes(workspace: string, graph: string, options: OffsetLimitSpec): AxiosPromise; + attributes(workspace: string, graph: string, nodeId: string): AxiosPromise<{}>; + edges(workspace: string, graph: string, nodeId: string, options: EdgesOptionsSpec): AxiosPromise; + createWorkspace(workspace: string): AxiosPromise; + deleteWorkspace(workspace: string): AxiosPromise; + renameWorkspace(workspace: string, name: string): AxiosPromise; + uploadTable(workspace: string, table: string, options: FileUploadOptionsSpec, config?: AxiosRequestConfig): AxiosPromise>; + downloadTable(workspace: string, table: string): AxiosPromise; + deleteTable(workspace: string, table: string): AxiosPromise; + tableMetadata(workspace: string, table: string): AxiosPromise; + createGraph(workspace: string, graph: string, options: CreateGraphOptionsSpec): AxiosPromise; + deleteGraph(workspace: string, graph: string): AxiosPromise; + aql(workspace: string, query: string): AxiosPromise; + createAQLTable(workspace: string, table: string, query: string): AxiosPromise; + downloadGraph(workspace: string, graph: string): AxiosPromise; +} + +export function multinetAxiosInstance(config: AxiosRequestConfig): MultinetAxiosInstance { + const axiosInstance = axios.create(config); + const Proto = Object.getPrototypeOf(axiosInstance); + + Proto.logout = function(): void { + this.get('/user/logout'); + } + + Proto.userInfo = function(): AxiosPromise { + return this.get('/user/info'); + } + + Proto.workspaces = function(): AxiosPromise { + return this.get('workspaces'); + }; + + Proto.getWorkspacePermissions = function(workspace: string): AxiosPromise { + return this.get(`workspaces/${workspace}/permissions`); + }; + + Proto.setWorkspacePermissions = function(workspace: string, permissions: WorkspacePermissionsSpec): AxiosPromise { + return this.put(`workspaces/${workspace}/permissions`, { + params: permissions, + }); + }; + + Proto.searchUsers = function(query: string): AxiosPromise { + return this.get('/user/search', { + params: { + query, + }, + }); + }; + + Proto.tables = function(workspace: string, options: TablesOptionsSpec = {}): AxiosPromise { + return this.get(`workspaces/${workspace}/tables`, { + params: options, + }); + }; + + Proto.table = function(workspace: string, table: string, options: OffsetLimitSpec = {}): AxiosPromise { + return this.get(`workspaces/${workspace}/tables/${table}`, { + params: options, + }); + }; + + Proto.graphs = function(workspace: string): AxiosPromise { + return this.get(`workspaces/${workspace}/graphs`); + }; + + Proto.graph = function(workspace: string, graph: string): AxiosPromise { + return this.get(`workspaces/${workspace}/graphs/${graph}`); + }; + + Proto.nodes = function(workspace: string, graph: string, options: OffsetLimitSpec = {}): AxiosPromise { + return this.get(`workspaces/${workspace}/graphs/${graph}/nodes`, { + params: options, + }); + }; + + Proto.attributes = function(workspace: string, graph: string, nodeId: string): AxiosPromise<{}> { + return this.get(`workspaces/${workspace}/graphs/${graph}/nodes/${nodeId}/attributes`); + }; + + Proto.edges = function(workspace: string, graph: string, nodeId: string, options: EdgesOptionsSpec = {}): AxiosPromise { + return this.get(`workspaces/${workspace}/graphs/${graph}/nodes/${nodeId}/edges`, { + params: options, + }); + }; + + Proto.createWorkspace = function(workspace: string): AxiosPromise { + return this.post(`/workspaces/${workspace}`); + }; + + Proto.deleteWorkspace = function(workspace: string): AxiosPromise { + return this.delete(`/workspaces/${workspace}`); + }; + + Proto.renameWorkspace = function(workspace: string, name: string): AxiosPromise { + return this.put(`workspaces/${workspace}/name`, null, { + params: { + name, + }, + }); + }; + + Proto.uploadTable = async function(workspace: string, table: string, options: FileUploadOptionsSpec, config?: AxiosRequestConfig): Promise>> { + const headers = config ? config.headers : undefined; + const params = config ? config.params : undefined; + const { type, data, key, overwrite, columnTypes } = options; + + let text; + + if (typeof data === 'string') { + text = data; + } else { + text = await fileToText(data); + } + + let metadata; + if (columnTypes) { + const columns = Object.keys(columnTypes).map((column) => ({ + key: column, + type: columnTypes[column], + })); + + metadata = { columns }; + } + + return this.post(`/${type}/${workspace}/${table}`, text, { + ...config, + headers: { ...headers, 'Content-Type': 'text/plain' }, + params: { + ...params, + key: key || undefined, + overwrite: overwrite || undefined, + metadata: metadata || undefined, + }, + }); + }; + + Proto.downloadTable = function(workspace: string, table: string): AxiosPromise { + return this.get(`/workspaces/${workspace}/tables/${table}/download`); + }; + + Proto.deleteTable = function(workspace: string, table: string): AxiosPromise { + return this.delete(`/workspaces/${workspace}/tables/${table}`); + }; + + Proto.tableMetadata = function(workspace: string, table: string): AxiosPromise { + return this.get(`/workspaces/${workspace}/tables/${table}/metadata`); + }; + + Proto.createGraph = function(workspace: string, graph: string, options: CreateGraphOptionsSpec): AxiosPromise { + return this.post(`/workspaces/${workspace}/graphs/${graph}`, { + edge_table: options.edgeTable, + }); + }; + + Proto.deleteGraph = function(workspace: string, graph: string): AxiosPromise { + return this.delete(`/workspaces/${workspace}/graphs/${graph}`); + }; + + Proto.aql = function(workspace: string, query: string): AxiosPromise { + return this.post(`/workspaces/${workspace}/aql`, query, { + headers: { + 'Content-Type': 'text/plain', + }, + }); + }; + + Proto.createAQLTable = function(workspace: string, table: string, query: string): AxiosPromise { + return this.post(`/workspaces/${workspace}/tables`, query, { + headers: { + 'Content-Type': 'text/plain', + }, + params: { + table, + }, + }); + } + + Proto.downloadGraph = function(workspace: string, graph: string): AxiosPromise { + return this.get(`/workspaces/${workspace}/graphs/${graph}/download`); + } + + return axiosInstance as MultinetAxiosInstance; +} diff --git a/src/client.ts b/src/client.ts deleted file mode 100644 index 6ebee52..0000000 --- a/src/client.ts +++ /dev/null @@ -1,57 +0,0 @@ -import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; - -export class Client { - public axios: AxiosInstance; - - constructor(baseURL: string) { - this.axios = axios.create({ baseURL, withCredentials: true }); - } - - public get(path: string, params: {} = {}): Promise { - return new Promise((resolve, reject) => { - this.axios.get(path, { params, }) - .then((resp) => { - resolve(resp.data); - }) - .catch((resp) => { - reject(resp.response); - }); - }); - } - - public post(path: string, data: any = null, params: AxiosRequestConfig = {}): Promise { - return new Promise((resolve, reject) => { - this.axios.post(path, data, params) - .then((resp) => { - resolve(resp.data); - }) - .catch((resp) => { - reject(resp.response); - }); - }); - } - - public put(path: string, data: any = null, params: AxiosRequestConfig = {}): Promise { - return new Promise((resolve, reject) => { - this.axios.put(path, data, params) - .then((resp) => { - resolve(resp.data); - }) - .catch((resp) => { - reject(resp.response); - }); - }); - } - - public delete(path: string, params: {} = {}): Promise { - return new Promise((resolve, reject) => { - this.axios.delete(path, params) - .then((resp) => { - resolve(resp.data); - }) - .catch((resp) => { - reject(resp.response); - }); - }); - } -} diff --git a/src/index.ts b/src/index.ts index 0648eda..785e972 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ -import { Client } from './client'; -import { AxiosPromise, AxiosRequestConfig } from 'axios'; +import { multinetAxiosInstance, MultinetAxiosInstance } from './axios'; + +import axios, { AxiosRequestConfig } from 'axios'; export interface TableRow { _key: string; @@ -107,151 +108,104 @@ export interface CreateGraphOptionsSpec { edgeTable: string; } -function fileToText(file: File): Promise { - return new Promise((resolve, reject) => { - const reader = new FileReader(); - reader.onload = (e) => { - if (e.target === null || typeof e.target.result !== 'string') { - throw new Error(); - } - resolve(e.target.result); - }; - reader.onerror = (e) => { - reject(); - }; - - reader.readAsText(file); - }); -} - class MultinetAPI { - public client: Client; + public axios: MultinetAxiosInstance; constructor(baseURL: string) { - this.client = new Client(baseURL); + this.axios = multinetAxiosInstance({ baseURL }); + } - public logout() { - this.client.get('/user/logout'); + public logout(): void { + this.axios.logout(); } - public userInfo(): Promise { - return this.client.get('/user/info'); + public async userInfo(): Promise { + return (await this.axios.userInfo()).data; } - public workspaces(): Promise { - return this.client.get('workspaces'); + public async workspaces(): Promise { + return (await this.axios.workspaces()).data; } - public getWorkspacePermissions(workspace: string): Promise { + public async getWorkspacePermissions(workspace: string): Promise { if (!workspace) { throw new Error('argument "workspace" must not be empty'); } - return this.client.get(`workspaces/${workspace}/permissions`); + return (await this.axios.getWorkspacePermissions(workspace)).data; } - public setWorkspacePermissions( + public async setWorkspacePermissions( workspace: string, permissions: WorkspacePermissionsSpec ): Promise { if (!workspace) { throw new Error('argument "workspace" must not be empty'); } - return this.client.put(`workspaces/${workspace}/permissions`, permissions); + return (await this.axios.setWorkspacePermissions(workspace, permissions)).data } - public searchUsers(query: string): Promise { - return this.client.get('/user/search', { query }); + public async searchUsers(query: string): Promise { + return (await this.axios.searchUsers(query)).data; } - public tables(workspace: string, options: TablesOptionsSpec = {}): Promise { - return this.client.get(`workspaces/${workspace}/tables`, options); + public async tables(workspace: string, options: TablesOptionsSpec = {}): Promise { + return (await this.axios.tables(workspace, options)).data; } - public table(workspace: string, table: string, options: OffsetLimitSpec = {}): Promise { - return this.client.get(`workspaces/${workspace}/tables/${table}`, options); + public async table(workspace: string, table: string, options: OffsetLimitSpec = {}): Promise { + return (await this.axios.table(workspace, table, options)).data; } - public graphs(workspace: string): Promise { - return this.client.get(`workspaces/${workspace}/graphs`); + public async graphs(workspace: string): Promise { + return (await this.axios.graphs(workspace)).data; } - public graph(workspace: string, graph: string): Promise { - return this.client.get(`workspaces/${workspace}/graphs/${graph}`); + public async graph(workspace: string, graph: string): Promise { + return (await this.axios.graph(workspace, graph)).data; } - public nodes(workspace: string, graph: string, options: OffsetLimitSpec = {}): Promise { - return this.client.get(`workspaces/${workspace}/graphs/${graph}/nodes`, options); + public async nodes(workspace: string, graph: string, options: OffsetLimitSpec = {}): Promise { + return (await this.axios.nodes(workspace, graph, options)).data; } - public attributes(workspace: string, graph: string, nodeId: string): Promise<{}> { - return this.client.get(`workspaces/${workspace}/graphs/${graph}/nodes/${nodeId}/attributes`); + public async attributes(workspace: string, graph: string, nodeId: string): Promise<{}> { + return (await this.axios.attributes(workspace, graph, nodeId)).data; } - public edges(workspace: string, graph: string, nodeId: string, options: EdgesOptionsSpec = {}): Promise { - return this.client.get(`workspaces/${workspace}/graphs/${graph}/nodes/${nodeId}/edges`, options); + public async edges(workspace: string, graph: string, nodeId: string, options: EdgesOptionsSpec = {}): Promise { + return (await this.axios.edges(workspace, graph, nodeId, options)).data; } - public createWorkspace(workspace: string): Promise { - return this.client.post(`/workspaces/${workspace}`); + public async createWorkspace(workspace: string): Promise { + return (await this.axios.createWorkspace(workspace)).data; } - public deleteWorkspace(workspace: string): Promise { - return this.client.delete(`/workspaces/${workspace}`); + public async deleteWorkspace(workspace: string): Promise { + return (await this.axios.deleteWorkspace(workspace)).data; } - public renameWorkspace(workspace: string, name: string): AxiosPromise { - return this.client.axios.put(`workspaces/${workspace}/name`, null, { params: { name } }); + public async renameWorkspace(workspace: string, name: string): Promise { + return (await this.axios.renameWorkspace(workspace, name)).data; } public async uploadTable( workspace: string, table: string, options: FileUploadOptionsSpec, config?: AxiosRequestConfig ): Promise> { - const headers = config ? config.headers : undefined; - const params = config ? config.params : undefined; - const { type, data, key, overwrite, columnTypes } = options; - - let text; - - if (typeof data === 'string') { - text = data; - } else { - text = await fileToText(data); - } - - let metadata; - if (columnTypes) { - const columns = Object.keys(columnTypes).map((column) => ({ - key: column, - type: columnTypes[column], - })); - - metadata = { columns }; - } - - return this.client.post(`/${type}/${workspace}/${table}`, text, { - ...config, - headers: { ...headers, 'Content-Type': 'text/plain' }, - params: { - ...params, - key: key || undefined, - overwrite: overwrite || undefined, - metadata: metadata || undefined, - }, - }); + return (await this.axios.uploadTable(workspace, table, options, config)).data; } - public downloadTable(workspace: string, table: string): AxiosPromise { - return this.client.axios.get(`/workspaces/${workspace}/tables/${table}/download`); + public async downloadTable(workspace: string, table: string): Promise { + return (await this.axios.downloadTable(workspace, table)).data; } - public deleteTable(workspace: string, table: string): Promise { - return this.client.delete(`/workspaces/${workspace}/tables/${table}`); + public async deleteTable(workspace: string, table: string): Promise { + return (await this.axios.deleteTable(workspace, table)).data; } - public tableMetadata(workspace: string, table: string): Promise { - return this.client.get(`/workspaces/${workspace}/tables/${table}/metadata`); + public async tableMetadata(workspace: string, table: string): Promise { + return (await this.axios.tableMetadata(workspace, table)).data; } public async tableColumnTypes(workspace: string, table: string): Promise { @@ -264,31 +218,24 @@ class MultinetAPI { return types; } - public createGraph(workspace: string, graph: string, options: CreateGraphOptionsSpec): Promise { - return this.client.post(`/workspaces/${workspace}/graphs/${graph}`, { - edge_table: options.edgeTable, - }); + public async createGraph(workspace: string, graph: string, options: CreateGraphOptionsSpec): Promise { + return (await this.axios.createGraph(workspace, graph, options)).data; } - public deleteGraph(workspace: string, graph: string): Promise { - return this.client.delete(`/workspaces/${workspace}/graphs/${graph}`); + public async deleteGraph(workspace: string, graph: string): Promise { + return (await this.axios.deleteGraph(workspace, graph)).data; } - public aql(workspace: string, query: string): Promise { - return this.client.post(`/workspaces/${workspace}/aql`, query, { headers: { 'Content-Type': 'text/plain' } }); + public async aql(workspace: string, query: string): Promise { + return (await this.axios.aql(workspace, query)).data; } - public createAQLTable(workspace: string, table: string, query: string): Promise { - return this.client.post(`/workspaces/${workspace}/tables`, query, { - headers: { 'Content-Type': 'text/plain' }, - params: { - table, - }, - }); + public async createAQLTable(workspace: string, table: string, query: string): Promise { + return (await this.axios.createAQLTable(workspace, table, query)).data; } - public downloadGraph(workspace: string, graph: string): AxiosPromise { - return this.client.axios.get(`/workspaces/${workspace}/graphs/${graph}/download`); + public async downloadGraph(workspace: string, graph: string): Promise { + return (await this.axios.downloadGraph(workspace, graph)).data; } }