@tailorkit/app 0.1.0-beta.3 → 0.1.0-beta.33

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 (58) hide show
  1. package/LICENSE.md +74 -0
  2. package/README.md +115 -0
  3. package/dist/chunk-7ZQPXG7O.js +34 -0
  4. package/dist/chunk-PJBDA2RU.js +415 -0
  5. package/dist/chunk-UDXPIO6D.js +618 -0
  6. package/dist/chunk-YX3JURMG.js +262 -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 +41 -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 -72
  20. package/dist/index.js +91 -76
  21. package/dist/preact.d.ts +52 -0
  22. package/dist/protocol.d.ts +74 -0
  23. package/dist/protocol.js +30 -0
  24. package/dist/routing.d.ts +34 -0
  25. package/dist/server/builder.d.ts +45 -0
  26. package/dist/server/bundle.d.ts +12 -0
  27. package/dist/server/functions.d.ts +105 -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/views.d.ts +126 -0
  32. package/package.json +56 -34
  33. package/LICENSE +0 -674
  34. package/dist/builder/index.d.ts +0 -11
  35. package/dist/builder/index.d.ts.map +0 -1
  36. package/dist/builder/upload-manifest.d.ts +0 -10
  37. package/dist/builder/upload-manifest.d.ts.map +0 -1
  38. package/dist/builder.js +0 -135
  39. package/dist/chunk-7L6ZZHCK.js +0 -26
  40. package/dist/chunk-AGGRUUWG.js +0 -22
  41. package/dist/config/config.d.ts +0 -17
  42. package/dist/config/config.d.ts.map +0 -1
  43. package/dist/config/index.d.ts +0 -2
  44. package/dist/config/index.d.ts.map +0 -1
  45. package/dist/config/loader.d.ts +0 -8
  46. package/dist/config/loader.d.ts.map +0 -1
  47. package/dist/config-loader.js +0 -7
  48. package/dist/config.js +0 -8
  49. package/dist/index.d.ts.map +0 -1
  50. package/dist/preact-version.d.ts +0 -8
  51. package/dist/preact-version.d.ts.map +0 -1
  52. package/src/builder/index.ts +0 -116
  53. package/src/builder/upload-manifest.ts +0 -18
  54. package/src/config/config.ts +0 -24
  55. package/src/config/index.ts +0 -1
  56. package/src/config/loader.ts +0 -35
  57. package/src/index.ts +0 -206
  58. package/src/preact-version.ts +0 -31
package/src/index.ts DELETED
@@ -1,206 +0,0 @@
1
- import { createContext, h, render } from "preact";
2
- import type { ComponentChild, ComponentChildren, ComponentType, VNode } from "preact";
3
- import { useContext } from "preact/hooks";
4
-
5
- declare const __PREACT_VERSION__: string;
6
-
7
- const preactVersion = __PREACT_VERSION__;
8
-
9
- // oxlint-disable-next-line typescript-eslint/no-empty-interface, typescript-eslint/no-empty-object-type
10
- export interface TailorKitScreens {}
11
-
12
- export type ScreenPath = Extract<keyof TailorKitScreens & string, `/${string}`>;
13
- export type AppScreenPath = ScreenPath;
14
-
15
- export type ScreenProps<TPath extends ScreenPath> = TailorKitScreens[TPath] extends object
16
- ? TailorKitScreens[TPath]
17
- : Record<string, never>;
18
-
19
- export type ScreenPropsForPath<TPath extends AppScreenPath> = TPath extends ScreenPath
20
- ? ScreenProps<TPath>
21
- : never;
22
-
23
- export type ScreenContext<TPath extends AppScreenPath> =
24
- ScreenPropsForPath<TPath> extends { context: infer TContext } ? TContext : Record<string, never>;
25
-
26
- export type View<TProps extends object = Record<string, never>> = (props: TProps) => ComponentChild;
27
-
28
- export type ScreenRuntimeProps<TPath extends AppScreenPath> =
29
- | {
30
- context: ScreenContext<TPath>;
31
- screen: TPath;
32
- status: "ready";
33
- }
34
- | {
35
- context?: never;
36
- screen: TPath;
37
- status: "loading";
38
- }
39
- | {
40
- context?: never;
41
- screen: TPath;
42
- status: "error";
43
- };
44
-
45
- export interface ScreenDefinition<TPath extends AppScreenPath = AppScreenPath> {
46
- component: View<ScreenRuntimeProps<TPath>>;
47
- path: TPath;
48
- useContext: () => ScreenContext<TPath>;
49
- }
50
-
51
- type RegisteredScreens = {
52
- [TPath in AppScreenPath]: ScreenDefinition<TPath>;
53
- };
54
-
55
- type ScreenDefinitions = Partial<RegisteredScreens>;
56
-
57
- type InvalidScreenPath<TScreens> = Exclude<keyof TScreens & string, `/${string}`>;
58
-
59
- type RequireScreenPaths<TScreens> =
60
- InvalidScreenPath<TScreens> extends never
61
- ? unknown
62
- : {
63
- readonly __tailorkit_error__: `Screen paths must start with "/". Invalid screen: ${InvalidScreenPath<TScreens>}`;
64
- };
65
-
66
- type ScreenKeyPathMismatch<TScreens> = {
67
- [TPath in keyof TScreens & string]: TScreens[TPath] extends ScreenDefinition<infer TScreenPath>
68
- ? TScreenPath extends TPath
69
- ? never
70
- : TPath
71
- : TPath;
72
- }[keyof TScreens & string];
73
-
74
- type RequireMatchingScreenKeys<TScreens> =
75
- ScreenKeyPathMismatch<TScreens> extends never
76
- ? unknown
77
- : {
78
- readonly __tailorkit_error__: `Screen key must match createScreen path. Invalid screen: ${ScreenKeyPathMismatch<TScreens>}`;
79
- };
80
-
81
- export interface TailorKitClient<TScreens extends ScreenDefinitions = ScreenDefinitions> {
82
- screens: TScreens;
83
- }
84
-
85
- export interface TailorKitClientMeta {
86
- preactVersion: string;
87
- }
88
-
89
- export interface TailorKitClientRuntime {
90
- h: typeof h;
91
- render: (vnode: VNode, parent: Element | Document | ShadowRoot | DocumentFragment) => void;
92
- }
93
-
94
- export type TailorKitClientWithMeta<TScreens extends ScreenDefinitions = ScreenDefinitions> =
95
- TailorKitClient<TScreens> & {
96
- $meta: TailorKitClientMeta;
97
- $runtime: TailorKitClientRuntime;
98
- };
99
-
100
- export const createScreen = <const TPath extends AppScreenPath>(
101
- path: TPath,
102
- options: {
103
- component: View<Record<string, never>>;
104
- error?: View<Record<string, never>>;
105
- loading?: View<Record<string, never>>;
106
- },
107
- ): ScreenDefinition<TPath> => {
108
- const Context = createContext<ScreenContext<TPath> | null>(null);
109
-
110
- const Screen = (props: ScreenRuntimeProps<TPath>) => {
111
- if (props.status === "loading") {
112
- return options.loading ? h(options.loading as ComponentType<object>, {}) : null;
113
- }
114
-
115
- if (props.status === "error") {
116
- return options.error ? h(options.error as ComponentType<object>, {}) : null;
117
- }
118
-
119
- return h(
120
- Context.Provider,
121
- { value: props.context as ScreenContext<TPath> },
122
- h(options.component as ComponentType<object>, {}),
123
- );
124
- };
125
-
126
- return {
127
- component: Screen,
128
- path,
129
- useContext: () => {
130
- const context = useContext(Context);
131
-
132
- if (context === null) {
133
- throw new Error(`Screen context is only available while rendering "${path}".`);
134
- }
135
-
136
- return context;
137
- },
138
- };
139
- };
140
-
141
- export const defineClient = <const TScreens extends ScreenDefinitions>(
142
- client: TailorKitClient<TScreens> &
143
- RequireScreenPaths<TScreens> &
144
- RequireMatchingScreenKeys<TScreens>,
145
- ): TailorKitClientWithMeta<TScreens> => ({
146
- ...client,
147
- $meta: {
148
- preactVersion,
149
- },
150
- $runtime: {
151
- h,
152
- render,
153
- },
154
- });
155
-
156
- const componentTagPrefix = "tailorkit-";
157
-
158
- const toComponentTagName = (name: string): string =>
159
- `${componentTagPrefix}${name
160
- .replaceAll(/([a-z0-9])([A-Z])/gu, "$1-$2")
161
- .replaceAll(/[\s_]+/gu, "-")
162
- .toLowerCase()}`;
163
-
164
- const toCallbackEventName = (name: string): string =>
165
- `tailorkitcallback${name.replaceAll(/[^A-Za-z0-9_$]/gu, "").toLowerCase()}`;
166
-
167
- const toEventProp = (event: string): string => `on${event}`;
168
-
169
- export const createRemoteComponent = <TProps extends object, TSlots extends readonly string[]>(
170
- name: string,
171
- options: { callbacks?: Record<string, number>; slots: TSlots },
172
- ): View<TProps & { children?: ComponentChildren }> => {
173
- const tagName = toComponentTagName(name);
174
- const callbacks = options.callbacks;
175
-
176
- if (!callbacks || Object.keys(callbacks).length === 0) {
177
- return function RemoteComponent({ children, ...props }) {
178
- return h(tagName, props, children);
179
- };
180
- }
181
-
182
- return function RemoteComponent({ children, ...props }) {
183
- const nextProps = { ...props } as Record<string, unknown>;
184
- const callbackMap: Record<string, { callback: string; inputCount: number }> = {};
185
-
186
- for (const [key, inputCount] of Object.entries(callbacks)) {
187
- const callback = nextProps[key];
188
- Reflect.deleteProperty(nextProps, key);
189
- if (typeof callback !== "function") {
190
- continue;
191
- }
192
-
193
- const eventName = toCallbackEventName(key);
194
- callbackMap[eventName] = { callback: key, inputCount };
195
- nextProps[toEventProp(eventName)] = (event: { detail?: unknown[] }) => {
196
- callback(...(event.detail ?? []).slice(0, inputCount));
197
- };
198
- }
199
-
200
- if (Object.keys(callbackMap).length > 0) {
201
- nextProps["data-tailorkit-callbacks"] = JSON.stringify(callbackMap);
202
- }
203
-
204
- return h(tagName, nextProps, children);
205
- };
206
- };
@@ -1,31 +0,0 @@
1
- export const MINIMUM_PREACT_MAJOR_VERSION = 10;
2
-
3
- export interface PreactVersionCheck {
4
- major: number;
5
- version: string;
6
- }
7
-
8
- export const parsePreactVersion = (version: string): PreactVersionCheck => {
9
- const match = /^(\d+)\./u.exec(version);
10
-
11
- if (!match?.[1]) {
12
- throw new Error(`Unable to parse Preact version "${version}".`);
13
- }
14
-
15
- return {
16
- major: Number.parseInt(match[1], 10),
17
- version,
18
- };
19
- };
20
-
21
- export const assertSupportedPreactVersion = (version: string): PreactVersionCheck => {
22
- const check = parsePreactVersion(version);
23
-
24
- if (check.major < MINIMUM_PREACT_MAJOR_VERSION) {
25
- throw new Error(
26
- `TailorKit requires Preact ${MINIMUM_PREACT_MAJOR_VERSION}.0.0 or newer, but found ${version}.`,
27
- );
28
- }
29
-
30
- return check;
31
- };