Last active
January 7, 2016 01:20
-
-
Save camkidman/26b26655e2a270ca7f9a to your computer and use it in GitHub Desktop.
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
import { | |
Http, | |
RequestOptions, | |
BaseRequestOptions, | |
ResponseOptions, | |
BaseResponseOptions, | |
BrowserXhr, | |
XHRBackend, | |
} from 'angular2/http'; | |
import {Config} from 'ng2-ui-auth'; | |
import {Shared} from 'ng2-ui-auth/src/shared'; | |
export class JwtHttp extends Http { | |
constructor(_backend: ConnectionBackend, | |
_defaultOptions: RequestOptions, | |
private _router: Router, | |
private _shared: Shared, | |
private _config: Config) { | |
super(_backend, _defaultOptions); | |
} | |
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { | |
if (!options.headers) { | |
options.headers = new Headers(); | |
} | |
if (!options.headers.has('Content-Type')) { | |
options.headers.set('Content-Type', 'application/json'); | |
} | |
if (this._auth.isAuthenticated()) { | |
options.headers.set('Authorization', this._config.authToken + ' ' + this._shared.getToken()); | |
} | |
return super.request(url, options) | |
.catch((err, source, caught) => { | |
handleError(err, this._router); | |
return Observable.empty(); | |
}); | |
} | |
get(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
options = options || {}; | |
options.method = RequestMethod.Get; | |
return this.request(url, options); | |
} | |
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { | |
options = options || {}; | |
options.method = RequestMethod.Post; | |
options.body = body; | |
return this.request(url, options); | |
} | |
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { | |
options = options || {}; | |
options.method = RequestMethod.Put; | |
options.body = body; | |
return super.put(url, body, options); | |
} | |
delete(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
options = options || {}; | |
options.method = RequestMethod.Delete; | |
return super.delete(url, options); | |
} | |
patch(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> { | |
options = options || {}; | |
options.method = RequestMethod.Patch; | |
options.body = body; | |
return super.patch(url, body, options); | |
} | |
head(url: string, options?: RequestOptionsArgs): Observable<Response> { | |
options = options || {}; | |
options.method = RequestMethod.Head; | |
return super.head(url, options); | |
} | |
} | |
export const JWT_HTTP_PROVIDERS = [ | |
provide( | |
Http, | |
{ | |
useFactory: | |
(xhrBackend, requestOptions, router, auth, config) => | |
new DHttp(xhrBackend, requestOptions, router, auth, config), | |
deps: [XHRBackend, RequestOptions, Router, Shared, Config], | |
}), | |
DefaultHandlers, | |
BrowserXhr, | |
provide(RequestOptions, {useClass: BaseRequestOptions}), | |
provide(ResponseOptions, {useClass: BaseResponseOptions}), | |
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
let headers:Headers=new Headers(); | |
headers.append("Content-Type","application/json"); | |
return this.http.post(this.apiURL,JSON.stringify(job),{headers: headers}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment