mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-15 09:11:57 +10:00
🚀 release v3.0.0
This commit is contained in:
77
client/services/auth.ts
Normal file
77
client/services/auth.ts
Normal file
@ -0,0 +1,77 @@
|
||||
import { User } from '@reactive-resume/schema';
|
||||
import { AxiosResponse } from 'axios';
|
||||
import toast from 'react-hot-toast';
|
||||
|
||||
import { setAccessToken, setUser } from '@/store/auth/authSlice';
|
||||
|
||||
import store from '../store';
|
||||
import axios from './axios';
|
||||
|
||||
export type LoginParams = {
|
||||
identifier: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type LoginWithGoogleParams = {
|
||||
accessToken: string;
|
||||
};
|
||||
|
||||
export type RegisterParams = {
|
||||
name: string;
|
||||
username: string;
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type AuthDTO = {
|
||||
user: User;
|
||||
accessToken: string;
|
||||
};
|
||||
|
||||
export type ForgotPasswordParams = {
|
||||
email: string;
|
||||
};
|
||||
|
||||
export type ResetPasswordParams = {
|
||||
resetToken: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export const login = async (loginParams: LoginParams) => {
|
||||
const {
|
||||
data: { user, accessToken },
|
||||
} = await axios.post<AuthDTO, AxiosResponse<AuthDTO>, LoginParams>('/auth/login', loginParams);
|
||||
|
||||
store.dispatch(setUser(user));
|
||||
store.dispatch(setAccessToken(accessToken));
|
||||
};
|
||||
|
||||
export const loginWithGoogle = async (loginWithGoogleParams: LoginWithGoogleParams) => {
|
||||
const {
|
||||
data: { user, accessToken },
|
||||
} = await axios.post<AuthDTO, AxiosResponse<AuthDTO>, LoginWithGoogleParams>('/auth/google', loginWithGoogleParams);
|
||||
|
||||
store.dispatch(setUser(user));
|
||||
store.dispatch(setAccessToken(accessToken));
|
||||
};
|
||||
|
||||
export const register = async (registerParams: RegisterParams) => {
|
||||
const {
|
||||
data: { user, accessToken },
|
||||
} = await axios.post<AuthDTO, AxiosResponse<AuthDTO>, RegisterParams>('/auth/register', registerParams);
|
||||
|
||||
store.dispatch(setUser(user));
|
||||
store.dispatch(setAccessToken(accessToken));
|
||||
};
|
||||
|
||||
export const forgotPassword = async (forgotPasswordParams: ForgotPasswordParams) => {
|
||||
await axios.post<void, AxiosResponse<void>, ForgotPasswordParams>('/auth/forgot-password', forgotPasswordParams);
|
||||
|
||||
toast.success('Please check your email for the password reset link.');
|
||||
};
|
||||
|
||||
export const resetPassword = async (resetPasswordParams: ResetPasswordParams) => {
|
||||
await axios.post<void, AxiosResponse<void>, ResetPasswordParams>('/auth/reset-password', resetPasswordParams);
|
||||
|
||||
toast.success('Your password has been changed successfully, please login again.');
|
||||
};
|
||||
55
client/services/axios.ts
Normal file
55
client/services/axios.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import _axios, { AxiosError } from 'axios';
|
||||
import Router from 'next/router';
|
||||
|
||||
import { logout } from '@/store/auth/authSlice';
|
||||
|
||||
import store from '../store';
|
||||
|
||||
export type ServerError = {
|
||||
statusCode: number;
|
||||
message: string;
|
||||
timestamp: string;
|
||||
path: string;
|
||||
};
|
||||
|
||||
const axios = _axios.create({
|
||||
baseURL: `${process.env.serverUrl}/api`,
|
||||
});
|
||||
|
||||
export const uninterceptedAxios = _axios.create({
|
||||
baseURL: `${process.env.serverUrl}/api`,
|
||||
});
|
||||
|
||||
axios.interceptors.request.use((config) => {
|
||||
const { accessToken } = store.getState().auth;
|
||||
|
||||
config.headers = {
|
||||
...config.headers,
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
};
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
axios.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error: AxiosError) => {
|
||||
const { response } = error;
|
||||
|
||||
if (response) {
|
||||
const errorObject: ServerError = response.data;
|
||||
const code = errorObject.statusCode;
|
||||
|
||||
if (code === 401 || code === 404) {
|
||||
store.dispatch(logout());
|
||||
Router.push('/');
|
||||
}
|
||||
|
||||
throw errorObject;
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
);
|
||||
|
||||
export default axios;
|
||||
5
client/services/fonts.ts
Normal file
5
client/services/fonts.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { Font } from '@reactive-resume/schema';
|
||||
|
||||
import axios from './axios';
|
||||
|
||||
export const fetchFonts = () => axios.get<Font[]>('/fonts').then((res) => res.data);
|
||||
19
client/services/integrations.ts
Normal file
19
client/services/integrations.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import { Integration, Resume } from '@reactive-resume/schema';
|
||||
import { AxiosResponse } from 'axios';
|
||||
|
||||
import axios from './axios';
|
||||
|
||||
export type ImportFromExternalParams = {
|
||||
integration: Integration;
|
||||
file: File;
|
||||
};
|
||||
|
||||
export const importFromExternal = async (importFromExternalParams: ImportFromExternalParams) => {
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('file', importFromExternalParams.file);
|
||||
|
||||
return axios
|
||||
.post<Resume, AxiosResponse<Resume>, FormData>(`/integrations/${importFromExternalParams.integration}`, formData)
|
||||
.then((res) => res.data);
|
||||
};
|
||||
9
client/services/printer.ts
Normal file
9
client/services/printer.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import axios from './axios';
|
||||
|
||||
export type PrintResumeAsPdfParams = {
|
||||
username: string;
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export const printResumeAsPdf = (printResumeAsPdfParams: PrintResumeAsPdfParams): Promise<string> =>
|
||||
axios.get(`/printer/${printResumeAsPdfParams.username}/${printResumeAsPdfParams.slug}`).then((res) => res.data);
|
||||
5
client/services/react-query.ts
Normal file
5
client/services/react-query.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { QueryClient } from 'react-query';
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
export default queryClient;
|
||||
121
client/services/resume.ts
Normal file
121
client/services/resume.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { Resume } from '@reactive-resume/schema';
|
||||
import { AxiosResponse } from 'axios';
|
||||
import isEmpty from 'lodash/isEmpty';
|
||||
|
||||
import axios from './axios';
|
||||
|
||||
export type CreateResumeParams = {
|
||||
name: string;
|
||||
slug: string;
|
||||
public: boolean;
|
||||
};
|
||||
|
||||
export type FetchResumeByIdentifierParams = {
|
||||
username: string;
|
||||
slug: string;
|
||||
options?: {
|
||||
secretKey?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type FetchResumeByShortIdParams = {
|
||||
shortId: string;
|
||||
options?: {
|
||||
secretKey?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type RenameResumeParams = {
|
||||
id: number;
|
||||
name: string;
|
||||
slug: string;
|
||||
};
|
||||
|
||||
export type DuplicateResumeParams = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export type LoadSampleDataParams = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export type ResetResumeParams = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export type UploadPhotoParams = {
|
||||
id: number;
|
||||
file: File;
|
||||
};
|
||||
|
||||
export type DeletePhotoParams = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export type DeleteResumeParams = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export const fetchResumes = () => axios.get<Resume[]>('/resume').then((res) => res.data);
|
||||
|
||||
export const fetchResumeByShortId = async ({ shortId, options = { secretKey: '' } }: FetchResumeByShortIdParams) => {
|
||||
const requestOptions = isEmpty(options.secretKey) ? {} : { params: { secretKey: options.secretKey } };
|
||||
|
||||
return axios.get<Resume>(`/resume/short/${shortId}`, requestOptions).then((res) => res.data);
|
||||
};
|
||||
|
||||
export const fetchResumeByIdentifier = async ({
|
||||
username,
|
||||
slug,
|
||||
options = { secretKey: '' },
|
||||
}: FetchResumeByIdentifierParams) => {
|
||||
const requestOptions = isEmpty(options.secretKey) ? {} : { params: { secretKey: options.secretKey } };
|
||||
|
||||
return axios.get<Resume>(`/resume/${username}/${slug}`, requestOptions).then((res) => res.data);
|
||||
};
|
||||
|
||||
export const createResume = (createResumeParams: CreateResumeParams) =>
|
||||
axios.post<Resume, AxiosResponse<Resume>, CreateResumeParams>('/resume', createResumeParams).then((res) => res.data);
|
||||
|
||||
export const renameResume = (renameResumeParams: RenameResumeParams) =>
|
||||
axios
|
||||
.patch<Resume, AxiosResponse<Resume>, RenameResumeParams>(`/resume/${renameResumeParams.id}`, renameResumeParams)
|
||||
.then((res) => res.data);
|
||||
|
||||
export const updateResume = (updateResumeParams: Partial<Resume>) =>
|
||||
axios
|
||||
.patch<Resume, AxiosResponse<Resume>, Partial<Resume>>(`/resume/${updateResumeParams.id}`, updateResumeParams)
|
||||
.then((res) => res.data);
|
||||
|
||||
export const duplicateResume = (duplicateResumeParams: DuplicateResumeParams) =>
|
||||
axios
|
||||
.post<Resume, AxiosResponse<Resume>, DuplicateResumeParams>(`/resume/${duplicateResumeParams.id}/duplicate`)
|
||||
.then((res) => res.data);
|
||||
|
||||
export const loadSampleData = (loadSampleDataParams: LoadSampleDataParams) =>
|
||||
axios
|
||||
.post<Resume, AxiosResponse<Resume>, LoadSampleDataParams>(`/resume/${loadSampleDataParams.id}/sample`)
|
||||
.then((res) => res.data);
|
||||
|
||||
export const resetResume = (resetResumeParams: ResetResumeParams) =>
|
||||
axios
|
||||
.post<Resume, AxiosResponse<Resume>, ResetResumeParams>(`/resume/${resetResumeParams.id}/reset`)
|
||||
.then((res) => res.data);
|
||||
|
||||
export const uploadPhoto = async (uploadPhotoParams: UploadPhotoParams) => {
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('file', uploadPhotoParams.file);
|
||||
|
||||
return axios
|
||||
.put<Resume, AxiosResponse<Resume>, FormData>(`/resume/${uploadPhotoParams.id}/photo`, formData)
|
||||
.then((res) => res.data);
|
||||
};
|
||||
|
||||
export const deletePhoto = async (deletePhotoParams: DeletePhotoParams) =>
|
||||
axios.delete<Resume, AxiosResponse<Resume>>(`/resume/${deletePhotoParams.id}/photo`).then((res) => res.data);
|
||||
|
||||
export const deleteResume = (deleteResumeParams: DeleteResumeParams) =>
|
||||
axios
|
||||
.delete<void, AxiosResponse<void>, DeleteResumeParams>(`/resume/${deleteResumeParams.id}`)
|
||||
.then((res) => res.data);
|
||||
Reference in New Issue
Block a user