Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

demo project #3

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions apps/demo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
# @authhero/demo

## 0.1.1

### Patch Changes

- remove the iife build files
- Updated dependencies
- [email protected]
- @authhero/[email protected]

## 0.1.0

### Minor Changes

- Get the demo project rendering

### Patch Changes

- Updated dependencies
- [email protected]
- @authhero/[email protected]

## 0.0.36

### Patch Changes
Expand Down
Empty file added apps/demo/db.sqlite
Empty file.
12 changes: 10 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
{
"name": "@authhero/demo",
"private": true,
"version": "0.0.36",
"version": "0.1.1",
"scripts": {
"dev": "bun --watch src/bun.ts"
},
"dependencies": {}
"dependencies": {
"@authhero/kysely-adapter": "^0.18.1",
"@hono/swagger-ui": "^0.4.1",
"@hono/zod-openapi": "^0.18.0",
"authhero": "^0.10.1",
"hono": "^4.6.11",
"hono-openapi-middlewares": "^1.0.11",
"kysely-bun-sqlite": "^0.3.2"
}
}
47 changes: 33 additions & 14 deletions apps/demo/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Context } from "hono";
import { OpenAPIHono } from "@hono/zod-openapi";
import { HTTPException } from "hono/http-exception";
import { DataAdapters, init } from "authhero";
import { swaggerUI } from "@hono/swagger-ui";
import {
createAuthMiddleware,
registerComponent,
} from "hono-openapi-middlewares";
import packageJson from "../package.json";
import authhero, { DataAdapters } from "authhero";
import { Bindings } from "./types/Bindings";

// Define the return type interface
interface CreateReturn {
app: ReturnType<typeof authhero.init>;
}

export default function create(dataAdapter: DataAdapters): CreateReturn {
const rootApp = new OpenAPIHono();
export default function create(dataAdapter: DataAdapters) {
const app = new OpenAPIHono<{ Bindings: Bindings }>();

rootApp
app
.onError((err, ctx) => {
if (err instanceof HTTPException) {
// Get the custom response
Expand All @@ -28,13 +29,31 @@ export default function create(dataAdapter: DataAdapters): CreateReturn {
name: tenantId,
version: packageJson.version,
});
});
})
.get("/docs", swaggerUI({ url: "/spec" }));
app.use(createAuthMiddleware(app));
app.use(registerComponent(app));

const app = authhero.init({
const { managementApp } = init({
dataAdapter,
issuer: "https://authhero.com",
});

return {
app,
};
managementApp.doc("/spec", (c) => ({
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Management API",
},
servers: [
{
url: new URL(c.req.url).origin,
description: "Current environment",
},
],
}));

app.route("/", managementApp);
markusahlstrand marked this conversation as resolved.
Show resolved Hide resolved

return app;
}
3 changes: 1 addition & 2 deletions apps/demo/src/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ const db = new Kysely<any>({

const dataAdapter = createAdapters(db);

// @ts-ignore
const { app } = createApp(dataAdapter);
const app = createApp(dataAdapter);

const server = {
async fetch(request: Request): Promise<Response> {
Expand Down
35 changes: 35 additions & 0 deletions apps/demo/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { OpenAPIHono } from "@hono/zod-openapi";
import { PlanetScaleDialect } from "kysely-planetscale";
import { Kysely } from "kysely";
import createApp from "./app";
import createAdapters from "@authhero/kysely-adapter";

interface Env {
DATABASE_HOST: string;
DATABASE_USERNAME: string;
DATABASE_PASSWORD: string;
}

let app: OpenAPIHono | undefined;

const server = {
async fetch(request: Request, env: Env): Promise<Response> {
if (!app) {
const dialect = new PlanetScaleDialect({
host: env.DATABASE_HOST,
username: env.DATABASE_USERNAME,
password: env.DATABASE_PASSWORD,
fetch: (opts, init) =>
fetch(new Request(opts, { ...init, cache: undefined })),
});
const db = new Kysely<any>({ dialect });
const dataAdapter = createAdapters(db);

app = createApp(dataAdapter);
}

return app.fetch(request);
},
};
Comment on lines +15 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling and improve type safety

Several critical improvements are needed in the server implementation:

  1. Replace any with proper database schema type
  2. Add error handling for database connection
  3. Consider connection pooling
  4. Implement proper cleanup
-      const db = new Kysely<any>({ dialect });
+      const db = new Kysely<DatabaseSchema>({ dialect });
       const dataAdapter = createAdapters(db);
 
+      try {
         app = createApp(dataAdapter);
+      } catch (error) {
+        console.error('Failed to initialize app:', error);
+        return new Response('Internal Server Error', { status: 500 });
+      }

Consider implementing:

  1. Connection pooling for better performance
  2. Retry logic for transient database connection issues
  3. Proper resource cleanup on server shutdown

Committable suggestion skipped: line range outside the PR's diff.


export default server;
7 changes: 7 additions & 0 deletions apps/demo/src/types/Bindings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type Bindings = {
JWKS_URL: string;
JWKS_SERVICE: {
fetch: typeof fetch;
};
AUTH_URL: string;
};
3 changes: 3 additions & 0 deletions apps/demo/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "authhero-demo"
main = "src/server.ts"
compatibility_date = "2024-11-20"
12 changes: 12 additions & 0 deletions packages/adapter-interfaces/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# @authhero/adapter-interfaces

## 0.22.1

### Patch Changes

- remove the iife build files

## 0.22.0

### Minor Changes

- Get the demo project rendering

## 0.21.0

### Minor Changes
Expand Down
9 changes: 5 additions & 4 deletions packages/adapter-interfaces/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/markusahlstrand/authhero"
},
"version": "0.21.0",
"version": "0.22.1",
"files": [
"dist"
],
Expand All @@ -29,14 +29,15 @@
"build": "tsc && vite build && dts-bundle-generator --config ./dts-bundle-generator.config.ts"
},
"devDependencies": {
"@hono/zod-openapi": "^0.18.0",
markusahlstrand marked this conversation as resolved.
Show resolved Hide resolved
"@rollup/plugin-commonjs": "^28.0.1",
"@rollup/plugin-node-resolve": "^15.3.0",
"@types/node": "^22.8.6",
"@types/node": "^22.9.1",
"dts-bundle-generator": "^9.5.1",
"typescript": "^5.6.3",
"vite": "^5.4.10"
"vite": "^5.4.11"
},
"dependencies": {
"peerDependencies": {
"@hono/zod-openapi": "^0.16.4"
}
}
1 change: 0 additions & 1 deletion packages/adapter-interfaces/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const getPackageNameCamelCase = () => {
const fileName = {
es: `${getPackageName()}.mjs`,
cjs: `${getPackageName()}.cjs`,
iife: `${getPackageName()}.iife.js`,
};

const formats = Object.keys(fileName) as Array<keyof typeof fileName>;
Expand Down
19 changes: 19 additions & 0 deletions packages/authhero/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# authhero

## 0.10.1

### Patch Changes

- remove the iife build files
- Updated dependencies
- @authhero/[email protected]

## 0.10.0

### Minor Changes

- Get the demo project rendering

### Patch Changes

- Updated dependencies
- @authhero/[email protected]

## 0.9.0

### Minor Changes
Expand Down
14 changes: 9 additions & 5 deletions packages/authhero/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "authhero",
"version": "0.9.0",
"version": "0.10.1",
"files": [
"dist"
],
Expand All @@ -22,21 +22,25 @@
},
"devDependencies": {
"@authhero/kysely-adapter": "workspace:^",
"@types/node": "^22.9.0",
"@hono/zod-openapi": "^0.18.0",
"@types/node": "^22.9.1",
"better-sqlite3": "^11.5.0",
"dts-bundle-generator": "^9.5.1",
"hono": "^4.6.11",
"typescript": "^5.6.3",
"vite": "^5.4.11",
"vite-plugin-dts": "^4.3.0",
"vitest": "^2.1.4"
"vitest": "^2.1.5"
markusahlstrand marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@authhero/adapter-interfaces": "workspace:^",
"@hono/zod-openapi": "^0.17.0",
"@peculiar/x509": "^1.12.3",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"hono": "^4.6.9",
"oslo": "^1.2.1"
},
"peerDependencies": {
"@hono/zod-openapi": "^0.18.0",
"hono": "^4.6.11"
}
}
1 change: 0 additions & 1 deletion packages/authhero/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const getPackageNameCamelCase = () => {
const fileName = {
es: `${getPackageName()}.mjs`,
cjs: `${getPackageName()}.cjs`,
iife: `${getPackageName()}.iife.js`,
};

const formats = Object.keys(fileName) as Array<keyof typeof fileName>;
Expand Down
15 changes: 15 additions & 0 deletions packages/drizzle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# @authhero/drizzle

## 0.1.57

### Patch Changes

- remove the iife build files
- Updated dependencies
- @authhero/[email protected]

## 0.1.56

### Patch Changes

- Updated dependencies
- @authhero/[email protected]

## 0.1.55

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/drizzle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"type": "git",
"url": "https://github.com/markusahlstrand/authhero"
},
"version": "0.1.55",
"version": "0.1.57",
"files": [
"dist"
],
Expand Down
1 change: 0 additions & 1 deletion packages/drizzle/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const getPackageNameCamelCase = () => {
const fileName = {
es: `${getPackageName()}.mjs`,
cjs: `${getPackageName()}.cjs`,
iife: `${getPackageName()}.iife.js`,
};

const formats = Object.keys(fileName) as Array<keyof typeof fileName>;
Expand Down
19 changes: 19 additions & 0 deletions packages/kysely/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# @authhero/kysely-adapter

## 0.18.1

### Patch Changes

- remove the iife build files
- Updated dependencies
- @authhero/[email protected]

## 0.18.0

### Minor Changes

- Get the demo project rendering

### Patch Changes

- Updated dependencies
- @authhero/[email protected]

## 0.17.1

### Patch Changes
Expand Down
Loading