@kosmojs/lib 0.0.26 → 0.0.28

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.
@@ -0,0 +1,25 @@
1
+ import type { PathToken } from "@kosmojs/core";
2
+ /**
3
+ * Parse a filesystem route path into structured PathToken array.
4
+ *
5
+ * Uses path-to-regexp v8 AST for parsing, with directory-friendly syntax:
6
+ * - [param] => required param - :param
7
+ * - {param} => optional param - {:param}
8
+ * - {...param} => splat - {*param}
9
+ *
10
+ * Direct :param syntax is prohibited outside {} to avoid ambiguity.
11
+ * Inside {} it is treated as path-to-regexp power syntax and used as-is.
12
+ *
13
+ * Examples:
14
+ * Required: [id], [name]
15
+ * Optional: {name}, {format}
16
+ * Splat: {...path}
17
+ * Mixed segments: shop/[id]-{name}
18
+ * Power syntax: {-v:version{-:pre}}, :name{@:version{.:min}}.js
19
+ * */
20
+ export declare const pathTokensFactory: (path: string, { transformStaticValue, }?: {
21
+ transformStaticValue?: (v: string) => string;
22
+ }) => Array<PathToken>;
23
+ export declare const createPathPattern: (tokens: Array<PathToken>) => string;
24
+ export declare const createHonoPattern: (tokens: Array<PathToken>) => string;
25
+ export declare const normalizeStaticValue: (value: string) => string;
@@ -1,3 +1,3 @@
1
- import type { RouteEntry, RouteResolverCacheFactory, SourceFolder } from "../types";
1
+ import { type RouteEntry, type RouteResolverCacheFactory, type SourceFolder } from "@kosmojs/core";
2
2
  import type { ResolverSignature } from "./base";
3
3
  export declare const resolverFactory: (sourceFolder: SourceFolder, cacheFactory?: RouteResolverCacheFactory) => Record<"pageLayoutResolver" | "pageRouteResolver" | "apiUseResolver" | "apiRouteResolver", (entry: RouteEntry) => ResolverSignature>;
@@ -0,0 +1,2 @@
1
+ export { default as resolvedTypes } from "#templates/resolved-types.hbs";
2
+ export { default as typesFile } from "#templates/types.hbs";
package/pkg/typebox.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import type { SourceFolder } from "./types";
1
+ import { type SourceFolder } from "@kosmojs/core";
2
2
  export declare const typeboxLiteralText: (text: string, sourceFolder: SourceFolder) => string;
package/pkg/vite.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Plugin } from "vite";
2
+ export declare const vitePlugins: {
3
+ nodePrefix: () => Plugin;
4
+ };
package/pkg/defaults.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export declare const defaults: {
2
- appPrefix: string;
3
- srcPrefix: string;
4
- libPrefix: string;
5
- coreDir: string;
6
- srcDir: string;
7
- libDir: string;
8
- configDir: string;
9
- apiDir: string;
10
- pagesDir: string;
11
- entryDir: string;
12
- fetchDir: string;
13
- refineTypeName: string;
14
- };
package/pkg/types.d.ts DELETED
@@ -1,314 +0,0 @@
1
- import type { IncomingMessage, ServerResponse } from "node:http";
2
- import type { ResolvedType } from "tfusion";
3
- import type { UserConfig } from "vite";
4
- import type { HTTPMethod, ValidationTarget } from "@kosmojs/api";
5
- export type FolderConfig = Pick<UserConfig, "publicDir" | "plugins" | "html" | "css" | "json" | "oxc" | "assetsInclude" | "server" | "logLevel" | "customLogger" | "clearScreen" | "envDir" | "envPrefix" | "optimizeDeps" | "ssr" | "dev" | "build" | "define" | "resolve"> & {
6
- /** Generators to run for this source folder (validation, fetch clients, OpenAPI, etc.) */
7
- generators?: Array<GeneratorFactory>;
8
- /**
9
- * Name to use for custom runtime validation refinements.
10
- * @default "VRefine"
11
- * */
12
- refineTypeName?: string;
13
- };
14
- export type SourceFolder = {
15
- /** Source folder name, e.g. "front", "admin", "app" */
16
- name: string;
17
- /** Resolved folder configuration */
18
- config: FolderConfig;
19
- /** Absolute path to the project root */
20
- root: string;
21
- /** Base URL this source folder is served from, e.g. "/" or "/admin" */
22
- baseurl: string;
23
- /** Base URL for API routes, e.g. "/api" */
24
- apiurl: string;
25
- /** output directory name, configured as `distDir` in package.json */
26
- distDir: string;
27
- };
28
- export type ProjectSettings = {
29
- root: string;
30
- sourceFolders: Array<SourceFolder>;
31
- command: "serve" | "build";
32
- devPort: number;
33
- };
34
- export type PathTokenStaticPart = {
35
- type: "static";
36
- value: string;
37
- };
38
- export type PathTokenParamPart = {
39
- type: "param";
40
- kind: "required" | "optional" | "splat";
41
- name: string;
42
- /** codegen-safe identifier: sanitized name + crc suffix when needed */
43
- const: string;
44
- };
45
- export type PathToken = {
46
- kind: "static" | "param" | "mixed";
47
- orig: string;
48
- pattern: string;
49
- parts: Array<PathTokenStaticPart | PathTokenParamPart>;
50
- };
51
- /**
52
- * route entry as found in file-system, before any processing
53
- * */
54
- export type RouteEntry = {
55
- id: string;
56
- name: string;
57
- folder: string;
58
- file: string;
59
- fileFullpath: string;
60
- pathTokens: Array<PathToken>;
61
- pathPattern: string;
62
- };
63
- export type NestedRouteEntry = {
64
- index: Omit<RouteEntry, "fileFullpath"> | undefined;
65
- layout: Omit<RouteEntry, "fileFullpath"> | undefined;
66
- parent: string | undefined;
67
- children: Array<NestedRouteEntry>;
68
- };
69
- export type ApiRoute = RouteEntry & {
70
- params: {
71
- id: string;
72
- schema: Array<PathTokenParamPart>;
73
- resolvedType: ResolvedType | undefined;
74
- };
75
- numericParams: Array<string>;
76
- optionalParams: boolean;
77
- methods: Array<string>;
78
- typeDeclarations: Array<TypeDeclaration>;
79
- validationDefinitions: Array<ValidationDefinition>;
80
- referencedFiles: Array<string>;
81
- };
82
- export type ApiUse = RouteEntry;
83
- export type PageRoute = RouteEntry & {
84
- params: {
85
- schema: Array<PathTokenParamPart>;
86
- };
87
- };
88
- export type PageLayout = RouteEntry;
89
- export type ResolvedEntry = {
90
- kind: "apiRoute";
91
- entry: ApiRoute;
92
- } | {
93
- kind: "apiUse";
94
- entry: ApiUse;
95
- } | {
96
- kind: "pageRoute";
97
- entry: PageRoute;
98
- } | {
99
- kind: "pageLayout";
100
- entry: PageLayout;
101
- };
102
- export type ValidationDefinition = {
103
- method: HTTPMethod;
104
- runtimeValidation?: boolean | undefined;
105
- customErrors?: Record<string, string> | undefined;
106
- } & ({
107
- target: "response";
108
- variants: Array<{
109
- id: string;
110
- status: number;
111
- contentType: string | undefined;
112
- body: string | undefined;
113
- resolvedType?: ResolvedType | undefined;
114
- }>;
115
- } | {
116
- target: Exclude<ValidationTarget, "response">;
117
- contentType?: string | undefined;
118
- schema: {
119
- id: string;
120
- text: string;
121
- resolvedType?: ResolvedType | undefined;
122
- };
123
- });
124
- export type RequestValidationDefinition = Exclude<ValidationDefinition, {
125
- target: "response";
126
- }>;
127
- export type ResponseValidationDefinition = Extract<ValidationDefinition, {
128
- target: "response";
129
- }>;
130
- export type TypeDeclaration = {
131
- text: string;
132
- importDeclaration?: {
133
- name: string;
134
- alias?: string | undefined;
135
- path: string;
136
- };
137
- exportDeclaration?: {
138
- name: string;
139
- alias?: string | undefined;
140
- path?: string | undefined;
141
- };
142
- typeAliasDeclaration?: {
143
- name: string;
144
- };
145
- interfaceDeclaration?: {
146
- name: string;
147
- };
148
- enumDeclaration?: {
149
- name: string;
150
- };
151
- };
152
- export type PathParams = {
153
- text: string;
154
- properties: Array<{
155
- name: string;
156
- type: string;
157
- }>;
158
- };
159
- export type WatcherEvent = {
160
- kind: "create" | "update" | "delete";
161
- file: string;
162
- };
163
- export type WatchHandler = (entries: Array<ResolvedEntry>, event?: WatcherEvent) => Promise<void>;
164
- type GeneratorFactoryReturn = {
165
- watch: WatchHandler;
166
- build: (entries: Array<ResolvedEntry>) => Promise<void>;
167
- };
168
- export type DefineGenerator = <O extends Record<string, unknown> | undefined = undefined, R extends boolean = false>(factory: [O] extends [undefined] ? () => GeneratorFactory : (o: O) => GeneratorFactory<O>, m?: GeneratorMeta) => [O] extends [undefined] ? () => GeneratorFactory : [R] extends [true] ? (o: O) => GeneratorFactory<O> : (o?: O) => GeneratorFactory<O>;
169
- export type DefineGeneratorFactory = <O extends Record<string, unknown> | undefined = undefined, R extends boolean = false>(f: GeneratorFactory<O, R>) => GeneratorFactory<O, R>;
170
- export type GeneratorFactory<O extends Record<string, unknown> | undefined = undefined, R extends boolean = false> = [O] extends [undefined] ? (f: SourceFolder) => Promise<GeneratorFactoryReturn> : [R] extends [true] ? (f: SourceFolder, o: O) => Promise<GeneratorFactoryReturn> : (f: SourceFolder, o?: O) => Promise<GeneratorFactoryReturn>;
171
- export type GeneratorMeta = {
172
- name: string;
173
- slot?: "api" | "fetch" | "ssr";
174
- /**
175
- * Package dependencies required by this generator.
176
- * The dev plugin checks installation status before running.
177
- * */
178
- dependencies?: Record<string, string>;
179
- devDependencies?: Record<string, string>;
180
- /**
181
- * Enables type resolution for generators that require fully resolved type information.
182
- *
183
- * When `true`, types are resolved to their flattened representations before
184
- * generator execution, making complete type data available.
185
- * */
186
- resolveTypes?: boolean;
187
- };
188
- type RouterSetup<R> = {
189
- clientRouter: () => R | Promise<R>;
190
- serverRouter: (ssrOpts: {
191
- url: URL;
192
- }) => R | Promise<R>;
193
- };
194
- export type RouterFactory<AppT, RouteT, RouterT> = (factory: (app: AppT, routes: Array<RouteT>) => RouterSetup<RouterT> | Promise<RouterSetup<RouterT>>) => (app: AppT, routes: Array<RouteT>, ssrOpts?: {
195
- url: URL;
196
- }) => RouterT | Promise<RouterT>;
197
- type RenderSetup = {
198
- clientRender: () => void | Promise<void>;
199
- serverRender: () => void | Promise<void>;
200
- };
201
- export type RenderFactory = (factory: () => RenderSetup | Promise<RenderSetup>) => void | Promise<void>;
202
- /**
203
- * Minimal shape of Vite's manifest.json entries.
204
- * */
205
- export type SSRManifestEntry = {
206
- file: string;
207
- src?: string;
208
- isEntry?: boolean;
209
- isDynamicEntry?: boolean;
210
- css?: string[];
211
- assets?: string[];
212
- imports?: string[];
213
- dynamicImports?: string[];
214
- };
215
- /**
216
- * SSR environment options passed to user-defined
217
- * renderToString / renderToStream functions.
218
- * */
219
- export type SSROptions = {
220
- template: string;
221
- manifest: Record<string, SSRManifestEntry>;
222
- criticalCss: Array<{
223
- text: string;
224
- path: string;
225
- }>;
226
- request: IncomingMessage;
227
- response: ServerResponse;
228
- };
229
- /**
230
- * Return type for string-based SSR rendering.
231
- *
232
- * - `head` is optional, user may choose to:
233
- * - insert the provided critical CSS (opt.criticalCss),
234
- * - override it (e.g. remove some styles),
235
- * - or supply additional <meta>/<link>/<style> tags.
236
- *
237
- * - `html` is the main server-rendered body markup for hydration.
238
- * */
239
- export type SSRStringReturn = {
240
- head?: string;
241
- html: string;
242
- };
243
- /**
244
- * SSR string mode
245
- *
246
- * Returns head + html, synchronously or async.
247
- *
248
- * The server will:
249
- * - insert returned `head` into the HTML template
250
- * - place returned `html` into the body placeholder
251
- * */
252
- export type SSRString = (url: URL, opt: SSROptions) => SSRStringReturn | Promise<SSRStringReturn>;
253
- /**
254
- * SSR stream mode
255
- *
256
- * Writes directly to the HTTP response.
257
- *
258
- * Responsibility of the user/render function:
259
- * - insert head + critical CSS at the correct time (before first flush)
260
- * - manage partial flushing, suspense boundaries, etc.
261
- *
262
- * The server will NOT modify the response body in this mode,
263
- * thus the renderer **must call `response.end()`** when streaming is finished,
264
- * otherwise the HTTP request will remain open and the client will hang.
265
- * */
266
- export type SSRStream = (url: URL, opt: SSROptions) => void | Promise<void>;
267
- /**
268
- * Default exported object from the SSR entry module (e.g. entry/server.ts).
269
- * */
270
- export type SSRSetup = {
271
- /**
272
- * The server calls render functions with current request URL and SSROptions.
273
- *
274
- * If both are provided, `renderToStream` takes precedence since streaming
275
- * enables earlier flushing and improved Time-to-First-Byte (TTFB).
276
- * `renderToString` will only be used if a streaming renderer is not available.
277
- * */
278
- renderToString?: SSRString;
279
- renderToStream?: SSRStream;
280
- /**
281
- * Controls whether the SSR server should handle static asset requests (JS, CSS, images, fonts, etc.)
282
- *
283
- * true (default)
284
- * → All built client assets are served directly from memory by SSR server.
285
- * → Easiest setup - no external file server required.
286
- *
287
- * false
288
- * → A reverse proxy/CDN *must* serve all static files.
289
- * → Otherwise any asset URL requested from browser (JS/CSS/img) will return `404`.
290
- *
291
- * Notes:
292
- * - `criticalCss.text` can still be inlined regardless of this setting.
293
- * - `criticalCss.url` remains provided for `<link>` usage,
294
- * but loading that URL is the responsibility of the external static server.
295
- *
296
- * This option enables deployments where SSR and static delivery
297
- * are cleanly separated (e.g., Node behind nginx / cloud static hosting).
298
- * */
299
- serveStaticAssets?: boolean;
300
- };
301
- export type SSRFactory = (factory: () => SSRSetup | Promise<SSRSetup>) => SSRSetup | Promise<SSRSetup>;
302
- export type RouteResolverCache = {
303
- hash: number;
304
- referencedFiles: Record<string, number>;
305
- } & Pick<ApiRoute, "params" | "methods" | "numericParams" | "typeDeclarations" | "validationDefinitions">;
306
- export type RouteResolverCacheFactory = (route: Pick<ApiRoute, "id" | "file" | "fileFullpath">, sourceFolder: SourceFolder, extraContext?: object) => {
307
- get: (opt?: {
308
- validate?: boolean;
309
- }) => Promise<RouteResolverCache | undefined>;
310
- set: (cache: Omit<RouteResolverCache, "hash" | "referencedFiles"> & {
311
- referencedFiles: Array<string>;
312
- }) => Promise<RouteResolverCache>;
313
- };
314
- export {};