Skip to content

Commit

Permalink
add formatting and types
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotBraem committed Sep 10, 2024
1 parent 512229e commit 0852640
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 48 deletions.
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
node_modules
public
test-results
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": false
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"fmt": "prettier --write '**/*.{js,jsx,ts,tsx,json}'",
"fmt:check": "prettier --check '**/*.{js,jsx,ts,tsx,json}'"
},
"dependencies": {
"@elysiajs/swagger": "^1.0.5",
Expand All @@ -20,7 +22,6 @@
"react-dom": "^18",
"react-markdown": "^9.0.1",
"react-syntax-highlighter": "^15.5.0",
"solid": "link:@heroicons/react/solid",
"stream": "^0.0.3",
"zod": "^3.23.8"
},
Expand All @@ -33,6 +34,7 @@
"eslint": "^8",
"eslint-config-next": "14.2.3",
"postcss": "^8",
"prettier": "^3.3.3",
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
Expand Down
13 changes: 10 additions & 3 deletions pnpm-lock.yaml

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

69 changes: 38 additions & 31 deletions src/app/api/[[...slugs]]/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Budget } from "@/app/types";
import { swagger } from "@elysiajs/swagger";
import Big from "big.js";
import { Elysia } from "elysia";
import axios from "axios";
import { Elysia } from "elysia";
import { Readable } from "stream";

const API_KEY = process.env.WORDWARE_API_KEY
const API_URL = process.env.WORDWARE_API_URL
const API_VERSION = process.env.WORDWARE_API_VERSION
const API_KEY = process.env.WORDWARE_API_KEY;
const API_URL = process.env.WORDWARE_API_URL;
const API_VERSION = process.env.WORDWARE_API_VERSION;

// BODY Type

const app = new Elysia({ prefix: "/api", aot: false })
.use(swagger())
.post("/budget", async ({ body }) => {
.post("/budget", async ({ body }: { body: Budget }) => {
if (!API_KEY) {
throw new Error('Wordware API key is not set');
throw new Error("API Key is not set");
}

if (!API_URL) {
throw new Error("API URL is not set");
}

const bodyFormatted = {
Expand All @@ -30,59 +36,60 @@ const app = new Elysia({ prefix: "/api", aot: false })
};

try {
const response = await axios.post(
API_URL,
bodyFormatted,
{
headers: {
Authorization: `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
},
responseType: 'stream',
const response = await axios.post(API_URL, bodyFormatted, {
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
},
);
responseType: "stream",
});

return new Promise((resolve, reject) => {
const stream = response.data as Readable;
let rawData = '';
let rawData = "";

stream.on('data', (chunk) => {
stream.on("data", (chunk) => {
rawData += chunk.toString();
});

stream.on('end', () => {
const lines = rawData.split('\n').filter(line => line.trim());
stream.on("end", () => {
const lines = rawData.split("\n").filter((line) => line.trim());
let completeObject: any = null;

for (const line of lines) {
try {
const parsedChunk = JSON.parse(line);
if (parsedChunk.type === 'chunk' && parsedChunk.value.state === 'complete') {
if (
parsedChunk.type === "chunk" &&
parsedChunk.value.state === "complete"
) {
completeObject = parsedChunk.value.output;
break;
}
} catch (error) {
console.error('Failed to parse JSON:', error);
console.error("Failed to parse JSON:", error);
}
}

if (completeObject) {
console.log('Complete Object:', completeObject);
resolve({ overview: completeObject.overview, csv: completeObject.CSV });
console.log("Complete Object:", completeObject);
resolve({
overview: completeObject.overview,
csv: completeObject.CSV,
});
} else {
console.log('No complete state found. Raw data:', rawData);
resolve({ error: 'No complete state found', rawData });
console.log("No complete state found. Raw data:", rawData);
resolve({ error: "No complete state found", rawData });
}
});

stream.on('error', (error) => {
console.error('Stream error:', error);
stream.on("error", (error) => {
console.error("Stream error:", error);
reject(error);
});
});

} catch (error) {
console.error('Request failed:', error);
console.error("Request failed:", error);
throw error;
}
})
Expand Down
16 changes: 4 additions & 12 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import Link from "next/link";
export default function Home() {
return (
<main className="flex flex-col p-2">
<h1 className="text-3xl font-bold">
Budgetoor
</h1>
<h1 className="text-3xl font-bold">Budgetoor</h1>
<ul>
<li>
{/* <a
Expand All @@ -17,19 +15,13 @@ export default function Home() {
</a> */}
</li>
<li>
<Link href="/.well-known/ai-plugin.json">
OpenAPI Spec
</Link>
<Link href="/.well-known/ai-plugin.json">OpenAPI Spec</Link>
</li>
<li>
<Link href="/api/swagger">
Swagger
</Link>
<Link href="/api/swagger">Swagger</Link>
</li>
<li>
<Link href="/playground">
Playground
</Link>
<Link href="/playground">Playground</Link>
</li>
<li>
<a
Expand Down
10 changes: 10 additions & 0 deletions src/app/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Budget {
description: string;
timeEstimate: string;
budgetBuffer: string;
task_breakdown: string;
roles_seniority: string;
location: string;
payroll: string;
profitMargin: string;
}

0 comments on commit 0852640

Please sign in to comment.