Skip to content

Commit

Permalink
Can set global custom headers and headers for each call
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidoz committed Aug 25, 2016
1 parent 3e67835 commit bef14f3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
26 changes: 26 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ A lightweight Angular 2 adapter for [JSON API](http://jsonapi.org/)
- [Querying for Multiple Records](#querying-for-multiple-records)
- [Retrieving a Single Record](#retrieving-a-single-record)
- [Creating Records](#creating-records)
- [Custom Headers](#custom-headers)
- [TODO](#todo)
- [Development](#development)
- [License](#licence)
Expand Down Expand Up @@ -201,6 +202,31 @@ this.datastore.createRecord(Comment, {
);
```

### Custom Headers

By default, the library adds these headers, according to the [JSON API MIME Types](http://jsonapi.org/#mime-types):

```
Accept: application/vnd.api+json
Content-Type: application/vnd.api+json
```

You can also add your custom headers to be appended to each http call:

```typescript
this.datastore.headers = new Headers({'Authorization': 'Bearer ' + accessToken});
```

Or you can pass the headers as last argument of any datastore call method:

```typescript
this.datastore.createRecord(Post, {
title: 'My post',
content: 'My content'
}, new Headers({'Authorization': 'Bearer ' + accessToken}));
```


## TODO
- Implement everything from the [JSON API](http://jsonapi.org/) specific.
- Add unit testing and E2E testing
Expand Down
28 changes: 20 additions & 8 deletions src/services/json-api-datastore.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,33 @@ import { JsonApiModel } from '../models/json-api.model';
export class JsonApiDatastore {

private http: Http;
private _headers: Headers;

constructor() {
let injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
this.http = injector.get(Http);
}

query(type: { new(data: any): JsonApiModel; }, params?: any): Observable<JsonApiModel[]> {
let options = this.getOptions();
query(type: { new(data: any): JsonApiModel; }, params?: any, headers?: Headers): Observable<JsonApiModel[]> {
let options = this.getOptions(headers);
let url = this.buildUrl(type, params);
return this.http.get(url, options)
.map((res: any) => this.extractQueryData(res, type))
.catch((res: any) => this.handleError(res));
}

findRecord(type: { new(data: any): JsonApiModel; }, id: number, params?: any): Observable<JsonApiModel> {
let options = this.getOptions();
findRecord(type: { new(data: any): JsonApiModel; }, id: number, params?: any, headers?: Headers): Observable<JsonApiModel> {
let options = this.getOptions(headers);
let url = this.buildUrl(type, params, id);
return this.http.get(url, options)
.map((res: any) => this.extractRecordData(res, type))
.catch((res: any) => this.handleError(res));
}

createRecord(type: { new(data: any): JsonApiModel; }, data?: any) {
createRecord(type: { new(data: any): JsonApiModel; }, data?: any, headers?: Headers) {
let typeName = Reflect.getMetadata('JsonApiModelConfig', type).type;
let baseUrl = Reflect.getMetadata('JsonApiDatastoreConfig', this.constructor).baseUrl;
let options = this.getOptions();
let options = this.getOptions(headers);
let relationships: any;
for (let key in data) {
if (data.hasOwnProperty(key)) {
Expand Down Expand Up @@ -62,6 +63,10 @@ export class JsonApiDatastore {
.catch((res: any) => this.handleError(res))
}

set headers(headers: Headers){
this._headers = headers;
}

private buildUrl(type: { new(data: any): JsonApiModel; }, params: any = {}, id?: number){
let typeName = Reflect.getMetadata('JsonApiModelConfig', type).type;
if (params.include && typeof params.include === 'function') {
Expand Down Expand Up @@ -101,8 +106,15 @@ export class JsonApiDatastore {
return Observable.throw(errMsg);
}

private getOptions() {
let headers = new Headers({ 'Accept': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json' });
private getOptions(customHeaders?: Headers) {
let headers: Headers = this._headers ? this._headers : new Headers();
headers.append('Accept', 'application/vnd.api+json');
headers.append('Content-Type', 'application/vnd.api+json');
if (customHeaders) {
customHeaders.forEach(function(values, name){
headers.append(name, values[0]);
});
}
return new RequestOptions({ headers: headers });
}

Expand Down

0 comments on commit bef14f3

Please sign in to comment.