@batijs/cli 0.0.187 → 0.0.188
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.
- package/dist/boilerplates/@batijs/fastify/files/$package.json.js +131 -0
- package/dist/boilerplates/@batijs/fastify/files/fastify-entry.ts +246 -0
- package/dist/boilerplates/@batijs/fastify/files/vike.d.ts +21 -0
- package/dist/boilerplates/@batijs/fastify/types/fastify-entry.d.ts +1 -0
- package/dist/boilerplates/boilerplates.json +11 -0
- package/dist/index.js +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// package.json
|
|
28
|
+
var require_package = __commonJS({
|
|
29
|
+
"package.json"(exports, module) {
|
|
30
|
+
module.exports = {
|
|
31
|
+
name: "@batijs/fastify",
|
|
32
|
+
private: true,
|
|
33
|
+
version: "0.0.1",
|
|
34
|
+
description: "",
|
|
35
|
+
type: "module",
|
|
36
|
+
scripts: {
|
|
37
|
+
"check-types": "tsc --noEmit",
|
|
38
|
+
build: "bati-compile-boilerplate"
|
|
39
|
+
},
|
|
40
|
+
keywords: [],
|
|
41
|
+
author: "",
|
|
42
|
+
license: "MIT",
|
|
43
|
+
devDependencies: {
|
|
44
|
+
"@auth/core": "^0.29.0",
|
|
45
|
+
"@batijs/compile": "workspace:*",
|
|
46
|
+
"@batijs/firebase-auth": "workspace:*",
|
|
47
|
+
"@batijs/trpc": "workspace:^",
|
|
48
|
+
"@fastify/cookie": "^9.3.1",
|
|
49
|
+
"@fastify/formbody": "^7.4.0",
|
|
50
|
+
"@fastify/middie": "^8.3.1",
|
|
51
|
+
"@fastify/static": "^7.0.4",
|
|
52
|
+
"@hattip/adapter-node": "^0.0.45",
|
|
53
|
+
"@trpc/server": "^10.45.2",
|
|
54
|
+
"@types/express": "^4.17.21",
|
|
55
|
+
"@types/node": "^18.19.14",
|
|
56
|
+
dotenv: "^16.4.5",
|
|
57
|
+
express: "^4.19.2",
|
|
58
|
+
"express-openid-connect": "^2.17.1",
|
|
59
|
+
fastify: "^4.27.0",
|
|
60
|
+
"firebase-admin": "^12.1.0",
|
|
61
|
+
telefunc: "^0.1.72",
|
|
62
|
+
tsx: "^4.9.3",
|
|
63
|
+
vike: "^0.4.171",
|
|
64
|
+
"vike-authjs": "^0.1.5",
|
|
65
|
+
vite: "^5.2.11"
|
|
66
|
+
},
|
|
67
|
+
dependencies: {
|
|
68
|
+
"@batijs/core": "workspace:*"
|
|
69
|
+
},
|
|
70
|
+
files: [
|
|
71
|
+
"dist/"
|
|
72
|
+
],
|
|
73
|
+
bati: {
|
|
74
|
+
if: {
|
|
75
|
+
flag: "fastify"
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
exports: {
|
|
79
|
+
"./fastify-entry": {
|
|
80
|
+
types: "./dist/types/fastify-entry.d.ts"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
typesVersions: {
|
|
84
|
+
"*": {
|
|
85
|
+
"fastify-entry": [
|
|
86
|
+
"./dist/types/fastify-entry.d.ts"
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// files/$package.json.ts
|
|
95
|
+
import { addDependency, loadAsJson, setScripts } from "@batijs/core";
|
|
96
|
+
async function getPackageJson(props) {
|
|
97
|
+
const packageJson = await loadAsJson(props);
|
|
98
|
+
setScripts(packageJson, {
|
|
99
|
+
dev: {
|
|
100
|
+
value: "tsx ./fastify-entry.ts",
|
|
101
|
+
precedence: 20,
|
|
102
|
+
warnIfReplaced: true
|
|
103
|
+
},
|
|
104
|
+
build: {
|
|
105
|
+
value: "vite build",
|
|
106
|
+
precedence: 1,
|
|
107
|
+
warnIfReplaced: true
|
|
108
|
+
},
|
|
109
|
+
preview: {
|
|
110
|
+
value: "NODE_ENV=production tsx ./fastify-entry.ts",
|
|
111
|
+
precedence: 20
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return addDependency(packageJson, await Promise.resolve().then(() => __toESM(require_package(), 1)).then((x) => x.default), {
|
|
115
|
+
devDependencies: ["@types/node", ...props.meta.BATI.has("auth0") ? ["@types/express", "dotenv"] : []],
|
|
116
|
+
dependencies: [
|
|
117
|
+
"@fastify/middie",
|
|
118
|
+
"@fastify/static",
|
|
119
|
+
"fastify",
|
|
120
|
+
"tsx",
|
|
121
|
+
"vike",
|
|
122
|
+
"vite",
|
|
123
|
+
...props.meta.BATI.has("authjs") ? ["@auth/core", "@fastify/formbody", "@hattip/adapter-node", "vike-authjs"] : [],
|
|
124
|
+
...props.meta.BATI.has("firebase-auth") ? ["@fastify/cookie"] : [],
|
|
125
|
+
...props.meta.BATI.has("auth0") ? ["express", "express-openid-connect"] : []
|
|
126
|
+
]
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
export {
|
|
130
|
+
getPackageJson as default
|
|
131
|
+
};
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
// BATI.has("auth0")
|
|
2
|
+
import "dotenv/config";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import CredentialsProvider from "@auth/core/providers/credentials";
|
|
6
|
+
import { firebaseAdmin } from "@batijs/firebase-auth/libs/firebaseAdmin";
|
|
7
|
+
import { appRouter, type AppRouter } from "@batijs/trpc/trpc/server";
|
|
8
|
+
import { createMiddleware } from "@hattip/adapter-node";
|
|
9
|
+
import {
|
|
10
|
+
fastifyTRPCPlugin,
|
|
11
|
+
type CreateFastifyContextOptions,
|
|
12
|
+
type FastifyTRPCPluginOptions,
|
|
13
|
+
} from "@trpc/server/adapters/fastify";
|
|
14
|
+
import express from "express";
|
|
15
|
+
import { auth, type ConfigParams } from "express-openid-connect";
|
|
16
|
+
import Fastify from "fastify";
|
|
17
|
+
import { getAuth } from "firebase-admin/auth";
|
|
18
|
+
import { telefunc } from "telefunc";
|
|
19
|
+
import { VikeAuth } from "vike-authjs";
|
|
20
|
+
import { renderPage } from "vike/server";
|
|
21
|
+
|
|
22
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
23
|
+
const __dirname = dirname(__filename);
|
|
24
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
25
|
+
const root = __dirname;
|
|
26
|
+
const port = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
|
|
27
|
+
const hmrPort = process.env.HMR_PORT ? parseInt(process.env.HMR_PORT, 10) : 24678;
|
|
28
|
+
|
|
29
|
+
startServer();
|
|
30
|
+
|
|
31
|
+
async function startServer() {
|
|
32
|
+
const app = Fastify();
|
|
33
|
+
|
|
34
|
+
await app.register(await import("@fastify/middie"));
|
|
35
|
+
|
|
36
|
+
if (isProduction) {
|
|
37
|
+
await app.register(await import("@fastify/static"), {
|
|
38
|
+
root: `${root}/dist/client`,
|
|
39
|
+
wildcard: false,
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
// Instantiate Vite's development server and integrate its middleware to our server.
|
|
43
|
+
// ⚠️ We should instantiate it *only* in development. (It isn't needed in production
|
|
44
|
+
// and would unnecessarily bloat our server in production.)
|
|
45
|
+
const vite = await import("vite");
|
|
46
|
+
const viteDevMiddleware = (
|
|
47
|
+
await vite.createServer({
|
|
48
|
+
root,
|
|
49
|
+
server: { middlewareMode: true, hmr: { port: hmrPort } },
|
|
50
|
+
})
|
|
51
|
+
).middlewares;
|
|
52
|
+
app.use(viteDevMiddleware);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (BATI.has("authjs")) {
|
|
56
|
+
await app.register(await import("@fastify/formbody"));
|
|
57
|
+
/**
|
|
58
|
+
* AuthJS
|
|
59
|
+
*
|
|
60
|
+
* TODO: Replace secret {@see https://authjs.dev/reference/core#secret}
|
|
61
|
+
* TODO: Choose and implement providers
|
|
62
|
+
*
|
|
63
|
+
* @link {@see https://authjs.dev/guides/providers/custom-provider}
|
|
64
|
+
**/
|
|
65
|
+
const Auth = VikeAuth({
|
|
66
|
+
secret: "MY_SECRET",
|
|
67
|
+
providers: [
|
|
68
|
+
CredentialsProvider({
|
|
69
|
+
name: "Credentials",
|
|
70
|
+
credentials: {
|
|
71
|
+
username: { label: "Username", type: "text", placeholder: "jsmith" },
|
|
72
|
+
password: { label: "Password", type: "password" },
|
|
73
|
+
},
|
|
74
|
+
async authorize() {
|
|
75
|
+
// Add logic here to look up the user from the credentials supplied
|
|
76
|
+
const user = { id: "1", name: "J Smith", email: "jsmith@example.com" };
|
|
77
|
+
|
|
78
|
+
// Any object returned will be saved in `user` property of the JWT
|
|
79
|
+
// If you return null then an error will be displayed advising the user to check their details.
|
|
80
|
+
// 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
|
|
81
|
+
return user ?? null;
|
|
82
|
+
},
|
|
83
|
+
}),
|
|
84
|
+
],
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
app.addHook("onRequest", async (request, reply) => {
|
|
88
|
+
const vikeAuth = createMiddleware(Auth, {
|
|
89
|
+
alwaysCallNext: false,
|
|
90
|
+
});
|
|
91
|
+
const next = () =>
|
|
92
|
+
new Promise<void>((resolve) => {
|
|
93
|
+
vikeAuth(request.raw, reply.raw, () => resolve());
|
|
94
|
+
});
|
|
95
|
+
if (request.url.startsWith("/api/auth/")) {
|
|
96
|
+
await next();
|
|
97
|
+
}
|
|
98
|
+
return;
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (BATI.has("firebase-auth")) {
|
|
103
|
+
await app.register(await import("@fastify/cookie"));
|
|
104
|
+
|
|
105
|
+
app.addHook("onRequest", async (request) => {
|
|
106
|
+
const sessionCookie = request.cookies.__session;
|
|
107
|
+
if (sessionCookie) {
|
|
108
|
+
try {
|
|
109
|
+
const auth = getAuth(firebaseAdmin);
|
|
110
|
+
const decodedIdToken = await auth.verifySessionCookie(sessionCookie);
|
|
111
|
+
const user = await auth.getUser(decodedIdToken.sub);
|
|
112
|
+
request.user = user;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.debug("verifySessionCookie:", error);
|
|
115
|
+
request.user = null;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
app.post<{ Body: { idToken: string } }>("/api/sessionLogin", async (request, reply) => {
|
|
121
|
+
const idToken = request.body.idToken || "";
|
|
122
|
+
|
|
123
|
+
const expiresIn = 60 * 60 * 24 * 5 * 1000; // 5 days
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
const auth = getAuth(firebaseAdmin);
|
|
127
|
+
const sessionCookie = await auth.createSessionCookie(idToken, { expiresIn });
|
|
128
|
+
reply.setCookie("__session", sessionCookie, {
|
|
129
|
+
path: "/", // The path needs to be set manually; otherwise, it will default to "/api" in this case.
|
|
130
|
+
maxAge: expiresIn,
|
|
131
|
+
httpOnly: true,
|
|
132
|
+
secure: true,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
reply.code(200);
|
|
136
|
+
return reply.send({ status: "success" });
|
|
137
|
+
} catch (error) {
|
|
138
|
+
console.error("createSessionCookie failed :", error);
|
|
139
|
+
|
|
140
|
+
reply.code(401);
|
|
141
|
+
return reply.send({ status: "Unauthorized" });
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
app.post("/api/sessionLogout", (_, reply) => {
|
|
146
|
+
reply.clearCookie("__session");
|
|
147
|
+
reply.code(200);
|
|
148
|
+
return reply.send({ status: "Logged Out" });
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (BATI.has("auth0")) {
|
|
153
|
+
const config: ConfigParams = {
|
|
154
|
+
authRequired: false, // Controls whether authentication is required for all routes
|
|
155
|
+
auth0Logout: true, // Uses Auth0 logout feature
|
|
156
|
+
baseURL: process.env.BASE_URL?.startsWith("http") ? process.env.BASE_URL : `http://localhost:${port}`, // The URL where the application is served
|
|
157
|
+
routes: {
|
|
158
|
+
login: "/api/auth/login", // Custom login route, default is "/login"
|
|
159
|
+
logout: "/api/auth/logout", // Custom logout route, default is "/logout"
|
|
160
|
+
callback: "/api/auth/callback", // Custom callback route, default is "/callback"
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const expressApp = express();
|
|
165
|
+
|
|
166
|
+
app.use(expressApp.use(auth(config)));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (BATI.has("trpc")) {
|
|
170
|
+
/**
|
|
171
|
+
* tRPC route
|
|
172
|
+
*
|
|
173
|
+
* @link {@see https://trpc.io/docs/server/adapters/fastify}
|
|
174
|
+
**/
|
|
175
|
+
await app.register(fastifyTRPCPlugin, {
|
|
176
|
+
prefix: "/api/trpc",
|
|
177
|
+
trpcOptions: {
|
|
178
|
+
router: appRouter,
|
|
179
|
+
createContext({ req, res }: CreateFastifyContextOptions) {
|
|
180
|
+
return { req, res };
|
|
181
|
+
},
|
|
182
|
+
onError({ path, error }) {
|
|
183
|
+
// report to error monitoring
|
|
184
|
+
console.error(`Error in tRPC handler on path '${path}':`, error);
|
|
185
|
+
},
|
|
186
|
+
} satisfies FastifyTRPCPluginOptions<AppRouter>["trpcOptions"],
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (BATI.has("telefunc")) {
|
|
191
|
+
/**
|
|
192
|
+
* Telefunc route
|
|
193
|
+
*
|
|
194
|
+
* @link {@see https://telefunc.com}
|
|
195
|
+
**/
|
|
196
|
+
app.post<{ Body: string }>("/_telefunc", async (request, reply) => {
|
|
197
|
+
const httpResponse = await telefunc({
|
|
198
|
+
url: request.originalUrl || request.url,
|
|
199
|
+
method: request.method,
|
|
200
|
+
body: request.body,
|
|
201
|
+
context: request,
|
|
202
|
+
});
|
|
203
|
+
const { body, statusCode, contentType } = httpResponse;
|
|
204
|
+
|
|
205
|
+
reply.code(statusCode);
|
|
206
|
+
reply.type(contentType);
|
|
207
|
+
return reply.send(body);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Vike route
|
|
213
|
+
*
|
|
214
|
+
* @link {@see https://vike.dev}
|
|
215
|
+
**/
|
|
216
|
+
app.all("/*", async function (request, reply) {
|
|
217
|
+
const pageContextInit = BATI.has("auth0")
|
|
218
|
+
? { urlOriginal: request.originalUrl || request.url, user: request.raw.oidc.user }
|
|
219
|
+
: BATI.has("firebase-auth")
|
|
220
|
+
? { urlOriginal: request.originalUrl || request.url, user: request.user }
|
|
221
|
+
: { urlOriginal: request.originalUrl || request.url };
|
|
222
|
+
|
|
223
|
+
const pageContext = await renderPage(pageContextInit);
|
|
224
|
+
const { httpResponse } = pageContext;
|
|
225
|
+
|
|
226
|
+
if (!httpResponse) {
|
|
227
|
+
return;
|
|
228
|
+
} else {
|
|
229
|
+
const { statusCode, headers, body } = httpResponse;
|
|
230
|
+
|
|
231
|
+
headers.forEach(([name, value]) => reply.header(name, value));
|
|
232
|
+
reply.code(statusCode);
|
|
233
|
+
|
|
234
|
+
return reply.send(body);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
app.listen(
|
|
239
|
+
{
|
|
240
|
+
port: port,
|
|
241
|
+
},
|
|
242
|
+
() => {
|
|
243
|
+
console.log(`Server listening on http://localhost:${port}`);
|
|
244
|
+
},
|
|
245
|
+
);
|
|
246
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RequestContext, ResponseContext } from "express-openid-connect";
|
|
2
|
+
import type { UserRecord } from "firebase-admin/auth";
|
|
3
|
+
|
|
4
|
+
//# BATI.has("auth0")
|
|
5
|
+
declare module "node:http" {
|
|
6
|
+
interface IncomingMessage {
|
|
7
|
+
oidc: RequestContext;
|
|
8
|
+
}
|
|
9
|
+
interface ServerResponse {
|
|
10
|
+
oidc: ResponseContext;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//# BATI.has("firebase-auth")
|
|
15
|
+
declare module "fastify" {
|
|
16
|
+
interface FastifyRequest {
|
|
17
|
+
user: UserRecord | null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "dotenv/config";
|
package/dist/index.js
CHANGED
|
@@ -1537,7 +1537,7 @@ var createDefaultQueryTester = function(query, options) {
|
|
|
1537
1537
|
// package.json
|
|
1538
1538
|
var package_default = {
|
|
1539
1539
|
name: "@batijs/cli",
|
|
1540
|
-
version: "0.0.
|
|
1540
|
+
version: "0.0.188",
|
|
1541
1541
|
type: "module",
|
|
1542
1542
|
scripts: {
|
|
1543
1543
|
"check-types": "tsc --noEmit",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@batijs/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.188",
|
|
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",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"tsup": "^8.0.2",
|
|
20
20
|
"typescript": "^5.4.5",
|
|
21
21
|
"vite": "^5.2.11",
|
|
22
|
-
"@batijs/
|
|
23
|
-
"@batijs/
|
|
22
|
+
"@batijs/compile": "0.0.188",
|
|
23
|
+
"@batijs/build": "0.0.188"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@batijs/core": "0.0.
|
|
27
|
-
"@batijs/features": "0.0.
|
|
26
|
+
"@batijs/core": "0.0.188",
|
|
27
|
+
"@batijs/features": "0.0.188"
|
|
28
28
|
},
|
|
29
29
|
"bin": "./dist/index.js",
|
|
30
30
|
"exports": {
|