Browse Source

web: typeorm integration

ghorsington 3 years ago
parent
commit
613b22df49

+ 1 - 0
web/package.json

@@ -37,6 +37,7 @@
 		"make-error": "^1.3.6",
 		"node-fetch": "^2.6.0",
 		"nodemailer": "^6.4.11",
+		"pg": "^8.3.3",
 		"rpc_ts": "^2.1.0",
 		"sirv": "^1.0.5",
 		"svelte-awesome": "^2.3.0",

+ 5 - 1
web/rollup.config.js

@@ -85,7 +85,11 @@ export default {
 
     server: {
         input: { server: config.server.input().server.replace(/\.js$/, ".ts") },
-        output: { ...config.server.output(), sourcemap, interop: "auto" },
+        output: {
+            ...config.server.output(),
+            sourcemap,
+            interop: "default",
+        },
         plugins: [
             replace({
                 "process.browser": false,

+ 1 - 0
web/src/cookie.d.ts

@@ -3,5 +3,6 @@ declare namespace CookieSessionInterfaces {
         authTokenCode?: string;
         userId?: string;
         username?: string;
+        avatarId?: string;
     }
 }

+ 10 - 5
web/src/routes/_layout.svelte

@@ -1,10 +1,15 @@
-<script lang="typescript">
-	import { stores } from "@sapper/app";
+<script lang="typescript" context="module">
+	import type { AppSession, PageData } from "src/utils/session";
 
-	// You may not want to use `segment`, but it is passed for the time being and will
-	// https://github.com/sveltejs/sapper/issues/824
-	export let segment: string = "";
+	export async function preload({ path }: PageData, session: AppSession) {
+		console.log(path);
+		console.log(session);
+		return {};
+	}
+</script>
 
+<script lang="typescript">
+	import { stores } from "@sapper/app";
 	const { page } = stores();
 
 	let path: string;

+ 1 - 0
web/src/routes/login/discord.ts

@@ -57,6 +57,7 @@ export const post = async (req: ExpressRequest, res: ExpressResponse):
     }
     req.session.authTokenCode = undefined;
     req.session.userId = userResult.id;
+    req.session.avatarId = userResult.avatar;
     req.session.username = `${userResult.username}#${userResult.discriminator}`;
     req.sessionOptions.maxAge = tokenResult.expires_in;
     return res.json({

+ 2 - 6
web/src/sapper.d.ts

@@ -1,4 +1,5 @@
 declare module "@sapper/app" {
+    import { PageData } from "src/utils/session";
     // from sapper/runtime/src/app/types.ts
     // sapper doesn't export its types yet
     interface Redirect {
@@ -12,12 +13,7 @@ declare module "@sapper/app" {
     function prefetchRoutes(pathnames: string[]): Promise<unknown>
     function start(opts: { target: Element | null }): Promise<unknown>
     const stores: () => {
-        page: PageStore<{
-            host: string,
-            path: string,
-            params: Record<string, unknown>,
-            query: Record<string, unknown>
-        }>,
+        page: PageStore<PageData>,
         preloading: boolean,
         session: Writable<unknown>
     };

+ 2 - 1
web/src/server.ts

@@ -9,6 +9,7 @@ import sirv from "sirv";
 import { createConnection, getConnectionOptions } from "typeorm";
 import { DB_ENTITIES } from "@shared/db/entities";
 import { logger } from "./utils/logging";
+import { AppSession } from "./utils/session";
 
 const PORT = process.env.PORT; // eslint-disable-line prefer-destructuring
 // @ts-ignore -- creates a warning after `rollup-plugin-replace` (set up in `rollup.config.js`)
@@ -40,7 +41,7 @@ const createSapperServer = async (): Promise<Express> => {
         compression({ threshold: 0 }),
         sirv("static", { dev }),
         sapper.middleware({
-            session: (req) => ({
+            session: (req): AppSession => ({
                 userId: req.session?.userId,
             }),
         }),

+ 10 - 0
web/src/utils/session.ts

@@ -0,0 +1,10 @@
+export interface AppSession {
+    userId?: string;
+}
+
+export interface PageData {
+    host: string;
+    path: string;
+    params: Record<string, unknown>;
+    query: Record<string, unknown>;
+}

+ 1 - 1
web/tsconfig.json

@@ -5,10 +5,10 @@
             "WebWorker",
             "ES2015"
         ],
-        "module": "ES2015",
         "esModuleInterop": true,
         "target": "ES6",
         "allowSyntheticDefaultImports": true,
+        "strictPropertyInitialization": false,
         "experimentalDecorators": true,
         "emitDecoratorMetadata": true,
         "moduleResolution": "node",