Skip to content

Commit

Permalink
Combine everything in one bundle for now
Browse files Browse the repository at this point in the history
  • Loading branch information
alechenninger committed Apr 26, 2015
1 parent 16925dc commit 4a70664
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 40 deletions.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Write...
```

### Browserify (CommonJS) or RequireJS (AMD)

```js
// commonjs
var lightblue = require("lightblue");
Expand All @@ -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"]);
Expand All @@ -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({
Expand Down
6 changes: 6 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = LightblueClient;

function LightblueClient(dataClient, metadataClient) {
this.data = dataClient;
this.metadata = metadataClient;
}
46 changes: 28 additions & 18 deletions lib/lightblue.js
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 10 additions & 7 deletions lib/nglightblue.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
* </pre></code>
*/

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", [])
Expand Down Expand Up @@ -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);
}];
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"
},
Expand Down
4 changes: 2 additions & 2 deletions test/data_test.js
Original file line number Diff line number Diff line change
@@ -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() {

Expand All @@ -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() {
Expand Down

0 comments on commit 4a70664

Please sign in to comment.