Skip to content

Commit 3efefbb

Browse files
First changes
1 parent 9a17728 commit 3efefbb

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

src/Auth.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Injectable, Inject } from '@angular/core';
2+
import { Http } from '@angular/http';
3+
4+
// Need to import interfaces dependencies
5+
// Bug TypeScript https://github.com/Microsoft/TypeScript/issues/5938
6+
import { Observable } from 'rxjs/Observable';
7+
import { RequestOptionsArgs } from '@angular/http/src/interfaces';
8+
import { Response } from '@angular/http/src/static_response';
9+
10+
import { WpApiParent } from './Parent';
11+
12+
import { WpApiLoader } from './Loaders';
13+
14+
export interface IWpApiAuth {
15+
auth(authCredentials: IAuthCredentials, options?: RequestOptionsArgs): Observable<Response>;
16+
credentials(options?: RequestOptionsArgs): Observable<Response>;
17+
}
18+
19+
export interface ICredentials {
20+
token?: string;
21+
email?: string;
22+
}
23+
24+
export interface IAuthCredentials {
25+
username: string;
26+
password: string;
27+
}
28+
29+
@Injectable()
30+
export class WpApiAuth extends WpApiParent implements IWpApiAuth {
31+
constructor(
32+
public wpApiLoader: WpApiLoader,
33+
public http: Http
34+
) {
35+
super(wpApiLoader, http);
36+
}
37+
38+
protected getWebServiceUrl(postfix: string): string {
39+
return super.getWebServiceUrl(postfix).replace(this.wpApiLoader.namespace, '/jwt-auth/v1');
40+
}
41+
42+
auth(authCredentials: IAuthCredentials, options = {}): Observable<Response> {
43+
return this.httpPost(`/token`, authCredentials, options);
44+
}
45+
credentials(options?: RequestOptionsArgs): Observable<Response> {
46+
return this.httpPost(`/token/validate`, {}, options);
47+
}
48+
}
49+

src/Loaders.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ import { stripTrailingSlash } from './utils';
33

44
export abstract class WpApiLoader {
55
abstract getWebServiceUrl(postfix: string): string;
6+
abstract baseUrl: string;
7+
abstract namespace: string;
68
}
79

810
export class WpApiStaticLoader implements WpApiLoader {
911
completeUrl: string;
1012
constructor(
1113
private http: Http,
12-
private baseUrl: string = 'http://changeYourDomainHere.com/wp-json',
13-
private namespace: string = '/wp/v2'
14+
private _baseUrl: string = 'http://changeYourDomainHere.com/wp-json',
15+
private _namespace: string = '/wp/v2'
1416
) {
1517
this.completeUrl = `${stripTrailingSlash(this.baseUrl)}${this.namespace}`;
1618
}
1719

20+
set baseUrl(val: string): void { }
21+
get baseUrl(): string { return this._baseUrl; }
22+
23+
set namespace(val: string): void {}
24+
get namespace(): string { return this._namespace; }
25+
1826
public getWebServiceUrl(postfix: string): string {
1927
return `${this.completeUrl}${postfix}`
2028
}

src/Parent.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,26 @@ export class WpApiParent implements IParent {
2727
public http: Http
2828
) { }
2929

30+
protected getWebServiceUrl(postfix: string): string {
31+
return this.wpApiLoader.getWebServiceUrl(url);
32+
}
33+
3034
httpGet(url: string, options = {}) {
31-
return this.http.get(this.wpApiLoader.getWebServiceUrl(url), options);
35+
return this.http.get(this.getWebServiceUrl(url), options);
3236
}
3337
httpHead(url: string, options = {}) {
34-
return this.http.head(this.wpApiLoader.getWebServiceUrl(url), options);
38+
return this.http.head(this.getWebServiceUrl(url), options);
3539
}
3640
httpDelete(url: string, options = {}) {
37-
return this.http.delete(this.wpApiLoader.getWebServiceUrl(url), options);
41+
return this.http.delete(this.getWebServiceUrl(url), options);
3842
}
3943
httpPost(url: string, body = {}, options = {}) {
40-
return this.http.post(this.wpApiLoader.getWebServiceUrl(url), body, options);
44+
return this.http.post(this.getWebServiceUrl(url), body, options);
4145
}
4246
httpPut(url: string, body = {}, options = {}) {
43-
return this.http.put(this.wpApiLoader.getWebServiceUrl(url), body, options);
47+
return this.http.put(this.getWebServiceUrl(url), body, options);
4448
}
4549
httpPatch(url: string, body = {}, options = {}) {
46-
return this.http.patch(this.wpApiLoader.getWebServiceUrl(url), body, options);
50+
return this.http.patch(this.getWebServiceUrl(url), body, options);
4751
}
4852
}

src/wp-api-angular.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { WpApiTaxonomies } from './Taxonomies';
1616
import { WpApiStatuses } from './Statuses';
1717
import { WpApiTerms } from './Terms';
1818
import { WpApiCustom } from './Custom';
19+
import { WpApiAuth } from './Auth';
1920
import { WpApiLoader, WpApiStaticLoader } from './Loaders';
2021

2122
export { WpApiPosts } from './Posts';
@@ -28,6 +29,7 @@ export { WpApiTaxonomies } from './Taxonomies';
2829
export { WpApiStatuses } from './Statuses';
2930
export { WpApiTerms } from './Terms';
3031
export { WpApiCustom } from './Custom';
32+
export { WpApiAuth } from './Auth';
3133
export { WpApiLoader, WpApiStaticLoader } from './Loaders';
3234

3335
export function WpApiLoaderFactory(http: Http) {
@@ -51,7 +53,8 @@ export function WpApiLoaderFactory(http: Http) {
5153
WpApiTaxonomies,
5254
WpApiStatuses,
5355
WpApiTerms,
54-
WpApiCustom
56+
WpApiCustom,
57+
WpApiAuth
5558
]
5659
})
5760
export class WpApiModule {

0 commit comments

Comments
 (0)