-
Notifications
You must be signed in to change notification settings - Fork 386
Small fixes and make it work with webpack #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yktoo
wants to merge
2
commits into
cornflourblue:master
Choose a base branch
from
yktoo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,154 +1,156 @@ | ||
import { Http, BaseRequestOptions, Response, ResponseOptions, RequestMethod, XHRBackend, RequestOptions } from '@angular/http'; | ||
import { MockBackend, MockConnection } from '@angular/http/testing'; | ||
|
||
export let fakeBackendProvider = { | ||
// use fake backend in place of Http service for backend-less development | ||
provide: Http, | ||
useFactory: (backend: MockBackend, options: BaseRequestOptions, realBackend: XHRBackend) => { | ||
// array in local storage for registered users | ||
let users: any[] = JSON.parse(localStorage.getItem('users')) || []; | ||
|
||
// configure fake backend | ||
backend.connections.subscribe((connection: MockConnection) => { | ||
// wrap in timeout to simulate server api call | ||
setTimeout(() => { | ||
|
||
// authenticate | ||
if (connection.request.url.endsWith('/api/authenticate') && connection.request.method === RequestMethod.Post) { | ||
// get parameters from post request | ||
let params = JSON.parse(connection.request.getBody()); | ||
|
||
// find if any user matches login credentials | ||
let filteredUsers = users.filter(user => { | ||
return user.username === params.username && user.password === params.password; | ||
}); | ||
|
||
if (filteredUsers.length) { | ||
// if login details are valid return 200 OK with user details and fake jwt token | ||
let user = filteredUsers[0]; | ||
connection.mockRespond(new Response(new ResponseOptions({ | ||
status: 200, | ||
body: { | ||
id: user.id, | ||
username: user.username, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
token: 'fake-jwt-token' | ||
} | ||
}))); | ||
} else { | ||
// else return 400 bad request | ||
connection.mockError(new Error('Username or password is incorrect')); | ||
} | ||
function fakeBackendFactory(backend: MockBackend, options: BaseRequestOptions, realBackend: XHRBackend) { | ||
// array in local storage for registered users | ||
let users: any[] = JSON.parse(localStorage.getItem('users')) || []; | ||
|
||
// configure fake backend | ||
backend.connections.subscribe((connection: MockConnection) => { | ||
// wrap in timeout to simulate server api call | ||
setTimeout(() => { | ||
|
||
// authenticate | ||
if (connection.request.url.endsWith('/api/authenticate') && connection.request.method === RequestMethod.Post) { | ||
// get parameters from post request | ||
let params = JSON.parse(connection.request.getBody()); | ||
|
||
// find if any user matches login credentials | ||
let filteredUsers = users.filter(user => { | ||
return user.username === params.username && user.password === params.password; | ||
}); | ||
|
||
return; | ||
if (filteredUsers.length) { | ||
// if login details are valid return 200 OK with user details and fake jwt token | ||
let user = filteredUsers[0]; | ||
connection.mockRespond(new Response(new ResponseOptions({ | ||
status: 200, | ||
body: { | ||
id: user.id, | ||
username: user.username, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
token: 'fake-jwt-token' | ||
} | ||
}))); | ||
} else { | ||
// else return 400 bad request | ||
connection.mockError(new Error('Username or password is incorrect')); | ||
} | ||
|
||
// get users | ||
if (connection.request.url.endsWith('/api/users') && connection.request.method === RequestMethod.Get) { | ||
// check for fake auth token in header and return users if valid, this security is implemented server side in a real application | ||
if (connection.request.headers.get('Authorization') === 'Bearer fake-jwt-token') { | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200, body: users }))); | ||
} else { | ||
// return 401 not authorised if token is null or invalid | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 401 }))); | ||
} | ||
|
||
return; | ||
return; | ||
} | ||
|
||
// get users | ||
if (connection.request.url.endsWith('/api/users') && connection.request.method === RequestMethod.Get) { | ||
// check for fake auth token in header and return users if valid, this security is implemented server side in a real application | ||
if (connection.request.headers.get('Authorization') === 'Bearer fake-jwt-token') { | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200, body: users }))); | ||
} else { | ||
// return 401 not authorised if token is null or invalid | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 401 }))); | ||
} | ||
|
||
// get user by id | ||
if (connection.request.url.match(/\/api\/users\/\d+$/) && connection.request.method === RequestMethod.Get) { | ||
// check for fake auth token in header and return user if valid, this security is implemented server side in a real application | ||
if (connection.request.headers.get('Authorization') === 'Bearer fake-jwt-token') { | ||
// find user by id in users array | ||
let urlParts = connection.request.url.split('/'); | ||
let id = parseInt(urlParts[urlParts.length - 1]); | ||
let matchedUsers = users.filter(user => { return user.id === id; }); | ||
let user = matchedUsers.length ? matchedUsers[0] : null; | ||
|
||
// respond 200 OK with user | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200, body: user }))); | ||
} else { | ||
// return 401 not authorised if token is null or invalid | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 401 }))); | ||
} | ||
|
||
return; | ||
return; | ||
} | ||
|
||
// get user by id | ||
if (connection.request.url.match(/\/api\/users\/\d+$/) && connection.request.method === RequestMethod.Get) { | ||
// check for fake auth token in header and return user if valid, this security is implemented server side in a real application | ||
if (connection.request.headers.get('Authorization') === 'Bearer fake-jwt-token') { | ||
// find user by id in users array | ||
let urlParts = connection.request.url.split('/'); | ||
let id = parseInt(urlParts[urlParts.length - 1]); | ||
let matchedUsers = users.filter(user => { return user.id === id; }); | ||
let user = matchedUsers.length ? matchedUsers[0] : null; | ||
|
||
// respond 200 OK with user | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200, body: user }))); | ||
} else { | ||
// return 401 not authorised if token is null or invalid | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 401 }))); | ||
} | ||
|
||
// create user | ||
if (connection.request.url.endsWith('/api/users') && connection.request.method === RequestMethod.Post) { | ||
// get new user object from post body | ||
let newUser = JSON.parse(connection.request.getBody()); | ||
|
||
// validation | ||
let duplicateUser = users.filter(user => { return user.username === newUser.username; }).length; | ||
if (duplicateUser) { | ||
return connection.mockError(new Error('Username "' + newUser.username + '" is already taken')); | ||
} | ||
|
||
// save new user | ||
newUser.id = users.length + 1; | ||
users.push(newUser); | ||
localStorage.setItem('users', JSON.stringify(users)); | ||
return; | ||
} | ||
|
||
// respond 200 OK | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200 }))); | ||
// create user | ||
if (connection.request.url.endsWith('/api/users') && connection.request.method === RequestMethod.Post) { | ||
// get new user object from post body | ||
let newUser = JSON.parse(connection.request.getBody()); | ||
|
||
return; | ||
// validation | ||
let duplicateUser = users.filter(user => { return user.username === newUser.username; }).length; | ||
if (duplicateUser) { | ||
return connection.mockError(new Error('Username "' + newUser.username + '" is already taken')); | ||
} | ||
|
||
// delete user | ||
if (connection.request.url.match(/\/api\/users\/\d+$/) && connection.request.method === RequestMethod.Delete) { | ||
// check for fake auth token in header and return user if valid, this security is implemented server side in a real application | ||
if (connection.request.headers.get('Authorization') === 'Bearer fake-jwt-token') { | ||
// find user by id in users array | ||
let urlParts = connection.request.url.split('/'); | ||
let id = parseInt(urlParts[urlParts.length - 1]); | ||
for (let i = 0; i < users.length; i++) { | ||
let user = users[i]; | ||
if (user.id === id) { | ||
// delete user | ||
users.splice(i, 1); | ||
localStorage.setItem('users', JSON.stringify(users)); | ||
break; | ||
} | ||
// save new user | ||
newUser.id = users.length + 1; | ||
users.push(newUser); | ||
localStorage.setItem('users', JSON.stringify(users)); | ||
|
||
// respond 200 OK | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200 }))); | ||
|
||
return; | ||
} | ||
|
||
// delete user | ||
if (connection.request.url.match(/\/api\/users\/\d+$/) && connection.request.method === RequestMethod.Delete) { | ||
// check for fake auth token in header and return user if valid, this security is implemented server side in a real application | ||
if (connection.request.headers.get('Authorization') === 'Bearer fake-jwt-token') { | ||
// find user by id in users array | ||
let urlParts = connection.request.url.split('/'); | ||
let id = parseInt(urlParts[urlParts.length - 1]); | ||
for (let i = 0; i < users.length; i++) { | ||
let user = users[i]; | ||
if (user.id === id) { | ||
// delete user | ||
users.splice(i, 1); | ||
localStorage.setItem('users', JSON.stringify(users)); | ||
break; | ||
} | ||
|
||
// respond 200 OK | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200 }))); | ||
} else { | ||
// return 401 not authorised if token is null or invalid | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 401 }))); | ||
} | ||
|
||
return; | ||
// respond 200 OK | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 200 }))); | ||
} else { | ||
// return 401 not authorised if token is null or invalid | ||
connection.mockRespond(new Response(new ResponseOptions({ status: 401 }))); | ||
} | ||
|
||
// pass through any requests not handled above | ||
let realHttp = new Http(realBackend, options); | ||
let requestOptions = new RequestOptions({ | ||
method: connection.request.method, | ||
headers: connection.request.headers, | ||
body: connection.request.getBody(), | ||
url: connection.request.url, | ||
withCredentials: connection.request.withCredentials, | ||
responseType: connection.request.responseType | ||
return; | ||
} | ||
|
||
// pass through any requests not handled above | ||
let realHttp = new Http(realBackend, options); | ||
let requestOptions = new RequestOptions({ | ||
method: connection.request.method, | ||
headers: connection.request.headers, | ||
body: connection.request.getBody(), | ||
url: connection.request.url, | ||
withCredentials: connection.request.withCredentials, | ||
responseType: connection.request.responseType | ||
}); | ||
realHttp.request(connection.request.url, requestOptions) | ||
.subscribe((response: Response) => { | ||
connection.mockRespond(response); | ||
}, | ||
(error: any) => { | ||
connection.mockError(error); | ||
}); | ||
realHttp.request(connection.request.url, requestOptions) | ||
.subscribe((response: Response) => { | ||
connection.mockRespond(response); | ||
}, | ||
(error: any) => { | ||
connection.mockError(error); | ||
}); | ||
|
||
}, 500); | ||
}, 500); | ||
|
||
}); | ||
|
||
}); | ||
return new Http(backend, options); | ||
} | ||
|
||
return new Http(backend, options); | ||
}, | ||
export let fakeBackendProvider = { | ||
// use fake backend in place of Http service for backend-less development | ||
provide: Http, | ||
useFactory: fakeBackendFactory, | ||
deps: [MockBackend, BaseRequestOptions, XHRBackend] | ||
}; | ||
}; |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still I can see the error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested it in Plunker, there was no error. What error do you get?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ERROR in Error encountered resolving symbol values statically. Reference to a non-exported function (position 4:10 in the original .ts file), resolving symbol fakeBackendProvider in ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, perhaps this function needs to be
export
ed too (works for me with webpack as it is). Anyway, the first commit is safe to merge, the second one may be a bit controversial.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simply it should be
export function fakeBackendFactory()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. Shall I add commit this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested the change and it seems good.