Skip to content

Commit

Permalink
Merge pull request #98 from mozilla-services/cors-option
Browse files Browse the repository at this point in the history
Fixes #91 - Added corsMode option.
  • Loading branch information
n1k0 committed Jul 28, 2015
2 parents 3afc876 + fe86d6f commit 1deefd2
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 43 deletions.
33 changes: 21 additions & 12 deletions demo/kinto.dev.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions demo/kinto.min.js

Large diffs are not rendered by default.

33 changes: 21 additions & 12 deletions dist/kinto.dev.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/kinto.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const db = new Kinto(options);
- `remote`: The remote Kinto server endpoint root URL (eg. `"https://server/v1"`). Not that you *must* define a URL matching the version of the protocol the client supports, otherwise you'll get an error;
- `headers`: The default headers to pass for every HTTP request performed to the Cliquet server (eg. `{"Authorization": "Basic bWF0Og=="}`);
- `adapter`: The persistence layer adapter to use for saving data locally (default: `Kinto.adapters.IDB`); alternatively, a `Kinto.adapters.LocalStorage` adapter is also provided; last, if you plan on writing your own adapter, you can read more about how to do so in the [Extending Kinto.js](extending.md) section.
- `requestMode`: The HTTP [CORS](https://fetch.spec.whatwg.org/#concept-request-mode) mode. Default: `cors`.

## Collections

Expand Down
7 changes: 4 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ export default class Api {
* Constructor.
*
* Options:
* - {Object} headers: The key-value headers to pass to each request.
* - {EventEmitter} events: The events handler.
* - {Object} headers The key-value headers to pass to each request.
* - {EventEmitter} events The events handler.
* - {String} events The HTTP request mode.
*
* @param {String} remote The remote URL.
* @param {Object} options The options object.
Expand All @@ -49,7 +50,7 @@ export default class Api {
}
if (this.version !== SUPPORTED_PROTOCOL_VERSION)
throw new Error(`Unsupported protocol version: ${this.version}`);
this.http = new HTTP({events: this.events});
this.http = new HTTP({events: this.events, requestMode: options.requestMode});
this._registerHTTPEvents();
}

Expand Down
14 changes: 10 additions & 4 deletions src/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ export default class HTTP {
* Constructor.
*
* Options:
* - {EventEmitter} events Events handler.
* - {EventEmitter} events Events handler.
* - {String} requestMode The HTTP request mode (default: "cors").
*
* @param {Object} options The options object.
*/
constructor(options={}) {
constructor(options={requestMode: "cors"}) {
// public properties
this.events = options.events || new EventEmitter();
this.requestMode = options.requestMode;
}

/**
* Performs an HTTP request to the Kinto server. Resolves with an objet
* containing the following properties:
* Performs an HTTP request to the Kinto server.
*
* Options:
* - {Object} headers The request headers object (default: {})
*
* Resolves with an objet containing the following HTTP response properties:
* - {Number} status The HTTP status code.
* - {Object} json The JSON response body.
* - {Headers} headers The response headers object; see the ES6 fetch() spec.
Expand All @@ -43,6 +48,7 @@ export default class HTTP {
var response, status, statusText, headers;
// Ensure default request headers are always set
options.headers = Object.assign({}, HTTP.DEFAULT_REQUEST_HEADERS, options.headers);
options.mode = this.requestMode;
return fetch(url, options)
.then(res => {
response = res;
Expand Down
14 changes: 8 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export default class Kinto {
* Constructor.
*
* Options:
* - {String} bucket The collection bucket name.
* - {EventEmitter} events Events handler.
* - {BaseAdapter} adapter The base DB adapter class.
* - {String} bucket The collection bucket name.
* - {EventEmitter} events Events handler.
* - {BaseAdapter} adapter The base DB adapter class.
* - {String} requestMode The HTTP CORS mode to use.
*
* @param {Object} options The options object.
*/
Expand All @@ -58,13 +59,14 @@ export default class Kinto {

const bucket = this._options.bucket || DEFAULT_BUCKET_NAME;
const api = new Api(this._options.remote || "http://localhost:8888/v1", {
headers: this._options.headers || {},
events: this.events,
headers: this._options.headers || {},
events: this.events,
requestMode: this._options.requestMode,
});

if (!this._collections.hasOwnProperty(collName)) {
this._collections[collName] = new Collection(bucket, collName, api, {
events: this.events,
events: this.events,
adapter: this._options.adapter || Kinto.adapters.IDB,
});
}
Expand Down
6 changes: 6 additions & 0 deletions test/api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ describe("Api", () => {
expect(() => new Api(`http://test/v999`))
.to.Throw(Error, /^Unsupported protocol version/);
});

it("should propagate the requestMode option to the child HTTP instance", () => {
const requestMode = "no-cors";
expect(new Api(`http://test/${SPV}`, {requestMode}).http.requestMode)
.eql(requestMode);
});
});

describe("get backoff()", () => {
Expand Down
24 changes: 24 additions & 0 deletions test/http_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ describe("HTTP class", () => {
it("should create an events property if none passed", () => {
expect(new HTTP().events).to.be.an.instanceOf(EventEmitter);
});

it("should accept a requestMode option", () => {
expect(new HTTP({requestMode: "no-cors"}).requestMode).eql("no-cors");
});
});

describe("#request()", () => {
Expand All @@ -56,6 +60,26 @@ describe("HTTP class", () => {
});
});

describe("Request CORS mode", () => {
beforeEach(() => {
sandbox.stub(root, "fetch").returns(fakeServerResponse(200, {}, {}));
});

it("should use default CORS mode", () => {
new HTTP().request("/");

expect(fetch.firstCall.args[1].mode)
.eql("cors");
});

it("should use configured custom CORS mode", () => {
new HTTP({requestMode: "no-cors"}).request("/");

expect(fetch.firstCall.args[1].mode)
.eql("no-cors");
});
});

describe("Succesful request", () => {
beforeEach(() => {
sandbox.stub(root, "fetch").returns(
Expand Down
6 changes: 6 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe("Kinto", () => {
expect(kinto.collection("x").api.events).eql(kinto.events);
expect(kinto.collection("x").api.http.events).eql(kinto.events);
});

it("should propagate the requestMode option to child dependencies", () => {
const requestMode = "no-cors";
expect(new Kinto({requestMode}).collection("x").api.http.requestMode)
.eql(requestMode);
});
});

describe("#collection()", () => {
Expand Down

0 comments on commit 1deefd2

Please sign in to comment.