-
Notifications
You must be signed in to change notification settings - Fork 2
/
jira.service.ts
63 lines (49 loc) · 2.14 KB
/
jira.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions, RequestOptionsArgs, Response } from '@angular/http';
import { Observable } from 'rxjs/';
import { pipe } from 'ramda';
import { JiraRequestOptionsService } from './jira-request-options.service';
import { jiraReduceUrlData } from '../helpers/jira-reduce-url-data';
import { jiraReduceUrlQuery } from '../helpers/jira-reduce-url-query';
import { jiraReduceUrlJql } from '../helpers/jira-reduce-url-jql';
function parseUrl(urlData, jqlQuery, urlQuery) {
return pipe(
jiraReduceUrlData(urlData),
jiraReduceUrlJql(jqlQuery),
jiraReduceUrlQuery(urlQuery),
);
}
@Injectable()
export class JiraService {
constructor(
private jiraRequestOptions: JiraRequestOptionsService,
private http: Http,
) {}
public get requestOptions(): RequestOptionsArgs {
const options: RequestOptions = new RequestOptions();
const headers: Headers = new Headers();
headers.set('Authorization', `Basic ${this.jiraRequestOptions.token}`);
options.headers = headers;
return options;
}
public get(type: string, urlData?: any, jqlQuery: string = '', urlQuery: any = {}): Observable<Response> {
return this.http.get(this.url(type, urlData, jqlQuery, urlQuery), this.requestOptions);
}
public delete(type: string, urlData?: any, jqlQuery: string = '', urlQuery: any = {}): Observable<Response> {
return this.http.delete(this.url(type, urlData, jqlQuery, urlQuery), this.requestOptions);
}
public post(type: string, urlData: any, postData: any): Observable<Response> {
return this.http.post(this.url(type, urlData), postData, this.requestOptions);
}
public put(type: string, urlData: any, postData: any): Observable<Response> {
return this.http.put(this.url(type, urlData), postData, this.requestOptions);
}
private url(type: string, urlData?: any, jqlQuery?: string, urlQuery?: any): string {
const url: string = this.jiraRequestOptions.url;
const uri: string = parseUrl(urlData, jqlQuery, urlQuery)(type);
return `${url}/${uri}`
.replace(/({.*?})/g, '')
.replace(/(?:\?jql=$)/, '')
.replace(/(?:\?$)/, '');
}
}