@axium/server 0.3.2 → 0.3.4

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.2",
3
+ "version": "0.3.4",
4
4
  "author": "James Prevett <axium@jamespre.dev> (https://jamespre.dev)",
5
5
  "funding": {
6
6
  "type": "individual",
@@ -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, null] | [null, ActionFailure<{ error: string }>] {
11
+ if (result.success) return [result.data, null];
12
+ return [null, failZod(result.error)];
13
+ }