Skip to content

Commit

Permalink
Improve TypeScript config (#558)
Browse files Browse the repository at this point in the history
* Ignore TS incremental build file

* Run Node server in production mode

* Add tsconfig.base.json

* Fixes for stricter type checking

* Add root tsconfig

* Run vitest from root

* Better clean and build for proxy server

* Run clean before checks in CI workflow

* Update changelog
  • Loading branch information
kmcginnes authored Aug 28, 2024
1 parent 9bc56c5 commit 0e23a46
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
**/.idea/
**/dist/
**/.env.local

# TypeScript build cache manifests
*.tsbuildinfo
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
[#548](https://github.com/aws/graph-explorer/pull/548))
- **Improved** SageMaker Lifecycle script handling of CloudWatch log driver
failures ([#550](https://github.com/aws/graph-explorer/pull/550))
- **Changed** Node to run in production mode
([#558](https://github.com/aws/graph-explorer/pull/558))
- **Removed** hosting production server using the client side Vite
configuration, requiring the use of the proxy server
([#565](https://github.com/aws/graph-explorer/pull/565))
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
"check:lint": "eslint .",
"format": "prettier --write .",
"check:format": "prettier --check .",
"test": "pnpm --stream -r run test",
"test:watch": "pnpm --stream -r run test:watch",
"coverage": "pnpm --stream -r run coverage",
"check:types": "pnpm --stream -r check:types",
"checks": "pnpm run '/^check:.*/'",
"test": "vitest run",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"check:types": "tsc --build",
"checks": "pnpm clean && pnpm run '/^check:.*/'",
"start": "pnpm --filter \"graph-explorer-proxy-server\" run start",
"clean": "pnpm --stream -r run clean",
"build": "pnpm --stream -r run build",
Expand All @@ -34,6 +34,7 @@
"@eslint/compat": "^1.1.1",
"@eslint/js": "^9.9.0",
"@tanstack/eslint-plugin-query": "^5.51.1",
"@vitest/coverage-v8": "^2.0.5",
"eslint": "^9.9.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-react": "^7.35.0",
Expand Down
10 changes: 3 additions & 7 deletions packages/graph-explorer-proxy-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
"main": "dist/node-server.js",
"type": "module",
"scripts": {
"clean": "rimraf dist",
"build": "tsc -p tsconfig.build.json",
"clean": "tsc --build tsconfig.build.json --clean && rimraf dist coverage",
"build": "tsc --build tsconfig.build.json",
"dev": "tsx watch src/node-server.ts",
"test": "vitest run",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"start": "node dist/node-server.js",
"check:types": "tsc --noEmit"
"start": "NODE_ENV=production node dist/node-server.js"
},
"author": "amazon",
"license": "Apache-2.0",
Expand Down
17 changes: 10 additions & 7 deletions packages/graph-explorer-proxy-server/src/node-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ const proxyLogger = createLogger();
app.use(requestLoggingMiddleware(proxyLogger));

// Function to get IAM headers for AWS4 signing process.
async function getIAMHeaders(options) {
async function getIAMHeaders(options: string | aws4.Request) {
const credentialProvider = fromNodeProviderChain();
const creds = await credentialProvider();
if (creds === undefined) {
throw new Error(
"IAM is enabled but credentials cannot be found on the credential provider chain."
);
}

const headers = aws4.sign(options, {
accessKeyId: creds.accessKeyId,
secretAccessKey: creds.secretAccessKey,
sessionToken: creds.sessionToken,
...(creds.sessionToken && { sessionToken: creds.sessionToken }),
});

return headers;
Expand Down Expand Up @@ -146,6 +147,8 @@ const retryFetch = async (
}
}
}
// Should never reach this code
throw new Error("retryFetch failed to complete retry logic");
};

// Function to fetch data from the given URL and send it as a response.
Expand All @@ -166,8 +169,8 @@ async function fetchData(
region,
serviceType
);
const data = await response!.json();
res.status(response!.status);
const data = await response.json();
res.status(response.status);
res.send(data);
} catch (error) {
next(error);
Expand Down Expand Up @@ -270,7 +273,7 @@ app.post("/sparql", (req, res, next) => {
body,
};

fetchData(
return fetchData(
res,
next,
rawUrl,
Expand Down Expand Up @@ -347,7 +350,7 @@ app.post("/gremlin", (req, res, next) => {
body: JSON.stringify(body),
};

fetchData(
return fetchData(
res,
next,
rawUrl,
Expand Down Expand Up @@ -384,7 +387,7 @@ app.post("/openCypher", (req, res, next) => {
? (headers["service-type"] ?? DEFAULT_SERVICE_TYPE)
: "";

fetchData(
return fetchData(
res,
next,
rawUrl,
Expand Down
4 changes: 0 additions & 4 deletions packages/graph-explorer-proxy-server/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
"extends": ["./tsconfig.json"],
"compilerOptions": {
"noEmit": false,
"outDir": "dist"
},
"include": ["src/**/*.ts"],
"exclude": ["**/*.test.ts", "./src/test-setup.ts"]
}
17 changes: 7 additions & 10 deletions packages/graph-explorer-proxy-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
{
"extends": ["../../tsconfig.base.json"],
"compilerOptions": {
/* Language and Environment */
"composite": true,
"target": "ES2022",
"lib": ["ES2023"],
"types": ["vitest/globals"],

/* Modules */
"module": "node16",
"noEmit": true,

/* Interop Constraints */
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,

/* Type Checking */
"strict": true,
"noImplicitAny": false,
"types": ["vitest/globals"],

/* Completeness */
"skipLibCheck": true,

/* Paths */
"outDir": "./dist",
"baseUrl": "./src",
"rootDir": "./src",
"paths": {
"@/*": ["./*"]
}
},
"include": ["src"]
"include": ["src"],
"references": [{ "path": "./tsconfig.build.json" }]
}
8 changes: 2 additions & 6 deletions packages/graph-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
},
"type": "module",
"scripts": {
"test": "vitest run",
"test:watch": "vitest",
"coverage": "vitest run --coverage",
"dev": "vite --force",
"clean": "rimraf dist",
"clean": "tsc --build --clean && rimraf dist coverage",
"vite-build": "NODE_OPTIONS=--max_old_space_size=6144 vite build",
"build": "pnpm vite-build -- --mode production",
"check:types": "tsc --noEmit"
"build": "pnpm vite-build -- --mode production"
},
"author": "amazon",
"license": "Apache-2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NoWidgetIcon, SearchSadIcon } from "@/components/icons";
import { PanelEmptyState } from "@/components/PanelEmptyState";

const getEmptyStateItems = (
empty: boolean,
_empty: boolean,
noSearchResults: boolean,
emptyState?: {
noSearchResultsTitle?: ReactNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ const reducer = (state: NotificationState, action: NotificationAction) => {
};
}
}

return state;
};

export default reducer;
6 changes: 3 additions & 3 deletions packages/graph-explorer/src/hooks/useSchemaSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ const useSchemaSync = (onSyncChange?: (isSyncing: boolean) => void) => {
type: "error",
stackable: true,
});
if (e.name === "AbortError") {
if (e instanceof Error && e.name === "AbortError") {
logger.error(
`[${
config.displayLabel || config.id
}] Fetch aborted, reached max time out ${config.connection?.fetchTimeoutMs} MS `
}] Fetch aborted, reached max time out ${config.connection?.fetchTimeoutMs} MS`
);
} else {
logger.error(
`[${
config.displayLabel || config.id
}] Error while fetching schema: ${e.message}`
}] Error while fetching schema: ${e instanceof Error ? e.message : "Unexpected error"}`
);
}
updateSchemaState(config.id);
Expand Down
4 changes: 3 additions & 1 deletion packages/graph-explorer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ const grabConfig = async (): Promise<RawConfiguration | undefined> => {
},
};
} catch (error) {
console.error(`Error when trying to create connection: ${error.message}`);
console.error(
`Error when trying to create connection: ${error instanceof Error ? error.message : "Unexpected error"}`
);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { recoilDiffSets } from "@/utils/recoilState";

type ToggleEdge = Edge & { __is_visible: boolean };

const EdgesTabular = forwardRef<TabularInstance<any>, any>((props, ref) => {
const EdgesTabular = forwardRef<TabularInstance<any>, any>((_props, ref) => {
const t = useTranslations();
const edges = useRecoilValue(edgesAtom);
const setEdgesOut = useSetRecoilState(edgesOutOfFocusIdsAtom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { recoilDiffSets } from "@/utils/recoilState";

type ToggleVertex = Vertex & { __is_visible: boolean };

const NodesTabular = forwardRef<TabularInstance<any>, any>((props, ref) => {
const NodesTabular = forwardRef<TabularInstance<any>, any>((_props, ref) => {
const t = useTranslations();
const nodes = useRecoilValue(nodesAtom);
const setNodesOut = useSetRecoilState(nodesOutOfFocusIdsAtom);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ function SearchResults({
items={resultItems}
draggable={true}
defaultItemType="graph-viewer__node"
onItemClick={(event, item) => {
onItemClick={(_event, item) => {
selection.toggle(item.id);
}}
selectedItemsIds={Array.from(selection.state)}
Expand Down
10 changes: 2 additions & 8 deletions packages/graph-explorer/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"extends": ["../../tsconfig.base.json"],
"compilerOptions": {
"target": "ES2022",
"useDefineForClassFields": true,
"lib": ["ES2022", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"types": ["vite/client", "vitest/globals", "@testing-library/jest-dom"],

/* Path aliasing */
Expand All @@ -20,13 +20,7 @@
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
"jsx": "react-jsx",

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"jsx": "react-jsx"
},
"include": ["src"]
}
1 change: 1 addition & 0 deletions packages/graph-explorer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": ["../../tsconfig.base.json"],
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
Expand Down
2 changes: 1 addition & 1 deletion packages/graph-explorer/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig(({ mode }) => {
transformIndexHtml: {
order: "pre",
handler: (html: string) => {
return html.replace(/%(.*?)%/g, function (match, p1) {
return html.replace(/%(.*?)%/g, function (_match, p1) {
return env[p1] ? env[p1] : "";
});
},
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
/* Useful for project references */
"composite": true,
"declaration": true,
"declarationMap": true,
"incremental": true,
"noEmitOnError": true,
"skipLibCheck": true,

/* Linting */
"strict": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,
"noImplicitOverride": true,
"noImplicitAny": true,
"checkJs": true
}
}
8 changes: 8 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.base.json",
"files": [],
"references": [
{ "path": "packages/graph-explorer" },
{ "path": "packages/graph-explorer-proxy-server" }
]
}

0 comments on commit 0e23a46

Please sign in to comment.