@batijs/cli 0.0.195 → 0.0.196

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/boilerplates/@batijs/auth0/files/$.env.js +5 -9
  2. package/dist/boilerplates/@batijs/auth0/files/$README.md.js +8 -17
  3. package/dist/boilerplates/@batijs/authjs/files/server/authjs-handler.ts +100 -0
  4. package/dist/boilerplates/@batijs/authjs/types/server/authjs-handler.d.ts +16 -0
  5. package/dist/boilerplates/@batijs/eslint/files/.eslintrc.json +15 -2
  6. package/dist/boilerplates/@batijs/express/files/$package.json.js +8 -9
  7. package/dist/boilerplates/@batijs/express/files/express-entry.ts +51 -144
  8. package/dist/boilerplates/@batijs/express/types/express-entry.d.ts +5 -0
  9. package/dist/boilerplates/@batijs/fastify/files/$package.json.js +6 -10
  10. package/dist/boilerplates/@batijs/fastify/files/fastify-entry.ts +63 -149
  11. package/dist/boilerplates/@batijs/fastify/types/fastify-entry.d.ts +6 -0
  12. package/dist/boilerplates/@batijs/firebase-auth/files/$package.json.js +8 -0
  13. package/dist/boilerplates/@batijs/firebase-auth/files/libs/firebaseAdmin.ts +2 -1
  14. package/dist/boilerplates/@batijs/firebase-auth/files/server/firebase-auth-middleware.ts +72 -0
  15. package/dist/boilerplates/@batijs/firebase-auth/types/libs/firebaseAdmin.d.ts +2 -1
  16. package/dist/boilerplates/@batijs/firebase-auth/types/server/firebase-auth-middleware.d.ts +4 -0
  17. package/dist/boilerplates/@batijs/h3/files/$package.json.js +4 -6
  18. package/dist/boilerplates/@batijs/h3/files/h3-entry.ts +36 -175
  19. package/dist/boilerplates/@batijs/h3/types/h3-entry.d.ts +5 -0
  20. package/dist/boilerplates/@batijs/hattip/files/$package.json.js +7 -3
  21. package/dist/boilerplates/@batijs/hattip/files/hattip-entry.ts +40 -60
  22. package/dist/boilerplates/@batijs/hattip/types/hattip-entry.d.ts +1 -0
  23. package/dist/boilerplates/@batijs/hono/files/$package.json.js +5 -3
  24. package/dist/boilerplates/@batijs/hono/files/hono-entry.ts +54 -116
  25. package/dist/boilerplates/@batijs/hono/types/hono-entry.d.ts +5 -0
  26. package/dist/boilerplates/@batijs/react/files/$.eslintrc.json.js +0 -4
  27. package/dist/boilerplates/@batijs/shared/files/package.json +1 -0
  28. package/dist/boilerplates/@batijs/shared-server/files/server/vike-handler.ts +15 -0
  29. package/dist/boilerplates/@batijs/shared-server/types/server/vike-handler.d.ts +2 -0
  30. package/dist/boilerplates/@batijs/telefunc/files/$package.json.js +12 -0
  31. package/dist/boilerplates/@batijs/telefunc/files/server/telefunc-handler.ts +20 -0
  32. package/dist/boilerplates/@batijs/telefunc/types/server/telefunc-handler.d.ts +2 -0
  33. package/dist/boilerplates/boilerplates.json +35 -0
  34. package/dist/index.js +1 -4
  35. package/package.json +5 -5
@@ -1,20 +1,49 @@
1
- import CredentialsProvider from "@auth/core/providers/credentials";
2
- import { firebaseAdmin } from "@batijs/firebase-auth/libs/firebaseAdmin";
1
+ // BATI.has("auth0")
2
+ import "dotenv/config";
3
+ import { authjsHandler, authjsSessionMiddleware } from "@batijs/authjs/server/authjs-handler";
4
+ import {
5
+ firebaseAuthLoginHandler,
6
+ firebaseAuthLogoutHandler,
7
+ firebaseAuthMiddleware,
8
+ } from "@batijs/firebase-auth/server/firebase-auth-middleware";
9
+ import { vikeHandler } from "@batijs/shared-server/server/vike-handler";
10
+ import { telefuncHandler } from "@batijs/telefunc/server/telefunc-handler";
3
11
  import { appRouter } from "@batijs/trpc/trpc/server";
4
12
  import { serve } from "@hono/node-server";
5
13
  import { serveStatic } from "@hono/node-server/serve-static";
6
14
  import { fetchRequestHandler, type FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch";
7
- import { getAuth } from "firebase-admin/auth";
8
15
  import { Hono } from "hono";
9
16
  import { compress } from "hono/compress";
10
- import { deleteCookie, getCookie, setCookie } from "hono/cookie";
11
- import { telefunc } from "telefunc";
12
- import { VikeAuth } from "vike-authjs";
13
- import { renderPage } from "vike/server";
17
+ import { createMiddleware } from "hono/factory";
14
18
 
15
19
  const isProduction = process.env.NODE_ENV === "production";
16
20
  const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
17
21
 
22
+ interface Middleware<Context extends Record<string | number | symbol, unknown>> {
23
+ (request: Request, context: Context): Response | void | Promise<Response> | Promise<void>;
24
+ }
25
+
26
+ export function handlerAdapter<Context extends Record<string | number | symbol, unknown>>(
27
+ handler: Middleware<Context>,
28
+ ) {
29
+ return createMiddleware(async (context, next) => {
30
+ let ctx = context.get("context");
31
+ if (!ctx) {
32
+ ctx = {};
33
+ context.set("context", ctx);
34
+ }
35
+
36
+ const res = await handler(context.req.raw, ctx as Context);
37
+ context.set("context", ctx);
38
+
39
+ if (!res) {
40
+ await next();
41
+ }
42
+
43
+ return res;
44
+ });
45
+ }
46
+
18
47
  const app = new Hono();
19
48
 
20
49
  app.use(compress());
@@ -28,86 +57,23 @@ if (isProduction) {
28
57
  );
29
58
  }
30
59
 
31
- if (BATI.has("authjs")) {
60
+ if (BATI.has("authjs") || BATI.has("auth0")) {
32
61
  /**
33
- * AuthJS
34
- *
35
- * TODO: Replace secret {@see https://authjs.dev/reference/core#secret}
36
- * TODO: Choose and implement providers
37
- *
38
- * @link {@see https://authjs.dev/guides/providers/custom-provider}
62
+ * Append Auth.js session to context
39
63
  **/
40
- const Auth = VikeAuth({
41
- secret: "MY_SECRET",
42
- providers: [
43
- CredentialsProvider({
44
- name: "Credentials",
45
- credentials: {
46
- username: { label: "Username", type: "text", placeholder: "jsmith" },
47
- password: { label: "Password", type: "password" },
48
- },
49
- async authorize() {
50
- // Add logic here to look up the user from the credentials supplied
51
- const user = { id: "1", name: "J Smith", email: "jsmith@example.com" };
52
-
53
- // Any object returned will be saved in `user` property of the JWT
54
- // If you return null then an error will be displayed advising the user to check their details.
55
- // You can also Reject this callback with an Error thus the user will be sent to the error page with the error message as a query parameter
56
- return user ?? null;
57
- },
58
- }),
59
- ],
60
- });
64
+ app.use(handlerAdapter(authjsSessionMiddleware));
61
65
 
62
- app.use("/api/auth/**", (c) =>
63
- Auth({
64
- request: c.req.raw,
65
- }),
66
- );
66
+ /**
67
+ * Auth.js route
68
+ * @link {@see https://authjs.dev/getting-started/installation}
69
+ **/
70
+ app.use("/api/auth/**", handlerAdapter(authjsHandler));
67
71
  }
68
72
 
69
73
  if (BATI.has("firebase-auth")) {
70
- app.use(async (c, next) => {
71
- const sessionCookie: string = getCookie(c, "__session") || "";
72
- if (sessionCookie) {
73
- const auth = getAuth(firebaseAdmin);
74
- try {
75
- const decodedIdToken = await auth.verifySessionCookie(sessionCookie, true);
76
- const user = await auth.getUser(decodedIdToken.sub);
77
- c.set("user", user);
78
- } catch (error) {
79
- console.debug("verifySessionCookie:", error);
80
- c.set("user", null);
81
- }
82
- }
83
- await next();
84
- });
85
-
86
- app.post("/api/sessionLogin", async (c) => {
87
- const body = await c.req.json();
88
- const idToken: string = body.idToken || "";
89
-
90
- let expiresIn = 60 * 60 * 24 * 5 * 1000; // 5 days. The auth.createSessionCookie() function of Firebase expects time to be specified in miliseconds.
91
-
92
- const auth = getAuth(firebaseAdmin);
93
- try {
94
- const sessionCookie = await auth.createSessionCookie(idToken, { expiresIn });
95
- const options = { maxAge: expiresIn, httpOnly: true, secure: true };
96
-
97
- expiresIn = 60 * 60 * 24 * 5; // 5 days. The setCookie() function of Hono expects time to be specified in seconds.
98
- setCookie(c, "__session", sessionCookie, options);
99
-
100
- return c.json({ status: "success" }, 200);
101
- } catch (error) {
102
- console.error("createSessionCookie failed :", error);
103
- return c.text("Unathorized", 401);
104
- }
105
- });
106
-
107
- app.post("/api/sessionLogout", (c) => {
108
- deleteCookie(c, "__session");
109
- return c.text("Logged Out", 200);
110
- });
74
+ app.use(handlerAdapter(firebaseAuthMiddleware));
75
+ app.post("/api/sessionLogin", handlerAdapter(firebaseAuthLoginHandler));
76
+ app.post("/api/sessionLogout", handlerAdapter(firebaseAuthLogoutHandler));
111
77
  }
112
78
 
113
79
  if (BATI.has("trpc")) {
@@ -134,43 +100,15 @@ if (BATI.has("telefunc")) {
134
100
  *
135
101
  * @link {@see https://telefunc.com}
136
102
  **/
137
- app.post("/_telefunc", async (c) => {
138
- const httpResponse = await telefunc({
139
- url: c.req.url.toString(),
140
- method: c.req.method,
141
- body: await c.req.text(),
142
- context: c,
143
- });
144
- const { body, statusCode, contentType } = httpResponse;
145
-
146
- c.status(statusCode);
147
- c.header("Content-Type", contentType);
148
-
149
- return c.body(body);
150
- });
103
+ app.post("/_telefunc", handlerAdapter(telefuncHandler));
151
104
  }
152
105
 
153
- app.all("*", async (c, next) => {
154
- const pageContextInit = BATI.has("firebase-auth")
155
- ? {
156
- urlOriginal: c.req.url,
157
- user: c.get("user"),
158
- }
159
- : {
160
- urlOriginal: c.req.url,
161
- };
162
- const pageContext = await renderPage(pageContextInit);
163
- const { httpResponse } = pageContext;
164
- if (!httpResponse) {
165
- return next();
166
- } else {
167
- const { body, statusCode, headers } = httpResponse;
168
- headers.forEach(([name, value]) => c.header(name, value));
169
- c.status(statusCode);
170
-
171
- return c.body(body);
172
- }
173
- });
106
+ /**
107
+ * Vike route
108
+ *
109
+ * @link {@see https://vike.dev}
110
+ **/
111
+ app.all("*", handlerAdapter(vikeHandler));
174
112
 
175
113
  if (isProduction) {
176
114
  console.log(`Server listening on http://localhost:${port}`);
@@ -1,3 +1,8 @@
1
+ import "dotenv/config";
1
2
  import { Hono } from "hono";
3
+ interface Middleware<Context extends Record<string | number | symbol, unknown>> {
4
+ (request: Request, context: Context): Response | void | Promise<Response> | Promise<void>;
5
+ }
6
+ export declare function handlerAdapter<Context extends Record<string | number | symbol, unknown>>(handler: Middleware<Context>): import("hono").MiddlewareHandler<any, any, {}>;
2
7
  declare const app: Hono<import("hono").Env, import("hono/types").BlankSchema, "/">;
3
8
  export default app;
@@ -3,11 +3,7 @@ import { loadAsJson } from "@batijs/core";
3
3
  async function getEslintConfig(props) {
4
4
  if (!props.meta.BATI.has("eslint")) return;
5
5
  const eslintConfig = await loadAsJson(props);
6
- eslintConfig.extends = eslintConfig.extends.filter(
7
- (p) => p !== "eslint:recommended" && p !== "plugin:@typescript-eslint/recommended"
8
- );
9
6
  eslintConfig.extends.push("plugin:react/recommended");
10
- eslintConfig.plugins = eslintConfig.plugins.filter((p) => p !== "@typescript-eslint");
11
7
  eslintConfig.plugins.push("react");
12
8
  eslintConfig.settings ??= {};
13
9
  eslintConfig.settings.react = { version: "detect" };
@@ -11,6 +11,7 @@
11
11
  "keywords": [],
12
12
  "author": "",
13
13
  "devDependencies": {
14
+ "@auth/core": "^0.31.0",
14
15
  "typescript": "^5.4.5",
15
16
  "vike": "^0.4.172",
16
17
  "vite": "^5.2.11"
@@ -0,0 +1,15 @@
1
+ import { renderPage } from "vike/server";
2
+
3
+ export async function vikeHandler<Context extends Record<string | number | symbol, unknown>>(
4
+ request: Request,
5
+ context?: Context,
6
+ ): Promise<Response> {
7
+ const pageContextInit = { ...(context ?? {}), urlOriginal: request.url };
8
+ const pageContext = await renderPage(pageContextInit);
9
+ const response = pageContext.httpResponse;
10
+
11
+ return new Response(response?.getReadableWebStream(), {
12
+ status: response?.statusCode,
13
+ headers: response?.headers,
14
+ });
15
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference types="node" />
2
+ export declare function vikeHandler<Context extends Record<string | number | symbol, unknown>>(request: Request, context?: Context): Promise<Response>;
@@ -57,6 +57,18 @@ var require_package = __commonJS({
57
57
  if: {
58
58
  flag: "telefunc"
59
59
  }
60
+ },
61
+ exports: {
62
+ "./server/telefunc-handler": {
63
+ types: "./dist/types/server/telefunc-handler.d.ts"
64
+ }
65
+ },
66
+ typesVersions: {
67
+ "*": {
68
+ "server/telefunc-handler": [
69
+ "./dist/types/server/telefunc-handler.d.ts"
70
+ ]
71
+ }
60
72
  }
61
73
  };
62
74
  }
@@ -0,0 +1,20 @@
1
+ import { telefunc } from "telefunc";
2
+
3
+ export async function telefuncHandler<Context extends Record<string | number | symbol, unknown>>(
4
+ request: Request,
5
+ context?: Context,
6
+ ): Promise<Response> {
7
+ const httpResponse = await telefunc({
8
+ url: request.url.toString(),
9
+ method: request.method,
10
+ body: await request.text(),
11
+ context,
12
+ });
13
+ const { body, statusCode, contentType } = httpResponse;
14
+ return new Response(body, {
15
+ status: statusCode,
16
+ headers: {
17
+ "content-type": contentType,
18
+ },
19
+ });
20
+ }
@@ -0,0 +1,2 @@
1
+ /// <reference types="node" />
2
+ export declare function telefuncHandler<Context extends Record<string | number | symbol, unknown>>(request: Request, context?: Context): Promise<Response>;
@@ -10,6 +10,22 @@
10
10
  "files"
11
11
  ]
12
12
  },
13
+ {
14
+ "config": {
15
+ "if": {
16
+ "flag": {
17
+ "$in": [
18
+ "authjs",
19
+ "auth0"
20
+ ]
21
+ }
22
+ }
23
+ },
24
+ "folder": "@batijs/authjs",
25
+ "subfolders": [
26
+ "files"
27
+ ]
28
+ },
13
29
  {
14
30
  "config": {
15
31
  "if": {
@@ -259,6 +275,25 @@
259
275
  "files"
260
276
  ]
261
277
  },
278
+ {
279
+ "config": {
280
+ "if": {
281
+ "flag": {
282
+ "$in": [
283
+ "h3",
284
+ "hattip",
285
+ "hono",
286
+ "express",
287
+ "fastify"
288
+ ]
289
+ }
290
+ }
291
+ },
292
+ "folder": "@batijs/shared-server",
293
+ "subfolders": [
294
+ "files"
295
+ ]
296
+ },
262
297
  {
263
298
  "config": {
264
299
  "if": {
package/dist/index.js CHANGED
@@ -1564,7 +1564,7 @@ var import_which_pm_runs = __toESM(require_which_pm_runs(), 1);
1564
1564
  // package.json
1565
1565
  var package_default = {
1566
1566
  name: "@batijs/cli",
1567
- version: "0.0.195",
1567
+ version: "0.0.196",
1568
1568
  type: "module",
1569
1569
  scripts: {
1570
1570
  "check-types": "tsc --noEmit",
@@ -1630,9 +1630,6 @@ var rulesMessages = {
1630
1630
  [RulesMessage.ERROR_COMPILED_R_REACT]: error(
1631
1631
  `${inverse(bold("React"))} is required when using ${inverse(bold("Compiled"))}.`
1632
1632
  ),
1633
- [RulesMessage.ERROR_AUTH0_E_HONO]: error(
1634
- `${inverse(bold("Auth0"))} does not support running on ${inverse(bold("Hono"))} with official plugins. Check https://batijs.dev for details`
1635
- ),
1636
1633
  [RulesMessage.INFO_HATTIP]: info(`${inverse(bold("HatTip"))} is an experimental project`)
1637
1634
  };
1638
1635
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@batijs/cli",
3
- "version": "0.0.195",
3
+ "version": "0.0.196",
4
4
  "type": "module",
5
5
  "keywords": [],
6
6
  "description": "Next-gen scaffolder. Get started with fully-functional apps, and choose any tool you want",
@@ -21,12 +21,12 @@
21
21
  "typescript": "^5.4.5",
22
22
  "vite": "^5.2.11",
23
23
  "which-pm-runs": "^1.1.0",
24
- "@batijs/build": "0.0.195",
25
- "@batijs/compile": "0.0.195"
24
+ "@batijs/build": "0.0.196",
25
+ "@batijs/compile": "0.0.196"
26
26
  },
27
27
  "dependencies": {
28
- "@batijs/core": "0.0.195",
29
- "@batijs/features": "0.0.195"
28
+ "@batijs/core": "0.0.196",
29
+ "@batijs/features": "0.0.196"
30
30
  },
31
31
  "bin": "./dist/index.js",
32
32
  "exports": {