]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/auth/auth-http.service.ts
Use typescript standard and lint all files
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / auth / auth-http.service.ts
CommitLineData
df98563e 1import { Injectable } from '@angular/core'
bd5c83a8
C
2import {
3 ConnectionBackend,
4 Headers,
5 Http,
6 Request,
7 RequestMethod,
8 RequestOptions,
9 RequestOptionsArgs,
693b1aba
C
10 Response,
11 XHRBackend
df98563e
C
12} from '@angular/http'
13import { Observable } from 'rxjs/Observable'
bd5c83a8 14
df98563e 15import { AuthService } from '../../core'
bd5c83a8
C
16
17@Injectable()
18export class AuthHttp extends Http {
df98563e
C
19 constructor (backend: ConnectionBackend, defaultOptions: RequestOptions, private authService: AuthService) {
20 super(backend, defaultOptions)
bd5c83a8
C
21 }
22
df98563e
C
23 request (url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
24 if (!options) options = {}
bd5c83a8 25
df98563e
C
26 options.headers = new Headers()
27 this.setAuthorizationHeader(options.headers)
bd5c83a8
C
28
29 return super.request(url, options)
30 .catch((err) => {
31 if (err.status === 401) {
df98563e 32 return this.handleTokenExpired(url, options)
bd5c83a8
C
33 }
34
df98563e
C
35 return Observable.throw(err)
36 })
bd5c83a8
C
37 }
38
df98563e
C
39 delete (url: string, options?: RequestOptionsArgs): Observable<Response> {
40 if (!options) options = {}
41 options.method = RequestMethod.Delete
bd5c83a8 42
df98563e 43 return this.request(url, options)
bd5c83a8
C
44 }
45
df98563e
C
46 get (url: string, options?: RequestOptionsArgs): Observable<Response> {
47 if (!options) options = {}
48 options.method = RequestMethod.Get
bd5c83a8 49
df98563e 50 return this.request(url, options)
bd5c83a8
C
51 }
52
df98563e
C
53 post (url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
54 if (!options) options = {}
55 options.method = RequestMethod.Post
56 options.body = body
bd5c83a8 57
df98563e 58 return this.request(url, options)
bd5c83a8
C
59 }
60
df98563e
C
61 put (url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
62 if (!options) options = {}
63 options.method = RequestMethod.Put
64 options.body = body
bd5c83a8 65
df98563e 66 return this.request(url, options)
bd5c83a8
C
67 }
68
df98563e 69 private handleTokenExpired (url: string | Request, options: RequestOptionsArgs) {
14ad0c27
C
70 return this.authService.refreshAccessToken()
71 .flatMap(() => {
df98563e 72 this.setAuthorizationHeader(options.headers)
bd5c83a8 73
df98563e
C
74 return super.request(url, options)
75 })
bd5c83a8
C
76 }
77
df98563e
C
78 private setAuthorizationHeader (headers: Headers) {
79 headers.set('Authorization', this.authService.getRequestHeaderValue())
bd5c83a8
C
80 }
81}
693b1aba 82
df98563e
C
83export function useFactory (backend: XHRBackend, defaultOptions: RequestOptions, authService: AuthService) {
84 return new AuthHttp(backend, defaultOptions, authService)
c16ce1de
C
85}
86
693b1aba
C
87export const AUTH_HTTP_PROVIDERS = [
88 {
89 provide: AuthHttp,
c16ce1de 90 useFactory,
693b1aba 91 deps: [ XHRBackend, RequestOptions, AuthService ]
df98563e
C
92 }
93]