@axium/client 0.9.12 → 0.10.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/dist/config.d.ts +1 -1
- package/lib/Preference.svelte +29 -25
- package/package.json +2 -2
package/dist/config.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare const ClientConfig: z.ZodObject<{
|
|
|
17
17
|
emailVerified: z.ZodOptional<z.ZodNullable<z.ZodDate>>;
|
|
18
18
|
image: z.ZodOptional<z.ZodNullable<z.ZodURL>>;
|
|
19
19
|
preferences: z.ZodObject<{
|
|
20
|
-
debug: z.ZodBoolean
|
|
20
|
+
debug: z.ZodDefault<z.ZodBoolean>;
|
|
21
21
|
}, z.core.$strip>;
|
|
22
22
|
roles: z.ZodArray<z.ZodString>;
|
|
23
23
|
registeredAt: z.ZodCoercedDate<unknown>;
|
package/lib/Preference.svelte
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { fetchAPI } from '@axium/client/requests';
|
|
3
|
-
import {
|
|
3
|
+
import type { Preferences, ZodPref } from '@axium/core';
|
|
4
4
|
import type { HTMLInputAttributes } from 'svelte/elements';
|
|
5
5
|
import { getByString, pick, setByString } from 'utilium';
|
|
6
6
|
import Icon from './Icon.svelte';
|
|
@@ -11,15 +11,16 @@
|
|
|
11
11
|
preferences: Preferences;
|
|
12
12
|
path: string;
|
|
13
13
|
schema: ZodPref;
|
|
14
|
+
defaultValue?: any;
|
|
14
15
|
optional?: boolean;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
let { preferences = $bindable(), userId, path, schema, optional = false }: Props = $props();
|
|
18
|
+
let { preferences = $bindable(), userId, path, schema, optional = false, defaultValue }: Props = $props();
|
|
18
19
|
const id = $props.id();
|
|
19
20
|
|
|
20
21
|
let input = $state<HTMLInputElement | HTMLSelectElement>()!;
|
|
21
22
|
let checked = $state(schema.def.type == 'boolean' && getByString<boolean>(preferences, path));
|
|
22
|
-
const initialValue = $derived<any>(getByString(preferences, path)
|
|
23
|
+
const initialValue = $derived<any>(getByString(preferences, path));
|
|
23
24
|
|
|
24
25
|
function dateAttr(date: Date | null, format: 'date' | 'time' | 'datetime' | 'time+sec') {
|
|
25
26
|
if (!date) return null;
|
|
@@ -50,9 +51,10 @@
|
|
|
50
51
|
|
|
51
52
|
function onchange(e: Event) {
|
|
52
53
|
const value = schema.parse(input instanceof HTMLInputElement && input.type === 'checkbox' ? input.checked : input.value);
|
|
53
|
-
|
|
54
|
+
const oldValue = getByString(preferences, path);
|
|
55
|
+
if (value == oldValue) return;
|
|
54
56
|
|
|
55
|
-
if (
|
|
57
|
+
if (defaultValue == value) {
|
|
56
58
|
const parts = path.split('.');
|
|
57
59
|
const prop = parts.pop()!;
|
|
58
60
|
delete getByString<Record<string, any>>(preferences, parts.join('.'))[prop];
|
|
@@ -63,70 +65,72 @@
|
|
|
63
65
|
</script>
|
|
64
66
|
|
|
65
67
|
{#snippet _in(rest: HTMLInputAttributes)}
|
|
66
|
-
<input bind:this={input} {id} {...rest} value={initialValue} {onchange} required={!optional} />
|
|
68
|
+
<input bind:this={input} {id} {...rest} value={initialValue} {onchange} required={!optional} {defaultValue} />
|
|
67
69
|
{/snippet}
|
|
68
70
|
|
|
69
|
-
{#if
|
|
71
|
+
{#if schema.type == 'string'}
|
|
70
72
|
{@render _in({ type: schema.format == 'email' ? 'email' : 'text', ...pick(schema, 'minLength', 'maxLength') })}
|
|
71
|
-
{:else if
|
|
73
|
+
{:else if schema.type == 'number'}
|
|
72
74
|
{@render _in({ type: 'number', min: schema.minValue, max: schema.maxValue, step: schema.format?.includes('int') ? 1 : 0.1 })}
|
|
73
|
-
{:else if
|
|
75
|
+
{:else if schema.type == 'bigint'}
|
|
74
76
|
{@render _in({ type: 'number', min: Number(schema.minValue), max: Number(schema.maxValue), step: 1 })}
|
|
75
|
-
{:else if
|
|
77
|
+
{:else if schema.type == 'boolean'}
|
|
76
78
|
<input bind:checked bind:this={input} {id} type="checkbox" {onchange} required={!optional} />
|
|
77
79
|
<label for={id} class="checkbox">
|
|
78
80
|
{#if checked}<Icon i="check" --size="1.3em" />{/if}
|
|
79
81
|
</label>
|
|
80
|
-
{:else if
|
|
82
|
+
{:else if schema.type == 'date'}
|
|
81
83
|
{@render _in({
|
|
82
84
|
type: 'date',
|
|
83
85
|
min: dateAttr(schema.minDate, 'date'),
|
|
84
86
|
max: dateAttr(schema.maxDate, 'date'),
|
|
85
87
|
})}
|
|
86
|
-
{:else if
|
|
88
|
+
{:else if schema.type == 'file'}
|
|
87
89
|
<!-- todo -->
|
|
88
|
-
{:else if
|
|
90
|
+
{:else if schema.type == 'literal'}
|
|
89
91
|
<select bind:this={input} {id} {onchange} required={!optional}>
|
|
90
92
|
{#each schema.values as value}
|
|
91
93
|
<option {value} selected={initialValue === value}>{value}</option>
|
|
92
94
|
{/each}
|
|
93
95
|
</select>
|
|
94
|
-
{:else if
|
|
96
|
+
{:else if schema.type == 'template_literal'}
|
|
95
97
|
<!-- todo -->
|
|
96
|
-
{:else if
|
|
98
|
+
{:else if schema.type == 'default'}
|
|
99
|
+
<Preference {userId} bind:preferences {path} schema={schema.def.innerType} defaultValue={schema.def.defaultValue} />
|
|
100
|
+
{:else if schema.type == 'nullable' || schema.type == 'optional'}
|
|
97
101
|
<!-- defaults are handled differently -->
|
|
98
|
-
<Preference {userId} bind:preferences {path} schema={schema.def.innerType} optional={true} />
|
|
99
|
-
{:else if
|
|
102
|
+
<Preference {userId} bind:preferences {path} {defaultValue} schema={schema.def.innerType} optional={true} />
|
|
103
|
+
{:else if schema.type == 'array'}
|
|
100
104
|
<div class="pref-sub">
|
|
101
105
|
{#each initialValue, i}
|
|
102
106
|
<div class="pref-record-entry">
|
|
103
|
-
<Preference {userId} bind:preferences path="{path}.{i}" schema={schema.element} />
|
|
107
|
+
<Preference {userId} bind:preferences {defaultValue} path="{path}.{i}" schema={schema.element} />
|
|
104
108
|
</div>
|
|
105
109
|
{/each}
|
|
106
110
|
</div>
|
|
107
|
-
{:else if
|
|
111
|
+
{:else if schema.type == 'record'}
|
|
108
112
|
<div class="pref-sub">
|
|
109
113
|
{#each Object.keys(initialValue) as key}
|
|
110
114
|
<div class="pref-record-entry">
|
|
111
115
|
<label for={id}>{key}</label>
|
|
112
|
-
<Preference {userId} bind:preferences path="{path}.{key}" schema={schema.valueType} />
|
|
116
|
+
<Preference {userId} bind:preferences {defaultValue} path="{path}.{key}" schema={schema.valueType} />
|
|
113
117
|
</div>
|
|
114
118
|
{/each}
|
|
115
119
|
</div>
|
|
116
|
-
{:else if
|
|
120
|
+
{:else if schema.type == 'object'}
|
|
117
121
|
{#each Object.entries(schema.shape) as [key, value]}
|
|
118
122
|
<div class="pref-sub">
|
|
119
123
|
<label for={id}>{key}</label>
|
|
120
|
-
<Preference {userId} bind:preferences path="{path}.{key}" schema={value} />
|
|
124
|
+
<Preference {userId} bind:preferences {defaultValue} path="{path}.{key}" schema={value} />
|
|
121
125
|
</div>
|
|
122
126
|
{/each}
|
|
123
|
-
{:else if
|
|
127
|
+
{:else if schema.type == 'tuple'}
|
|
124
128
|
<div class="pref-sub" data-rest={schema.def.rest}>
|
|
125
129
|
{#each schema.def.items as item, i}
|
|
126
|
-
<Preference {userId} bind:preferences path="{path}.{i}" schema={item} />
|
|
130
|
+
<Preference {userId} bind:preferences {defaultValue} path="{path}.{i}" schema={item} />
|
|
127
131
|
{/each}
|
|
128
132
|
</div>
|
|
129
|
-
{:else if
|
|
133
|
+
{:else if schema.type == 'enum'}
|
|
130
134
|
<select bind:this={input} {id} {onchange} required={!optional}>
|
|
131
135
|
{#each Object.entries(schema.enum) as [key, value]}
|
|
132
136
|
<option {value} selected={initialValue === value}>{key}</option>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axium/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"author": "James Prevett <jp@jamespre.dev>",
|
|
5
5
|
"funding": {
|
|
6
6
|
"type": "individual",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"build": "tsc"
|
|
41
41
|
},
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"@axium/core": ">=0.
|
|
43
|
+
"@axium/core": ">=0.16.0",
|
|
44
44
|
"utilium": "^2.3.8",
|
|
45
45
|
"zod": "^4.0.5",
|
|
46
46
|
"svelte": "^5.36.0"
|