@molecule/app-svelte 1.0.0
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 +115 -0
- package/README.md +2032 -0
- package/dist/context.d.ts +126 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +217 -0
- package/dist/context.js.map +1 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/stores/async-state.d.ts +47 -0
- package/dist/stores/async-state.d.ts.map +1 -0
- package/dist/stores/async-state.js +89 -0
- package/dist/stores/async-state.js.map +1 -0
- package/dist/stores/auth.d.ts +54 -0
- package/dist/stores/auth.d.ts.map +1 -0
- package/dist/stores/auth.js +93 -0
- package/dist/stores/auth.js.map +1 -0
- package/dist/stores/capacitor-app.d.ts +46 -0
- package/dist/stores/capacitor-app.d.ts.map +1 -0
- package/dist/stores/capacitor-app.js +59 -0
- package/dist/stores/capacitor-app.js.map +1 -0
- package/dist/stores/change-password.d.ts +17 -0
- package/dist/stores/change-password.d.ts.map +1 -0
- package/dist/stores/change-password.js +17 -0
- package/dist/stores/change-password.js.map +1 -0
- package/dist/stores/device.d.ts +39 -0
- package/dist/stores/device.d.ts.map +1 -0
- package/dist/stores/device.js +51 -0
- package/dist/stores/device.js.map +1 -0
- package/dist/stores/forms.d.ts +121 -0
- package/dist/stores/forms.d.ts.map +1 -0
- package/dist/stores/forms.js +178 -0
- package/dist/stores/forms.js.map +1 -0
- package/dist/stores/http.d.ts +119 -0
- package/dist/stores/http.d.ts.map +1 -0
- package/dist/stores/http.js +206 -0
- package/dist/stores/http.js.map +1 -0
- package/dist/stores/i18n.d.ts +50 -0
- package/dist/stores/i18n.d.ts.map +1 -0
- package/dist/stores/i18n.js +67 -0
- package/dist/stores/i18n.js.map +1 -0
- package/dist/stores/index.d.ts +27 -0
- package/dist/stores/index.d.ts.map +1 -0
- package/dist/stores/index.js +27 -0
- package/dist/stores/index.js.map +1 -0
- package/dist/stores/logger.d.ts +83 -0
- package/dist/stores/logger.d.ts.map +1 -0
- package/dist/stores/logger.js +104 -0
- package/dist/stores/logger.js.map +1 -0
- package/dist/stores/login.d.ts +33 -0
- package/dist/stores/login.d.ts.map +1 -0
- package/dist/stores/login.js +32 -0
- package/dist/stores/login.js.map +1 -0
- package/dist/stores/oauth.d.ts +74 -0
- package/dist/stores/oauth.d.ts.map +1 -0
- package/dist/stores/oauth.js +183 -0
- package/dist/stores/oauth.js.map +1 -0
- package/dist/stores/password-reset.d.ts +31 -0
- package/dist/stores/password-reset.d.ts.map +1 -0
- package/dist/stores/password-reset.js +26 -0
- package/dist/stores/password-reset.js.map +1 -0
- package/dist/stores/platform.d.ts +41 -0
- package/dist/stores/platform.d.ts.map +1 -0
- package/dist/stores/platform.js +44 -0
- package/dist/stores/platform.js.map +1 -0
- package/dist/stores/promise.d.ts +41 -0
- package/dist/stores/promise.d.ts.map +1 -0
- package/dist/stores/promise.js +76 -0
- package/dist/stores/promise.js.map +1 -0
- package/dist/stores/push.d.ts +41 -0
- package/dist/stores/push.d.ts.map +1 -0
- package/dist/stores/push.js +73 -0
- package/dist/stores/push.js.map +1 -0
- package/dist/stores/router.d.ts +74 -0
- package/dist/stores/router.d.ts.map +1 -0
- package/dist/stores/router.js +131 -0
- package/dist/stores/router.js.map +1 -0
- package/dist/stores/signup.d.ts +18 -0
- package/dist/stores/signup.d.ts.map +1 -0
- package/dist/stores/signup.js +17 -0
- package/dist/stores/signup.js.map +1 -0
- package/dist/stores/state.d.ts +62 -0
- package/dist/stores/state.d.ts.map +1 -0
- package/dist/stores/state.js +74 -0
- package/dist/stores/state.js.map +1 -0
- package/dist/stores/storage.d.ts +76 -0
- package/dist/stores/storage.d.ts.map +1 -0
- package/dist/stores/storage.js +159 -0
- package/dist/stores/storage.js.map +1 -0
- package/dist/stores/theme.d.ts +68 -0
- package/dist/stores/theme.d.ts.map +1 -0
- package/dist/stores/theme.js +119 -0
- package/dist/stores/theme.js.map +1 -0
- package/dist/stores/version.d.ts +44 -0
- package/dist/stores/version.d.ts.map +1 -0
- package/dist/stores/version.js +73 -0
- package/dist/stores/version.js.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +59 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte stores for form management.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { derived, readable } from 'svelte/store';
|
|
7
|
+
/**
|
|
8
|
+
* Create form stores from a form provider and options.
|
|
9
|
+
*
|
|
10
|
+
* @param provider - Form provider instance
|
|
11
|
+
* @param options - Form creation options
|
|
12
|
+
* @returns Form stores and actions
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```svelte
|
|
16
|
+
* <script>
|
|
17
|
+
* import { createFormStores } from '@molecule/app-svelte'
|
|
18
|
+
* import { nativeProvider } from '@molecule/app-forms'
|
|
19
|
+
*
|
|
20
|
+
* const {
|
|
21
|
+
* formState, isValid, isDirty, isSubmitting, errors,
|
|
22
|
+
* register, setValue, handleSubmit, reset
|
|
23
|
+
* } = createFormStores(nativeProvider, {
|
|
24
|
+
* defaultValues: { name: '', email: '' },
|
|
25
|
+
* mode: 'onChange',
|
|
26
|
+
* })
|
|
27
|
+
*
|
|
28
|
+
* const nameField = register('name', { required: true })
|
|
29
|
+
* const emailField = register('email', { required: true, email: true })
|
|
30
|
+
*
|
|
31
|
+
* const onSubmit = handleSubmit(async (values) => {
|
|
32
|
+
* console.log('Submitted:', values)
|
|
33
|
+
* })
|
|
34
|
+
* </script>
|
|
35
|
+
*
|
|
36
|
+
* <form on:submit|preventDefault={onSubmit}>
|
|
37
|
+
* <input {...nameField} />
|
|
38
|
+
* {#if $errors.name}<span>{$errors.name}</span>{/if}
|
|
39
|
+
*
|
|
40
|
+
* <input {...emailField} />
|
|
41
|
+
* {#if $errors.email}<span>{$errors.email}</span>{/if}
|
|
42
|
+
*
|
|
43
|
+
* <button type="submit" disabled={!$isValid || $isSubmitting}>Submit</button>
|
|
44
|
+
* </form>
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function createFormStores(provider, options) {
|
|
48
|
+
const controller = provider.createForm(options);
|
|
49
|
+
// Main form state store
|
|
50
|
+
const formState = readable(controller.getState(), (set) => {
|
|
51
|
+
return controller.subscribe((state) => {
|
|
52
|
+
set(state);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
// Derived stores
|
|
56
|
+
const isValid = derived(formState, ($state) => $state.isValid);
|
|
57
|
+
const isDirty = derived(formState, ($state) => $state.isDirty);
|
|
58
|
+
const isSubmitting = derived(formState, ($state) => $state.isSubmitting);
|
|
59
|
+
const errors = derived(formState, ($state) => $state.errors);
|
|
60
|
+
// Actions (plain functions, not stores)
|
|
61
|
+
const register = (name, registerOptions) => {
|
|
62
|
+
if (typeof name === 'string') {
|
|
63
|
+
return controller.register({ ...registerOptions, name });
|
|
64
|
+
}
|
|
65
|
+
return controller.register(name);
|
|
66
|
+
};
|
|
67
|
+
const getValue = (name) => {
|
|
68
|
+
return controller.getValue(name);
|
|
69
|
+
};
|
|
70
|
+
const setValue = (name, value, opts) => {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
controller.setValue(name, value, opts);
|
|
73
|
+
};
|
|
74
|
+
const getError = (name) => {
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
76
|
+
return controller.getError(name);
|
|
77
|
+
};
|
|
78
|
+
const setError = (name, error) => {
|
|
79
|
+
if (error !== undefined) {
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
+
controller.setError(name, error);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
controller.clearError(name);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const clearErrors = () => {
|
|
88
|
+
controller.clearErrors();
|
|
89
|
+
};
|
|
90
|
+
const handleSubmit = (onSubmit, onError) => {
|
|
91
|
+
return controller.handleSubmit(onSubmit, onError);
|
|
92
|
+
};
|
|
93
|
+
const reset = (values) => {
|
|
94
|
+
controller.reset(values);
|
|
95
|
+
};
|
|
96
|
+
const validate = () => {
|
|
97
|
+
return controller.validate();
|
|
98
|
+
};
|
|
99
|
+
const destroy = () => {
|
|
100
|
+
controller.destroy();
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
formState,
|
|
104
|
+
isValid,
|
|
105
|
+
isDirty,
|
|
106
|
+
isSubmitting,
|
|
107
|
+
errors,
|
|
108
|
+
register,
|
|
109
|
+
getValue,
|
|
110
|
+
setValue,
|
|
111
|
+
getError,
|
|
112
|
+
setError,
|
|
113
|
+
clearErrors,
|
|
114
|
+
handleSubmit,
|
|
115
|
+
reset,
|
|
116
|
+
validate,
|
|
117
|
+
destroy,
|
|
118
|
+
/** The underlying controller, for advanced use or passing to createFieldStore/createWatchStore. */
|
|
119
|
+
controller,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Create a readable store that tracks a single field's state.
|
|
124
|
+
*
|
|
125
|
+
* @param controller - Form controller
|
|
126
|
+
* @param name - Field name to track
|
|
127
|
+
* @returns Readable store of the field state
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```svelte
|
|
131
|
+
* <script>
|
|
132
|
+
* import { createFormStores, createFieldStore } from '@molecule/app-svelte'
|
|
133
|
+
*
|
|
134
|
+
* const { controller } = createFormStores(provider, { defaultValues: { email: '' } })
|
|
135
|
+
* const emailField = createFieldStore(controller, 'email')
|
|
136
|
+
* </script>
|
|
137
|
+
*
|
|
138
|
+
* <p>Value: {$emailField.value}</p>
|
|
139
|
+
* <p>Error: {$emailField.error}</p>
|
|
140
|
+
* <p>Touched: {$emailField.touched}</p>
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export function createFieldStore(controller, name) {
|
|
144
|
+
return readable(controller.getFieldState(name), (set) => {
|
|
145
|
+
return controller.subscribe(() => {
|
|
146
|
+
set(controller.getFieldState(name));
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Create a readable store that tracks a single field's value.
|
|
152
|
+
*
|
|
153
|
+
* @param controller - Form controller
|
|
154
|
+
* @param name - Field name to watch
|
|
155
|
+
* @returns Readable store of the field value
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```svelte
|
|
159
|
+
* <script>
|
|
160
|
+
* import { createFormStores, createWatchStore } from '@molecule/app-svelte'
|
|
161
|
+
*
|
|
162
|
+
* const { controller } = createFormStores(provider, { defaultValues: { plan: 'free' } })
|
|
163
|
+
* const plan = createWatchStore(controller, 'plan')
|
|
164
|
+
* </script>
|
|
165
|
+
*
|
|
166
|
+
* {#if $plan === 'pro'}
|
|
167
|
+
* <ProFeatures />
|
|
168
|
+
* {/if}
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export function createWatchStore(controller, name) {
|
|
172
|
+
return readable(controller.getValue(name), (set) => {
|
|
173
|
+
return controller.subscribe(() => {
|
|
174
|
+
set(controller.getValue(name));
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=forms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"forms.js","sourceRoot":"","sources":["../../src/stores/forms.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAA;AAyC/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,UAAU,gBAAgB,CAC9B,QAAsB,EACtB,OAAuB;IAEvB,MAAM,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAI,OAAO,CAAC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AAO;QACP,mGAAmG;QACnG,UAAU;KACX,CAAA;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAA6B,EAC7B,IAAO;IAEP,OAAO,QAAQ,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,GAAsC,EAAE,EAAE;QACzF,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE;YAC/B,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAA;QACrC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,gBAAgB,CAC9B,UAA6B,EAC7B,IAAO;IAEP,OAAO,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,GAA0B,EAAE,EAAE;QACxE,OAAO,UAAU,CAAC,SAAS,CAAC,GAAG,EAAE;YAC/B,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAA;QAChC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte stores for HTTP requests.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Subscriber, type Unsubscriber } from 'svelte/store';
|
|
7
|
+
import type { HttpClient, RequestConfig } from '@molecule/app-http';
|
|
8
|
+
import type { HttpState } from '../types.js';
|
|
9
|
+
/**
|
|
10
|
+
* An HTTP store with subscribe, execute, and reset.
|
|
11
|
+
*/
|
|
12
|
+
interface HttpStore<T> {
|
|
13
|
+
subscribe: (run: Subscriber<HttpState<T>>, invalidate?: () => void) => Unsubscriber;
|
|
14
|
+
execute: (overrideConfig?: RequestConfig) => Promise<T | null>;
|
|
15
|
+
reset: () => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* An HTTP store created from a specific client (execute takes no params).
|
|
19
|
+
*/
|
|
20
|
+
interface HttpStoreWithClient<T> {
|
|
21
|
+
subscribe: (run: Subscriber<HttpState<T>>, invalidate?: () => void) => Unsubscriber;
|
|
22
|
+
execute: () => Promise<T | null>;
|
|
23
|
+
reset: () => void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Factory methods for creating HTTP stores from a client.
|
|
27
|
+
*/
|
|
28
|
+
interface HttpStoreFactory {
|
|
29
|
+
get: <T>(url: string, config?: RequestConfig) => HttpStoreWithClient<T>;
|
|
30
|
+
post: <T>(url: string, config?: RequestConfig) => HttpStoreWithClient<T>;
|
|
31
|
+
put: <T>(url: string, config?: RequestConfig) => HttpStoreWithClient<T>;
|
|
32
|
+
patch: <T>(url: string, config?: RequestConfig) => HttpStoreWithClient<T>;
|
|
33
|
+
delete: <T>(url: string, config?: RequestConfig) => HttpStoreWithClient<T>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create an HTTP request store.
|
|
37
|
+
*
|
|
38
|
+
* @param method - HTTP method
|
|
39
|
+
* @param url - Request URL
|
|
40
|
+
* @param config - Request configuration
|
|
41
|
+
* @returns Store with state and actions
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```svelte
|
|
45
|
+
* <script>
|
|
46
|
+
* import { createHttpStore } from '@molecule/app-svelte'
|
|
47
|
+
*
|
|
48
|
+
* const users = createHttpStore('GET', '/api/users')
|
|
49
|
+
*
|
|
50
|
+
* $: if ($users.loading) console.log('Loading...')
|
|
51
|
+
* $: if ($users.error) console.error($users.error)
|
|
52
|
+
* $: if ($users.data) console.log('Users:', $users.data)
|
|
53
|
+
*
|
|
54
|
+
* users.execute()
|
|
55
|
+
* </script>
|
|
56
|
+
*
|
|
57
|
+
* {#if $users.loading}
|
|
58
|
+
* <p>Loading...</p>
|
|
59
|
+
* {:else if $users.error}
|
|
60
|
+
* <p>Error: {$users.error.message}</p>
|
|
61
|
+
* {:else if $users.data}
|
|
62
|
+
* <ul>
|
|
63
|
+
* {#each $users.data as user}
|
|
64
|
+
* <li>{user.name}</li>
|
|
65
|
+
* {/each}
|
|
66
|
+
* </ul>
|
|
67
|
+
* {/if}
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function createHttpStore<T>(method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE', url: string, config?: RequestConfig): HttpStore<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Creates a GET request store.
|
|
73
|
+
*
|
|
74
|
+
* @param url - The request endpoint URL.
|
|
75
|
+
* @param config - Optional request configuration such as headers or params.
|
|
76
|
+
* @returns A subscribable HTTP store for the GET request with execute and reset methods.
|
|
77
|
+
*/
|
|
78
|
+
export declare function createGetStore<T>(url: string, config?: RequestConfig): HttpStore<T>;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a POST request store.
|
|
81
|
+
*
|
|
82
|
+
* @param url - The request endpoint URL.
|
|
83
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
84
|
+
* @returns A subscribable HTTP store for the POST request with execute and reset methods.
|
|
85
|
+
*/
|
|
86
|
+
export declare function createPostStore<T>(url: string, config?: RequestConfig): HttpStore<T>;
|
|
87
|
+
/**
|
|
88
|
+
* Creates a PUT request store.
|
|
89
|
+
*
|
|
90
|
+
* @param url - The request endpoint URL.
|
|
91
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
92
|
+
* @returns A subscribable HTTP store for the PUT request with execute and reset methods.
|
|
93
|
+
*/
|
|
94
|
+
export declare function createPutStore<T>(url: string, config?: RequestConfig): HttpStore<T>;
|
|
95
|
+
/**
|
|
96
|
+
* Creates a PATCH request store.
|
|
97
|
+
*
|
|
98
|
+
* @param url - The request endpoint URL.
|
|
99
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
100
|
+
* @returns A subscribable HTTP store for the PATCH request with execute and reset methods.
|
|
101
|
+
*/
|
|
102
|
+
export declare function createPatchStore<T>(url: string, config?: RequestConfig): HttpStore<T>;
|
|
103
|
+
/**
|
|
104
|
+
* Creates a DELETE request store.
|
|
105
|
+
*
|
|
106
|
+
* @param url - The request endpoint URL.
|
|
107
|
+
* @param config - Optional request configuration such as headers or params.
|
|
108
|
+
* @returns A subscribable HTTP store for the DELETE request with execute and reset methods.
|
|
109
|
+
*/
|
|
110
|
+
export declare function createDeleteStore<T>(url: string, config?: RequestConfig): HttpStore<T>;
|
|
111
|
+
/**
|
|
112
|
+
* Creates HTTP store factory methods bound to a specific client instance.
|
|
113
|
+
*
|
|
114
|
+
* @param client - The HTTP client to use for all created stores.
|
|
115
|
+
* @returns An object with get, post, put, patch, and delete factory methods.
|
|
116
|
+
*/
|
|
117
|
+
export declare function createHttpStoresFromClient(client: HttpClient): HttpStoreFactory;
|
|
118
|
+
export {};
|
|
119
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/stores/http.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,YAAY,EAAY,MAAM,cAAc,CAAA;AAE3E,OAAO,KAAK,EAAE,UAAU,EAAgB,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAGjF,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE5C;;GAEG;AACH,UAAU,SAAS,CAAC,CAAC;IACnB,SAAS,EAAE,CAAC,GAAG,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,MAAM,IAAI,KAAK,YAAY,CAAA;IACnF,OAAO,EAAE,CAAC,cAAc,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IAC9D,KAAK,EAAE,MAAM,IAAI,CAAA;CAClB;AAED;;GAEG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AG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAEnF;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAEpF;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAEnF;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAErF;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAEtF;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,UAAU,GAAG,gBAAgB,CAa/E"}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte stores for HTTP requests.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { writable } from 'svelte/store';
|
|
7
|
+
import { getHttpClient } from '../context.js';
|
|
8
|
+
/**
|
|
9
|
+
* Create an HTTP request store.
|
|
10
|
+
*
|
|
11
|
+
* @param method - HTTP method
|
|
12
|
+
* @param url - Request URL
|
|
13
|
+
* @param config - Request configuration
|
|
14
|
+
* @returns Store with state and actions
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```svelte
|
|
18
|
+
* <script>
|
|
19
|
+
* import { createHttpStore } from '@molecule/app-svelte'
|
|
20
|
+
*
|
|
21
|
+
* const users = createHttpStore('GET', '/api/users')
|
|
22
|
+
*
|
|
23
|
+
* $: if ($users.loading) console.log('Loading...')
|
|
24
|
+
* $: if ($users.error) console.error($users.error)
|
|
25
|
+
* $: if ($users.data) console.log('Users:', $users.data)
|
|
26
|
+
*
|
|
27
|
+
* users.execute()
|
|
28
|
+
* </script>
|
|
29
|
+
*
|
|
30
|
+
* {#if $users.loading}
|
|
31
|
+
* <p>Loading...</p>
|
|
32
|
+
* {:else if $users.error}
|
|
33
|
+
* <p>Error: {$users.error.message}</p>
|
|
34
|
+
* {:else if $users.data}
|
|
35
|
+
* <ul>
|
|
36
|
+
* {#each $users.data as user}
|
|
37
|
+
* <li>{user.name}</li>
|
|
38
|
+
* {/each}
|
|
39
|
+
* </ul>
|
|
40
|
+
* {/if}
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export function createHttpStore(method, url, config) {
|
|
44
|
+
const client = getHttpClient();
|
|
45
|
+
const store = writable({
|
|
46
|
+
data: null,
|
|
47
|
+
loading: false,
|
|
48
|
+
error: null,
|
|
49
|
+
});
|
|
50
|
+
const execute = async (overrideConfig) => {
|
|
51
|
+
store.update((s) => ({ ...s, loading: true, error: null }));
|
|
52
|
+
try {
|
|
53
|
+
let response;
|
|
54
|
+
const requestConfig = { ...config, ...overrideConfig };
|
|
55
|
+
switch (method) {
|
|
56
|
+
case 'GET':
|
|
57
|
+
response = await client.get(url, requestConfig);
|
|
58
|
+
break;
|
|
59
|
+
case 'POST':
|
|
60
|
+
response = await client.post(url, requestConfig?.data, requestConfig);
|
|
61
|
+
break;
|
|
62
|
+
case 'PUT':
|
|
63
|
+
response = await client.put(url, requestConfig?.data, requestConfig);
|
|
64
|
+
break;
|
|
65
|
+
case 'PATCH':
|
|
66
|
+
response = await client.patch(url, requestConfig?.data, requestConfig);
|
|
67
|
+
break;
|
|
68
|
+
case 'DELETE':
|
|
69
|
+
response = await client.delete(url, requestConfig);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
store.set({ data: response.data, loading: false, error: null });
|
|
73
|
+
return response.data;
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
77
|
+
store.set({ data: null, loading: false, error: err });
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const reset = () => {
|
|
82
|
+
store.set({ data: null, loading: false, error: null });
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
subscribe: store.subscribe,
|
|
86
|
+
execute,
|
|
87
|
+
reset,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Creates a GET request store.
|
|
92
|
+
*
|
|
93
|
+
* @param url - The request endpoint URL.
|
|
94
|
+
* @param config - Optional request configuration such as headers or params.
|
|
95
|
+
* @returns A subscribable HTTP store for the GET request with execute and reset methods.
|
|
96
|
+
*/
|
|
97
|
+
export function createGetStore(url, config) {
|
|
98
|
+
return createHttpStore('GET', url, config);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Creates a POST request store.
|
|
102
|
+
*
|
|
103
|
+
* @param url - The request endpoint URL.
|
|
104
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
105
|
+
* @returns A subscribable HTTP store for the POST request with execute and reset methods.
|
|
106
|
+
*/
|
|
107
|
+
export function createPostStore(url, config) {
|
|
108
|
+
return createHttpStore('POST', url, config);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates a PUT request store.
|
|
112
|
+
*
|
|
113
|
+
* @param url - The request endpoint URL.
|
|
114
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
115
|
+
* @returns A subscribable HTTP store for the PUT request with execute and reset methods.
|
|
116
|
+
*/
|
|
117
|
+
export function createPutStore(url, config) {
|
|
118
|
+
return createHttpStore('PUT', url, config);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Creates a PATCH request store.
|
|
122
|
+
*
|
|
123
|
+
* @param url - The request endpoint URL.
|
|
124
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
125
|
+
* @returns A subscribable HTTP store for the PATCH request with execute and reset methods.
|
|
126
|
+
*/
|
|
127
|
+
export function createPatchStore(url, config) {
|
|
128
|
+
return createHttpStore('PATCH', url, config);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates a DELETE request store.
|
|
132
|
+
*
|
|
133
|
+
* @param url - The request endpoint URL.
|
|
134
|
+
* @param config - Optional request configuration such as headers or params.
|
|
135
|
+
* @returns A subscribable HTTP store for the DELETE request with execute and reset methods.
|
|
136
|
+
*/
|
|
137
|
+
export function createDeleteStore(url, config) {
|
|
138
|
+
return createHttpStore('DELETE', url, config);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Creates HTTP store factory methods bound to a specific client instance.
|
|
142
|
+
*
|
|
143
|
+
* @param client - The HTTP client to use for all created stores.
|
|
144
|
+
* @returns An object with get, post, put, patch, and delete factory methods.
|
|
145
|
+
*/
|
|
146
|
+
export function createHttpStoresFromClient(client) {
|
|
147
|
+
return {
|
|
148
|
+
get: (url, config) => createHttpStoreWithClient(client, 'GET', url, config),
|
|
149
|
+
post: (url, config) => createHttpStoreWithClient(client, 'POST', url, config),
|
|
150
|
+
put: (url, config) => createHttpStoreWithClient(client, 'PUT', url, config),
|
|
151
|
+
patch: (url, config) => createHttpStoreWithClient(client, 'PATCH', url, config),
|
|
152
|
+
delete: (url, config) => createHttpStoreWithClient(client, 'DELETE', url, config),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Creates an HTTP store bound to a specific client, method, and URL.
|
|
157
|
+
*
|
|
158
|
+
* @param client - The HTTP client to execute requests with.
|
|
159
|
+
* @param method - The HTTP method (GET, POST, PUT, PATCH, or DELETE).
|
|
160
|
+
* @param url - The request endpoint URL.
|
|
161
|
+
* @param config - Optional request configuration such as headers or body data.
|
|
162
|
+
* @returns A subscribable HTTP store with execute and reset methods.
|
|
163
|
+
*/
|
|
164
|
+
function createHttpStoreWithClient(client, method, url, config) {
|
|
165
|
+
const store = writable({
|
|
166
|
+
data: null,
|
|
167
|
+
loading: false,
|
|
168
|
+
error: null,
|
|
169
|
+
});
|
|
170
|
+
const execute = async () => {
|
|
171
|
+
store.update((s) => ({ ...s, loading: true, error: null }));
|
|
172
|
+
try {
|
|
173
|
+
let response;
|
|
174
|
+
switch (method) {
|
|
175
|
+
case 'GET':
|
|
176
|
+
response = await client.get(url, config);
|
|
177
|
+
break;
|
|
178
|
+
case 'POST':
|
|
179
|
+
response = await client.post(url, config?.data, config);
|
|
180
|
+
break;
|
|
181
|
+
case 'PUT':
|
|
182
|
+
response = await client.put(url, config?.data, config);
|
|
183
|
+
break;
|
|
184
|
+
case 'PATCH':
|
|
185
|
+
response = await client.patch(url, config?.data, config);
|
|
186
|
+
break;
|
|
187
|
+
case 'DELETE':
|
|
188
|
+
response = await client.delete(url, config);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
store.set({ data: response.data, loading: false, error: null });
|
|
192
|
+
return response.data;
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
196
|
+
store.set({ data: null, loading: false, error: err });
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
return {
|
|
201
|
+
subscribe: store.subscribe,
|
|
202
|
+
execute,
|
|
203
|
+
reset: () => store.set({ data: null, loading: false, error: null }),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/stores/http.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAsC,QAAQ,EAAE,MAAM,cAAc,CAAA;AAI3E,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAA;AAgC7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;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KAAK,CAAC,CAAC,CAAA;YACrE,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAA;YACrD,OAAO,IAAI,CAAA;QACb,CAAC;IACH,CAAC,CAAA;IAED,OAAO;QACL,SAAS,EAAE,KAAK,CAAC,SAAS;QAC1B,OAAO;QACP,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;KACpE,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte stores for internationalization.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type Readable } from 'svelte/store';
|
|
7
|
+
import type { DateFormatOptions, I18nProvider, InterpolationValues, LocaleConfig, NumberFormatOptions } from '@molecule/app-i18n';
|
|
8
|
+
/**
|
|
9
|
+
* I18n stores and actions.
|
|
10
|
+
*/
|
|
11
|
+
interface I18nStores {
|
|
12
|
+
locale: Readable<string>;
|
|
13
|
+
direction: Readable<'ltr' | 'rtl'>;
|
|
14
|
+
locales: Readable<LocaleConfig[]>;
|
|
15
|
+
t: Readable<(key: string, values?: InterpolationValues) => string>;
|
|
16
|
+
setLocale: (code: string) => Promise<void>;
|
|
17
|
+
formatNumber: (value: number, options?: NumberFormatOptions) => string;
|
|
18
|
+
formatDate: (value: Date | number | string, options?: DateFormatOptions) => string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create i18n stores from the i18n provider in context.
|
|
22
|
+
*
|
|
23
|
+
* @returns I18n stores and actions
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```svelte
|
|
27
|
+
* <script>
|
|
28
|
+
* import { createI18nStores } from '@molecule/app-svelte'
|
|
29
|
+
*
|
|
30
|
+
* const { locale, direction, t, setLocale, formatNumber } = createI18nStores()
|
|
31
|
+
* </script>
|
|
32
|
+
*
|
|
33
|
+
* <h1>{t('welcome.title', { name: 'User' })}</h1>
|
|
34
|
+
* <p>Price: {formatNumber(99.99, { style: 'currency', currency: 'USD' })}</p>
|
|
35
|
+
* <select bind:value={$locale} on:change={(e) => setLocale(e.target.value)}>
|
|
36
|
+
* <option value="en">English</option>
|
|
37
|
+
* <option value="es">Spanish</option>
|
|
38
|
+
* </select>
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function createI18nStores(): I18nStores;
|
|
42
|
+
/**
|
|
43
|
+
* Create i18n stores from a specific provider.
|
|
44
|
+
*
|
|
45
|
+
* @param provider - I18n provider
|
|
46
|
+
* @returns I18n stores and actions
|
|
47
|
+
*/
|
|
48
|
+
export declare function createI18nStoresFromProvider(provider: I18nProvider): I18nStores;
|
|
49
|
+
export {};
|
|
50
|
+
//# sourceMappingURL=i18n.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../../src/stores/i18n.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAW,KAAK,QAAQ,EAAY,MAAM,cAAc,CAAA;AAE/D,OAAO,KAAK,EACV,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,YAAY,EACZ,mBAAmB,EACpB,MAAM,oBAAoB,CAAA;AAI3B;;GAEG;AACH,UAAU,UAAU;IAClB,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;IACxB,SAAS,EAAE,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAC,CAAA;IAClC,OAAO,EAAE,QAAQ,CAAC,YAAY,EAAE,CAAC,CAAA;IACjC,CAAC,EAAE,QAAQ,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAC,CAAA;IAClE,SAAS,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAC1C,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAA;IACtE,UAAU,EAAE,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,KAAK,MAAM,CAAA;CACnF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,gBAAgB,IAAI,UAAU,CAuB7C;AAED;;;;;GAKG;AACH,wBAAgB,4BAA4B,CAAC,QAAQ,EAAE,YAAY,GAAG,UAAU,CAoB/E"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte stores for internationalization.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { derived, readable } from 'svelte/store';
|
|
7
|
+
import { getI18nProvider } from '../context.js';
|
|
8
|
+
/**
|
|
9
|
+
* Create i18n stores from the i18n provider in context.
|
|
10
|
+
*
|
|
11
|
+
* @returns I18n stores and actions
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```svelte
|
|
15
|
+
* <script>
|
|
16
|
+
* import { createI18nStores } from '@molecule/app-svelte'
|
|
17
|
+
*
|
|
18
|
+
* const { locale, direction, t, setLocale, formatNumber } = createI18nStores()
|
|
19
|
+
* </script>
|
|
20
|
+
*
|
|
21
|
+
* <h1>{t('welcome.title', { name: 'User' })}</h1>
|
|
22
|
+
* <p>Price: {formatNumber(99.99, { style: 'currency', currency: 'USD' })}</p>
|
|
23
|
+
* <select bind:value={$locale} on:change={(e) => setLocale(e.target.value)}>
|
|
24
|
+
* <option value="en">English</option>
|
|
25
|
+
* <option value="es">Spanish</option>
|
|
26
|
+
* </select>
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function createI18nStores() {
|
|
30
|
+
const provider = getI18nProvider();
|
|
31
|
+
// Internal version counter — bumps on every locale or translation change
|
|
32
|
+
const _version = readable(0, (set) => {
|
|
33
|
+
let v = 0;
|
|
34
|
+
return provider.onLocaleChange(() => set(++v));
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
locale: derived(_version, () => provider.getLocale()),
|
|
38
|
+
direction: derived(_version, () => provider.getDirection()),
|
|
39
|
+
locales: derived(_version, () => provider.getLocales()),
|
|
40
|
+
t: derived(_version, () => (key, values) => provider.t(key, values)),
|
|
41
|
+
setLocale: (code) => provider.setLocale(code),
|
|
42
|
+
formatNumber: (value, options) => provider.formatNumber(value, options),
|
|
43
|
+
formatDate: (value, options) => provider.formatDate(value, options),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create i18n stores from a specific provider.
|
|
48
|
+
*
|
|
49
|
+
* @param provider - I18n provider
|
|
50
|
+
* @returns I18n stores and actions
|
|
51
|
+
*/
|
|
52
|
+
export function createI18nStoresFromProvider(provider) {
|
|
53
|
+
const _version = readable(0, (set) => {
|
|
54
|
+
let v = 0;
|
|
55
|
+
return provider.onLocaleChange(() => set(++v));
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
locale: derived(_version, () => provider.getLocale()),
|
|
59
|
+
direction: derived(_version, () => provider.getDirection()),
|
|
60
|
+
locales: derived(_version, () => provider.getLocales()),
|
|
61
|
+
t: derived(_version, () => (key, values) => provider.t(key, values)),
|
|
62
|
+
setLocale: (code) => provider.setLocale(code),
|
|
63
|
+
formatNumber: (value, options) => provider.formatNumber(value, options),
|
|
64
|
+
formatDate: (value, options) => provider.formatDate(value, options),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=i18n.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.js","sourceRoot":"","sources":["../../src/stores/i18n.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH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