Skip to content

Commit

Permalink
convert repo as ES Module
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Oct 25, 2023
1 parent 59d3ac9 commit db19388
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
package-lock.json
substreams-atomicmarket-api

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
Expand Down
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from "./src/config";
import { logger } from "./src/logger";
import GET from "./src/fetch/GET";
import { config } from "./src/config.js";
import { logger } from "./src/logger.js";
import GET from "./src/fetch/GET.js";
import * as prometheus from "./src/prometheus.js";

if (config.verbose) logger.enable();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Atomicmarket sales API powered by Substreams",
"version": "v0.2.0",
"homepage": "https://github.com/pinax-network/substreams-atomicmarket-api",
"type": "module",
"author": {
"name": "Paul Meignan",
"email": "paulm@pinax.network",
Expand Down
6 changes: 3 additions & 3 deletions src/clickhouse/makeQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { logger } from "../logger";
import * as prometheus from "../prometheus";
import client from "./createClient";
import { logger } from "../logger.js";
import * as prometheus from "../prometheus.js";
import client from "./createClient.js";

export interface Meta {
name: string,
Expand Down
2 changes: 1 addition & 1 deletion src/clickhouse/ping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PingResult } from "@clickhouse/client-web";
import client from "./createClient";
import client from "./createClient.js";

// Does not work with Bun's implementation of Node streams.
export async function ping(): Promise<PingResult> {
Expand Down
8 changes: 2 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import { Option, program } from "commander";

import pkg from "../package.json";

export const DEFAULT_PORT = "3001";
export const DEFAULT_PORT = "8080";
export const DEFAULT_HOSTNAME = "localhost";
export const DEFAULT_HOST = "http://localhost:8123";
export const DEFAULT_DATABASE = "default";
export const DEFAULT_TABLE = "demo";
export const DEFAULT_USERNAME = "default";
export const DEFAULT_PASSWORD = "";
export const DEFAULT_MAX_LIMIT = 500;
export const DEFAULT_MAX_LIMIT = 10000;
export const DEFAULT_VERBOSE = false;
export const APP_NAME = pkg.name;
export const DEFAULT_SORT_BY = "DESC";


// parse command line options
const opts = program
.name(pkg.name)
Expand All @@ -30,7 +28,6 @@ const opts = program
.addOption(new Option("--username <string>", "Database user").env("USERNAME").default(DEFAULT_USERNAME))
.addOption(new Option("--password <string>", "Password associated with the specified username").env("PASSWORD").default(DEFAULT_PASSWORD))
.addOption(new Option("--database <string>", "The database to use inside ClickHouse").env("DATABASE").default(DEFAULT_DATABASE))
.addOption(new Option("--table <string>", "Clickhouse table name").env("TABLE").default(DEFAULT_TABLE))
.addOption(new Option("--max-limit <number>", "Maximum LIMIT queries").env("MAX_LIMIT").default(DEFAULT_MAX_LIMIT))
.parse()
.opts();
Expand All @@ -39,7 +36,6 @@ export const config = z.object({
port: z.string(),
hostname: z.string(),
host: z.string(),
table: z.string(),
database: z.string(),
username: z.string(),
password: z.string(),
Expand Down
6 changes: 3 additions & 3 deletions src/fetch/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import pkg from "../../package.json" assert { type: "json" };

import { OpenApiBuilder, SchemaObject, ExampleObject, ParameterObject } from "openapi3-ts/oas31";
import { config } from "../config";
import { registry } from "../prometheus";
import { makeQuery } from "../clickhouse/makeQuery";
import { config } from "../config.js";
import { registry } from "../prometheus.js";
import { makeQuery } from "../clickhouse/makeQuery.js";

const TAGS = {
MONITORING: "Monitoring",
Expand Down
5 changes: 2 additions & 3 deletions src/fetch/salescount.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { makeQuery } from "../clickhouse/makeQuery.js";
import { logger } from "../logger.js";
import { DEFAULT_SORT_BY, config } from '../config';
import * as prometheus from "../prometheus.js";
import { getSalesCount } from "../queries.js";

export default async function (req: Request) {
try {
const { searchParams } = new URL(req.url);
logger.info({searchParams: Object.fromEntries(Array.from(searchParams))});
const collection_name = searchParams.get("collection_name");
const query = `SELECT count(sale_id)FROM ${config.table} WHERE collection_name = '${collection_name}'`;
const query = getSalesCount(searchParams);
const response = await makeQuery(query)
return new Response(JSON.stringify(response.data), { headers: { "Content-Type": "application/json" } });
} catch (e: any) {
Expand Down
4 changes: 2 additions & 2 deletions src/fetch/totalvolume.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { makeQuery } from "../clickhouse/makeQuery.js";
import { logger } from "../logger.js";
import { DEFAULT_SORT_BY, config } from '../config';
import { DEFAULT_SORT_BY, config } from '../config.js';
import * as prometheus from "../prometheus.js";

export default async function (req: Request) {
try {
const { searchParams } = new URL(req.url);
logger.info({searchParams: Object.fromEntries(Array.from(searchParams))});
const collection_name = searchParams.get("collection_name");
const query = `SELECT sum(listing_price) FROM ${config.table} WHERE collection_name = '${collection_name}'`;
const query = `SELECT sum(listing_price) FROM Sales WHERE collection_name = '${collection_name}'`;
const response = await makeQuery(query)
return new Response(JSON.stringify(response.data), { headers: { "Content-Type": "application/json" } });
} catch (e: any) {
Expand Down
14 changes: 14 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module "*.png" {
const content: string;
export default content;
}

declare module "*.html" {
const content: string;
export default content;
}

declare module "*.sql" {
const content: string;
export default content;
}
43 changes: 9 additions & 34 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
{
"compilerOptions": {
// add Bun type definitions
"types": ["bun-types"],

// enable latest features
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",

// if TS 5.x+
"moduleResolution": "bundler",
"noEmit": true,
"allowImportingTsExtensions": true,
"moduleDetection": "force",
// if TS 4.x or earlier
// "moduleResolution": "nodenext",

"jsx": "react-jsx", // support JSX
"allowJs": true, // allow importing `.js` from `.ts`

// best practices
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"resolveJsonModule": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"strictNullChecks": true,
"alwaysStrict": true,
"skipLibCheck": true,
"composite": true,
"downlevelIteration": true,
"allowSyntheticDefaultImports": true
},
"include": [
"index.ts",
"src/**/*.ts",
"package.json"
],
"exclude": [
"node_modules",
"src/tests/*.spec.ts"
]
"types": ["bun-types"]
}
}

0 comments on commit db19388

Please sign in to comment.