mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-09 09:41:37 +00:00
feat: use throw error
This commit is contained in:
parent
1c7b6eac55
commit
e64be7cce2
2 changed files with 49 additions and 33 deletions
|
|
@ -3,48 +3,59 @@ import type { CandidateData, CandidateLogin } from '$lib/stores/candidate';
|
||||||
import type { SubmissionProgress } from '$lib/stores/portfolio';
|
import type { SubmissionProgress } from '$lib/stores/portfolio';
|
||||||
import { API_URL, errorHandler, type Fetch } from '.';
|
import { API_URL, errorHandler, type Fetch } from '.';
|
||||||
|
|
||||||
|
|
||||||
// SSR Compatible
|
// SSR Compatible
|
||||||
export const apiLogout = async (fetchSsr?: Fetch) => {
|
export const apiLogout = async (fetchSsr?: Fetch) => {
|
||||||
try {
|
try {
|
||||||
fetchSsr ? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' }) : await axios.post(API_URL + '/candidate/logout', { withCredentials: true });
|
fetchSsr
|
||||||
|
? await fetchSsr(API_URL + '/candidate/logout', { method: 'POST', credentials: 'include' })
|
||||||
|
: await axios.post(API_URL + '/candidate/logout', { withCredentials: true });
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Logout failed');
|
throw errorHandler(e, 'Logout failed');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// SSR Compatible
|
// SSR Compatible
|
||||||
export const apiFetchDetails = async (fetchSsr?: Fetch): Promise<CandidateData | null> => {
|
export const apiFetchDetails = async (fetchSsr?: Fetch): Promise<CandidateData> => {
|
||||||
try {
|
try {
|
||||||
if (fetchSsr) {
|
if (fetchSsr) {
|
||||||
const res = await fetchSsr(API_URL + '/candidate/details', { method: "GET", credentials: 'include' });
|
const res = await fetchSsr(API_URL + '/candidate/details', {
|
||||||
const body = await res.text();
|
method: 'GET',
|
||||||
console.log(body);
|
credentials: 'include'
|
||||||
|
});
|
||||||
if (res.status != 200) {
|
if (res.status != 200) {
|
||||||
return null;
|
throw new Error(await res.text());
|
||||||
}
|
}
|
||||||
return JSON.parse(body);
|
return await res.json();
|
||||||
}
|
}
|
||||||
const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true });
|
const res = await axios.get(API_URL + '/candidate/details', { withCredentials: true });
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
|
console.log(e);
|
||||||
throw errorHandler(e, 'Failed to fill details');
|
throw errorHandler(e, 'Failed to fill details');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
// SSR Compatible
|
// SSR Compatible
|
||||||
export const apiFetchSubmissionProgress = async (fetchSsr?: Fetch): Promise<SubmissionProgress> => {
|
export const apiFetchSubmissionProgress = async (fetchSsr?: Fetch): Promise<SubmissionProgress> => {
|
||||||
try {
|
try {
|
||||||
if (fetchSsr) {
|
if (fetchSsr) {
|
||||||
const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', { method: "GET", credentials: 'include' });
|
const res = await fetchSsr(API_URL + '/candidate/portfolio/submission_progress', {
|
||||||
|
method: 'GET',
|
||||||
|
credentials: 'include'
|
||||||
|
});
|
||||||
|
if (res.status != 200) {
|
||||||
|
throw Error(await res.text());
|
||||||
|
}
|
||||||
return await res.json();
|
return await res.json();
|
||||||
}
|
}
|
||||||
const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', { withCredentials: true });
|
const res = await axios.get(API_URL + '/candidate/portfolio/submission_progress', {
|
||||||
|
withCredentials: true
|
||||||
|
});
|
||||||
return res.data;
|
return res.data;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Failed to fetch submission progress');
|
throw errorHandler(e, 'Failed to fetch submission progress');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const apiWhoami = async (): Promise<string> => {
|
export const apiWhoami = async (): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -53,7 +64,7 @@ export const apiWhoami = async (): Promise<string> => {
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Whoami failed');
|
throw errorHandler(e, 'Whoami failed');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const apiLogin = async (data: CandidateLogin): Promise<number> => {
|
export const apiLogin = async (data: CandidateLogin): Promise<number> => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -62,8 +73,7 @@ export const apiLogin = async (data: CandidateLogin): Promise<number> => {
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Login failed');
|
throw errorHandler(e, 'Login failed');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
|
||||||
export const apiFillDetails = async (data: CandidateData): Promise<CandidateData> => {
|
export const apiFillDetails = async (data: CandidateData): Promise<CandidateData> => {
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
|
@ -73,7 +83,7 @@ export const apiFillDetails = async (data: CandidateData): Promise<CandidateData
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Failed to fill details');
|
throw errorHandler(e, 'Failed to fill details');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const apiUploadCoverLetter = async (
|
export const apiUploadCoverLetter = async (
|
||||||
letter: File,
|
letter: File,
|
||||||
|
|
@ -84,15 +94,15 @@ export const apiUploadCoverLetter = async (
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
data: letter,
|
data: letter,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/pdf',
|
'Content-Type': 'application/pdf'
|
||||||
},
|
},
|
||||||
onUploadProgress: progressReporter,
|
onUploadProgress: progressReporter
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Failed to upload cover letter');
|
throw errorHandler(e, 'Failed to upload cover letter');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const apiUploadPortfolioLetter = async (
|
export const apiUploadPortfolioLetter = async (
|
||||||
letter: File,
|
letter: File,
|
||||||
|
|
@ -103,15 +113,15 @@ export const apiUploadPortfolioLetter = async (
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
data: letter,
|
data: letter,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/pdf',
|
'Content-Type': 'application/pdf'
|
||||||
},
|
},
|
||||||
onUploadProgress: progressReporter,
|
onUploadProgress: progressReporter
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Failed to upload cover letter');
|
throw errorHandler(e, 'Failed to upload cover letter');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export const apiUploadPortfolioZip = async (
|
export const apiUploadPortfolioZip = async (
|
||||||
portfolio: File,
|
portfolio: File,
|
||||||
|
|
@ -122,12 +132,12 @@ export const apiUploadPortfolioZip = async (
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
data: portfolio,
|
data: portfolio,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/zip',
|
'Content-Type': 'application/zip'
|
||||||
},
|
},
|
||||||
onUploadProgress: progressReporter,
|
onUploadProgress: progressReporter
|
||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw errorHandler(e, 'Failed to upload cover letter');
|
throw errorHandler(e, 'Failed to upload cover letter');
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
|
||||||
|
|
@ -3,22 +3,28 @@ import { redirect } from '@sveltejs/kit';
|
||||||
import type { LayoutServerLoad } from './$types';
|
import type { LayoutServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: LayoutServerLoad = async ({ fetch }) => {
|
export const load: LayoutServerLoad = async ({ fetch }) => {
|
||||||
const details = await apiFetchDetails(fetch);
|
let details;
|
||||||
|
|
||||||
if (details === null) {
|
try {
|
||||||
|
details = await apiFetchDetails(fetch);
|
||||||
|
} catch {
|
||||||
throw redirect(302, '/register');
|
throw redirect(302, '/register');
|
||||||
}
|
}
|
||||||
|
|
||||||
const submissionProgress = await apiFetchSubmissionProgress(fetch);
|
try {
|
||||||
|
await apiFetchSubmissionProgress(fetch);
|
||||||
|
} catch {
|
||||||
|
// TODO:
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
candidate: {
|
candidate: {
|
||||||
name: details.name,
|
name: details.name,
|
||||||
surname: details.surname,
|
surname: details.surname,
|
||||||
email: details.email
|
email: details.email
|
||||||
},
|
|
||||||
submission: {
|
|
||||||
...submissionProgress
|
|
||||||
}
|
}
|
||||||
|
/*submission: {
|
||||||
|
...submissionProgress
|
||||||
|
}*/
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue