@axium/server 0.3.1 → 0.3.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axium/server",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "author": "James Prevett <axium@jamespre.dev> (https://jamespre.dev)",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -0,0 +1,36 @@
1
+ .account-section {
2
+ width: 50%;
3
+ padding-top: 4em;
4
+
5
+ > div:has(+ div) {
6
+ border-bottom: 1px solid #8888;
7
+ }
8
+ }
9
+
10
+ .account-section .account-item {
11
+ display: grid;
12
+ grid-template-columns: 10em 1fr 2em;
13
+ align-items: center;
14
+ width: 100%;
15
+ gap: 1em;
16
+ text-wrap: nowrap;
17
+ padding-bottom: 1em;
18
+
19
+ > :first-child {
20
+ margin: 0 5em 0 1em;
21
+ grid-column: 1;
22
+ }
23
+
24
+ > :nth-child(2) {
25
+ margin: 0;
26
+ grid-column: 2;
27
+ }
28
+
29
+ > :last-child:nth-child(3) {
30
+ margin: 0;
31
+ display: inline;
32
+ grid-column: 3;
33
+ font-size: 0.75em;
34
+ cursor: pointer;
35
+ }
36
+ }
@@ -65,3 +65,8 @@ button:hover {
65
65
  background-color: #733;
66
66
  color: #ccc;
67
67
  }
68
+
69
+ .subtle {
70
+ color: #bbbb;
71
+ font-size: 0.9em;
72
+ }
@@ -2,6 +2,7 @@
2
2
  import { getUserImage } from '@axium/core';
3
3
  import Icon from '../lib/Icon.svelte';
4
4
  import '../lib/styles.css';
5
+ import '../lib/account.css';
5
6
  import type { PageData } from './$types.js';
6
7
 
7
8
  const {
@@ -27,18 +28,18 @@
27
28
  <img id="pfp" src={image} alt="User profile" />
28
29
  <p id="greeting">Welcome, {user.name}</p>
29
30
  <div class="account-section main">
30
- <div>
31
- <p>Name</p>
31
+ <div class="account-item">
32
+ <p class="subtle">Name</p>
32
33
  <p>{user.name}</p>
33
- <a href="{prefix}/name"><Icon id="chevron-right" /></a>
34
+ <a class="change" href="{prefix}/name"><Icon id="chevron-right" /></a>
34
35
  </div>
35
- <div>
36
- <p>Email</p>
36
+ <div class="account-item">
37
+ <p class="subtle">Email</p>
37
38
  <p>{user.email}</p>
38
- <a href="{prefix}/email"><Icon id="chevron-right" /></a>
39
+ <a class="change" href="{prefix}/email"><Icon id="chevron-right" /></a>
39
40
  </div>
40
- <div>
41
- <p>User ID <dfn title="This is your UUID."><Icon id="regular/circle-info" /></dfn></p>
41
+ <div class="account-item">
42
+ <p class="subtle">User ID <dfn title="This is your UUID."><Icon id="regular/circle-info" /></dfn></p>
42
43
  <p>{user.id}</p>
43
44
  </div>
44
45
  <a id="signout" href="/auth/signout"><button>Sign out</button></a>
@@ -66,45 +67,6 @@
66
67
  font-size: 2em;
67
68
  }
68
69
 
69
- :global(.account-section) {
70
- width: 50%;
71
- padding-top: 4em;
72
-
73
- > div:has(+ div) {
74
- border-bottom: 1px solid #8888;
75
- }
76
- }
77
-
78
- :global(.account-section) > div {
79
- display: grid;
80
- grid-template-columns: 10em 1fr 2em;
81
- align-items: center;
82
- width: 100%;
83
- gap: 1em;
84
- text-wrap: nowrap;
85
- padding-bottom: 1em;
86
-
87
- > :first-child {
88
- margin: 0 5em 0 1em;
89
- color: #bbbb;
90
- font-size: 0.9em;
91
- grid-column: 1;
92
- }
93
-
94
- > :nth-child(2) {
95
- margin: 0;
96
- grid-column: 2;
97
- }
98
-
99
- > a:last-child {
100
- margin: 0;
101
- display: inline;
102
- grid-column: 3;
103
- font-size: 0.75em;
104
- cursor: pointer;
105
- }
106
- }
107
-
108
70
  #signout {
109
71
  margin-top: 2em;
110
72
  }
@@ -1,8 +1,9 @@
1
1
  import { fail, redirect, type Actions } from '@sveltejs/kit';
2
2
  import * as z from 'zod';
3
3
  import { adapter } from '../../../dist/auth.js';
4
- import type { PageServerLoadEvent } from './$types.js';
5
4
  import { web } from '../../../dist/config.js';
5
+ import { tryZod } from '../../utils.js';
6
+ import type { PageServerLoadEvent } from './$types.js';
6
7
 
7
8
  export async function load(event: PageServerLoadEvent) {
8
9
  const session = await event.locals.auth();
@@ -15,13 +16,8 @@ export const actions = {
15
16
  const session = await event.locals.auth();
16
17
 
17
18
  const rawEmail = (await event.request.formData()).get('email');
18
- const { data: email, success, error } = z.string().email().safeParse(rawEmail);
19
-
20
- if (!success)
21
- return fail(400, {
22
- email,
23
- error: error.flatten().formErrors[0] || Object.values(error.flatten().fieldErrors).flat()[0],
24
- });
19
+ const [email, error] = tryZod(z.string().email().safeParse(rawEmail));
20
+ if (error) return error;
25
21
 
26
22
  const user = await adapter.getUserByEmail(session.user.email);
27
23
  if (!user) return fail(500, { email, error: 'User does not exist' });
@@ -2,6 +2,7 @@ import { Name } from '@axium/core/schemas';
2
2
  import { fail, redirect, type Actions } from '@sveltejs/kit';
3
3
  import { adapter } from '../../../dist/auth.js';
4
4
  import { web } from '../../../dist/config.js';
5
+ import { tryZod } from '../../utils.js';
5
6
  import type { PageServerLoadEvent } from './$types.js';
6
7
 
7
8
  export async function load(event: PageServerLoadEvent) {
@@ -15,13 +16,8 @@ export const actions = {
15
16
  const session = await event.locals.auth();
16
17
 
17
18
  const rawName = (await event.request.formData()).get('name');
18
- const { data: name, success, error } = Name.safeParse(rawName);
19
-
20
- if (!success)
21
- return fail(400, {
22
- name,
23
- error: error.flatten().formErrors[0] || Object.values(error.flatten().fieldErrors).flat()[0],
24
- });
19
+ const [name, error] = tryZod(Name.safeParse(rawName));
20
+ if (error) error;
25
21
 
26
22
  const user = await adapter.getUserByEmail(session.user.email);
27
23
  if (!user) return fail(500, { name, error: 'User does not exist' });
package/web/utils.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { fail, type ActionFailure } from '@sveltejs/kit';
2
+ import type * as z from 'zod';
3
+
4
+ export function failZod(error: z.ZodError): ActionFailure<{ error: string }> {
5
+ return fail(400, {
6
+ error: error.flatten().formErrors[0] || Object.values(error.flatten().fieldErrors).flat()[0],
7
+ });
8
+ }
9
+
10
+ export function tryZod<Input, Output>(result: z.SafeParseReturnType<Input, Output>): [Output?, ActionFailure<{ error: string }>?] {
11
+ if (result.success) return [result.data, undefined];
12
+ return [undefined, failZod(result.error)];
13
+ }