Skip to content
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

Create an Http service extension #14

Closed
sinedied opened this issue Jan 19, 2017 · 4 comments
Closed

Create an Http service extension #14

sinedied opened this issue Jan 19, 2017 · 4 comments
Assignees
Labels
Milestone

Comments

@sinedied
Copy link
Member

Provide support for server configuration depending of the environment, default headers and user-configurable cache policy.

Should go in core module.

@sinedied sinedied self-assigned this Jan 19, 2017
@sinedied sinedied modified the milestone: 1.0 Feb 15, 2017
@moshmage
Copy link

moshmage commented Apr 6, 2017

How would I go about extending this?
I tried to provide a AuthService (which holds a Authorization: Bearer <token>), and then (on private httpRequest) made an extend({}, options, { headers: new Headers('Authorization', this.authService.token) })

but the constructor never get the authService (comming with undefined as its value on a console.log)

What I did:

  • core.module.ts add authService to the providers
  • add authService to the createHttpService return function
  • add authService to http.service.ts constructor
  • console.log(authService)
  • ???
  • don't profit because it comes out undefined :(

What I'm trying to do:
Hook the "Authorization: Bearer <token>" header to every http request call

... Further down the line I'll need to hook on every 401 answer so I know when the is no longer valid; But Ill cross that bridge when I get there :)

@sinedied
Copy link
Member Author

sinedied commented Apr 7, 2017

Seems like an injection issue, is your core.module.ts createHttpService like this

export function createHttpService(backend: ConnectionBackend,
                                  defaultOptions: RequestOptions,
                                  httpCacheService: HttpCacheService,
                                  authService: AuthService) {
  return new HttpService(backend, defaultOptions, httpCacheService, authService);
}

and your HttpService constructor like this?

  constructor(backend: ConnectionBackend,
              private defaultOptions: RequestOptions,
              private httpCacheService: HttpCacheService,
              private authService: AuthService) {
    super(backend, defaultOptions);
  }

If it works for httpCacheService, it should work for your authService.

As for catching 401 error, you can just hook to the httpRequest or errorHandler methods.

@moshmage
Copy link

moshmage commented Apr 7, 2017

Yep, That's exactly how I set up - and what I thought ("if it works for httpCache it should work for auth")

I ended up making a layer between the Http and the app, httpClient, which will then call the Http. It's ugly, but I ain't have that much time;

Will revisit this approach later and submit a propper issue if stuff goes haywire :)

ps: My approach would also induce in a circular dependency, because HttpService required authService and authService required Http (which, if I understood the architecutre correctly.. is HttpService). So I guess that whatever I was doing, it wasn't correct ^^'

@sinedied
Copy link
Member Author

sinedied commented Apr 7, 2017

ps: My approach would also induce in a circular dependency

Ah, that's the issue here! Since there's no way to properly resolve such dependencies, one of the required service ends up undefined. I'm surprised Angular did not throw some error or warning in the console though.

You should properly separate the 3 concerns here: getting authentified, storing/getting credentials and providing an customized http service. That should be 3 different services, and the service for storing/getting the credentials will act as the middleman here to avoid having a circular dependency.

Good luck ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants