Skip to content

Commit e4f44c2

Browse files
author
Hector Virgen
committed
Adds support for commonjs (e.g. WebPack)
Only fixes the vanilla adapter. The angular adapter may still be busted when using WebPack.
1 parent 5fecad6 commit e4f44c2

File tree

11 files changed

+152
-127
lines changed

11 files changed

+152
-127
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function(grunt) {
3030
}
3131
},
3232
vanilla: {
33-
src: ['lib/http_adapter/vanilla.js', 'lib/index.js', 'lib/vanilla.js'],
33+
src: ['lib/http_adapter/vanilla.js', 'lib/index.js'],
3434
dest: 'api-client.js'
3535
}
3636
},

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,23 @@ app.get('/', function(req, res) {
5151
});
5252
```
5353

54-
**browser**
54+
**Browser (no package manager)**
5555

5656
```js
57-
<script src="bower_components/tagged-api/tagged-api-min.js"></script>
58-
<script>
57+
var adapter = new TaggedApiVanillaAdapter(XMLHttpRequest, Promise);
5958
var api = new TaggedApi('/api.php', {
6059
session_id: 'abc123' // Session cookie ID from `document.cookie`
61-
});
62-
</script>
60+
}, adapter);
61+
```
62+
63+
**Browser (CommonJS)**
64+
65+
```js
66+
import {Client, VanillaAdapter} from 'bower_components/tagged-api/tagged-api-min.js';
67+
var adapter = new VanillaAdapter(XMLHttpRequest, Promise);
68+
var api = new Client('/api.php', {
69+
session_id: 'abc123' // Session cookie ID from `document.cookie`
70+
}, adapter);
6371
```
6472

6573
Executing API Calls

api-angular-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api-angular.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
var TaggedApi = function(endpoint, options, http) {
2525
this._endpoint = endpoint;
2626

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

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

262-
TaggedApi.middleware = function(url, options) {
262+
var middleware = function(url, options) {
263263
var NodeAdapter = require('./http_adapter/node');
264264
var http = new NodeAdapter();
265265

@@ -420,11 +420,12 @@
420420
return obj1;
421421
}
422422

423-
if (typeof exports !== 'undefined') {
424-
// We're in a nodejs environment, export this module
425-
module.exports = TaggedApi;
423+
if (typeof module !== 'undefined' && module.exports) {
424+
// We're in a commonjs environment
425+
module.exports.Client = TaggedApi;
426+
module.exports.middleware = middleware;
426427
} else {
427-
// We're in a browser environment, expose this module globally
428+
// All other environments, expose TaggedApi globally
428429
context.TaggedApi = TaggedApi;
429430
}
430431
})();

0 commit comments

Comments
 (0)