Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Route Delete Table #47

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum RequestMethod {
GET = "GET",
POST = "POST",
PATCH = "PATCH",
DELETE = "DELETE",
}

/**
Expand Down Expand Up @@ -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;
}
Expand All @@ -136,6 +137,10 @@ export class Router {
return this._request<T>(RequestMethod.GET, this.url(route), params, raw);
}

protected async _delete<T>(route: string): Promise<T> {
return this._request<T>(RequestMethod.DELETE, this.url(route));
}

protected async _getByUrl<T>(
url: string,
params?: RequestPayload,
Expand Down
13 changes: 13 additions & 0 deletions src/api/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
CreateTableArgs,
InsertTableArgs,
InsertTableResult,
DeleteTableArgs,
DeleteTableResult,
} from "../types";
import { withDefaults } from "../utils";

Expand Down Expand Up @@ -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<DeleteTableResult> {
const route = `table/${args.namespace}/${args.table_name}`;
return this._delete<DeleteTableResult>(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.
Expand Down
7 changes: 7 additions & 0 deletions src/types/requestArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 20 additions & 9 deletions tests/e2e/table.spec.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) => {
Expand All @@ -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,
Expand All @@ -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`,
});
Expand All @@ -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,
});
Expand All @@ -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`,
});
});
});
3 changes: 2 additions & 1 deletion tests/e2e/util.ts
Original file line number Diff line number Diff line change
@@ -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");
}
Expand All @@ -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<unknown>,
Expand Down