@lumibase/svelte 0.0.0-stage → 2.2.1
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/LICENSE +192 -0
- package/README.md +18 -2
- package/dist/index.d.ts +115 -0
- package/dist/index.js +169 -0
- package/dist/kit.d.ts +55 -0
- package/dist/kit.js +17 -0
- package/package.json +67 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
Copyright 2024-present LumiBase Contributors
|
|
181
|
+
|
|
182
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
183
|
+
you may not use this file except in compliance with the License.
|
|
184
|
+
You may obtain a copy of the License at
|
|
185
|
+
|
|
186
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
187
|
+
|
|
188
|
+
Unless required by applicable law or agreed to in writing, software
|
|
189
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
190
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
191
|
+
See the License for the specific language governing permissions and
|
|
192
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@lumibase/svelte`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Svelte 4/5 stores and actions, plus SvelteKit server helpers, for [LumiBase](https://lumibase.dev). A thin, typed layer over [`@lumibase/sdk`](../sdk).
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @lumibase/svelte @lumibase/sdk svelte
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { defineLumiStores } from '@lumibase/svelte';
|
|
11
|
+
const lumi = defineLumiStores(client, { realtime });
|
|
12
|
+
const posts = lumi.items('posts', { limit: 10 }, { live: true }); // $posts.data?.data
|
|
13
|
+
|
|
14
|
+
// src/hooks.server.ts
|
|
15
|
+
import { createLumiHandle } from '@lumibase/svelte/kit';
|
|
16
|
+
export const handle = createLumiHandle(() => ({ url, siteId, token: env.LUMIBASE_TOKEN }));
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
SSR-safe. A store's realtime subscription ends when its last subscriber leaves. Full guide: [docs/en/sdk/frameworks.md](../../docs/en/sdk/frameworks.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Readable } from 'svelte/store';
|
|
2
|
+
import { Action } from 'svelte/action';
|
|
3
|
+
import { DefaultSchema, RealtimeClient, ItemRow, ListItemsResponse, LumiClient, ListItemsParams, PresenceEntry, RealtimeEvent } from '@lumibase/sdk';
|
|
4
|
+
|
|
5
|
+
/** The content shape of `collection` in `TSchema` (what lives under `row.data`). */
|
|
6
|
+
type CollectionData<TSchema extends DefaultSchema, C extends keyof TSchema> = TSchema[C] extends Record<string, unknown> ? TSchema[C] : Record<string, unknown>;
|
|
7
|
+
/**
|
|
8
|
+
* Extension hook — the documented seam for features that layer on top of the
|
|
9
|
+
* adapter. Visual editing (V03, `@lumibase/visual-editing`, issue #681) is
|
|
10
|
+
* expected to plug in here once it exists; nothing here implements it.
|
|
11
|
+
* `setup` runs in the browser only; its return value runs on `destroy()`.
|
|
12
|
+
*/
|
|
13
|
+
interface LumiExtension<TSchema extends DefaultSchema = DefaultSchema> {
|
|
14
|
+
name: string;
|
|
15
|
+
setup(ctx: LumiBaseContext<TSchema>): void | (() => void);
|
|
16
|
+
}
|
|
17
|
+
interface LumiBaseContext<TSchema extends DefaultSchema = DefaultSchema> {
|
|
18
|
+
client: LumiClient<TSchema>;
|
|
19
|
+
realtime?: RealtimeClient | null;
|
|
20
|
+
}
|
|
21
|
+
/** True in a browser. Evaluated per call — never at import. */
|
|
22
|
+
declare function isBrowser(): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Provide the client to descendants (call during component init, e.g. in the
|
|
25
|
+
* root `+layout.svelte`). Runs extensions in the browser and returns a
|
|
26
|
+
* `destroy` that runs their cleanups — call it from `onDestroy`.
|
|
27
|
+
*/
|
|
28
|
+
declare function setLumiContext<TSchema extends DefaultSchema>(ctx: LumiBaseContext<TSchema> & {
|
|
29
|
+
extensions?: LumiExtension<TSchema>[];
|
|
30
|
+
}): {
|
|
31
|
+
destroy: () => void;
|
|
32
|
+
};
|
|
33
|
+
/** The context from {@link setLumiContext}; throws when missing. */
|
|
34
|
+
declare function getLumiContext<TSchema extends DefaultSchema = DefaultSchema>(): LumiBaseContext<TSchema>;
|
|
35
|
+
declare function getLumiClient<TSchema extends DefaultSchema = DefaultSchema>(): LumiClient<TSchema>;
|
|
36
|
+
interface AsyncValue<T> {
|
|
37
|
+
data: T | null;
|
|
38
|
+
error: unknown;
|
|
39
|
+
loading: boolean;
|
|
40
|
+
}
|
|
41
|
+
type RefreshableStore<T> = Readable<AsyncValue<T>> & {
|
|
42
|
+
/** Fetch again now (no-op while nobody is subscribed). */
|
|
43
|
+
refresh: () => Promise<void>;
|
|
44
|
+
};
|
|
45
|
+
interface StoreOptions<T> {
|
|
46
|
+
/** Seed value (e.g. from a SvelteKit `load`); skips the first fetch. */
|
|
47
|
+
initialData?: T | null;
|
|
48
|
+
/** Realtime client for `live` (browser only). */
|
|
49
|
+
realtime?: RealtimeClient | null;
|
|
50
|
+
}
|
|
51
|
+
interface ItemsStoreOptions<TSchema extends DefaultSchema, C extends keyof TSchema & string> extends StoreOptions<ListItemsResponse<CollectionData<TSchema, C>>> {
|
|
52
|
+
/** Re-fetch on realtime events for the collection (browser only). */
|
|
53
|
+
live?: boolean | {
|
|
54
|
+
filter?: Record<string, unknown>;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A store of `readItems(collection, params)`.
|
|
59
|
+
*
|
|
60
|
+
* ```svelte
|
|
61
|
+
* <script>
|
|
62
|
+
* const posts = itemsStore(client, 'posts', { limit: 10 }, { live: true, realtime });
|
|
63
|
+
* </script>
|
|
64
|
+
* {#each $posts.data?.data ?? [] as post}<li>{post.data.title}</li>{/each}
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
declare function itemsStore<TSchema extends DefaultSchema, C extends keyof TSchema & string>(client: LumiClient<TSchema>, collection: C, params?: ListItemsParams, options?: ItemsStoreOptions<TSchema, C>): RefreshableStore<ListItemsResponse<CollectionData<TSchema, C>>>;
|
|
68
|
+
interface ItemStoreOptions<TSchema extends DefaultSchema, C extends keyof TSchema & string> extends StoreOptions<ItemRow<CollectionData<TSchema, C>>> {
|
|
69
|
+
fields?: string[];
|
|
70
|
+
/** Re-fetch on realtime events for this item id (browser only). */
|
|
71
|
+
live?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/** A store of `readItem(collection, id)`. */
|
|
74
|
+
declare function itemStore<TSchema extends DefaultSchema, C extends keyof TSchema & string>(client: LumiClient<TSchema>, collection: C, id: string, options?: ItemStoreOptions<TSchema, C>): RefreshableStore<ItemRow<CollectionData<TSchema, C>>>;
|
|
75
|
+
/**
|
|
76
|
+
* The latest realtime event for a collection (`null` until one arrives).
|
|
77
|
+
* Subscribes on first subscriber, unsubscribes after the last; browser only.
|
|
78
|
+
*/
|
|
79
|
+
declare function realtimeStore(realtime: RealtimeClient, collection: string, options?: {
|
|
80
|
+
filter?: Record<string, unknown>;
|
|
81
|
+
}): Readable<RealtimeEvent | null>;
|
|
82
|
+
interface PresenceTarget {
|
|
83
|
+
collection?: string;
|
|
84
|
+
itemId?: string;
|
|
85
|
+
meta?: Record<string, unknown>;
|
|
86
|
+
}
|
|
87
|
+
/** Peers present on the target; announces our own presence while subscribed. */
|
|
88
|
+
declare function presenceStore(realtime: RealtimeClient, target: PresenceTarget): Readable<PresenceEntry[]>;
|
|
89
|
+
interface PresenceActionParams extends PresenceTarget {
|
|
90
|
+
realtime: RealtimeClient;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* `use:lumiPresence={{ realtime, collection: 'posts', itemId }}` — announce
|
|
94
|
+
* presence while the element exists; updates follow param changes, and
|
|
95
|
+
* presence is cleared (and the connection released) on destroy.
|
|
96
|
+
*/
|
|
97
|
+
declare const lumiPresence: Action<HTMLElement, PresenceActionParams>;
|
|
98
|
+
/**
|
|
99
|
+
* Bind the store factories to your schema once:
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* export const lumi = defineLumiStores(client); // client: LumiClient<Schema>
|
|
103
|
+
* const posts = lumi.items('posts', { limit: 10 });
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
declare function defineLumiStores<TSchema extends DefaultSchema>(client: LumiClient<TSchema>, defaults?: {
|
|
107
|
+
realtime?: RealtimeClient | null;
|
|
108
|
+
}): {
|
|
109
|
+
client: LumiClient<TSchema>;
|
|
110
|
+
items: <C extends keyof TSchema & string>(collection: C, params?: ListItemsParams, options?: ItemsStoreOptions<TSchema, C>) => RefreshableStore<ListItemsResponse<CollectionData<TSchema, C>>>;
|
|
111
|
+
item: <C extends keyof TSchema & string>(collection: C, id: string, options?: ItemStoreOptions<TSchema, C>) => RefreshableStore<ItemRow<CollectionData<TSchema, C>>>;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export { defineLumiStores, getLumiClient, getLumiContext, isBrowser, itemStore, itemsStore, lumiPresence, presenceStore, realtimeStore, setLumiContext };
|
|
115
|
+
export type { AsyncValue, CollectionData, ItemStoreOptions, ItemsStoreOptions, LumiBaseContext, LumiExtension, PresenceActionParams, PresenceTarget, RefreshableStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { getContext, setContext } from "svelte";
|
|
3
|
+
import { readable } from "svelte/store";
|
|
4
|
+
import {
|
|
5
|
+
readItem,
|
|
6
|
+
readItems,
|
|
7
|
+
retainRealtime
|
|
8
|
+
} from "@lumibase/sdk";
|
|
9
|
+
var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@lumibase/svelte");
|
|
10
|
+
function isBrowser() {
|
|
11
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
12
|
+
}
|
|
13
|
+
function setLumiContext(ctx) {
|
|
14
|
+
const value = { client: ctx.client, realtime: ctx.realtime ?? null };
|
|
15
|
+
setContext(CONTEXT_KEY, value);
|
|
16
|
+
const cleanups = [];
|
|
17
|
+
if (isBrowser()) {
|
|
18
|
+
for (const extension of ctx.extensions ?? []) {
|
|
19
|
+
const cleanup = extension.setup(value);
|
|
20
|
+
if (typeof cleanup === "function") cleanups.push(cleanup);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return { destroy: () => cleanups.splice(0).forEach((cleanup) => cleanup()) };
|
|
24
|
+
}
|
|
25
|
+
function getLumiContext() {
|
|
26
|
+
const ctx = getContext(CONTEXT_KEY);
|
|
27
|
+
if (!ctx) {
|
|
28
|
+
throw new Error("[@lumibase/svelte] No LumiBase client in context. Call setLumiContext({ client }) in a parent.");
|
|
29
|
+
}
|
|
30
|
+
return ctx;
|
|
31
|
+
}
|
|
32
|
+
function getLumiClient() {
|
|
33
|
+
return getLumiContext().client;
|
|
34
|
+
}
|
|
35
|
+
function attachRealtime(realtime, collection, handler, filter) {
|
|
36
|
+
const release = retainRealtime(realtime);
|
|
37
|
+
const off = realtime.subscribe(collection, handler, { filter });
|
|
38
|
+
return () => {
|
|
39
|
+
off();
|
|
40
|
+
release();
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function asyncStore(load, options, live) {
|
|
44
|
+
let set = null;
|
|
45
|
+
let state = { data: options.initialData ?? null, error: null, loading: false };
|
|
46
|
+
let ticket = 0;
|
|
47
|
+
let running = false;
|
|
48
|
+
let dirty = false;
|
|
49
|
+
const write = (next) => {
|
|
50
|
+
state = next;
|
|
51
|
+
set?.(state);
|
|
52
|
+
};
|
|
53
|
+
async function refresh() {
|
|
54
|
+
if (!set) return;
|
|
55
|
+
const mine = ++ticket;
|
|
56
|
+
write({ ...state, loading: true });
|
|
57
|
+
try {
|
|
58
|
+
const data = await load();
|
|
59
|
+
if (mine === ticket) write({ data, error: null, loading: false });
|
|
60
|
+
} catch (error) {
|
|
61
|
+
if (mine === ticket) write({ ...state, error, loading: false });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const coalesced = () => {
|
|
65
|
+
if (running) {
|
|
66
|
+
dirty = true;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
running = true;
|
|
70
|
+
void (async () => {
|
|
71
|
+
try {
|
|
72
|
+
do {
|
|
73
|
+
dirty = false;
|
|
74
|
+
await refresh();
|
|
75
|
+
} while (dirty);
|
|
76
|
+
} finally {
|
|
77
|
+
running = false;
|
|
78
|
+
}
|
|
79
|
+
})();
|
|
80
|
+
};
|
|
81
|
+
const store = readable(state, (setter) => {
|
|
82
|
+
set = setter;
|
|
83
|
+
setter(state);
|
|
84
|
+
if (!options.initialData) void refresh();
|
|
85
|
+
const stopLive = live && isBrowser() ? live(coalesced) : null;
|
|
86
|
+
return () => {
|
|
87
|
+
stopLive?.();
|
|
88
|
+
set = null;
|
|
89
|
+
ticket++;
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
return { subscribe: store.subscribe, refresh };
|
|
93
|
+
}
|
|
94
|
+
function itemsStore(client, collection, params, options = {}) {
|
|
95
|
+
const realtime = options.realtime;
|
|
96
|
+
const filter = typeof options.live === "object" ? options.live.filter : void 0;
|
|
97
|
+
return asyncStore(
|
|
98
|
+
() => client.request(readItems(collection, params)),
|
|
99
|
+
options,
|
|
100
|
+
options.live && realtime ? (refresh) => attachRealtime(realtime, collection, () => refresh(), filter) : void 0
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
function itemStore(client, collection, id, options = {}) {
|
|
104
|
+
const realtime = options.realtime;
|
|
105
|
+
return asyncStore(
|
|
106
|
+
() => client.request(readItem(collection, id, options.fields)),
|
|
107
|
+
options,
|
|
108
|
+
options.live && realtime ? (refresh) => attachRealtime(realtime, collection, (event) => {
|
|
109
|
+
if (event.itemId === id) refresh();
|
|
110
|
+
}) : void 0
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
function realtimeStore(realtime, collection, options = {}) {
|
|
114
|
+
return readable(null, (set) => {
|
|
115
|
+
if (!isBrowser()) return void 0;
|
|
116
|
+
return attachRealtime(realtime, collection, set, options.filter);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
function presenceStore(realtime, target) {
|
|
120
|
+
return readable([], (set) => {
|
|
121
|
+
if (!isBrowser()) return void 0;
|
|
122
|
+
const release = retainRealtime(realtime);
|
|
123
|
+
const off = realtime.onPresence(set);
|
|
124
|
+
realtime.presence(target);
|
|
125
|
+
return () => {
|
|
126
|
+
off();
|
|
127
|
+
realtime.presence({});
|
|
128
|
+
release();
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
var lumiPresence = (_node, params) => {
|
|
133
|
+
let current = params;
|
|
134
|
+
const release = retainRealtime(current.realtime);
|
|
135
|
+
const announce = (p) => {
|
|
136
|
+
const { realtime, ...target } = p;
|
|
137
|
+
realtime.presence(target);
|
|
138
|
+
};
|
|
139
|
+
announce(current);
|
|
140
|
+
return {
|
|
141
|
+
update(next) {
|
|
142
|
+
current = next;
|
|
143
|
+
announce(current);
|
|
144
|
+
},
|
|
145
|
+
destroy() {
|
|
146
|
+
current.realtime.presence({});
|
|
147
|
+
release();
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
function defineLumiStores(client, defaults = {}) {
|
|
152
|
+
return {
|
|
153
|
+
client,
|
|
154
|
+
items: (collection, params, options = {}) => itemsStore(client, collection, params, { realtime: defaults.realtime, ...options }),
|
|
155
|
+
item: (collection, id, options = {}) => itemStore(client, collection, id, { realtime: defaults.realtime, ...options })
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
defineLumiStores,
|
|
160
|
+
getLumiClient,
|
|
161
|
+
getLumiContext,
|
|
162
|
+
isBrowser,
|
|
163
|
+
itemStore,
|
|
164
|
+
itemsStore,
|
|
165
|
+
lumiPresence,
|
|
166
|
+
presenceStore,
|
|
167
|
+
realtimeStore,
|
|
168
|
+
setLumiContext
|
|
169
|
+
};
|
package/dist/kit.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { LumiClientOptions, DefaultSchema, LumiClient } from '@lumibase/sdk';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `@lumibase/svelte/kit` — SvelteKit server helpers.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* // src/hooks.server.ts
|
|
8
|
+
* import { env } from '$env/dynamic/private';
|
|
9
|
+
* import { createLumiHandle } from '@lumibase/svelte/kit';
|
|
10
|
+
* export const handle = createLumiHandle(() => ({
|
|
11
|
+
* url: env.LUMIBASE_URL, siteId: env.LUMIBASE_SITE_ID, token: env.LUMIBASE_TOKEN,
|
|
12
|
+
* }));
|
|
13
|
+
*
|
|
14
|
+
* // src/routes/+page.server.ts
|
|
15
|
+
* import { readItems } from '@lumibase/sdk';
|
|
16
|
+
* export const load = async ({ locals }) => ({ posts: await locals.lumi.request(readItems('posts')) });
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Typed structurally against the SvelteKit `Handle` / `RequestEvent` shape so
|
|
20
|
+
* this package does not depend on `@sveltejs/kit` (any 2.x or 3.x works).
|
|
21
|
+
* Configuration is resolved per request through a function, so the token is
|
|
22
|
+
* read from `$env/dynamic/private` at runtime and never baked into a bundle.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** The part of SvelteKit's `RequestEvent` these helpers use. */
|
|
26
|
+
interface KitEventLike {
|
|
27
|
+
fetch: typeof fetch;
|
|
28
|
+
locals: object;
|
|
29
|
+
}
|
|
30
|
+
type LumiKitConfig = Pick<LumiClientOptions, "url" | "siteId" | "token" | "headers">;
|
|
31
|
+
/**
|
|
32
|
+
* A client for one request. Uses SvelteKit's `event.fetch`, which carries
|
|
33
|
+
* cookies for same-origin calls and lets SSR responses be inlined for
|
|
34
|
+
* hydration instead of fetched twice.
|
|
35
|
+
*/
|
|
36
|
+
declare function lumiForEvent<TSchema extends DefaultSchema = DefaultSchema>(event: Pick<KitEventLike, "fetch">, config: LumiKitConfig): LumiClient<TSchema>;
|
|
37
|
+
interface KitHandleInput<E extends KitEventLike> {
|
|
38
|
+
event: E;
|
|
39
|
+
resolve: (event: E) => Response | Promise<Response>;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* A `handle` hook that puts a per-request client on `event.locals.lumi`
|
|
43
|
+
* (or `event.locals[localsKey]`). Compose with `sequence()` if you have other
|
|
44
|
+
* hooks. Declare the type in `src/app.d.ts`:
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* declare global { namespace App { interface Locals { lumi: import('@lumibase/sdk').LumiClient<Schema> } } }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function createLumiHandle<E extends KitEventLike>(config: LumiKitConfig | ((event: E) => LumiKitConfig), options?: {
|
|
51
|
+
localsKey?: string;
|
|
52
|
+
}): (input: KitHandleInput<E>) => Response | Promise<Response>;
|
|
53
|
+
|
|
54
|
+
export { createLumiHandle, lumiForEvent };
|
|
55
|
+
export type { KitEventLike, KitHandleInput, LumiKitConfig };
|
package/dist/kit.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/kit.ts
|
|
2
|
+
import { createLumiClient } from "@lumibase/sdk";
|
|
3
|
+
function lumiForEvent(event, config) {
|
|
4
|
+
return createLumiClient({ ...config, fetcher: event.fetch });
|
|
5
|
+
}
|
|
6
|
+
function createLumiHandle(config, options = {}) {
|
|
7
|
+
const key = options.localsKey ?? "lumi";
|
|
8
|
+
return ({ event, resolve }) => {
|
|
9
|
+
const resolved = typeof config === "function" ? config(event) : config;
|
|
10
|
+
event.locals[key] = lumiForEvent(event, resolved);
|
|
11
|
+
return resolve(event);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
createLumiHandle,
|
|
16
|
+
lumiForEvent
|
|
17
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lumibase/svelte",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "2.2.1",
|
|
4
|
+
"description": "Svelte stores, actions and SvelteKit server helpers for LumiBase — a thin, typed layer over @lumibase/sdk.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/khuepm/lumibase.git",
|
|
10
|
+
"directory": "packages/svelte"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://docs.lumibase.dev",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/khuepm/lumibase/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"lumibase",
|
|
18
|
+
"cms",
|
|
19
|
+
"headless-cms",
|
|
20
|
+
"svelte",
|
|
21
|
+
"sveltekit",
|
|
22
|
+
"stores",
|
|
23
|
+
"realtime"
|
|
24
|
+
],
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"svelte": "./dist/index.js",
|
|
31
|
+
"import": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"./kit": {
|
|
34
|
+
"types": "./dist/kit.d.ts",
|
|
35
|
+
"import": "./dist/kit.js"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@lumibase/sdk": "2.2.1"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"svelte": "^4.0.0 || ^5.0.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"@sveltejs/kit": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"jsdom": "^31.0.0",
|
|
55
|
+
"svelte": "^5.57.3",
|
|
56
|
+
"tsup": "^9.0.0",
|
|
57
|
+
"typescript": "^5.6.2",
|
|
58
|
+
"vitest": "^5.1.0"
|
|
59
|
+
},
|
|
60
|
+
"files": [
|
|
61
|
+
"dist"
|
|
62
|
+
],
|
|
63
|
+
"scripts": {
|
|
64
|
+
"build": "tsup",
|
|
65
|
+
"test": "vitest run",
|
|
66
|
+
"typecheck": "tsc --noEmit",
|
|
67
|
+
"clean": "rm -rf .turbo dist"
|
|
68
|
+
},
|
|
69
|
+
"module": "./dist/index.js"
|
|
6
70
|
}
|