@zap-js/client 0.0.2 → 0.0.5
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/README.md +310 -24
- package/bin/zap +0 -0
- package/bin/zap-codegen +0 -0
- package/dist/cli/commands/build.d.ts +11 -0
- package/dist/cli/commands/build.js +282 -0
- package/dist/cli/commands/codegen.d.ts +8 -0
- package/dist/cli/commands/codegen.js +95 -0
- package/dist/cli/commands/dev.d.ts +20 -0
- package/dist/cli/commands/dev.js +78 -0
- package/dist/cli/commands/new.d.ts +9 -0
- package/dist/cli/commands/new.js +307 -0
- package/dist/cli/commands/routes-old.d.ts +9 -0
- package/dist/cli/commands/routes-old.js +106 -0
- package/dist/cli/commands/routes.d.ts +11 -0
- package/dist/cli/commands/routes.js +280 -0
- package/dist/cli/commands/serve.d.ts +17 -0
- package/dist/cli/commands/serve.js +386 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +76 -0
- package/dist/cli/utils/index.d.ts +2 -0
- package/dist/cli/utils/index.js +2 -0
- package/dist/cli/utils/logger.d.ts +84 -0
- package/dist/cli/utils/logger.js +181 -0
- package/dist/cli/utils/port-finder.d.ts +8 -0
- package/dist/cli/utils/port-finder.js +48 -0
- package/dist/dev-server/codegen-runner.d.ts +41 -0
- package/dist/dev-server/codegen-runner.js +172 -0
- package/dist/dev-server/hot-reload.d.ts +72 -0
- package/dist/dev-server/hot-reload.js +280 -0
- package/dist/dev-server/index.d.ts +8 -0
- package/dist/dev-server/index.js +8 -0
- package/dist/dev-server/route-scanner.d.ts +84 -0
- package/dist/dev-server/route-scanner.js +113 -0
- package/dist/dev-server/rust-builder.d.ts +66 -0
- package/dist/dev-server/rust-builder.js +286 -0
- package/dist/dev-server/server.d.ts +147 -0
- package/dist/dev-server/server.js +660 -0
- package/dist/dev-server/vite-proxy.d.ts +56 -0
- package/dist/dev-server/vite-proxy.js +212 -0
- package/dist/dev-server/watcher.d.ts +48 -0
- package/dist/dev-server/watcher.js +127 -0
- package/dist/router/codegen-enhanced.d.ts +5 -0
- package/dist/router/codegen-enhanced.js +275 -0
- package/dist/router/codegen.d.ts +17 -0
- package/dist/router/codegen.js +654 -0
- package/dist/router/index.d.ts +16 -0
- package/dist/router/index.js +19 -0
- package/dist/router/scanner.d.ts +86 -0
- package/dist/router/scanner.js +689 -0
- package/dist/router/ssg.d.ts +115 -0
- package/dist/router/ssg.js +202 -0
- package/dist/router/types.d.ts +124 -0
- package/dist/router/types.js +9 -0
- package/dist/router/watch.d.ts +38 -0
- package/dist/router/watch.js +135 -0
- package/dist/runtime/csrf.d.ts +146 -0
- package/dist/runtime/csrf.js +166 -0
- package/dist/runtime/error-boundary.d.ts +129 -0
- package/dist/runtime/error-boundary.js +287 -0
- package/dist/runtime/hooks.d.ts +83 -0
- package/dist/runtime/hooks.js +96 -0
- package/dist/runtime/index.d.ts +229 -0
- package/dist/runtime/index.js +449 -0
- package/dist/runtime/ipc-client.d.ts +144 -0
- package/dist/runtime/ipc-client.js +621 -0
- package/dist/runtime/logger.d.ts +71 -0
- package/dist/runtime/logger.js +164 -0
- package/dist/runtime/middleware.d.ts +66 -0
- package/dist/runtime/middleware.js +114 -0
- package/dist/runtime/process-manager.d.ts +51 -0
- package/dist/runtime/process-manager.js +207 -0
- package/dist/runtime/router-simple.d.ts +98 -0
- package/dist/runtime/router-simple.js +330 -0
- package/dist/runtime/router.d.ts +103 -0
- package/dist/runtime/router.js +435 -0
- package/dist/runtime/rpc-client.d.ts +35 -0
- package/dist/runtime/rpc-client.js +140 -0
- package/dist/runtime/streaming-utils.d.ts +86 -0
- package/dist/runtime/streaming-utils.js +150 -0
- package/dist/runtime/types.d.ts +465 -0
- package/dist/runtime/types.js +60 -0
- package/dist/runtime/websockets-utils.d.ts +50 -0
- package/dist/runtime/websockets-utils.js +92 -0
- package/package.json +30 -20
- package/index.js +0 -29
- package/internal/cli/package.json +0 -46
- package/internal/cli/tsconfig.tsbuildinfo +0 -1
- package/internal/dev-server/node_modules/ora/index.d.ts +0 -332
- package/internal/dev-server/node_modules/ora/index.js +0 -416
- package/internal/dev-server/node_modules/ora/license +0 -9
- package/internal/dev-server/node_modules/ora/node_modules/string-width/index.d.ts +0 -36
- package/internal/dev-server/node_modules/ora/node_modules/string-width/index.js +0 -65
- package/internal/dev-server/node_modules/ora/node_modules/string-width/license +0 -9
- package/internal/dev-server/node_modules/ora/node_modules/string-width/node_modules/emoji-regex/LICENSE-MIT.txt +0 -20
- package/internal/dev-server/node_modules/ora/node_modules/string-width/node_modules/emoji-regex/README.md +0 -107
- package/internal/dev-server/node_modules/ora/node_modules/string-width/node_modules/emoji-regex/index.d.ts +0 -3
- package/internal/dev-server/node_modules/ora/node_modules/string-width/node_modules/emoji-regex/index.js +0 -4
- package/internal/dev-server/node_modules/ora/node_modules/string-width/node_modules/emoji-regex/index.mjs +0 -4
- package/internal/dev-server/node_modules/ora/node_modules/string-width/node_modules/emoji-regex/package.json +0 -46
- package/internal/dev-server/node_modules/ora/node_modules/string-width/package.json +0 -60
- package/internal/dev-server/node_modules/ora/node_modules/string-width/readme.md +0 -62
- package/internal/dev-server/node_modules/ora/package.json +0 -66
- package/internal/dev-server/node_modules/ora/readme.md +0 -325
- package/internal/dev-server/package.json +0 -41
- package/internal/router/package.json +0 -28
- package/internal/runtime/package.json +0 -41
- package/internal/runtime/src/error-boundary.tsx +0 -476
- package/internal/runtime/src/router-simple.tsx +0 -640
- package/internal/runtime/src/router.tsx +0 -771
- package/internal/runtime/tsconfig.tsbuildinfo +0 -1
- package/src/errors.js +0 -33
- package/src/logger.js +0 -10
- package/src/middleware.js +0 -32
- package/src/router.js +0 -41
- package/src/types.js +0 -39
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enhanced Route Code Generator with Layout Support
|
|
3
|
+
*/
|
|
4
|
+
import { writeFileSync, mkdirSync, existsSync } from 'fs';
|
|
5
|
+
import { join, relative } from 'path';
|
|
6
|
+
export function generateEnhancedRouteTree(options) {
|
|
7
|
+
const { outputDir, routeTree } = options;
|
|
8
|
+
if (!existsSync(outputDir)) {
|
|
9
|
+
mkdirSync(outputDir, { recursive: true });
|
|
10
|
+
}
|
|
11
|
+
// Generate TypeScript route tree
|
|
12
|
+
const tsContent = generateTypeScriptRouteTree(routeTree, outputDir);
|
|
13
|
+
writeFileSync(join(outputDir, 'routeTree.ts'), tsContent);
|
|
14
|
+
// Generate JSON manifest
|
|
15
|
+
const manifest = generateManifest(routeTree);
|
|
16
|
+
writeFileSync(join(outputDir, 'routeManifest.json'), JSON.stringify(manifest, null, 2));
|
|
17
|
+
// Generate router configuration with layouts
|
|
18
|
+
const routerConfig = generateRouterConfigWithLayouts(routeTree, outputDir);
|
|
19
|
+
writeFileSync(join(outputDir, 'routerConfig.ts'), routerConfig);
|
|
20
|
+
}
|
|
21
|
+
function generateTypeScriptRouteTree(tree, outputDir) {
|
|
22
|
+
const lines = [
|
|
23
|
+
'/**',
|
|
24
|
+
' * Auto-generated by @zap-js/server',
|
|
25
|
+
' * DO NOT EDIT MANUALLY',
|
|
26
|
+
' */',
|
|
27
|
+
'',
|
|
28
|
+
"import { lazy } from 'react';",
|
|
29
|
+
'',
|
|
30
|
+
];
|
|
31
|
+
// Import ErrorBoundary if needed
|
|
32
|
+
const hasAnyErrorComponents = tree.routes.some((r) => r.hasErrorComponent);
|
|
33
|
+
if (hasAnyErrorComponents) {
|
|
34
|
+
lines.push("import { ErrorBoundary } from '@zap-js/client';");
|
|
35
|
+
}
|
|
36
|
+
lines.push('');
|
|
37
|
+
// Generate layout imports
|
|
38
|
+
if (tree.layouts.length > 0) {
|
|
39
|
+
lines.push('// Layout components');
|
|
40
|
+
for (const layout of tree.layouts) {
|
|
41
|
+
const varName = layoutToVarName(layout);
|
|
42
|
+
const relativePath = getRelativeImportPath(outputDir, layout.filePath);
|
|
43
|
+
lines.push(`const ${varName} = lazy(() => import('${relativePath}'));`);
|
|
44
|
+
}
|
|
45
|
+
lines.push('');
|
|
46
|
+
}
|
|
47
|
+
// Generate route imports
|
|
48
|
+
lines.push('// Route components');
|
|
49
|
+
for (const route of tree.routes.filter(r => r.type === 'page')) {
|
|
50
|
+
const varName = routeToVarName(route);
|
|
51
|
+
const relativePath = getRelativeImportPath(outputDir, route.filePath);
|
|
52
|
+
lines.push(`const ${varName} = lazy(() => import('${relativePath}'));`);
|
|
53
|
+
if (route.hasErrorComponent) {
|
|
54
|
+
const errorVarName = routeToErrorVarName(route);
|
|
55
|
+
const exportName = route.errorComponentExport || 'errorComponent';
|
|
56
|
+
lines.push(`const ${errorVarName} = lazy(() => import('${relativePath}').then(m => ({ default: m.${exportName} })));`);
|
|
57
|
+
}
|
|
58
|
+
if (route.hasPendingComponent) {
|
|
59
|
+
const pendingVarName = routeToPendingVarName(route);
|
|
60
|
+
const exportName = route.pendingComponentExport || 'pendingComponent';
|
|
61
|
+
lines.push(`const ${pendingVarName} = lazy(() => import('${relativePath}').then(m => ({ default: m.${exportName} })));`);
|
|
62
|
+
}
|
|
63
|
+
if (route.hasMeta) {
|
|
64
|
+
const metaVarName = routeToMetaVarName(route);
|
|
65
|
+
lines.push(`const ${metaVarName} = () => import('${relativePath}').then(m => m.meta);`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
lines.push('');
|
|
69
|
+
// Generate layout configuration
|
|
70
|
+
if (tree.layouts.length > 0) {
|
|
71
|
+
lines.push('// Layout definitions');
|
|
72
|
+
lines.push('export const layoutDefinitions = [');
|
|
73
|
+
for (const layout of tree.layouts) {
|
|
74
|
+
lines.push(' {');
|
|
75
|
+
lines.push(` path: '${layout.urlPath}',`);
|
|
76
|
+
lines.push(` component: ${layoutToVarName(layout)},`);
|
|
77
|
+
if (layout.parentLayout) {
|
|
78
|
+
const parent = tree.layouts.find(l => l.filePath === layout.parentLayout);
|
|
79
|
+
if (parent) {
|
|
80
|
+
lines.push(` parentLayout: '${parent.urlPath}',`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
lines.push(' },');
|
|
84
|
+
}
|
|
85
|
+
lines.push('];');
|
|
86
|
+
lines.push('');
|
|
87
|
+
}
|
|
88
|
+
// Generate route configuration
|
|
89
|
+
lines.push('// Route definitions');
|
|
90
|
+
lines.push('export const routeDefinitions = [');
|
|
91
|
+
for (const route of tree.routes.filter(r => r.type === 'page')) {
|
|
92
|
+
const { pattern, paramNames } = pathToRegexForCodegen(route.urlPath);
|
|
93
|
+
lines.push(' {');
|
|
94
|
+
lines.push(` path: '${route.urlPath}',`);
|
|
95
|
+
lines.push(` pattern: ${pattern},`);
|
|
96
|
+
lines.push(` paramNames: ${JSON.stringify(paramNames)},`);
|
|
97
|
+
lines.push(` component: ${routeToVarName(route)},`);
|
|
98
|
+
if (route.layoutPath) {
|
|
99
|
+
const layout = tree.layouts.find(l => l.filePath === route.layoutPath);
|
|
100
|
+
if (layout) {
|
|
101
|
+
lines.push(` layoutPath: '${layout.urlPath}',`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (route.hasErrorComponent) {
|
|
105
|
+
lines.push(` errorComponent: ${routeToErrorVarName(route)},`);
|
|
106
|
+
}
|
|
107
|
+
if (route.hasPendingComponent) {
|
|
108
|
+
lines.push(` pendingComponent: ${routeToPendingVarName(route)},`);
|
|
109
|
+
}
|
|
110
|
+
if (route.hasMeta) {
|
|
111
|
+
lines.push(` meta: ${routeToMetaVarName(route)},`);
|
|
112
|
+
}
|
|
113
|
+
lines.push(' },');
|
|
114
|
+
}
|
|
115
|
+
lines.push('];');
|
|
116
|
+
lines.push('');
|
|
117
|
+
// Generate API routes
|
|
118
|
+
if (tree.apiRoutes.length > 0) {
|
|
119
|
+
lines.push('// API routes for server');
|
|
120
|
+
lines.push('export const apiRoutes = [');
|
|
121
|
+
for (const route of tree.apiRoutes) {
|
|
122
|
+
lines.push(' {');
|
|
123
|
+
lines.push(` path: '${route.urlPath}',`);
|
|
124
|
+
lines.push(` filePath: '${route.relativePath}',`);
|
|
125
|
+
lines.push(` methods: ${JSON.stringify(route.methods || ['GET'])},`);
|
|
126
|
+
lines.push(' },');
|
|
127
|
+
}
|
|
128
|
+
lines.push('];');
|
|
129
|
+
lines.push('');
|
|
130
|
+
}
|
|
131
|
+
// Generate route path type
|
|
132
|
+
const paths = tree.routes
|
|
133
|
+
.filter(r => r.type === 'page')
|
|
134
|
+
.map(r => ` | '${r.urlPath}'`);
|
|
135
|
+
lines.push('// Type-safe route paths');
|
|
136
|
+
lines.push(`export type RoutePath =\n${paths.join('\n')};`);
|
|
137
|
+
lines.push('');
|
|
138
|
+
// Generate path builder functions
|
|
139
|
+
lines.push('// Path builder functions');
|
|
140
|
+
for (const route of tree.routes.filter(r => r.type === 'page' && r.params.length > 0)) {
|
|
141
|
+
const fnName = urlPathToFunctionName(route.urlPath);
|
|
142
|
+
const params = route.params
|
|
143
|
+
.map(p => `${p.name}${p.optional ? '?' : ''}: string`)
|
|
144
|
+
.join(', ');
|
|
145
|
+
let template = route.urlPath;
|
|
146
|
+
for (const param of route.params) {
|
|
147
|
+
const optionalMarker = param.optional ? '?' : '';
|
|
148
|
+
if (param.catchAll) {
|
|
149
|
+
template = template.replace(`*${param.name}${optionalMarker}`, `\${${param.name} || ''}`);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
template = template.replace(`:${param.name}${optionalMarker}`, `\${${param.name} || ''}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
lines.push(`export function ${fnName}(${params}): string {`);
|
|
156
|
+
lines.push(` return \`${template}\`.replace(/\\/+$/, '') || '/';`);
|
|
157
|
+
lines.push('}');
|
|
158
|
+
lines.push('');
|
|
159
|
+
}
|
|
160
|
+
return lines.join('\n');
|
|
161
|
+
}
|
|
162
|
+
function generateRouterConfigWithLayouts(tree, outputDir) {
|
|
163
|
+
const lines = [
|
|
164
|
+
'/**',
|
|
165
|
+
' * Production Router Configuration',
|
|
166
|
+
' * Auto-generated by @zap-js/server',
|
|
167
|
+
' */',
|
|
168
|
+
'',
|
|
169
|
+
"import type { RouteDefinition, LayoutDefinition } from '@zap-js/client';",
|
|
170
|
+
"import { routeDefinitions as routes, layoutDefinitions as layouts } from './routeTree.js';",
|
|
171
|
+
'',
|
|
172
|
+
'// Re-export for convenience',
|
|
173
|
+
'export { routes, layouts };',
|
|
174
|
+
'',
|
|
175
|
+
'// Route configuration for RouterProvider',
|
|
176
|
+
'export const routerConfig = {',
|
|
177
|
+
' routes,',
|
|
178
|
+
' layouts,',
|
|
179
|
+
'};',
|
|
180
|
+
'',
|
|
181
|
+
];
|
|
182
|
+
return lines.join('\n');
|
|
183
|
+
}
|
|
184
|
+
function generateManifest(tree) {
|
|
185
|
+
return {
|
|
186
|
+
version: '1.0.0',
|
|
187
|
+
generatedAt: new Date().toISOString(),
|
|
188
|
+
routes: tree.routes.map((r) => ({
|
|
189
|
+
...r,
|
|
190
|
+
filePath: r.relativePath,
|
|
191
|
+
})),
|
|
192
|
+
apiRoutes: tree.apiRoutes.map((r) => ({
|
|
193
|
+
...r,
|
|
194
|
+
filePath: r.relativePath,
|
|
195
|
+
})),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
// Helper functions
|
|
199
|
+
function routeToVarName(route) {
|
|
200
|
+
let name = route.urlPath
|
|
201
|
+
.replace(/^\//, '')
|
|
202
|
+
.replace(/\//g, '_')
|
|
203
|
+
.replace(/[:\*\[\]]/g, '')
|
|
204
|
+
.replace(/-/g, '_')
|
|
205
|
+
.replace(/\?/g, '');
|
|
206
|
+
if (!name)
|
|
207
|
+
name = 'Index';
|
|
208
|
+
name = name
|
|
209
|
+
.split('_')
|
|
210
|
+
.map((s) => s.charAt(0).toUpperCase() + s.slice(1))
|
|
211
|
+
.join('');
|
|
212
|
+
return `${name}Route`;
|
|
213
|
+
}
|
|
214
|
+
function routeToErrorVarName(route) {
|
|
215
|
+
return routeToVarName(route).replace(/Route$/, 'ErrorComponent');
|
|
216
|
+
}
|
|
217
|
+
function routeToPendingVarName(route) {
|
|
218
|
+
return routeToVarName(route).replace(/Route$/, 'PendingComponent');
|
|
219
|
+
}
|
|
220
|
+
function routeToMetaVarName(route) {
|
|
221
|
+
return routeToVarName(route).replace(/Route$/, 'Meta');
|
|
222
|
+
}
|
|
223
|
+
function layoutToVarName(layout) {
|
|
224
|
+
let name = layout.scopePath
|
|
225
|
+
.replace(/\//g, '_')
|
|
226
|
+
.replace(/-/g, '_')
|
|
227
|
+
.replace(/[\[\]]/g, '');
|
|
228
|
+
if (!name)
|
|
229
|
+
name = 'Root';
|
|
230
|
+
name = name
|
|
231
|
+
.split('_')
|
|
232
|
+
.filter(Boolean)
|
|
233
|
+
.map((s) => s.charAt(0).toUpperCase() + s.slice(1))
|
|
234
|
+
.join('');
|
|
235
|
+
return `${name}Layout`;
|
|
236
|
+
}
|
|
237
|
+
function urlPathToFunctionName(urlPath) {
|
|
238
|
+
let name = urlPath
|
|
239
|
+
.replace(/^\//, '')
|
|
240
|
+
.replace(/\//g, '_')
|
|
241
|
+
.replace(/[:\*\[\]]/g, '')
|
|
242
|
+
.replace(/-/g, '_')
|
|
243
|
+
.replace(/\?/g, '');
|
|
244
|
+
if (!name)
|
|
245
|
+
name = 'index';
|
|
246
|
+
const parts = name.split('_');
|
|
247
|
+
name = parts[0] + parts.slice(1).map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join('');
|
|
248
|
+
return `${name}Path`;
|
|
249
|
+
}
|
|
250
|
+
function pathToRegexForCodegen(path) {
|
|
251
|
+
const paramNames = [];
|
|
252
|
+
let regexStr = path
|
|
253
|
+
.replace(/\//g, '\\/')
|
|
254
|
+
.replace(/:\w+\??/g, (match) => {
|
|
255
|
+
const isOptional = match.endsWith('?');
|
|
256
|
+
const name = match.slice(1).replace('?', '');
|
|
257
|
+
paramNames.push(name);
|
|
258
|
+
return isOptional ? '([^/]*)?' : '([^/]+)';
|
|
259
|
+
})
|
|
260
|
+
.replace(/\*\w+\??/g, (match) => {
|
|
261
|
+
const isOptional = match.endsWith('?');
|
|
262
|
+
const name = match.slice(1).replace('?', '');
|
|
263
|
+
paramNames.push(name);
|
|
264
|
+
return isOptional ? '(.*)?' : '(.+)';
|
|
265
|
+
});
|
|
266
|
+
return { pattern: `/^${regexStr}$/`, paramNames };
|
|
267
|
+
}
|
|
268
|
+
function getRelativeImportPath(fromDir, toFile) {
|
|
269
|
+
let rel = relative(fromDir, toFile);
|
|
270
|
+
if (!rel.startsWith('.')) {
|
|
271
|
+
rel = './' + rel;
|
|
272
|
+
}
|
|
273
|
+
rel = rel.replace(/\.(tsx?|jsx?)$/, '');
|
|
274
|
+
return rel;
|
|
275
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Route tree code generator
|
|
3
|
+
*
|
|
4
|
+
* Generates TypeScript files for:
|
|
5
|
+
* - routeTree.ts - Route definitions and type-safe paths
|
|
6
|
+
* - routeManifest.json - JSON manifest for Rust server
|
|
7
|
+
* - routerConfig.ts - Client-side router configuration
|
|
8
|
+
*/
|
|
9
|
+
import type { RouteTree, CodegenOptions } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Generate route tree TypeScript file and manifest
|
|
12
|
+
*/
|
|
13
|
+
export declare function generateRouteTree(options: CodegenOptions): void;
|
|
14
|
+
/**
|
|
15
|
+
* Generate a Rust-compatible route manifest
|
|
16
|
+
*/
|
|
17
|
+
export declare function generateRustManifest(tree: RouteTree): string;
|