Skip to content

Commit

Permalink
add eden
Browse files Browse the repository at this point in the history
  • Loading branch information
bastipnt committed Dec 11, 2024
1 parent 7b25c9a commit 86c1a08
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"setup": "edgedb migrate; bunx @edgedb/generate edgeql-js"
},
"dependencies": {
"@elysiajs/cors": "^1.1.1",
"@elysiajs/opentelemetry": "^1.1.7",
"edgedb": "^1.5.12",
"elysia": "latest"
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// import { opentelemetry } from "@elysiajs/opentelemetry";
import { cors } from "@elysiajs/cors";
import { Elysia } from "elysia";
import { edgedb } from "../dbschema/edgeql-js/imports";
import { survey } from "./Survey";
Expand All @@ -8,6 +9,13 @@ const client = edgedb.createClient();

const app = new Elysia()
// .use(opentelemetry())
.use(
cors({
origin: "localhost:5173",
credentials: true,
methods: ["GET", "POST"],
}),
)
.get("/", () => "Hello Elysia")
.use(user(client))
.use(survey(client))
Expand All @@ -16,3 +24,5 @@ const app = new Elysia()
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);

export type App = typeof app;
5 changes: 1 addition & 4 deletions packages/backend/src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ const userObject = t.Object({
visitorId: t.String(),
});

type UserObject = typeof userObject.static;

class UserHandler {
private client: edgedb.Client;

Expand Down Expand Up @@ -95,7 +93,6 @@ export const user = (client: edgedb.Client) =>
body: { visitorId },
store: { session },
cookie: { token },
error,
}) => {
let key: string;

Expand All @@ -104,7 +101,7 @@ export const user = (client: edgedb.Client) =>
key = token.value;

if (existingSessionUserId !== undefined) {
return error(400, { success: false, message: "already signed in" });
return { success: true, message: "already signed in" };
}
} else {
key = crypto.getRandomValues(new Uint32Array(1))[0].toString();
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@tsconfig/svelte": "^5.0.4",
"@types/node": "^22.10.1",
"autoprefixer": "^10.4.20",
"elysia": "^1.1.26",
"postcss": "^8.4.49",
"prettier": "^3.4.2",
"prettier-plugin-svelte": "^3.3.2",
Expand All @@ -31,6 +32,7 @@
"vite": "^6.0.1"
},
"dependencies": {
"@elysiajs/eden": "^1.1.3",
"@fingerprintjs/fingerprintjs": "^4.5.1"
}
}
18 changes: 11 additions & 7 deletions packages/frontend/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { onMount } from "svelte";
import Client from "./Client";
import FingerprintPage from "./Pages/Fingerprint.svelte";
import Questions from "./Pages/Questions.svelte";
import Questions2 from "./Pages/Questions2.svelte";
Expand All @@ -12,18 +13,21 @@
let fingerprintArr = $state<FingerprintArr>();
const fingerprinter = new Fingerprinter();
const client = new Client();
onMount(() => {
const init = async () => {
if (fingerprintArr !== undefined) return;
const createFingerprint = async () => {
await fingerprinter.createFingerprint();
await fingerprinter.createFingerprint();
fingerprintArr = fingerprinter.fingerprintArr;
const visitorId = fingerprinter.fingerprint?.fingerprintJS.visitorId;
if (!visitorId) return;
fingerprintArr = fingerprinter.fingerprintArr;
};
await client.signIn(visitorId);
await client.getAll();
};
createFingerprint();
});
onMount(() => init());
$effect(() => {
const anchor = Number(document.location.hash);
Expand Down
28 changes: 28 additions & 0 deletions packages/frontend/src/Client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { treaty } from "@elysiajs/eden";
import type { App } from "../../backend/src";

export default class Client {
private signedIn = false;
private app = treaty<App>("localhost:3000", {
fetch: {
credentials: "include",
},
});

async signIn(visitorId: string) {
const res = await this.app.user["sign-up-in"].post({ visitorId });
console.log(res);
if (res.status === 200) this.signedIn = true;
}

async getAll() {
if (!this.signedIn) return;

const { data } = await this.app.survey.all.get();
console.log(data);
}

async submitPart1() {
const res = await this.app.survey[1].post();
}
}

0 comments on commit 86c1a08

Please sign in to comment.