From 4a7066487b30ccb7867c41cc77e6436451f413ad Mon Sep 17 00:00:00 2001 From: Alec Henninger Date: Sun, 26 Apr 2015 18:22:21 +0000 Subject: [PATCH] Combine everything in one bundle for now --- README.md | 26 ++++++++++++++++---------- lib/client.js | 6 ++++++ lib/lightblue.js | 46 ++++++++++++++++++++++++++++------------------ lib/nglightblue.js | 17 ++++++++++------- package.json | 5 ++--- test/data_test.js | 4 ++-- 6 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 lib/client.js diff --git a/README.md b/README.md index 658afb9..f4e0621 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ Write... ``` ### Browserify (CommonJS) or RequireJS (AMD) + ```js // commonjs var lightblue = require("lightblue"); @@ -35,9 +36,17 @@ require(["lightblue"], function(lightblue) { }); ``` +Once you have a `lightblue` object, you can get a client: + +```js +// Assumes /data and /metadata for data and metadata services respectively, +// but you can override. +var client = lightblue.getClient("http://my.lightblue.host.com/rest"); +``` + ### AngularJS -Include `dist/ng.lightblue.min.js` for a "lightblue" angular module which -defines a `lightblueProvider` (and therefore a `lightblue` service). +If angular is detected, a "lightblue" module will be registered with a +"lightblue" service as the client. ```js var app = angular.module("app", ["lightblue"]); @@ -46,23 +55,20 @@ app.config(["lightblueProvider", function(lightblueProvider) { lightblueProvider.setHost("http://my.lightblue.com"); }]); -app.controller("ctrl", ["lightblue", function(lightblue) { - lightblue.data.find(...) +app.controller("ctrl", ["lightblue", function(lightblueClient) { + lightblueClient.data.find(...) .then(...); }]); ``` -**At the moment you will also need to include the global lightblue namespace -via standard "lightblue.min.js" to get query builder API and such. See +**At the moment you will also need to use the global "lightblue" namespace if +you want query builder API. So don't name your client variable `lightblue` +just yet. See [issue #9](https://github.com/alechenninger/lightblue.js/issues/9).** ## Construct a find request ```javascript -// Assumes /data and /metadata for data and metadata services respectively, -// but you can override. -// If you're using the angular module, the client is the `lightblue` service. -var client = lightblue.getClient("http://my.lightblue.host.com/rest"); var field = lightblue.field; var find = client.data.find({ diff --git a/lib/client.js b/lib/client.js new file mode 100644 index 0000000..98d269f --- /dev/null +++ b/lib/client.js @@ -0,0 +1,6 @@ +module.exports = LightblueClient; + +function LightblueClient(dataClient, metadataClient) { + this.data = dataClient; + this.metadata = metadataClient; +} \ No newline at end of file diff --git a/lib/lightblue.js b/lib/lightblue.js index 72015db..b46bf83 100644 --- a/lib/lightblue.js +++ b/lib/lightblue.js @@ -1,49 +1,59 @@ -var DataClient = require("./data.js"); -var MetadataClient = require("./metadata.js"); -var query = require("./query.js"); +var DataClient = require("./data.js"); +var MetadataClient = require("./metadata.js"); +var LightblueClient = require("./client.js"); +var query = require("./query.js"); +var NodeHttpClient = require("./nodehttp.js"); + +// TODO: reorganize modules +// Install angular module if angular is present +require("./nglightblue.js"); var resolve = require("./clientutil.js").resolve; -// TODO: Refactor exports and modules usage exports.getDataClient = getDataClient; exports.getMetadataClient = getMetadataClient; exports.getClient = getClient; exports.field = query.field; -function LightblueClient(dataClient, metadataClient) { - this.data = dataClient; - this.metadata = metadataClient; -} - /** * Returns a LightblueDataClient. * @param {String} dataHost The full path for the base Lightblue data REST * context. * @return {LightblueDataClient} */ -function getDataClient(httpClient, dataHost) { - return new DataClient(httpClient, dataHost); +function getDataClient(dataHost, options) { + return new DataClient(getHttpClient(options), dataHost); } -function getMetadataClient(httpClient, metadataHost) { - return new MetadataClient(httpClient, metadataHost); +function getMetadataClient(metadataHost, options) { + return new MetadataClient(getHttpClient(options), metadataHost); } /** * Returns a LightblueClient. * @param {String} dataHost The full path for the base Lightblue data service. - * @param {String} metadataHost The full path for the base Lightblue metadata + * @param {String=} metadataHost The full path for the base Lightblue metadata * service. + * @param {Object=} options Configuration for http client. * @param {HttpClient} http An http client. */ -function getClient(httpClient, dataHost, metadataHost) { - if (typeof metadataHost === "undefined") { +function getClient(dataHost, metadataHost, options) { + // metadataHost and options are optional + if (typeof metadataHost != "string") { + if (typeof metadataHost == "object") { + options = metadataHost; + } + dataHost = resolve(dataHost, "data"); metadataHost = resolve(dataHost, "metadata"); } - var dataClient = getDataClient(httpClient, dataHost); - var metadataClient = getMetadataClient(httpClient, metadataHost); + var dataClient = getDataClient(dataHost, options); + var metadataClient = getMetadataClient(metadataHost, options); return new LightblueClient(dataClient, metadataClient); +} + +function getHttpClient(options) { + return new NodeHttpClient(options); } \ No newline at end of file diff --git a/lib/nglightblue.js b/lib/nglightblue.js index b9082a0..5400a92 100644 --- a/lib/nglightblue.js +++ b/lib/nglightblue.js @@ -23,9 +23,11 @@ * */ -var lightblue = require("./lightblue.js"); -var NgHttpClient = require("./nghttp.js"); -var url = require("url"); +var LightblueClient = require("./client.js"); +var DataClient = require("./data.js"); +var MetadataClient = require("./metadata.js"); +var NgHttpClient = require("./nghttp.js"); +var url = require("url"); if (typeof angular == "object" && angular != null && "module" in angular) { angular.module("lightblue", []) @@ -54,8 +56,9 @@ LightblueProvider.prototype.setMetadataHost = function(host) { }; LightblueProvider.prototype.$get = ["$http", function($http) { - return lightblue.getClient( - new NgHttpClient($http), - this._dataHost, - this._metadataHost); + var httpClient = new NgHttpClient($http); + var dataClient = new DataClient(httpClient, this._dataHost); + var metadataClient = new MetadataClient(httpClient, this._metadataHost); + + return new LightblueClient(dataClient, metadataClient); }]; \ No newline at end of file diff --git a/package.json b/package.json index 7ef5837..0d60abd 100644 --- a/package.json +++ b/package.json @@ -6,10 +6,9 @@ "type": "git", "url": "https://github.com/alechenninger/lightblue.js.git" }, + "main": "lib/lightblue.js", "scripts": { - "bundle_lightblue": "browserify lib/lightblue.js --standalone lightblue -d -p [minifyify --map lightblue.map.json --output dist/lightblue.map.json] > dist/lightblue.min.js", - "bundle_ng": "browserify lib/nglightblue.js --standalone nglightblue -d -p [minifyify --map nglightblue.map.json --output dist/nglightblue.map.json] > dist/nglightblue.min.js", - "bundle": "npm run bundle_lightblue && npm run bundle_ng", + "bundle": "browserify lib/lightblue.js --standalone lightblue -d -p [minifyify --map lightblue.map.json --output dist/lightblue.map.json] > dist/lightblue.min.js", "test": "mocha test/*", "tdd": "mocha -w test/*" }, diff --git a/test/data_test.js b/test/data_test.js index cabce66..0df07d0 100644 --- a/test/data_test.js +++ b/test/data_test.js @@ -1,6 +1,6 @@ /* globals describe it */ var expect = require("chai").expect; -var getDataClient = require("../lib/lightblue").getDataClient; +var DataClient = require("../lib/data.js"); describe("LightblueDataClient", function() { @@ -12,7 +12,7 @@ describe("LightblueDataClient", function() { } }; - var dataClient = getDataClient(mockHttpClient, "myhost.com"); + var dataClient = new DataClient(mockHttpClient, "myhost.com"); describe("find", function() { it("should construct urls like ${host}/find/${entity}/${version}", function() {