Skip to content

Commit

Permalink
feat: factor out common.ts and errors.ts into a utils package (#1587)
Browse files Browse the repository at this point in the history
* There's a bunch of TypeScript utilities that we can use across the
  monorepo here
  • Loading branch information
ryscheng committed Jun 4, 2024
1 parent 57d88f3 commit 73cf528
Show file tree
Hide file tree
Showing 20 changed files with 277 additions and 93 deletions.
2 changes: 1 addition & 1 deletion apps/frontend/components/dataprovider/auth-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
CommonDataProviderRegistration,
DataProviderView,
} from "./provider-view";
import { spawn } from "@opensource-observer/utils";
import { RegistrationProps } from "../../lib/types/plasmic";
import { logger } from "../../lib/logger";
import { spawn } from "../../lib/common";
import { analytics } from "../../lib/clients/segment";
import { supabaseClient } from "../../lib/clients/supabase";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from "@apollo/experimental-nextjs-app-support/ssr";
import dayjs from "dayjs";
import _ from "lodash";
import React from "react";
import { assertNever, ensure, uncheckedCast } from "../../lib/common";
import { assertNever, ensure, uncheckedCast } from "@opensource-observer/utils";
import {
GET_ARTIFACTS_BY_IDS,
GET_PROJECTS_BY_IDS,
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/components/widgets/analytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { useEffect } from "react";
import { usePathname, useSearchParams } from "next/navigation";
import { spawn } from "@opensource-observer/utils";
import { analytics } from "../../lib/clients/segment";
import { spawn } from "../../lib/common";

function Analytics() {
const pathname = usePathname();
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/components/widgets/auth-actions.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { ReactNode } from "react";
import { Provider } from "@supabase/supabase-js";
import { useRouter, usePathname } from "next/navigation";
import { assertNever, spawn } from "@opensource-observer/utils";
import { supabaseClient } from "../../lib/clients/supabase";
import { RegistrationProps } from "../../lib/types/plasmic";
import { assertNever, spawn } from "../../lib/common";
import { NODE_ENV, DOMAIN } from "../../lib/config";

type AuthActionType = "signInWithOAuth" | "signOut";
Expand Down
3 changes: 1 addition & 2 deletions apps/frontend/components/widgets/supabase-write.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { useRouter } from "next/navigation";
import Snackbar from "@mui/material/Snackbar";
import Alert from "@mui/material/Alert";
import { ADT } from "ts-adt";
import { HttpError, assertNever, spawn } from "@opensource-observer/utils";
import { RegistrationProps } from "../../lib/types/plasmic";
import { HttpError } from "../../lib/types/errors";
import { assertNever, spawn } from "../../lib/common";
import { supabaseClient } from "../../lib/clients/supabase";

type SnackbarState = ADT<{
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/lib/clients/supabase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createClient } from "@supabase/supabase-js";
import { HttpError } from "../types/errors";
import { HttpError } from "@opensource-observer/utils";
import {
SUPABASE_URL,
SUPABASE_ANON_KEY,
Expand Down
2 changes: 1 addition & 1 deletion apps/frontend/lib/projects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as _ from "lodash";
import { generate } from "random-words";
import { v4 as uuid4 } from "uuid";
import { safeCast } from "./common";
import { safeCast } from "@opensource-observer/utils";
import {
Collection,
NullSortingState,
Expand Down
1 change: 1 addition & 0 deletions apps/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@mui/icons-material": "^5.15.6",
"@mui/material": "^5.15.6",
"@mui/x-date-pickers": "^6.19.0",
"@opensource-observer/utils": "workspace:*",
"@plasmicapp/loader-nextjs": "^1.0.360",
"@segment/analytics-next": "^1.66.0",
"@supabase/auth-helpers-nextjs": "^0.8.7",
Expand Down
12 changes: 6 additions & 6 deletions lib/oss-artifact-validators/src/onchain/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface EVMNetworkValidator {
isDeployer(addr: string): Promise<boolean>;
}

export interface GenericEVMNetworkValidtorOptions {
interface GenericEVMNetworkValidatorOptions {
rpcUrl: string;
deployerTable: string;
bqOptions?: BigQueryOptions;
Expand All @@ -24,7 +24,7 @@ export class GenericEVMNetworkValidator implements EVMNetworkValidator {
private deployerTable: string;

static create(
options: GenericEVMNetworkValidtorOptions,
options: GenericEVMNetworkValidatorOptions,
): EVMNetworkValidator {
const web3 = new Web3(options.rpcUrl);
const bq = new BigQuery(options.bqOptions);
Expand Down Expand Up @@ -71,7 +71,7 @@ export class GenericEVMNetworkValidator implements EVMNetworkValidator {
}

export type EthereumOptions = Omit<
GenericEVMNetworkValidtorOptions,
GenericEVMNetworkValidatorOptions,
"deployerTable"
>;

Expand All @@ -84,7 +84,7 @@ export function EthereumValidator(options: EthereumOptions) {
}

export type ArbitrumOptions = Omit<
GenericEVMNetworkValidtorOptions,
GenericEVMNetworkValidatorOptions,
"deployerTable"
>;

Expand All @@ -97,7 +97,7 @@ export function ArbitrumValidator(options: ArbitrumOptions) {
}

export type BaseOptions = Omit<
GenericEVMNetworkValidtorOptions,
GenericEVMNetworkValidatorOptions,
"deployerTable"
>;

Expand All @@ -110,7 +110,7 @@ export function BaseValidator(options: BaseOptions) {
}

export type OptimismOptions = Omit<
GenericEVMNetworkValidtorOptions,
GenericEVMNetworkValidatorOptions,
"deployerTable"
>;

Expand Down
7 changes: 7 additions & 0 deletions lib/utils/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": ["../../.eslintrc.js"],
"root": false,
"parserOptions": {
"project": ["./lib/utils/tsconfig.json"]
}
}
3 changes: 3 additions & 0 deletions lib/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OSO Utils

Utility functions used in the OSO monorepo
39 changes: 39 additions & 0 deletions lib/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@opensource-observer/utils",
"version": "0.0.1",
"description": "Utility functions used in OSO",
"author": "Kariba Labs",
"license": "Apache-2.0",
"private": false,
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"type": "module",
"files": [
"dist",
"README.md",
"package.json"
],
"repository": {
"type": "git",
"url": "git+https://github.com/opensource-observer/oso.git"
},
"engines": {
"node": ">=20"
},
"scripts": {
"build": "tsc",
"lint": "tsc --noEmit && pnpm lint:eslint && pnpm lint:prettier",
"lint:eslint": "eslint --ignore-path ../../.gitignore --max-warnings 0 .",
"lint:prettier": "prettier --ignore-path ../../.gitignore --log-level warn --check **/*.{js,jsx,ts,tsx,sol,md,json}"
},
"keywords": [],
"devDependencies": {
"@types/node": "^20.14.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
},
"dependencies": {
"@types/lodash": "^4.17.4",
"lodash": "^4.17.21"
}
}
2 changes: 1 addition & 1 deletion apps/frontend/lib/common.ts → lib/utils/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NullOrUndefinedValueError, AssertionError } from "./types/errors";
import { NullOrUndefinedValueError, AssertionError } from "./errors.js";
/**
* Explicitly marks a promise as something we won't await
* @param _promise
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions lib/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./errors.js";
export * from "./common.js";
21 changes: 21 additions & 0 deletions lib/utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"outDir": "dist",
"target": "ES6",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
},
"exclude": ["node_modules"],
"include": ["./src/*.ts", "./src/**/*.ts", "./test"],
}
1 change: 1 addition & 0 deletions ops/external-prs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@google-cloud/bigquery": "^7.5.1",
"@opensource-observer/oss-artifact-validators": "workspace:*",
"@opensource-observer/utils": "workspace:*",
"@types/columnify": "^1.5.4",
"@types/libsodium-wrappers": "^0.7.13",
"@types/mustache": "^4.2.5",
Expand Down
4 changes: 3 additions & 1 deletion ops/external-prs/src/ossd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
BaseValidator,
OptimismValidator,
} from "@opensource-observer/oss-artifact-validators";
import { uncheckedCast } from "@opensource-observer/utils";
import { GithubOutput } from "../github.js";
import { CheckConclusion, CheckStatus } from "../checks.js";

Expand Down Expand Up @@ -642,7 +643,8 @@ class OSSDirectoryPullRequest {
for (const item of this.changes.artifacts.toValidate.blockchain) {
const address = item.address;
for (const network of item.networks) {
const validator = this.validators[network as BlockchainNetwork];
const validator =
this.validators[uncheckedCast<BlockchainNetwork>(network)];
if (!validator) {
logger.error({
message: "no validator found for network",
Expand Down
Loading

0 comments on commit 73cf528

Please sign in to comment.