Skip to content

Commit d53038d

Browse files
authored
Merge pull request #7 from wadetandy/master
Allow arbitrary fetch() options to be specified
2 parents ef6d534 + bba3584 commit d53038d

File tree

5 files changed

+54
-30
lines changed

5 files changed

+54
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ lib
66
lib-esm
77
_bundles
88
yarn.lock
9+
yarn-error.log

src/model.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ export default class Model {
7070
}
7171
}
7272

73+
static fetchOptions() : RequestInit {
74+
let options = {
75+
headers: {
76+
Accept: 'application/json',
77+
['Content-Type']: 'application/json'
78+
} as any
79+
}
80+
81+
if (this.getJWT()) {
82+
options.headers.Authorization = `Token token="${this.getJWT()}"`;
83+
}
84+
85+
return options
86+
}
87+
7388
static getJWTOwner() : typeof Model {
7489
if (this.isJWTOwner) {
7590
return this;

src/request.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,27 @@ import Config from './configuration';
22
import colorize from './util/colorize';
33

44
export default class Request {
5-
get(url : string, options: Object) : Promise<any> {
6-
options['method'] = 'GET';
5+
get(url : string, options: RequestInit) : Promise<any> {
6+
options.method = 'GET';
77
return this._fetchWithLogging(url, options);
88
}
99

10-
post(url: string, payload: Object, options: Object) : Promise<any> {
11-
options['method'] = 'POST';
12-
options['body'] = JSON.stringify(payload);
10+
post(url: string, payload: Object, options: RequestInit) : Promise<any> {
11+
options.method = 'POST';
12+
options.body = JSON.stringify(payload);
1313

1414
return this._fetchWithLogging(url, options);
1515
}
1616

17-
put(url: string, payload: Object, options: Object) : Promise<any> {
18-
options['method'] = 'PUT';
19-
options['body'] = JSON.stringify(payload);
17+
put(url: string, payload: Object, options: RequestInit) : Promise<any> {
18+
options.method = 'PUT';
19+
options.body = JSON.stringify(payload);
2020

2121
return this._fetchWithLogging(url, options);
2222
}
2323

24-
delete(url: string, options: Object) : Promise<any> {
25-
options['method'] = 'DELETE';
24+
delete(url: string, options: RequestInit) : Promise<any> {
25+
options.method = 'DELETE';
2626
return this._fetchWithLogging(url, options);
2727
}
2828

@@ -36,40 +36,26 @@ export default class Request {
3636
Config.logger.debug(colorize('bold', JSON.stringify(responseJSON, null, 4)));
3737
}
3838

39-
private _fetchWithLogging(url: string, options: Object) : Promise<any> {
40-
this._logRequest(options['method'], url);
39+
private _fetchWithLogging(url: string, options: RequestInit) : Promise<any> {
40+
this._logRequest(options.method, url);
4141
let promise = this._fetch(url, options);
4242
promise.then((response : any) => {
4343
this._logResponse(response['jsonPayload']);
4444
});
4545
return promise;
4646
}
4747

48-
private _fetch(url: string, options: Object) : Promise<any> {
48+
private _fetch(url: string, options: RequestInit) : Promise<any> {
4949
return new Promise((resolve, reject) => {
50-
let headers = this.buildHeaders(options);
51-
options['headers'] = headers;
52-
5350
let fetchPromise = fetch(url, options);
5451
fetchPromise.then((response) => {
5552
response.json().then((json) => {
5653
response['jsonPayload'] = json;
5754
resolve(response);
5855
}).catch((e) => { throw(e); });
5956
});
57+
6058
fetchPromise.catch(reject);
6159
});
6260
}
63-
64-
private buildHeaders(options: Object) : any {
65-
let headers = {};
66-
headers['Accept'] = 'application/json';
67-
headers['Content-Type'] = 'application/json';
68-
69-
if (options['jwt']) {
70-
headers['Authorization'] = `Token token="${options['jwt']}"`;
71-
}
72-
73-
return headers;
74-
}
7561
}

src/scope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ export default class Scope {
219219
url = `${url}?${qp}`;
220220
}
221221
let request = new Request();
222-
let jwt = this.model.getJWT();
222+
let fetchOpts = this.model.fetchOptions()
223223

224-
return request.get(url, { jwt }).then((response) => {
224+
return request.get(url, fetchOpts).then((response) => {
225225
let jwtHeader = response.headers.get('X-JWT');
226226
if (jwtHeader) {
227227
this.model.setJWT(jwtHeader);

test/unit/model-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,28 @@ describe('Model', function() {
134134
});
135135
});
136136

137+
describe('#fetchOptions', function() {
138+
context('jwt is set', function() {
139+
beforeEach(function() {
140+
ApplicationRecord.jwt = 'g3tm3';
141+
});
142+
143+
afterEach(function() {
144+
ApplicationRecord.jwt = null;
145+
});
146+
147+
it('sets the auth header', function() {
148+
expect(Author.fetchOptions().headers.Authorization).to.eq('Token token="g3tm3"');
149+
});
150+
})
151+
152+
it('includes the content headers', function() {
153+
let headers = Author.fetchOptions().headers
154+
expect(headers.Accept).to.eq('application/json')
155+
expect(headers['Content-Type']).to.eq('application/json')
156+
})
157+
})
158+
137159
describe('#isType', function() {
138160
it('checks the jsonapiType of class', function() {
139161
instance = new Author()

0 commit comments

Comments
 (0)