Skip to content

Commit

Permalink
Merge pull request #13 from codedthemes/bugfix
Browse files Browse the repository at this point in the history
revert fake-backend.ts file and fetch-wrapper.ts file
  • Loading branch information
ct-anjali-rana authored Mar 4, 2024
2 parents ae90be9 + ecdb8d7 commit fa809cc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/layouts/full/vertical-sidebar/extrabox/ExtraBox.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<script setup lang="ts">
</script>
<script setup lang="ts"></script>

<template>
<div class="bg-lightwarning rounded-md pa-5 my-3 circle sm-circle lg-circle hide-menu">
Expand Down
2 changes: 1 addition & 1 deletion src/router/MainRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const MainRoutes = {
meta: {
requiresAuth: true
},
redirect: '/main/dashboard/default',
redirect: '/main',
component: () => import('@/layouts/full/FullLayout.vue'),
children: [
{
Expand Down
40 changes: 14 additions & 26 deletions src/utils/helpers/fake-backend.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
export { fakeBackend };

interface User {
id: number;
username: string;
password: string;
firstName: string;
lastName: string;
}

interface ResponseBody {
id: number;
username: string;
firstName: string;
lastName: string;
token: string;
}

function fakeBackend() {
const users: User[] = [{ id: 1, username: '[email protected]', password: 'admin123', firstName: 'Codedthemes', lastName: '.com' }];
const users = [{ id: 1, username: '[email protected]', password: 'admin123', firstName: 'Codedthemes', lastName: '.com' }];
const realFetch = window.fetch;

window.fetch = function (url: string, opts: { method: string; headers: { [key: string]: string }; body?: string }) {
return new Promise<Response>((resolve, reject) => {
/* eslint-disable @typescript-eslint/no-explicit-any */
window.fetch = function (url: any, opts: any) {
return new Promise((resolve: any, reject) => {
// wrap in timeout to simulate server api call
setTimeout(handleRoute, 500);

Expand All @@ -40,10 +24,13 @@ function fakeBackend() {
}

// route functions

function authenticate() {
const { username, password } = body();
const user = users.find((x) => x.username === username && x.password === password);
const user: any = users.find((x) => x.username === username && x.password === password);

if (!user) return error('Username or password is incorrect');

return ok({
id: user.id,
username: user.username,
Expand All @@ -59,16 +46,17 @@ function fakeBackend() {
}

// helper functions
function ok(body: User[] | ResponseBody): void {
resolve({ ok: true, text: () => Promise.resolve(JSON.stringify(body)) } as Response);

function ok(body: any) {
resolve({ ok: true, text: () => Promise.resolve(JSON.stringify(body)) });
}

function unauthorized() {
resolve({ status: 401, text: () => Promise.resolve(JSON.stringify({ message: 'Unauthorized' })) } as Response);
resolve({ status: 401, text: () => Promise.resolve(JSON.stringify({ message: 'Unauthorized' })) });
}

function error(message: string) {
resolve({ status: 400, text: () => Promise.resolve(JSON.stringify({ message })) } as Response);
resolve({ status: 400, text: () => Promise.resolve(JSON.stringify({ message })) });
}

function isAuthenticated() {
Expand All @@ -79,5 +67,5 @@ function fakeBackend() {
return opts.body && JSON.parse(opts.body);
}
});
} as typeof window.fetch; // Type assertion here
};
}
23 changes: 6 additions & 17 deletions src/utils/helpers/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ export const fetchWrapper = {
delete: request('DELETE')
};

interface temp {
method: string;
headers: Record<string, string>;
body?: string;
}

interface UserData {
username: string;
password: string;
}

function request(method: string) {
return (url: string, body?: object) => {
const requestOptions: temp = {
/* eslint-disable @typescript-eslint/no-explicit-any */
const requestOptions: any = {
method,
headers: authHeader(url)
};
Expand All @@ -34,7 +24,7 @@ function request(method: string) {

// helper functions

function authHeader(url: string): Record<string, string> {
function authHeader(url: string) {
// return auth header with jwt if user is logged in and request is to the api url
const { user } = useAuthStore();
const isLoggedIn = !!user?.token;
Expand All @@ -46,7 +36,7 @@ function authHeader(url: string): Record<string, string> {
}
}

function handleResponse(response: Response): Promise<UserData> {
function handleResponse(response: any) {
return response.text().then((text: string) => {
const data = text && JSON.parse(text);

Expand All @@ -57,11 +47,10 @@ function handleResponse(response: Response): Promise<UserData> {
logout();
}

const error: string = (data && data.message) || response.statusText;
const error = (data && data.message) || response.statusText;
return Promise.reject(error);
}

// Ensure data is of type UserData
return data as UserData;
return data;
});
}

0 comments on commit fa809cc

Please sign in to comment.