Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for commonjs (e.g. WebPack) #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = function(grunt) {
}
},
vanilla: {
src: ['lib/http_adapter/vanilla.js', 'lib/index.js', 'lib/vanilla.js'],
src: ['lib/http_adapter/vanilla.js', 'lib/index.js'],
dest: 'api-client.js'
}
},
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,23 @@ app.get('/', function(req, res) {
});
```

**browser**
**Browser (no package manager)**

```js
<script src="bower_components/tagged-api/tagged-api-min.js"></script>
<script>
var adapter = new TaggedApiVanillaAdapter(XMLHttpRequest, Promise);
var api = new TaggedApi('/api.php', {
session_id: 'abc123' // Session cookie ID from `document.cookie`
});
</script>
}, adapter);
```

**Browser (CommonJS)**

```js
import {Client, VanillaAdapter} from 'bower_components/tagged-api/tagged-api-min.js';
var adapter = new VanillaAdapter(XMLHttpRequest, Promise);
var api = new Client('/api.php', {
session_id: 'abc123' // Session cookie ID from `document.cookie`
}, adapter);
```

Executing API Calls
Expand Down
2 changes: 1 addition & 1 deletion api-angular-min.js

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

15 changes: 8 additions & 7 deletions api-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
var TaggedApi = function(endpoint, options, http) {
this._endpoint = endpoint;

// Default to the vanilla adapter should clients not pass adapter
this._http = (typeof http !== 'undefined') ? http : new VanillaAdapter(context.XMLHttpRequest, context.Promise || require('bluebird'));
// HTTP adapter to perform HTTP requests
this._http = http;

// API calls that are made within a single JS execution frame will be added
// to the queue and processed as a whole on the next tick.
Expand Down Expand Up @@ -259,7 +259,7 @@
this._events[stat].push(callback);
};

TaggedApi.middleware = function(url, options) {
var middleware = function(url, options) {
var NodeAdapter = require('./http_adapter/node');
var http = new NodeAdapter();

Expand Down Expand Up @@ -420,11 +420,12 @@
return obj1;
}

if (typeof exports !== 'undefined') {
// We're in a nodejs environment, export this module
module.exports = TaggedApi;
if (typeof module !== 'undefined' && module.exports) {
// We're in a commonjs environment
module.exports.Client = TaggedApi;
module.exports.middleware = middleware;
} else {
// We're in a browser environment, expose this module globally
// All other environments, expose TaggedApi globally
context.TaggedApi = TaggedApi;
}
})();
Expand Down
Loading