Skip to content

Commit bef14f3

Browse files
committed
Can set global custom headers and headers for each call
1 parent 3e67835 commit bef14f3

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

README.MD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ A lightweight Angular 2 adapter for [JSON API](http://jsonapi.org/)
1212
- [Querying for Multiple Records](#querying-for-multiple-records)
1313
- [Retrieving a Single Record](#retrieving-a-single-record)
1414
- [Creating Records](#creating-records)
15+
- [Custom Headers](#custom-headers)
1516
- [TODO](#todo)
1617
- [Development](#development)
1718
- [License](#licence)
@@ -201,6 +202,31 @@ this.datastore.createRecord(Comment, {
201202
);
202203
```
203204

205+
### Custom Headers
206+
207+
By default, the library adds these headers, according to the [JSON API MIME Types](http://jsonapi.org/#mime-types):
208+
209+
```
210+
Accept: application/vnd.api+json
211+
Content-Type: application/vnd.api+json
212+
```
213+
214+
You can also add your custom headers to be appended to each http call:
215+
216+
```typescript
217+
this.datastore.headers = new Headers({'Authorization': 'Bearer ' + accessToken});
218+
```
219+
220+
Or you can pass the headers as last argument of any datastore call method:
221+
222+
```typescript
223+
this.datastore.createRecord(Post, {
224+
title: 'My post',
225+
content: 'My content'
226+
}, new Headers({'Authorization': 'Bearer ' + accessToken}));
227+
```
228+
229+
204230
## TODO
205231
- Implement everything from the [JSON API](http://jsonapi.org/) specific.
206232
- Add unit testing and E2E testing

src/services/json-api-datastore.service.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,33 @@ import { JsonApiModel } from '../models/json-api.model';
99
export class JsonApiDatastore {
1010

1111
private http: Http;
12+
private _headers: Headers;
1213

1314
constructor() {
1415
let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
1516
this.http = injector.get(Http);
1617
}
1718

18-
query(type: { new(data: any): JsonApiModel; }, params?: any): Observable<JsonApiModel[]> {
19-
let options = this.getOptions();
19+
query(type: { new(data: any): JsonApiModel; }, params?: any, headers?: Headers): Observable<JsonApiModel[]> {
20+
let options = this.getOptions(headers);
2021
let url = this.buildUrl(type, params);
2122
return this.http.get(url, options)
2223
.map((res: any) => this.extractQueryData(res, type))
2324
.catch((res: any) => this.handleError(res));
2425
}
2526

26-
findRecord(type: { new(data: any): JsonApiModel; }, id: number, params?: any): Observable<JsonApiModel> {
27-
let options = this.getOptions();
27+
findRecord(type: { new(data: any): JsonApiModel; }, id: number, params?: any, headers?: Headers): Observable<JsonApiModel> {
28+
let options = this.getOptions(headers);
2829
let url = this.buildUrl(type, params, id);
2930
return this.http.get(url, options)
3031
.map((res: any) => this.extractRecordData(res, type))
3132
.catch((res: any) => this.handleError(res));
3233
}
3334

34-
createRecord(type: { new(data: any): JsonApiModel; }, data?: any) {
35+
createRecord(type: { new(data: any): JsonApiModel; }, data?: any, headers?: Headers) {
3536
let typeName = Reflect.getMetadata('JsonApiModelConfig', type).type;
3637
let baseUrl = Reflect.getMetadata('JsonApiDatastoreConfig', this.constructor).baseUrl;
37-
let options = this.getOptions();
38+
let options = this.getOptions(headers);
3839
let relationships: any;
3940
for (let key in data) {
4041
if (data.hasOwnProperty(key)) {
@@ -62,6 +63,10 @@ export class JsonApiDatastore {
6263
.catch((res: any) => this.handleError(res))
6364
}
6465

66+
set headers(headers: Headers){
67+
this._headers = headers;
68+
}
69+
6570
private buildUrl(type: { new(data: any): JsonApiModel; }, params: any = {}, id?: number){
6671
let typeName = Reflect.getMetadata('JsonApiModelConfig', type).type;
6772
if (params.include && typeof params.include === 'function') {
@@ -101,8 +106,15 @@ export class JsonApiDatastore {
101106
return Observable.throw(errMsg);
102107
}
103108

104-
private getOptions() {
105-
let headers = new Headers({ 'Accept': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json' });
109+
private getOptions(customHeaders?: Headers) {
110+
let headers: Headers = this._headers ? this._headers : new Headers();
111+
headers.append('Accept', 'application/vnd.api+json');
112+
headers.append('Content-Type', 'application/vnd.api+json');
113+
if (customHeaders) {
114+
customHeaders.forEach(function(values, name){
115+
headers.append(name, values[0]);
116+
});
117+
}
106118
return new RequestOptions({ headers: headers });
107119
}
108120

0 commit comments

Comments
 (0)