├── .gitignore
├── test
├── api
│ ├── .htaccess
│ ├── cors.php
│ ├── test.json
│ └── twitter.json
├── test.js
├── test.html
├── unit.html
├── setup.js
└── unit.js
├── README.md
├── package.json
├── CHANGELOG.md
├── lib
├── main.js
└── apiconnect.js
├── demo
├── sh_custom.css
├── sh_javascript.min.js
├── sh_main.min.js
└── index.html
├── vendor
├── qunit
│ ├── qunit.css
│ └── qunit.js
└── json2.js
└── docs
├── generator.rb
└── methods.html
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 |
--------------------------------------------------------------------------------
/test/api/.htaccess:
--------------------------------------------------------------------------------
1 |
2 | # Parse json files as PHP
3 | AddType application/x-httpd-php .json
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # APIConnect
2 |
3 | A simplified Javascript interface for working with APIs.
4 | Detailed documentation [here](http://andrewplummer.github.com/APIConnect/docs.html).
5 |
--------------------------------------------------------------------------------
/test/test.js:
--------------------------------------------------------------------------------
1 |
2 |
3 | var APIConnect = require('../lib/main');
4 |
5 |
6 | var api = new APIConnect('localhost:4000');
7 |
8 | api.connect('PUT APIConnect/test/api/test.json');
9 |
10 |
11 | api.contentType('json');
12 |
13 | api.updateTest({ foo: 'bar' }).always(function() {
14 | console.info('aahhahaha', arguments);
15 | });
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/test/api/cors.php:
--------------------------------------------------------------------------------
1 |
15 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "APIConnect",
3 | "version": "0.6.0",
4 | "description": "A simplified Javascript interface for working with APIs.",
5 | "keywords": ["api", "rest", "routing","cross-domain","ajax"],
6 | "homepage": "http://andrewplummer.github.com/APIConnect/",
7 | "author": "Andrew Plummer",
8 | "main": "./lib/main",
9 | "directories" : {"lib" : "./lib"},
10 | "repository" : {"type" : "git", "url": "https://github.com/andrewplummer/APIConnect"},
11 | "dependencies": {
12 | "request": ">= 2.9.153",
13 | "jquery-deferred": ">= 0.2.0"
14 | },
15 | "engines" : {"node" : ">= 0.6.0"}
16 | }
17 |
--------------------------------------------------------------------------------
/test/test.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/test/unit.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | test markup, will be hidden
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 |
2 | v0.6
3 | ======
4 |
5 | - Released for npm.
6 | - APIs on the same domain will not use JSONP.
7 | - Constructor now accepts a single string as the domain (may include port).
8 | - Params can be burned directly when connecting routes by adding ?key=value.
9 | - Routes can accept a single function as a shortcut for a success callback.
10 | - Data format can now differ from append format.
11 | - Fix for POST being misinterpreted in "posts"
12 |
13 |
14 | v0.5
15 | ======
16 |
17 | - Adding the code, starting here.
18 | - Allowed a callback in place of params/options if defined as a shortcut for .then().
19 | - Allowed the domain to be set in the constructor via a single string.
20 | - Allowed the burn in of params via normal syntax ?key=value, etc.
21 |
--------------------------------------------------------------------------------
/test/api/test.json:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | require('cors.php');
5 |
6 | if ($_SERVER['REQUEST_METHOD'] == "PUT") {
7 | $v = parse_str(file_get_contents("php://input"), $puts);
8 | echo "REQUEST WAS PUT AND DATA IS: ";
9 | print_r($puts);
10 | } elseif ($_SERVER['REQUEST_METHOD'] == "POST") {
11 | echo "REQUEST WAS POST AND DATA IS: ";
12 | print_r($_POST);
13 | } elseif ($_SERVER['REQUEST_METHOD'] == "GET") {
14 | echo "REQUEST WAS GET AND DATA IS: ";
15 | print_r($_GET);
16 | }
17 |
18 |
19 | die();
20 |
21 |
22 | $json = <<
48 |
--------------------------------------------------------------------------------
/test/api/twitter.json:
--------------------------------------------------------------------------------
1 | 0))
24 | {
25 | $api_url .= '?' . http_build_query($options);
26 | }
27 |
28 | curl_setopt($curl_handle, CURLOPT_URL, $api_url);
29 |
30 | if ($require_credentials)
31 | {
32 | curl_setopt($curl_handle, CURLOPT_USERPWD, $credentials);
33 | }
34 | if ($http_method == 'post')
35 | {
36 | curl_setopt($curl_handle, CURLOPT_POST, true);
37 | curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query($options));
38 | }
39 |
40 | curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
41 | curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
42 |
43 | $twitter_data = curl_exec($curl_handle);
44 |
45 | header('HTTP/1.1 200 OK');
46 | curl_close($curl_handle);
47 |
48 | return $twitter_data;
49 | }
50 |
51 | // Gets the 20 most recent statuses
52 | $f = apiCall('foobar', 'get', 'json', array(), false);
53 |
54 | echo $f;
55 |
56 | ?>
57 |
--------------------------------------------------------------------------------
/lib/main.js:
--------------------------------------------------------------------------------
1 |
2 | var url = require('url');
3 | var request = require('request');
4 | var deferred = require('jquery-deferred');
5 | var APIConnect = require('./apiconnect');
6 |
7 |
8 |
9 | APIConnect.util.Deferred = function() {
10 | return new deferred.Deferred;
11 | }
12 | APIConnect.util.supportsCORS = function() {
13 | return true;
14 | }
15 | APIConnect.util.isArray = function(arr) {
16 | return Array.isArray(arr);
17 | }
18 | APIConnect.util.ajax = function(context, method, url, params, options) {
19 | var deferred = this.Deferred(),
20 | isGET = method == 'GET',
21 | isJSON = options.contentType == 'json';
22 | request({
23 | url: url,
24 | method: method,
25 | qs: isGET ? params : null,
26 | form: !isGET && !isJSON ? params : null,
27 | json: !isGET && isJSON ? params : null
28 | }, function (error, response, body) {
29 | if(!error) {
30 | try {
31 | var data = options.dataFormat == 'json' ? JSON.parse(body) : body;
32 | if(options.complete) options.complete.call(context, data);
33 | if(options.success) options.success.call(context, data);
34 | return deferred.resolve(data);
35 | } catch(e) {};
36 | }
37 | if(options.error) options.error.call(context, body);
38 | deferred.reject(body);
39 | });
40 | return deferred;
41 | }
42 | APIConnect.util.getLocationValue = function() {
43 | return null;
44 | }
45 | APIConnect.util.getLocationValue = function() {
46 | return null;
47 | }
48 | APIConnect.util.when = function() {
49 | return deferred.when.apply(this, arguments);
50 | }
51 | APIConnect.util.getFullURL = function(base, params) {
52 | var obj = url.parse(base);
53 | obj.query = params;
54 | return url.format(obj);
55 | }
56 |
57 | module.exports = APIConnect;
58 |
--------------------------------------------------------------------------------
/demo/sh_custom.css:
--------------------------------------------------------------------------------
1 | pre {
2 | color: #eeeeee;
3 | font-weight: normal;
4 | font-style: normal;
5 | }
6 |
7 | .sh_keyword {
8 | color: #b62;
9 | font-weight: normal;
10 | font-style: normal;
11 | }
12 |
13 | .sh_predef_func {
14 | color: #a7c;
15 | font-weight: normal;
16 | font-style: normal;
17 | }
18 |
19 | .sh_type {
20 | color: #00ff00;
21 | font-weight: normal;
22 | font-style: normal;
23 | }
24 |
25 | .sh_string {
26 | color: #8b0;
27 | font-weight: normal;
28 | font-style: normal;
29 | }
30 |
31 | .sh_cbracket {
32 | color: #fa3;
33 | font-weight: normal;
34 | font-style: normal;
35 | }
36 |
37 | .sh_regexp {
38 | color: #e5e;
39 | font-weight: normal;
40 | font-style: normal;
41 | }
42 |
43 | .sh_specialchar {
44 | color: #ff22ff;
45 | font-weight: normal;
46 | font-style: normal;
47 | }
48 |
49 | .sh_comment {
50 | color: #888;
51 | font-weight: normal;
52 | font-style: normal;
53 | }
54 |
55 | .sh_number {
56 | color: #f93;
57 | font-weight: normal;
58 | font-style: normal;
59 | }
60 |
61 | .sh_preproc {
62 | color: #ff22ff;
63 | font-weight: normal;
64 | font-style: normal;
65 | }
66 |
67 | .sh_function {
68 | color: #eee;
69 | font-weight: normal;
70 | font-style: normal;
71 | }
72 |
73 | .sh_url {
74 | color: #ff0000;
75 | font-weight: normal;
76 | font-style: normal;
77 | }
78 |
79 | .sh_date {
80 | color: #B26818;
81 | font-weight: normal;
82 | font-style: normal;
83 | }
84 |
85 | .sh_time {
86 | color: #B26818;
87 | font-weight: normal;
88 | font-style: normal;
89 | }
90 |
91 | .sh_file {
92 | color: #B26818;
93 | font-weight: normal;
94 | font-style: normal;
95 | }
96 |
97 | .sh_ip {
98 | color: #ff0000;
99 | font-weight: normal;
100 | font-style: normal;
101 | }
102 |
103 | .sh_name {
104 | color: #ff0000;
105 | font-weight: normal;
106 | font-style: normal;
107 | }
108 |
109 | .sh_variable {
110 | color: #B26818;
111 | font-weight: normal;
112 | font-style: normal;
113 | }
114 |
115 | .sh_oldfile {
116 | color: #ff22ff;
117 | font-weight: normal;
118 | font-style: normal;
119 | }
120 |
121 | .sh_newfile {
122 | color: #ff0000;
123 | font-weight: normal;
124 | font-style: normal;
125 | }
126 |
127 | .sh_difflines {
128 | color: #B26818;
129 | font-weight: normal;
130 | font-style: normal;
131 | }
132 |
133 | .sh_selector {
134 | color: #B26818;
135 | font-weight: normal;
136 | font-style: normal;
137 | }
138 |
139 | .sh_property {
140 | color: #B26818;
141 | font-weight: normal;
142 | font-style: normal;
143 | }
144 |
145 | .sh_value {
146 | color: #ff0000;
147 | font-weight: normal;
148 | font-style: normal;
149 | }
150 |
151 |
--------------------------------------------------------------------------------
/demo/sh_javascript.min.js:
--------------------------------------------------------------------------------
1 | if(!this.sh_languages){this.sh_languages={}}sh_languages.javascript=[[[/\/\/\//g,"sh_comment",1],[/\/\//g,"sh_comment",7],[/\/\*\*/g,"sh_comment",8],[/\/\*/g,"sh_comment",9],[/\b(?:abstract|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|false|final|finally|for|function|goto|if|implements|in|instanceof|interface|native|new|null|private|protected|prototype|public|return|static|super|switch|synchronized|throw|throws|this|transient|true|try|typeof|var|volatile|while|with)\b/g,"sh_keyword",-1],[/(\+\+|--|\)|\])(\s*)(\/=?(?![*\/]))/g,["sh_symbol","sh_normal","sh_symbol"],-1],[/(0x[A-Fa-f0-9]+|(?:[\d]*\.)?[\d]+(?:[eE][+-]?[\d]+)?)(\s*)(\/(?![*\/]))/g,["sh_number","sh_normal","sh_symbol"],-1],[/([A-Za-z$_][A-Za-z0-9$_]*\s*)(\/=?(?![*\/]))/g,["sh_normal","sh_symbol"],-1],[/\/(?:\\.|[^*\\\/])(?:\\.|[^\\\/])*\/[gim]*/g,"sh_regexp",-1],[/\b[+-]?(?:(?:0x[A-Fa-f0-9]+)|(?:(?:[\d]*\.)?[\d]+(?:[eE][+-]?[\d]+)?))u?(?:(?:int(?:8|16|32|64))|L)?\b/g,"sh_number",-1],[/"/g,"sh_string",10],[/'/g,"sh_string",11],[/~|!|%|\^|\*|\(|\)|-|\+|=|\[|\]|\\|:|;|,|\.|\/|\?|&|<|>|\|/g,"sh_symbol",-1],[/\{|\}/g,"sh_cbracket",-1],[/\b(?:Math|Infinity|NaN|undefined|arguments)\b/g,"sh_predef_var",-1],[/\b(?:Array|Boolean|Date|Error|EvalError|Function|Number|Object|RangeError|ReferenceError|RegExp|String|SyntaxError|TypeError|URIError|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|eval|isFinite|isNaN|parseFloat|parseInt)\b/g,"sh_predef_func",-1],[/(?:[A-Za-z]|_)[A-Za-z0-9_]*(?=[ \t]*\()/g,"sh_function",-1]],[[/$/g,null,-2],[/(?:)[A-Za-z0-9_\.\/\-_~]+@[A-Za-z0-9_\.\/\-_~]+(?:>?)|(?:)[A-Za-z0-9_]+:\/\/[A-Za-z0-9_\.\/\-_~]+(?:>?)/g,"sh_url",-1],[/<\?xml/g,"sh_preproc",2,1],[//g,"sh_keyword",-1],[/<(?:\/)?[A-Za-z](?:[A-Za-z0-9_:.-]*)/g,"sh_keyword",6,1],[/&(?:[A-Za-z0-9]+);/g,"sh_preproc",-1],[/<(?:\/)?[A-Za-z][A-Za-z0-9]*(?:\/)?>/g,"sh_keyword",-1],[/<(?:\/)?[A-Za-z][A-Za-z0-9]*/g,"sh_keyword",6,1],[/@[A-Za-z]+/g,"sh_type",-1],[/(?:TODO|FIXME|BUG)(?:[:]?)/g,"sh_todo",-1]],[[/\?>/g,"sh_preproc",-2],[/([^=" \t>]+)([ \t]*)(=?)/g,["sh_type","sh_normal","sh_symbol"],-1],[/"/g,"sh_string",3]],[[/\\(?:\\|")/g,null,-1],[/"/g,"sh_string",-2]],[[/>/g,"sh_preproc",-2],[/([^=" \t>]+)([ \t]*)(=?)/g,["sh_type","sh_normal","sh_symbol"],-1],[/"/g,"sh_string",3]],[[/-->/g,"sh_comment",-2],[/
379 |
380 |
381 |
438 |