@tailorkit/app 0.1.0-beta.2 → 0.1.0-beta.21

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 (54) hide show
  1. package/LICENSE.md +201 -0
  2. package/README.md +3 -0
  3. package/dist/builder/client-views.d.ts +16 -0
  4. package/dist/builder/drizzle.d.ts +2 -0
  5. package/dist/builder/generate-migrations.d.ts +6 -0
  6. package/dist/builder/index.d.ts +1 -2
  7. package/dist/builder/instances.d.ts +19 -0
  8. package/dist/builder/migrations.d.ts +6 -0
  9. package/dist/builder/schema-check.d.ts +2 -0
  10. package/dist/builder/server.d.ts +6 -0
  11. package/dist/builder/upload-manifest.d.ts +13 -3
  12. package/dist/builder.js +986 -21
  13. package/dist/chunk-7NOEYJOB.js +197 -0
  14. package/dist/{chunk-7L6ZZHCK.js → chunk-DEU6RITM.js} +6 -1
  15. package/dist/chunk-GVB2PAK7.js +28 -0
  16. package/dist/chunk-IJBQ77AH.js +35 -0
  17. package/dist/chunk-RHCGWGSG.js +645 -0
  18. package/dist/client/connection.d.ts +31 -0
  19. package/dist/client/query-store.d.ts +23 -0
  20. package/dist/client/reference.d.ts +20 -0
  21. package/dist/client/session.d.ts +14 -0
  22. package/dist/client.d.ts +7 -0
  23. package/dist/client.js +32 -0
  24. package/dist/config/config.d.ts +88 -4
  25. package/dist/config/index.d.ts +0 -1
  26. package/dist/config/loader.d.ts +0 -1
  27. package/dist/config-loader.js +2 -2
  28. package/dist/config.js +1 -1
  29. package/dist/database/schema.d.ts +78 -0
  30. package/dist/database/types.d.ts +7 -0
  31. package/dist/errors.d.ts +6 -0
  32. package/dist/index.d.ts +2 -72
  33. package/dist/index.js +81 -76
  34. package/dist/preact-version.d.ts +0 -1
  35. package/dist/preact.d.ts +52 -0
  36. package/dist/protocol.d.ts +74 -0
  37. package/dist/protocol.js +28 -0
  38. package/dist/server/builder.d.ts +40 -0
  39. package/dist/server/bundle.d.ts +17 -0
  40. package/dist/server/functions.d.ts +89 -0
  41. package/dist/server/instances.d.ts +16 -0
  42. package/dist/server.d.ts +9 -0
  43. package/dist/server.js +58 -0
  44. package/dist/views.d.ts +139 -0
  45. package/package.json +30 -23
  46. package/LICENSE +0 -674
  47. package/dist/builder/index.d.ts.map +0 -1
  48. package/dist/builder/upload-manifest.d.ts.map +0 -1
  49. package/dist/chunk-AGGRUUWG.js +0 -22
  50. package/dist/config/config.d.ts.map +0 -1
  51. package/dist/config/index.d.ts.map +0 -1
  52. package/dist/config/loader.d.ts.map +0 -1
  53. package/dist/index.d.ts.map +0 -1
  54. package/dist/preact-version.d.ts.map +0 -1
@@ -0,0 +1,197 @@
1
+ import {
2
+ AppError
3
+ } from "./chunk-IJBQ77AH.js";
4
+
5
+ // src/server/functions.ts
6
+ function query(definition) {
7
+ return Object.freeze({ ...definition, kind: "query" });
8
+ }
9
+ function mutation(definition) {
10
+ return Object.freeze({ ...definition, kind: "mutation" });
11
+ }
12
+ function action(definition) {
13
+ return Object.freeze({ ...definition, kind: "action" });
14
+ }
15
+ function isFunction(value) {
16
+ return typeof value.kind === "string" && typeof value.handler === "function";
17
+ }
18
+ function defineServer(functions) {
19
+ for (const [name, value] of Object.entries(functions)) {
20
+ if (!/^[a-zA-Z][a-zA-Z0-9_]{0,63}$/u.test(name)) {
21
+ throw new Error("Invalid function name");
22
+ }
23
+ if (!isFunction(value)) defineServer(value);
24
+ }
25
+ return Object.freeze({ functions });
26
+ }
27
+
28
+ // src/server/builder.ts
29
+ import { z } from "zod";
30
+ var FunctionBuilder = class _FunctionBuilder {
31
+ kind;
32
+ args;
33
+ result;
34
+ calls;
35
+ constructor(kind, args, result, calls) {
36
+ this.kind = kind;
37
+ this.args = args;
38
+ this.result = result;
39
+ this.calls = calls;
40
+ }
41
+ input(schema) {
42
+ return new _FunctionBuilder(this.kind, schema, this.result, this.calls);
43
+ }
44
+ output(schema) {
45
+ return new _FunctionBuilder(this.kind, this.args, schema, this.calls);
46
+ }
47
+ functions(functions) {
48
+ return new _FunctionBuilder(this.kind, this.args, this.result, functions);
49
+ }
50
+ handler(handler) {
51
+ return Object.freeze({
52
+ kind: this.kind,
53
+ args: this.args,
54
+ ...this.result ? { result: this.result } : {},
55
+ ...this.kind === "action" ? { functions: this.calls } : {},
56
+ handler: (context) => handler({ ...context, input: context.args })
57
+ });
58
+ }
59
+ };
60
+ var tk = Object.freeze({
61
+ query: new FunctionBuilder("query", z.undefined(), void 0, {}),
62
+ mutation: new FunctionBuilder("mutation", z.undefined(), void 0, {}),
63
+ action: new FunctionBuilder("action", z.undefined(), void 0, {})
64
+ });
65
+
66
+ // src/server/instances.ts
67
+ import { z as z2 } from "zod";
68
+ var addressSchema = z2.object({
69
+ slot: z2.string().min(1).max(255),
70
+ path: z2.string().startsWith("/").max(1024)
71
+ });
72
+ function registeredQueries(functions) {
73
+ const queries = {};
74
+ for (const [name, fn] of Object.entries(functions)) {
75
+ if (isFunction(fn)) {
76
+ if (fn.kind === "query") queries[name] = fn;
77
+ } else {
78
+ const nested = registeredQueries(fn);
79
+ if (Object.keys(nested).length) queries[name] = nested;
80
+ }
81
+ }
82
+ return queries;
83
+ }
84
+ function withInstanceResolvers(app, resolvers) {
85
+ if (Object.hasOwn(app.functions, "_tailorkit")) {
86
+ throw new Error("The _tailorkit function namespace is reserved by TailorKit.");
87
+ }
88
+ const queries = registeredQueries(app.functions);
89
+ const registry = /* @__PURE__ */ new Map();
90
+ for (const resolver of resolvers) {
91
+ addressSchema.parse(resolver);
92
+ let paths = registry.get(resolver.slot);
93
+ if (!paths) {
94
+ paths = /* @__PURE__ */ new Map();
95
+ registry.set(resolver.slot, paths);
96
+ }
97
+ if (paths.has(resolver.path)) {
98
+ throw new Error(
99
+ `Duplicate instance resolver for slot "${resolver.slot}" and path "${resolver.path}".`
100
+ );
101
+ }
102
+ const result = z2.array(
103
+ z2.object({
104
+ key: z2.string().min(1),
105
+ metadata: z2.record(z2.string(), z2.unknown()),
106
+ data: resolver.dataSchema
107
+ })
108
+ ).refine((items) => new Set(items.map((item) => item.key)).size === items.length, {
109
+ message: "Instance keys must be unique"
110
+ });
111
+ paths.set(resolver.path, { resolver, result });
112
+ }
113
+ const instances = {
114
+ resolve: action({
115
+ functions: queries,
116
+ args: addressSchema.extend({ context: z2.record(z2.string(), z2.unknown()) }),
117
+ async handler({ args, identity, signal, queries: queries2 }) {
118
+ const entry = registry.get(args.slot)?.get(args.path);
119
+ if (!entry) throw new AppError("NOT_FOUND", "Instance resolver not found");
120
+ const value = await entry.resolver.resolve({
121
+ context: args.context,
122
+ identity,
123
+ signal,
124
+ queries: queries2
125
+ });
126
+ const result = entry.result.safeParse(value);
127
+ if (!result.success) throw new AppError("INTERNAL_SERVER_ERROR", "Invalid function result");
128
+ return result.data;
129
+ }
130
+ })
131
+ };
132
+ const internal = defineServer({ instances });
133
+ return Object.freeze({
134
+ functions: { ...app.functions, _tailorkit: internal.functions }
135
+ });
136
+ }
137
+
138
+ // src/database/schema.ts
139
+ import {
140
+ sqliteTable,
141
+ text as sqliteText,
142
+ integer as sqliteInteger,
143
+ real as sqliteReal
144
+ } from "drizzle-orm/sqlite-core";
145
+ import {
146
+ eq,
147
+ ne,
148
+ gt,
149
+ gte,
150
+ lt,
151
+ lte,
152
+ and,
153
+ or,
154
+ asc,
155
+ desc,
156
+ count,
157
+ isNull,
158
+ isNotNull,
159
+ inArray
160
+ } from "drizzle-orm";
161
+ function table(name, columns) {
162
+ if (!/^[a-zA-Z][a-zA-Z0-9_]{0,62}$/u.test(name) || name.startsWith("sqlite_") || name.startsWith("tailorkit_"))
163
+ throw new Error("Invalid or reserved table name");
164
+ return sqliteTable(name, columns);
165
+ }
166
+ var text = (name = "") => sqliteText(name);
167
+ var integer = (name = "") => sqliteInteger(name);
168
+ var real = (name = "") => sqliteReal(name);
169
+ var boolean = (name = "") => sqliteInteger(name, { mode: "boolean" });
170
+
171
+ export {
172
+ query,
173
+ mutation,
174
+ action,
175
+ defineServer,
176
+ tk,
177
+ withInstanceResolvers,
178
+ table,
179
+ text,
180
+ integer,
181
+ real,
182
+ boolean,
183
+ eq,
184
+ ne,
185
+ gt,
186
+ gte,
187
+ lt,
188
+ lte,
189
+ and,
190
+ or,
191
+ asc,
192
+ desc,
193
+ count,
194
+ isNull,
195
+ isNotNull,
196
+ inArray
197
+ };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  tailorkitConfigSchema
3
- } from "./chunk-AGGRUUWG.js";
3
+ } from "./chunk-GVB2PAK7.js";
4
4
 
5
5
  // src/config/loader.ts
6
6
  import { cosmiconfig } from "cosmiconfig";
@@ -14,6 +14,11 @@ var loadTailorKitConfig = async (configPath, cwd = process.cwd()) => {
14
14
  if (result === null) {
15
15
  throw new Error(`Could not find tailorkit config from ${searchFrom}.`);
16
16
  }
17
+ if (result.config && typeof result.config === "object" && "storage" in result.config) {
18
+ throw new Error(
19
+ "The legacy storage configuration was removed. Use server configuration and @tailorkit/app."
20
+ );
21
+ }
17
22
  return {
18
23
  config: tailorkitConfigSchema.parse(result.config),
19
24
  filepath: result.filepath,
@@ -0,0 +1,28 @@
1
+ // src/config/config.ts
2
+ import { z } from "zod";
3
+ var buildConfigSchema = z.object({
4
+ outDir: z.string().default(".tailorkit")
5
+ });
6
+ var logosConfigSchema = z.object({
7
+ dark: z.string().min(1).optional(),
8
+ light: z.string().min(1).optional()
9
+ });
10
+ var tailorkitConfigSchema = z.strictObject({
11
+ appId: z.string().min(1).optional(),
12
+ build: buildConfigSchema.optional(),
13
+ client: z.strictObject({ entry: z.string().min(1).default("src/client.ts") }).optional(),
14
+ host: z.string().url(),
15
+ logos: logosConfigSchema.optional(),
16
+ server: z.strictObject({
17
+ entry: z.string().min(1).default("src/server.ts"),
18
+ migrations: z.string().min(1).optional()
19
+ }).optional()
20
+ });
21
+ var defineConfig = (config) => config;
22
+ var defineTailorKitConfig = defineConfig;
23
+
24
+ export {
25
+ tailorkitConfigSchema,
26
+ defineConfig,
27
+ defineTailorKitConfig
28
+ };
@@ -0,0 +1,35 @@
1
+ // src/errors.ts
2
+ var AppError = class extends Error {
3
+ constructor(code, message = code) {
4
+ super(message);
5
+ this.code = code;
6
+ this.name = "AppError";
7
+ }
8
+ code;
9
+ };
10
+ function appError(error) {
11
+ if (error instanceof AppError) return error;
12
+ if (error instanceof Error && error.name === "AbortError")
13
+ return new AppError("UNAVAILABLE", "App connection interrupted");
14
+ if (error && typeof error === "object" && "code" in error && error.code === "NETWORK_ERROR")
15
+ return new AppError("UNAVAILABLE", "App connection interrupted");
16
+ if (error && typeof error === "object" && "code" in error && [
17
+ "BAD_REQUEST",
18
+ "UNAUTHORIZED",
19
+ "FORBIDDEN",
20
+ "NOT_FOUND",
21
+ "CONFLICT",
22
+ "INCOMPATIBLE_VERSION",
23
+ "UNAVAILABLE"
24
+ ].includes(String(error.code)))
25
+ return new AppError(
26
+ error.code,
27
+ "message" in error ? String(error.message) : String(error.code)
28
+ );
29
+ return new AppError("INTERNAL_SERVER_ERROR", "App function failed");
30
+ }
31
+
32
+ export {
33
+ AppError,
34
+ appError
35
+ };