From 6c48edc49cf24a65831dcb2155c8200ee1424467 Mon Sep 17 00:00:00 2001 From: Ben Smith Date: Thu, 4 Apr 2024 21:48:33 +0200 Subject: [PATCH] add delete table route --- src/api/router.ts | 13 +++++++++---- src/api/table.ts | 13 +++++++++++++ src/types/requestArgs.ts | 7 +++++++ src/types/response.ts | 5 +++++ tests/e2e/table.spec.ts | 29 ++++++++++++++++++++--------- tests/e2e/util.ts | 3 ++- 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/src/api/router.ts b/src/api/router.ts index c37b59c..c10c21b 100644 --- a/src/api/router.ts +++ b/src/api/router.ts @@ -16,6 +16,7 @@ enum RequestMethod { GET = "GET", POST = "POST", PATCH = "PATCH", + DELETE = "DELETE", } /** @@ -114,14 +115,14 @@ export class Router { body, }), }; - let queryParams = ""; + let pathParams = ""; /// Build Url Search Parameters on GET if (method === "GET" && payload) { const searchParams = new URLSearchParams(payloadSearchParams(payload)).toString(); - queryParams = `?${searchParams}`; + pathParams = `?${searchParams}`; } - log.debug("Final request URL", url + queryParams); - const response = fetch(url + queryParams, requestData); + log.debug("Final request URL", url + pathParams); + const response = fetch(url + pathParams, requestData); if (raw) { return response as T; } @@ -136,6 +137,10 @@ export class Router { return this._request(RequestMethod.GET, this.url(route), params, raw); } + protected async _delete(route: string): Promise { + return this._request(RequestMethod.DELETE, this.url(route)); + } + protected async _getByUrl( url: string, params?: RequestPayload, diff --git a/src/api/table.ts b/src/api/table.ts index 5d38339..d85ddad 100644 --- a/src/api/table.ts +++ b/src/api/table.ts @@ -7,6 +7,8 @@ import { CreateTableArgs, InsertTableArgs, InsertTableResult, + DeleteTableArgs, + DeleteTableResult, } from "../types"; import { withDefaults } from "../utils"; @@ -49,6 +51,17 @@ export class TableAPI extends Router { ); } + /** + * https://docs.dune.com/api-reference/tables/endpoint/delete + * Delete a Dune table with the specified name and namespace. + * + * To be able to delete a table, it must have been created with the /create endpoint. + */ + async delete(args: DeleteTableArgs): Promise { + const route = `table/${args.namespace}/${args.table_name}`; + return this._delete(route); + } + /** * https://docs.dune.com/api-reference/tables/endpoint/insert * The insert table endpoint allows you to insert data into an existing table in Dune. diff --git a/src/types/requestArgs.ts b/src/types/requestArgs.ts index 9ed4d0e..0b93f8e 100644 --- a/src/types/requestArgs.ts +++ b/src/types/requestArgs.ts @@ -224,6 +224,13 @@ export interface SchemaRecord { type: ColumnType; } +export interface DeleteTableArgs { + /// The namespace of the table to delete (e.g. my_user). + namespace: string; + /// The name of the table to delete (e.g. interest_rates). + table_name: string; +} + export interface CreateTableArgs { /// A description of the table. description?: string; diff --git a/src/types/response.ts b/src/types/response.ts index ad72b19..06838e5 100644 --- a/src/types/response.ts +++ b/src/types/response.ts @@ -189,6 +189,11 @@ function concatResultMetadata( }; } +export interface DeleteTableResult { + /// The confirmation message of the deleted table + message: string; +} + export interface CreateTableResult { /// An example query to use on Dune querying your new table. example_query: string; diff --git a/tests/e2e/table.spec.ts b/tests/e2e/table.spec.ts index 26e64d0..938762e 100644 --- a/tests/e2e/table.spec.ts +++ b/tests/e2e/table.spec.ts @@ -1,6 +1,6 @@ import { expect } from "chai"; import log from "loglevel"; -import { PLUS_KEY } from "./util"; +import { PLUS_KEY, DUNE_USER_NAME } from "./util"; import * as fs from "fs/promises"; import { TableAPI } from "../../src/api"; import { ColumnType, ContentType } from "../../src"; @@ -9,9 +9,12 @@ log.setLevel("silent", true); describe("Table API", () => { let tableClient: TableAPI; + let namespace: string; + const table_name = "dataset_e2e_test"; before(() => { tableClient = new TableAPI(PLUS_KEY); + namespace = DUNE_USER_NAME; }); beforeEach((done) => { @@ -36,8 +39,6 @@ describe("Table API", () => { // Skipped because needs valid user name. it.skip("creates table", async () => { - const namespace = "your_username"; - const table_name = "dataset_e2e_test"; const createResult = await tableClient.create({ namespace, table_name, @@ -50,8 +51,8 @@ describe("Table API", () => { }); expect(createResult).to.be.deep.equal({ - namespace: namespace, - table_name: table_name, + namespace, + table_name, full_name: `dune.${namespace}.${table_name}`, example_query: `select * from dune.${namespace}.${table_name} limit 10`, }); @@ -60,8 +61,8 @@ describe("Table API", () => { it.skip("inserts JSON to Table", async () => { const data: Buffer = await fs.readFile("./tests/fixtures/sample_table_insert.json"); const insertResult = await tableClient.insert({ - namespace: "your_username", - table_name: "dataset_e2e_test", + namespace, + table_name, data, content_type: ContentType.NDJson, }); @@ -72,11 +73,21 @@ describe("Table API", () => { it.skip("inserts CSV to Table", async () => { const data = await fs.readFile("./tests/fixtures/sample_table_insert.csv"); const insertResult = await tableClient.insert({ - namespace: "your_username", - table_name: "dataset_e2e_test", + namespace, + table_name, data, content_type: ContentType.Csv, }); expect(insertResult).to.be.deep.equal({ rows_written: 1 }); }); + + it.skip("deletes table", async () => { + const result = await tableClient.delete({ + namespace, + table_name, + }); + expect(result).to.be.deep.equal({ + message: `Table ${namespace}.dataset_e2e_test successfully deleted`, + }); + }); }); diff --git a/tests/e2e/util.ts b/tests/e2e/util.ts index 8a6e27b..9c5af5b 100644 --- a/tests/e2e/util.ts +++ b/tests/e2e/util.ts @@ -1,7 +1,7 @@ import { expect } from "chai"; import { DuneError } from "../../src"; -const { BASIC_API_KEY, PLUS_API_KEY } = process.env; +const { BASIC_API_KEY, PLUS_API_KEY, DUNE_USER_NAME } = process.env; if (BASIC_API_KEY === undefined) { throw Error("Missing ENV var: BASIC_API_KEY"); } @@ -10,6 +10,7 @@ if (PLUS_API_KEY === undefined) { } export const BASIC_KEY: string = BASIC_API_KEY!; export const PLUS_KEY: string = PLUS_API_KEY!; +export const DUNE_USER_NAME: string = DUNE_USER_NAME || "your_username"; export const expectAsyncThrow = async ( promise: Promise,