Skip to content

Commit

Permalink
Merge pull request #55 from mozilla-services/fetch-server-settings
Browse files Browse the repository at this point in the history
Fixes #41 - Added Api#fetchServerSettings.
  • Loading branch information
n1k0 committed Jul 13, 2015
2 parents 2c17c51 + ea48889 commit e17ee10
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 135 deletions.
2 changes: 1 addition & 1 deletion demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function main() {
var db = new Kinto({
remote: "https://kinto.dev.mozaws.net/v1",
remote: "http://0.0.0.0:8888/v1",
headers: {Authorization: "Basic " + btoa("user:pass")}
});
var tasks = db.collection("tasks");
Expand Down
60 changes: 46 additions & 14 deletions dist/kinto.dev.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kinto.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 46 additions & 14 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default class Api {
throw new Error("Invalid remote URL: " + remote);
this.remote = remote;
this.optionHeaders = options.headers;
this.serverSettings = null;
try {
this.version = remote.match(/\/(v\d+)\/?$/)[1];
} catch (err) {
Expand All @@ -62,7 +63,7 @@ export default class Api {
endpoints(options={fullUrl: true}) {
var root = options.fullUrl ? this.remote : `/${this.version}`;
var urls = {
root: () => root,
root: () => `${root}/`,
batch: () => `${root}/batch`,
bucket: (bucket) => `${root}/buckets/${bucket}`,
collection: (bucket, coll) => `${urls.bucket(bucket)}/collections/${coll}`,
Expand All @@ -72,6 +73,33 @@ export default class Api {
return urls;
}

/**
* Retrieves Kinto server settings.
*
* @return {Promise}
*/
fetchServerSettings() {
if (this.serverSettings)
return Promise.resolve(this.serverSettings);
var response;
const errPrefix = "Fetching server settings failed";
return fetch(this.endpoints().root(), {
headers: DEFAULT_REQUEST_HEADERS
})
.then(res => {
response = res;
return res.json();
})
.catch(err => {
const httpStatus = response && response.status || 0;
throw new Error(`${errPrefix}: HTTP ${httpStatus}; ${err}`);
})
.then(json => {
this.serverSettings = json.settings;
return this.serverSettings;
});
}

/**
* Fetches latest changes from the remote server.
*
Expand All @@ -95,7 +123,8 @@ export default class Api {
headers["If-None-Match"] = quote(options.lastModified);
}

return fetch(recordsUrl + queryString, {headers})
return this.fetchServerSettings()
.then(_ => fetch(recordsUrl + queryString, {headers}))
.then(res => {
response = res;
// If HTTP 304, nothing has changed
Expand Down Expand Up @@ -180,25 +209,28 @@ export default class Api {
};
if (!records.length)
return Promise.resolve(results);
return fetch(this.endpoints().batch(), {
method: "POST",
headers: headers,
body: JSON.stringify({
defaults: {headers},
requests: records.map(record => {
const path = this.endpoints({full: false})
.record(bucketName, collName, record.id);
return this._buildRecordBatchRequest(record, path, safe);
})
return this.fetchServerSettings()
.then(serverSettings => {
return fetch(this.endpoints().batch(), {
method: "POST",
headers: headers,
body: JSON.stringify({
defaults: {headers},
requests: records.map(record => {
const path = this.endpoints({full: false})
.record(bucketName, collName, record.id);
return this._buildRecordBatchRequest(record, path, safe);
})
})
});
})
})
.then(res => {
response = res;
return res.json();
})
.catch(err => {
const httpStatus = response && response.status || 0;
throw new Error(`${errPrefix}: HTTP ${httpStatus}; ${err}`);
throw new Error(`${errPrefix} HTTP ${httpStatus}; ${err}`);
})
.then(json => {
// Handle main request errors
Expand Down

0 comments on commit e17ee10

Please sign in to comment.