Move next js project to archive (#207)

This commit is contained in:
Aaron Po
2026-04-20 02:30:25 -04:00
committed by GitHub
parent 92ec16ce93
commit d47e3ed7f0
347 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,169 @@
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { BasicUserInfoSchema } from '@/config/auth/types';
import GetUserSchema from '@/services/users/auth/schema/GetUserSchema';
import type {
SendEditUserRequest,
SendForgotPasswordRequest,
SendLoginUserRequest,
SendRegisterUserRequest,
SendUpdatePasswordRequest,
SendUserFollowRequest,
ValidateEmailRequest,
} from './types';
import { z } from 'zod';
export const sendEditUserRequest: SendEditUserRequest = async ({ user, data }) => {
const response = await fetch(`/api/users/${user!.id}`, {
body: JSON.stringify(data),
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
});
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('API response validation failed.');
}
return parsed.data;
};
export const sendForgotPasswordRequest: SendForgotPasswordRequest = async ({ email }) => {
const response = await fetch('/api/users/forgot-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email }),
});
if (!response.ok) {
throw new Error("Something went wrong and we couldn't send the reset link.");
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error(parsed.error.message);
}
return parsed.data;
};
export const sendLoginUserRequest: SendLoginUserRequest = async ({
username,
password,
}) => {
const response = await fetch('/api/users/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
password,
}),
});
const json: unknown = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('API response validation failed');
}
if (!parsed.data.success) {
throw new Error(parsed.data.message);
}
const parsedPayload = BasicUserInfoSchema.safeParse(parsed.data.payload);
if (!parsedPayload.success) {
throw new Error('API response payload validation failed');
}
return parsedPayload.data;
};
export const sendRegisterUserRequest: SendRegisterUserRequest = async (data) => {
const response = await fetch('/api/users/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('API response validation failed.');
}
if (!parsed.data.success) {
throw new Error(parsed.data.message);
}
const parsedPayload = GetUserSchema.safeParse(parsed.data.payload);
if (!parsedPayload.success) {
throw new Error('API response payload validation failed.');
}
return parsedPayload.data;
};
export const sendUpdatePasswordRequest: SendUpdatePasswordRequest = async (data) => {
const response = await fetch('/api/users/edit-password', {
body: JSON.stringify(data),
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
});
if (!response.ok) {
throw new Error(response.statusText);
}
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('API response validation failed.');
}
return parsed.data;
};
export const sendUserFollowRequest: SendUserFollowRequest = async ({ userId }) => {
const response = await fetch(`/api/users/${userId}/follow-user`, { method: 'POST' });
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
throw new Error('Invalid API response.');
}
return parsed.data;
};
export const validateEmailRequest: ValidateEmailRequest = async ({ email }) => {
const response = await fetch(`/api/users/check-email?email=${email}`);
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
return false;
}
const parsedPayload = z
.object({ emailIsTaken: z.boolean() })
.safeParse(parsed.data.payload);
if (!parsedPayload.success) {
return false;
}
return !parsedPayload.data.emailIsTaken;
};

View File

@@ -0,0 +1,41 @@
import { BasicUserInfoSchema } from '@/config/auth/types';
import {
CreateUserValidationSchema,
UpdatePasswordSchema,
} from '@/services/users/auth/schema/CreateUserValidationSchemas';
import GetUserSchema from '@/services/users/auth/schema/GetUserSchema';
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
export type SendEditUserRequest = (args: {
user: z.infer<typeof GetUserSchema>;
data: {
username: string;
email: string;
firstName: string;
lastName: string;
};
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendForgotPasswordRequest = (args: {
email: string;
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendLoginUserRequest = (args: {
username: string;
password: string;
}) => Promise<z.infer<typeof BasicUserInfoSchema>>;
export type SendRegisterUserRequest = (
args: z.infer<typeof CreateUserValidationSchema>,
) => Promise<z.infer<typeof GetUserSchema>>;
export type SendUpdatePasswordRequest = (
args: z.infer<typeof UpdatePasswordSchema>,
) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type SendUserFollowRequest = (args: {
userId: string;
}) => Promise<z.infer<typeof APIResponseValidationSchema>>;
export type ValidateEmailRequest = (args: { email: string }) => Promise<boolean>;

View File

@@ -0,0 +1,27 @@
interface UpdateProfileRequestParams {
file: File;
userId: string;
}
const sendUpdateUserAvatarRequest = async ({
file,
userId,
}: UpdateProfileRequestParams) => {
const formData = new FormData();
formData.append('image', file);
const response = await fetch(`/api/users/${userId}/profile/update-avatar`, {
method: 'PUT',
body: formData,
});
if (!response.ok) {
throw new Error('Failed to update profile.');
}
const json = await response.json();
return json;
};
export default sendUpdateUserAvatarRequest;

View File

@@ -0,0 +1,28 @@
import UpdateProfileSchema from '@/services/users/auth/schema/UpdateProfileSchema';
import { z } from 'zod';
interface UpdateProfileRequestParams {
userId: string;
bio: z.infer<typeof UpdateProfileSchema>['bio'];
}
const sendUpdateUserProfileRequest = async ({
bio,
userId,
}: UpdateProfileRequestParams) => {
const response = await fetch(`/api/users/${userId}/profile/update-profile`, {
method: 'PUT',
body: JSON.stringify({ bio }),
headers: { 'Content-Type': 'application/json' },
});
if (!response.ok) {
throw new Error('Failed to update profile.');
}
const json = await response.json();
return json;
};
export default sendUpdateUserProfileRequest;

View File

@@ -0,0 +1,25 @@
import APIResponseValidationSchema from '@/validation/APIResponseValidationSchema';
import { z } from 'zod';
const validateUsernameRequest = async (username: string) => {
const response = await fetch(`/api/users/check-username?username=${username}`);
const json = await response.json();
const parsed = APIResponseValidationSchema.safeParse(json);
if (!parsed.success) {
return false;
}
const parsedPayload = z
.object({ usernameIsTaken: z.boolean() })
.safeParse(parsed.data.payload);
if (!parsedPayload.success) {
return false;
}
return !parsedPayload.data.usernameIsTaken;
};
export default validateUsernameRequest;