forked from konfig-dev/konfig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ENG-1090] Add script to bundle typescript SDK that can dynamically i…
…mported into a web page (konfig-dev#135) * testing bundled SDK works * regenerate openapi for rce * add proxy to nextjs * add postgres/data to .gitignore * add postgres instructions * add prisma migrate dev instructions * add webpack template for TypeScript SDK * add yarn.lock to files to hard-coded files to not delete * add webpack + remove yarn.lock template + fix trailing slash bug + make requestAfterHook async * add changeset * hard-code client variable * update snaptrade-sdks
- Loading branch information
dphuang2
authored
Aug 26, 2023
1 parent
7f4ce23
commit c1e90ed
Showing
29 changed files
with
2,007 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule snaptrade-sdks
updated
13 files
+1 −1 | README.md | |
+2 −2 | STATISTICS.md | |
+2 −2 | konfig.yaml | |
+1 −1 | sdks/typescript/.konfig/generate-id.txt | |
+2 −1 | sdks/typescript/.konfigignore | |
+1 −1 | sdks/typescript/README.md | |
+9 −2 | sdks/typescript/common.ts | |
+1 −1 | sdks/typescript/configuration.ts | |
+23 −0 | sdks/typescript/mock.test.ts | |
+6 −3 | sdks/typescript/package.json | |
+45 −4 | sdks/typescript/requestAfterHook.ts | |
+27 −0 | sdks/typescript/webpack.config.js | |
+598 −5 | sdks/typescript/yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'konfig-cli': minor | ||
--- | ||
|
||
add yarn.lock to hard-coded list of files to not delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
generator/konfig-generator-api/src/main/resources/typescript-axios/requestAfterHook.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { RequestArgs } from "./base"; | ||
import { Configuration } from "./configuration"; | ||
|
||
export function requestAfterHook(request: { | ||
export async function requestAfterHook(request: { | ||
axiosArgs: RequestArgs; | ||
basePath: string; | ||
url: string; | ||
configuration?: Configuration; | ||
}): void {} | ||
}): Promise<void> {} |
27 changes: 27 additions & 0 deletions
27
generator/konfig-generator-api/src/main/resources/typescript-axios/webpack.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
mode: "production", | ||
entry: "./index.ts", | ||
output: { | ||
filename: "browser.js", | ||
path: path.resolve(__dirname, "dist"), | ||
library: "client", | ||
libraryTarget: "umd", | ||
}, | ||
resolve: { | ||
extensions: [".ts", ".js"], | ||
fallback: { | ||
crypto: false, | ||
}, | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.ts$/, | ||
use: "ts-loader", | ||
exclude: /node_modules/, | ||
}, | ||
], | ||
}, | ||
}; |
93 changes: 93 additions & 0 deletions
93
generator/konfig-next-app/src/pages/api/proxy/[[...path]].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
import axios from "axios"; | ||
import { URL } from "url"; | ||
|
||
// CORS middleware | ||
function handleCors(req: NextApiRequest, res: NextApiResponse) { | ||
const allowedOrigin = | ||
process.env.NODE_ENV === "production" ? "https://demo.konfigthis.com" : "*"; | ||
|
||
const requestHeaders = req.headers["access-control-request-headers"]; | ||
|
||
res.setHeader("Access-Control-Allow-Origin", allowedOrigin); | ||
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); | ||
res.setHeader("Access-Control-Allow-Headers", requestHeaders || "*"); | ||
|
||
if (req.method === "OPTIONS") { | ||
// Preflight request. Reply successfully: | ||
res.status(204).send(""); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
if (!handleCors(req, res)) return; // handle preflight CORS requests | ||
|
||
const targetUrl = req.headers["x-proxy-target"]; | ||
|
||
if (!targetUrl || typeof targetUrl !== "string") { | ||
return res | ||
.status(400) | ||
.json({ error: "Missing or invalid x-proxy-target header" }); | ||
} | ||
|
||
// Construct the full URL including any subpaths and query parameters | ||
const url = new URL(targetUrl); | ||
const extraPath = ((req.query.path as string[]) || []).join("/"); // Added check for undefined | ||
if (extraPath) { | ||
url.pathname += "/" + extraPath; | ||
} | ||
|
||
// Append any query parameters | ||
if (req.url) { | ||
url.search = req.url.split("?")[1] || ""; | ||
} | ||
|
||
// Fixes: Hostname/IP does not match certificate's altnames: Host: localhost. | ||
// is not in the cert's altnames: DNS:*.passiv.com, DNS:passiv.com" | ||
delete req.headers["host"]; | ||
|
||
// remove other headers that should be generated when making request by axios | ||
// delete req.headers["accept-encoding"]; | ||
// delete req.headers["content-length"]; | ||
// delete req.headers["transfer-encoding"]; | ||
// delete req.headers["content-type"]; | ||
|
||
try { | ||
const response = await axios({ | ||
method: req.method as any, | ||
url: url.toString(), | ||
headers: req.headers, | ||
data: req.body, | ||
validateStatus: () => true, // ensure all responses are forwarded, not just successful ones | ||
}); | ||
|
||
// Forward status code | ||
res.status(response.status); | ||
|
||
// Forward headers | ||
for (const [key, value] of Object.entries(response.headers)) { | ||
// skip content-length | ||
if (key === "content-length") continue; | ||
if (key === "transfer-encoding") continue; | ||
|
||
res.setHeader(key, value as any); | ||
} | ||
|
||
// Forward response | ||
res.send(response.data); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
return res.status(500).json({ | ||
error: `Fetching from the target URL failed with message: ${error.message}`, | ||
}); | ||
} else { | ||
return res.status(500).json({ error: "An unknown error occurred." }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
root: true, | ||
env: { browser: true, es2020: true }, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:react-hooks/recommended', | ||
], | ||
ignorePatterns: ['dist', '.eslintrc.cjs'], | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['react-refresh'], | ||
rules: { | ||
'react-refresh/only-export-components': [ | ||
'warn', | ||
{ allowConstantExport: true }, | ||
], | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# React + TypeScript + Vite | ||
|
||
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules. | ||
|
||
Currently, two official plugins are available: | ||
|
||
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh | ||
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh | ||
|
||
## Expanding the ESLint configuration | ||
|
||
If you are developing a production application, we recommend updating the configuration to enable type aware lint rules: | ||
|
||
- Configure the top-level `parserOptions` property like this: | ||
|
||
```js | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module', | ||
project: ['./tsconfig.json', './tsconfig.node.json'], | ||
tsconfigRootDir: __dirname, | ||
}, | ||
``` | ||
|
||
- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked` | ||
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked` | ||
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite + React + TS</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/browser.js"></script> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "test-bundled-typescript-sdk", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "vite", | ||
"build": "tsc && vite build", | ||
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@types/react": "^18.2.15", | ||
"@types/react-dom": "^18.2.7", | ||
"@typescript-eslint/eslint-plugin": "^6.0.0", | ||
"@typescript-eslint/parser": "^6.0.0", | ||
"@vitejs/plugin-react": "^4.0.3", | ||
"eslint": "^8.45.0", | ||
"eslint-plugin-react-hooks": "^4.6.0", | ||
"eslint-plugin-react-refresh": "^0.4.3", | ||
"typescript": "^5.0.2", | ||
"vite": "^4.4.5" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.