@tailorkit/app 0.1.0-beta.9 → 1.0.0-beta.44

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 (45) hide show
  1. package/LICENSE.md +74 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-7ZQPXG7O.js +34 -0
  4. package/dist/chunk-MYK3WL3S.js +428 -0
  5. package/dist/chunk-QDZXHVTU.js +277 -0
  6. package/dist/chunk-YFKW5FBP.js +623 -0
  7. package/dist/client/connection.d.ts +31 -0
  8. package/dist/client/query-store.d.ts +23 -0
  9. package/dist/client/reference.d.ts +20 -0
  10. package/dist/client/session.d.ts +20 -0
  11. package/dist/client/tools.d.ts +16 -0
  12. package/dist/client-connection.js +16 -0
  13. package/dist/client.d.ts +11 -0
  14. package/dist/client.js +40 -0
  15. package/dist/database/definition.d.ts +8 -0
  16. package/dist/database/schema.d.ts +78 -0
  17. package/dist/database/types.d.ts +8 -0
  18. package/dist/errors.d.ts +6 -0
  19. package/dist/index.d.ts +2 -87
  20. package/dist/index.js +91 -70
  21. package/dist/preact.d.ts +52 -0
  22. package/dist/protocol.d.ts +74 -0
  23. package/dist/protocol.js +39 -0
  24. package/dist/routing.d.ts +35 -0
  25. package/dist/server/builder.d.ts +46 -0
  26. package/dist/server/bundle.d.ts +12 -0
  27. package/dist/server/functions.d.ts +113 -0
  28. package/dist/server/instances.d.ts +17 -0
  29. package/dist/server.d.ts +11 -0
  30. package/dist/server.js +60 -0
  31. package/dist/validation.d.ts +5 -0
  32. package/dist/views.d.ts +126 -0
  33. package/package.json +59 -22
  34. package/LICENSE +0 -674
  35. package/dist/builder/index.d.ts +0 -10
  36. package/dist/builder/upload-manifest.d.ts +0 -13
  37. package/dist/builder.js +0 -224
  38. package/dist/chunk-EKCUETAV.js +0 -26
  39. package/dist/chunk-ZFYKVXDO.js +0 -27
  40. package/dist/config/config.d.ts +0 -20
  41. package/dist/config/index.d.ts +0 -1
  42. package/dist/config/loader.d.ts +0 -7
  43. package/dist/config-loader.js +0 -7
  44. package/dist/config.js +0 -8
  45. package/dist/preact-version.d.ts +0 -7
@@ -0,0 +1,277 @@
1
+ import {
2
+ AppError
3
+ } from "./chunk-7ZQPXG7O.js";
4
+
5
+ // src/server/functions.ts
6
+ var query = (definition) => Object.freeze({ ...definition, kind: "query" });
7
+ var mutation = (definition) => Object.freeze({ ...definition, kind: "mutation" });
8
+ var action = (definition) => Object.freeze({ ...definition, kind: "action" });
9
+ var isFunction = (value) => typeof value.kind === "string" && typeof value.handler === "function";
10
+ var defineServer = (functions) => {
11
+ for (const [name, value] of Object.entries(functions)) {
12
+ if (!/^[a-zA-Z][a-zA-Z0-9_]{0,63}$/u.test(name)) {
13
+ throw new Error("Invalid function name");
14
+ }
15
+ if (!isFunction(value)) {
16
+ defineServer(value);
17
+ }
18
+ }
19
+ return Object.freeze({ functions });
20
+ };
21
+
22
+ // src/database/definition.ts
23
+ var defineDatabase = (definition) => Object.freeze({ relations: definition.relations });
24
+
25
+ // src/server/builder.ts
26
+ import * as v from "valibot";
27
+ var FunctionBuilder = class _FunctionBuilder {
28
+ kind;
29
+ args;
30
+ result;
31
+ calls;
32
+ databaseDefinition;
33
+ constructor(kind, args, result, calls, database) {
34
+ this.kind = kind;
35
+ this.args = args;
36
+ this.result = result;
37
+ this.calls = calls;
38
+ this.databaseDefinition = database;
39
+ }
40
+ input(schema) {
41
+ return new _FunctionBuilder(
42
+ this.kind,
43
+ schema,
44
+ this.result,
45
+ this.calls,
46
+ this.databaseDefinition
47
+ );
48
+ }
49
+ output(schema) {
50
+ return new _FunctionBuilder(
51
+ this.kind,
52
+ this.args,
53
+ schema,
54
+ this.calls,
55
+ this.databaseDefinition
56
+ );
57
+ }
58
+ functions(functions) {
59
+ return new _FunctionBuilder(
60
+ this.kind,
61
+ this.args,
62
+ this.result,
63
+ functions,
64
+ this.databaseDefinition
65
+ );
66
+ }
67
+ database(database) {
68
+ return new _FunctionBuilder(
69
+ this.kind,
70
+ this.args,
71
+ this.result,
72
+ this.calls,
73
+ database
74
+ );
75
+ }
76
+ handler(handler) {
77
+ const metadata = {};
78
+ if (this.databaseDefinition) {
79
+ metadata.database = this.databaseDefinition;
80
+ }
81
+ if (this.result) {
82
+ metadata.result = this.result;
83
+ }
84
+ if (this.kind === "action") {
85
+ metadata.functions = this.calls;
86
+ }
87
+ return (
88
+ /* oxlint-disable typescript/no-unsafe-type-assertion, anti-slop/no-chained-type-assertions -- This builder or fixture preserves the declared capabilities across an erased generic boundary. */
89
+ Object.freeze({
90
+ ...metadata,
91
+ args: this.args,
92
+ handler: (context) => (
93
+ // SAFETY: The adapter reconstructs the same runtime contract, but TypeScript cannot relate the erased generic parameters across this library boundary.
94
+ // oxlint-disable-next-line anti-slop/no-chained-type-assertions -- The adapter reconstructs the same runtime contract, but TypeScript cannot relate the erased generic parameters across this library boundary.
95
+ handler({
96
+ ...context,
97
+ input: context.args
98
+ })
99
+ ),
100
+ kind: this.kind
101
+ })
102
+ );
103
+ }
104
+ };
105
+ var fn = Object.freeze({
106
+ action: new FunctionBuilder("action", v.undefined(), void 0, {}),
107
+ mutation: new FunctionBuilder("mutation", v.undefined(), void 0, {}),
108
+ query: new FunctionBuilder("query", v.undefined(), void 0, {})
109
+ });
110
+
111
+ // src/server/instances.ts
112
+ import * as v2 from "valibot";
113
+ var addressSchema = v2.object({
114
+ path: v2.pipe(v2.string(), v2.startsWith("/"), v2.maxLength(1024)),
115
+ slot: v2.pipe(v2.string(), v2.minLength(1), v2.maxLength(255))
116
+ });
117
+ var instanceResults = v2.pipe(
118
+ v2.array(
119
+ v2.object({
120
+ data: v2.unknown(),
121
+ key: v2.pipe(v2.string(), v2.minLength(1)),
122
+ metadata: v2.record(v2.string(), v2.unknown())
123
+ })
124
+ ),
125
+ v2.check(
126
+ (items) => new Set(items.map((item) => item.key)).size === items.length,
127
+ "Instance keys must be unique"
128
+ )
129
+ );
130
+ var registeredQueries = (functions) => {
131
+ const queries = {};
132
+ for (const [name, fn2] of Object.entries(functions)) {
133
+ if (isFunction(fn2)) {
134
+ if (fn2.kind === "query") {
135
+ queries[name] = fn2;
136
+ }
137
+ } else {
138
+ const nested = registeredQueries(fn2);
139
+ if (Object.keys(nested).length) {
140
+ queries[name] = nested;
141
+ }
142
+ }
143
+ }
144
+ return queries;
145
+ };
146
+ var withInstanceResolvers = (app, resolvers) => {
147
+ if (Object.hasOwn(app.functions, "_tailorkit")) {
148
+ throw new Error(
149
+ "The _tailorkit function namespace is reserved by TailorKit."
150
+ );
151
+ }
152
+ const queries = registeredQueries(app.functions);
153
+ const registry = /* @__PURE__ */ new Map();
154
+ for (const resolver of resolvers) {
155
+ v2.parse(addressSchema, resolver);
156
+ let paths = registry.get(resolver.slot);
157
+ if (!paths) {
158
+ paths = /* @__PURE__ */ new Map();
159
+ registry.set(resolver.slot, paths);
160
+ }
161
+ if (paths.has(resolver.path)) {
162
+ throw new Error(
163
+ `Duplicate instance resolver for slot "${resolver.slot}" and path "${resolver.path}".`
164
+ );
165
+ }
166
+ paths.set(resolver.path, resolver);
167
+ }
168
+ const instances = {
169
+ resolve: action({
170
+ args: v2.object({
171
+ ...addressSchema.entries,
172
+ context: v2.record(v2.string(), v2.unknown())
173
+ }),
174
+ functions: queries,
175
+ handler: async ({ args, identity, signal, queries: queriesValue }) => {
176
+ const resolver = registry.get(args.slot)?.get(args.path);
177
+ if (!resolver) {
178
+ throw new AppError("NOT_FOUND", "Instance resolver not found");
179
+ }
180
+ const value = await resolver.resolve({
181
+ context: args.context,
182
+ identity,
183
+ queries: queriesValue,
184
+ signal
185
+ });
186
+ const result = v2.safeParse(instanceResults, value);
187
+ if (!result.success) {
188
+ throw new AppError(
189
+ "INTERNAL_SERVER_ERROR",
190
+ "Invalid function result"
191
+ );
192
+ }
193
+ return await Promise.all(
194
+ result.output.map(async (item) => {
195
+ const parsed = await resolver.dataSchema["~standard"].validate(
196
+ item.data
197
+ );
198
+ if (parsed.issues) {
199
+ throw new AppError(
200
+ "INTERNAL_SERVER_ERROR",
201
+ "Invalid function result"
202
+ );
203
+ }
204
+ return { ...item, data: parsed.value };
205
+ })
206
+ );
207
+ }
208
+ })
209
+ };
210
+ const internal = defineServer({ instances });
211
+ return Object.freeze({
212
+ functions: { ...app.functions, _tailorkit: internal.functions }
213
+ });
214
+ };
215
+
216
+ // src/database/schema.ts
217
+ import {
218
+ sqliteTable,
219
+ text as sqliteText,
220
+ integer as sqliteInteger,
221
+ real as sqliteReal
222
+ } from "drizzle-orm/sqlite-core";
223
+ import {
224
+ eq,
225
+ ne,
226
+ gt,
227
+ gte,
228
+ lt,
229
+ lte,
230
+ and,
231
+ or,
232
+ asc,
233
+ desc,
234
+ count,
235
+ isNull,
236
+ isNotNull,
237
+ inArray
238
+ } from "drizzle-orm";
239
+ var table = (name, columns) => {
240
+ if (!/^[a-zA-Z][a-zA-Z0-9_]{0,62}$/u.test(name) || name.startsWith("sqlite_") || name.startsWith("tailorkit_")) {
241
+ throw new Error("Invalid or reserved table name");
242
+ }
243
+ return sqliteTable(name, columns);
244
+ };
245
+ var text = (name = "") => sqliteText(name);
246
+ var integer = (name = "") => sqliteInteger(name);
247
+ var real = (name = "") => sqliteReal(name);
248
+ var boolean = (name = "") => sqliteInteger(name, { mode: "boolean" });
249
+
250
+ export {
251
+ query,
252
+ mutation,
253
+ action,
254
+ defineServer,
255
+ defineDatabase,
256
+ fn,
257
+ withInstanceResolvers,
258
+ table,
259
+ text,
260
+ integer,
261
+ real,
262
+ boolean,
263
+ eq,
264
+ ne,
265
+ gt,
266
+ gte,
267
+ lt,
268
+ lte,
269
+ and,
270
+ or,
271
+ asc,
272
+ desc,
273
+ count,
274
+ isNull,
275
+ isNotNull,
276
+ inArray
277
+ };