-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* http module draft * http module docs * ninja add single run flag to npm test * added general request method to IHttp interface * fixed index.ts with file renamed (rest.service -> http-rest.service) * added general request method to http-interceptor * updated readme * updated docs with request method * added typescript readme highlight, rearranged readme and added note for constructor restservice
- Loading branch information
1 parent
b213c7d
commit 2093930
Showing
16 changed files
with
584 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<md-card> | ||
<md-card-title>HttpInterceptorService</md-card-title> | ||
<md-card-subtitle>How to use this service</md-card-subtitle> | ||
<md-divider></md-divider> | ||
<md-card-content> | ||
<h2><code>HttpInterceptorService</code></h2> | ||
<p>Service provided with methods that wrap the ng2 [Http] service and provide an easier experience for interceptor implementation.</p> | ||
<p>To add a desired interceptor, it needs to implement the [IHttpInterceptor] interface.</p> | ||
<td-highlight lang="typescript"> | ||
export interface IHttpInterceptor { | ||
onRequest?: (requestOptions: RequestOptionsArgs) => RequestOptionsArgs; | ||
onResponse?: (response: Response) => Response; | ||
onResponseError?: (error: Response) => Response; | ||
} | ||
</td-highlight> | ||
<h3>Methods:</h3> | ||
<p>The [HttpInterceptorService] service has {{interceptorServiceMethods.length}} properties:</p> | ||
<md-list> | ||
<template let-attr let-last="attr" ngFor [ngForOf]="interceptorServiceMethods"> | ||
<a md-list-item layout-align="row"> | ||
<h3 md-line> {{attr.name}}: <span>{{attr.type}}</span></h3> | ||
<p md-line> {{attr.description}} </p> | ||
</a> | ||
<md-divider *ngIf="!last"></md-divider> | ||
</template> | ||
</md-list> | ||
<h3>Example:</h3> | ||
<p>Typescript:</p> | ||
<td-highlight lang="typescript"> | ||
<![CDATA[ | ||
import { Injectable } from '@angular/core'; | ||
import { IHttpInterceptor } from '@covalent/http'; | ||
|
||
@Injectable() | ||
export class CustomInterceptor implements IHttpInterceptor { | ||
|
||
onRequest(requestOptions: RequestOptionsArgs): RequestOptionsArgs { | ||
... // do something to requestOptions | ||
return requestOptions; | ||
} | ||
|
||
onResponse(response: Response): Response { | ||
... // check response status and do something | ||
return response; | ||
} | ||
|
||
onResponseError(error: Response): Response { | ||
... // check error status and do something | ||
return error; | ||
} | ||
} | ||
]]> | ||
</td-highlight> | ||
<p>Bootstrap interceptor providers:</p> | ||
<td-highlight lang="typescript"> | ||
<![CDATA[ | ||
import { HTTP_PROVIDERS } from '@angular/http'; | ||
import { provideInterceptors } from '@covalent/http'; | ||
import { CustomInterceptor } from 'dir/to/interceptor'; | ||
|
||
bootstrap(AppComponent, [ | ||
HTTP_PROVIDERS, | ||
provideInterceptors([CustomInterceptor]), | ||
]); | ||
]]> | ||
</td-highlight> | ||
<p>After that, just inject [HttpInterceptorService] and use it for your requests.</p> | ||
</md-card-content> | ||
</md-card> | ||
<md-card> | ||
<md-card-title>RESTService</md-card-title> | ||
<md-card-subtitle>How to use this class</md-card-subtitle> | ||
<md-divider></md-divider> | ||
<md-card-content> | ||
<h2><code>RESTService</code></h2> | ||
<p>Abstract class provided with methods that wraps http services to facilitate REST API calls.</p> | ||
<h3>Methods:</h3> | ||
<p>The <code>RESTService</code> class has {{restServiceMethods.length}} methods:</p> | ||
<md-list> | ||
<template let-attr let-last="attr" ngFor [ngForOf]="restServiceMethods"> | ||
<a md-list-item layout-align="row"> | ||
<h3 md-line> {{attr.name}}: <span>{{attr.type}}</span></h3> | ||
<p md-line> {{attr.description}} </p> | ||
</a> | ||
<md-divider *ngIf="!last"></md-divider> | ||
</template> | ||
</md-list> | ||
<h3>Example:</h3> | ||
<p>Typescript:</p> | ||
<td-highlight lang="typescript"> | ||
<![CDATA[ | ||
import { Injectable } from '@angular/core'; | ||
import { Response, Http } from '@angular/http'; | ||
import { RESTService, HttpInterceptorService } from '@covalent/http'; | ||
|
||
@Injectable() | ||
export class CustomRESTService extends RESTService<any> { | ||
|
||
constructor(private _http: Http /* or HttpInterceptorService */) { | ||
super(_http, { | ||
baseUrl: 'www.api.com', | ||
path: '/path/to/endpoint', | ||
transform: (res: Response): any => res.json(), | ||
}); | ||
} | ||
} | ||
]]> | ||
</td-highlight> | ||
<p>Note: the constructor takes any object that implements the methods in [IHttp] interface.</p> | ||
<p>This can be the angular2 [Http] service, the covalent [HttpInterceptorService] or a custom service.</p> | ||
<td-highlight lang="typescript"> | ||
<![CDATA[ | ||
export interface IHttp { | ||
delete: (url: string, options?: RequestOptionsArgs) => Observable<Response>; | ||
get: (url: string, options?: RequestOptionsArgs) => Observable<Response>; | ||
head: (url: string, options?: RequestOptionsArgs) => Observable<Response>; | ||
patch: (url: string, body: any, options?: RequestOptionsArgs) => Observable<Response>; | ||
post: (url: string, body: any, options?: RequestOptionsArgs) => Observable<Response>; | ||
put: (url: string, body: any, options?: RequestOptionsArgs) => Observable<Response>; | ||
request: (url: string | Request, options: RequestOptionsArgs) => Observable<Response>; | ||
} | ||
]]> | ||
</td-highlight> | ||
</md-card-content> | ||
</md-card> |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
beforeEach, | ||
addProviders, | ||
describe, | ||
expect, | ||
it, | ||
inject, | ||
} from '@angular/core/testing'; | ||
import { ComponentFixture, TestComponentBuilder } from '@angular/compiler/testing'; | ||
import { HttpDemoComponent } from './http.component'; | ||
|
||
describe('Component: MediaDemo', () => { | ||
let builder: TestComponentBuilder; | ||
|
||
beforeEach(() => { | ||
addProviders([ | ||
HttpDemoComponent, | ||
]); | ||
}); | ||
|
||
beforeEach(inject([TestComponentBuilder], function (tcb: TestComponentBuilder): void { | ||
builder = tcb; | ||
})); | ||
|
||
it('should inject the component', inject([HttpDemoComponent], (component: HttpDemoComponent) => { | ||
expect(component).toBeTruthy(); | ||
})); | ||
|
||
it('should create the component', inject([], () => { | ||
return builder.createAsync(HttpDemoComponent) | ||
.then((fixture: ComponentFixture<any>) => { | ||
let httpDemoComp: HttpDemoComponent = fixture.componentInstance; | ||
expect(httpDemoComp).toBeTruthy(); | ||
}); | ||
})); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
import { MD_CARD_DIRECTIVES } from '@angular2-material/card'; | ||
import { MD_LIST_DIRECTIVES } from '@angular2-material/list'; | ||
import { MdButton } from '@angular2-material/button'; | ||
import { MD_INPUT_DIRECTIVES } from '@angular2-material/input'; | ||
|
||
import { TdHighlightComponent } from '../../../../platform/highlight'; | ||
|
||
@Component({ | ||
directives: [ | ||
MD_CARD_DIRECTIVES, | ||
MD_LIST_DIRECTIVES, | ||
MdButton, | ||
MD_INPUT_DIRECTIVES, | ||
TdHighlightComponent, | ||
], | ||
moduleId: module.id, | ||
selector: 'http-demo', | ||
styleUrls: [ 'http.component.css' ], | ||
templateUrl: 'http.component.html', | ||
}) | ||
export class HttpDemoComponent { | ||
|
||
interceptorServiceMethods: Object[] = [{ | ||
description: `Uses underlying ng2 [http] to request a DELETE method to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'delete', | ||
type: 'function(url: string, options: RequestOptionsArgs)', | ||
}, { | ||
description: `Uses underlying ng2 [http] to request a GET method to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'get', | ||
type: 'function(url: string, options: RequestOptionsArgs)', | ||
}, { | ||
description: `Uses underlying ng2 [http] to request a HEAD method to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'head', | ||
type: 'function(url: string, options: RequestOptionsArgs)', | ||
}, { | ||
description: `Uses underlying ng2 [http] to request a PATCH method to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'patch', | ||
type: 'function(url: string, data: any, options: RequestOptionsArgs)', | ||
}, { | ||
description: `Uses underlying ng2 [http] to request a POST method to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'post', | ||
type: 'function(url: string, data: any, options: RequestOptionsArgs)', | ||
}, { | ||
description: `Uses underlying ng2 [http] to request a PUT method to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'put', | ||
type: 'function(url: string, data: any, options: RequestOptionsArgs)', | ||
}, { | ||
description: `Uses underlying ng2 [http] to request a generic request to a URL, | ||
executing the interceptors as part of the request pipeline.`, | ||
name: 'request', | ||
type: 'function(url: string | Request, options: RequestOptionsArgs)', | ||
}]; | ||
|
||
restServiceMethods: Object[] = [{ | ||
description: `Creates a GET request to the generated endpoint URL.`, | ||
name: 'query', | ||
type: 'function(query?: IRestQuery)', | ||
}, { | ||
description: `Creates a GET request to the generated endpoint URL, adding the ID at the end.`, | ||
name: 'get', | ||
type: 'function(id: string | number)', | ||
}, { | ||
description: `Creates a POST request to the generated endpoint URL.`, | ||
name: 'create', | ||
type: 'function(obj: T)', | ||
}, { | ||
description: `Creates a PATCH request to the generated endpoint URL, adding the ID at the end.`, | ||
name: 'update', | ||
type: 'function(id: string | number, obj: T)', | ||
}, { | ||
description: `Creates a DELETE request to the generated endpoint URL, adding the ID at the end.`, | ||
name: 'delete', | ||
type: 'function(id: string | number)', | ||
}, { | ||
description: `Builds the endpoint URL with the configured properties and arguments passed in the method.`, | ||
name: 'buildUrl', | ||
type: 'function(id?: string | number, query?: IRestQuery)', | ||
}]; | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { HttpDemoComponent } from './http.component'; |
This file contains 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
This file contains 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
Oops, something went wrong.