-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from codedthemes/bugfix
revert fake-backend.ts file and fetch-wrapper.ts file
- Loading branch information
Showing
4 changed files
with
22 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
@@ -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, | ||
|
@@ -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() { | ||
|
@@ -79,5 +67,5 @@ function fakeBackend() { | |
return opts.body && JSON.parse(opts.body); | ||
} | ||
}); | ||
} as typeof window.fetch; // Type assertion here | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters