Skip to content

Commit

Permalink
Fix the Linter (#34)
Browse files Browse the repository at this point in the history
Old lint config was not picking up nearly enough issues.
  • Loading branch information
bh2smith authored Mar 8, 2024
1 parent 3d5d5bb commit 0b9c87c
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 79 deletions.
28 changes: 28 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"parser": "@typescript-eslint/parser",
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module"
},
"rules": {
// TypeScript specific rules
"@typescript-eslint/explicit-function-return-type": ["warn", { "allowExpressions": true }],
"@typescript-eslint/type-annotation-spacing": "error",

"@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }],
"@typescript-eslint/no-shadow": ["warn"],
"no-unused-vars": "off",
"no-shadow": "off",

"semi": "error",
// Basic JavaScript/ESLint rules
"eqeqeq": ["error", "always"],
"no-unused-expressions": "warn",
"no-return-await": "error",
"no-console": ["warn", { "allow": ["warn", "error"] }]
}
}
4 changes: 2 additions & 2 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: yarn --frozen-lockfile
- run: yarn
- run: yarn build
- run: yarn lint

Expand All @@ -41,5 +41,5 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- run: yarn --frozen-lockfile
- run: yarn
- run: yarn test
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build": "tsc",
"test": "ts-mocha 'tests/**/*.spec.ts' -r dotenv/config --timeout 10000",
"fmt": "prettier --write \"./**/*.ts\"",
"lint": "eslint"
"lint": "eslint ./src"
},
"dependencies": {
"cross-fetch": "^4.0.0",
Expand All @@ -24,10 +24,12 @@
"@types/chai": "^4.3.3",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.17",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"dotenv": "^16.0.3",
"eslint": "^8.56.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"mocha": "^10.3.0",
"prettier": "^3.2.5",
Expand Down
6 changes: 3 additions & 3 deletions src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ export class DuneClient {
batchSize: number = MAX_NUM_ROWS_PER_BATCH,
pingFrequency: number = POLL_FREQUENCY_SECONDS,
): Promise<ResultsResponse> {
let { state, execution_id: jobID } = await this._runInner(
const { state, execution_id: jobID } = await this._runInner(
queryID,
params,
pingFrequency,
);
if (state === ExecutionState.COMPLETED) {
let result = await this.getLatestResult(
const result = await this.getLatestResult(
queryID,
params?.query_parameters,
batchSize,
Expand Down Expand Up @@ -99,7 +99,7 @@ export class DuneClient {
params?: ExecutionParams,
pingFrequency: number = POLL_FREQUENCY_SECONDS,
): Promise<ExecutionResponseCSV> {
let { state, execution_id: jobID } = await this._runInner(
const { state, execution_id: jobID } = await this._runInner(
queryID,
params,
pingFrequency,
Expand Down
4 changes: 2 additions & 2 deletions src/api/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class ExecutionAPI extends Router {
params: GetResultParams = DEFAULT_GET_PARAMS,
): Promise<ResultsResponse> {
// The first bit might only return a page.
let results = await this._get<ResultsResponse>(`query/${queryId}/results`, params);
const results = await this._get<ResultsResponse>(`query/${queryId}/results`, params);
return this._fetchEntireResult(results);
}

Expand All @@ -144,7 +144,7 @@ export class ExecutionAPI extends Router {
queryId: number,
params: GetResultParams = DEFAULT_GET_PARAMS,
): Promise<ExecutionResponseCSV> {
let response = await this._get<Response>(
const response = await this._get<Response>(
`query/${queryId}/results/csv`,
params,
true,
Expand Down
4 changes: 2 additions & 2 deletions src/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Router {
): Promise<T> {
const payloadData = payloadJSON(payload);
log.debug(logPrefix, `${method} received input url=${url}, payload=${payloadData}`);
let requestData: RequestInit = {
const requestData: RequestInit = {
method,
headers: {
"x-dune-api-key": this.apiKey,
Expand All @@ -91,7 +91,7 @@ export class Router {
queryParams = `?${searchParams}`;
}

let response = fetch(url + queryParams, requestData);
const response = fetch(url + queryParams, requestData);
if (raw) {
return response as T;
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/queryParameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class QueryParameter {
value: string;
name: string;

constructor(type: ParameterType, name: string, value: any) {
constructor(type: ParameterType, name: string, value: string) {
this.type = type;
this.value = value.toString();
this.name = name;
Expand Down Expand Up @@ -66,7 +66,7 @@ export class QueryParameter {
*/
static unravel(params?: QueryParameter[]): Record<string, string> | undefined {
// Transform Query Parameter list into "dict"
let reducedParams = params?.reduce<Record<string, string>>(
const reducedParams = params?.reduce<Record<string, string>>(
(acc, { name, value }) => ({ ...acc, [name]: value }),
{},
);
Expand Down
5 changes: 4 additions & 1 deletion src/types/requestPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function payloadJSON(payload?: RequestPayload): string {
return JSON.stringify(payloadRecords(payload));
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function payloadRecords(payload?: RequestPayload): Record<string, any> {
if (payload !== undefined) {
if ("query_parameters" in payload) {
Expand All @@ -41,12 +42,14 @@ function payloadRecords(payload?: RequestPayload): Record<string, any> {
return {};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function payloadSearchParams(payload?: RequestPayload): Record<string, any> {
if (payload !== undefined) {
if ("query_parameters" in payload) {
// Destructure to separate parameters and the rest of the payload
const { query_parameters, ...rest } = payload;
let result: Record<string, any> = { ...rest };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: Record<string, any> = { ...rest };
if (Array.isArray(payload.query_parameters)) {
for (const qp of payload.query_parameters) {
result[`params.${qp.name}`] = qp.value;
Expand Down
9 changes: 2 additions & 7 deletions src/types/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@ export function concatResultResponse(
left: ResultsResponse,
right: ResultsResponse,
): ResultsResponse {
const combineConditions = [
left.execution_id === right.execution_id,
left.result !== undefined,
right.result !== undefined,
];
if (left.execution_id !== right.execution_id) {
throw new Error(
`Can't combine results: ExecutionIds (${left.execution_id} != ${right.execution_id})`,
Expand All @@ -153,7 +148,7 @@ export function concatResultResponse(
throw new Error(`Can't combine results: Right Entry has no results`);
}

let { next_offset, next_uri, result: _, ...remainingValues } = right;
const { next_offset, next_uri, result: _, ...remainingValues } = right;
return {
next_uri,
next_offset,
Expand All @@ -176,7 +171,7 @@ function concatResultMetadata(
if (right === undefined) {
throw new Error("Can not concatenate with empty metadata");
}
let { row_count, result_set_bytes, datapoint_count, ...remainingValues } = right;
const { row_count, result_set_bytes, datapoint_count, ...remainingValues } = right;
return {
row_count: left.row_count + row_count,
result_set_bytes: left.result_set_bytes + result_set_bytes,
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const logPrefix = "dune-client:";
* @param seconds number of seconds to sleep for.
* @returns void
*/
export function sleep(seconds: number) {
export function sleep(seconds: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
}

Expand All @@ -18,7 +18,7 @@ export function ageInHours(timestamp: Date | string): number {
// Get the current date and time
const now: Date = new Date();
// Given date time:
let time = new Date(timestamp);
const time = new Date(timestamp);
// Calculate the difference in milliseconds
const resultAge: number = now.getTime() - time.getTime();
// Convert milliseconds to hours and return
Expand Down
22 changes: 6 additions & 16 deletions tests/e2e/executionAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ log.setLevel("silent", true);
describe("ExecutionAPI: native routes", () => {
let client: ExecutionAPI;
let testQueryId: number;
let multiRowQueryId: number;

beforeEach(() => {
client = new ExecutionAPI(BASIC_KEY);
Expand All @@ -32,7 +31,7 @@ describe("ExecutionAPI: native routes", () => {

// Cancel execution and verify it was canceled.
const canceled = await client.cancelExecution(execution.execution_id);
expect(canceled).to.be.true;
expect(canceled).to.be.eq(true);

// Get execution status
const status = await client.getExecutionStatus(execution.execution_id);
Expand Down Expand Up @@ -60,14 +59,14 @@ describe("ExecutionAPI: native routes", () => {
const execution = await client.executeQuery(testQueryId, {
query_parameters: parameters,
});
expect(execution.execution_id).is.not.null;
expect(execution.execution_id).is.not.eq(null);
});

it("execute with Large tier performance", async () => {
const execution = await client.executeQuery(testQueryId, {
performance: ExecutionPerformance.Large,
});
expect(execution.execution_id).is.not.null;
expect(execution.execution_id).is.not.eq(null);
});

it("returns expected results on cancelled query exection", async () => {
Expand All @@ -92,13 +91,13 @@ describe("ExecutionAPI: native routes", () => {
const execution = await client.executeQuery(testQueryId);
await sleep(1);
// expect basic query has completed after 1s
let status = await client.getExecutionStatus(execution.execution_id);
const status = await client.getExecutionStatus(execution.execution_id);
expect(status.state).to.be.eq(ExecutionState.COMPLETED);

// let resultJSON = await client.getExecutionResults(execution.execution_id);
await expect(() => client.getExecutionResults(execution.execution_id)).to.not.throw();

let resultCSV = await client.getResultCSV(execution.execution_id);
const resultCSV = await client.getResultCSV(execution.execution_id);
const expectedRows = [
"text_field,number_field,date_field,list_field\n",
"Plain Text,3.1415926535,2022-05-04T00:00:00Z,Option 1\n",
Expand Down Expand Up @@ -167,20 +166,11 @@ describe("ExecutionAPI: Errors", () => {
client = new ExecutionAPI(BASIC_KEY);
});

beforeEach(function () {
const client = new ExecutionAPI(BASIC_KEY);
});

it("returns invalid API key", async () => {
const bad_client = new ExecutionAPI("Bad Key");
await expectAsyncThrow(bad_client.executeQuery(1), "invalid API Key");
});
it("returns Invalid request path (queryId too large)", async () => {
await expectAsyncThrow(
client.executeQuery(99999999999999999999999999),
"Invalid request path",
);
});

it("returns query not found error", async () => {
await expectAsyncThrow(client.executeQuery(999999999), "Query not found");
await expectAsyncThrow(client.executeQuery(0), "Query not found");
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/queryAPI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ describe("QueryAPI: Premium - CRUD Operations", () => {

// This creates too many queries!
it.skip("create, get & update", async () => {
let newQueryId = await plusClient.createQuery({
const newQueryId = await plusClient.createQuery({
name: "Query Name",
query_sql: "select 1",
query_parameters: [QueryParameter.text("What", "name")],
is_private: true,
});
let recoveredQuery = await plusClient.readQuery(newQueryId);
const recoveredQuery = await plusClient.readQuery(newQueryId);
expect(newQueryId).to.be.equal(newQueryId);
let updatedQueryId = await plusClient.updateQuery(newQueryId, {
const updatedQueryId = await plusClient.updateQuery(newQueryId, {
name: "New Name",
query_sql: "select 10",
});
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const BASIC_KEY: string = BASIC_API_KEY!;
export const PLUS_KEY: string = PLUS_API_KEY!;

export const expectAsyncThrow = async (
promise: Promise<any>,
promise: Promise<unknown>,
message?: string | object,
) => {
): Promise<void> => {
try {
await promise;
// Make sure to fail if promise does resolve!
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import {

describe("Type Concatenation", () => {
it("concatResultCSV", async () => {
let left = {
const left = {
data: "col_1\n1\n2\n",
next_offset: 1,
next_uri: "left",
};
let right: ExecutionResponseCSV = {
const right: ExecutionResponseCSV = {
data: "col_1\n3\n4\n",
next_offset: 2,
next_uri: "right",
};

let result = concatResultCSV(left, right);
const result = concatResultCSV(left, right);
expect(result).to.be.deep.equal({
data: "col_1\n1\n2\n3\n4\n",
next_offset: 2,
Expand Down Expand Up @@ -69,22 +69,22 @@ describe("Type Concatenation", () => {
},
};

let left: ResultsResponse = {
const left: ResultsResponse = {
execution_id: "XYZ",
next_offset: 0,
next_uri: "left",
result: leftResult,
...irrelevantFieldsForTest,
};

let right: ResultsResponse = {
const right: ResultsResponse = {
execution_id: "XYZ",
next_offset: 1,
next_uri: "right",
result: rightResult,
...irrelevantFieldsForTest,
};
let result = concatResultResponse(left, right);
const result = concatResultResponse(left, right);

expect(result).to.be.deep.equal({
execution_id: "XYZ",
Expand Down
3 changes: 0 additions & 3 deletions tslint.json

This file was deleted.

Loading

0 comments on commit 0b9c87c

Please sign in to comment.