initial port of lib
This commit is contained in:
commit
8d4d1cfbdb
|
@ -0,0 +1,32 @@
|
||||||
|
{
|
||||||
|
"env": {
|
||||||
|
"es6": true,
|
||||||
|
"node": true,
|
||||||
|
"mocha": true
|
||||||
|
},
|
||||||
|
"globals": {
|
||||||
|
"riot": true
|
||||||
|
},
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": 2015
|
||||||
|
},
|
||||||
|
"extends": "eslint:recommended",
|
||||||
|
"rules": {
|
||||||
|
"indent": [
|
||||||
|
"error",
|
||||||
|
4
|
||||||
|
],
|
||||||
|
"linebreak-style": [
|
||||||
|
"error",
|
||||||
|
"unix"
|
||||||
|
],
|
||||||
|
"quotes": [
|
||||||
|
"error",
|
||||||
|
"single"
|
||||||
|
],
|
||||||
|
"semi": [
|
||||||
|
"error",
|
||||||
|
"always"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
node_modules/
|
|
@ -0,0 +1,72 @@
|
||||||
|
# Contributing
|
||||||
|
|
||||||
|
So you want to contribute, nice. **Thank you**.
|
||||||
|
|
||||||
|
Bug reports and code and documentation patches are all welcome. You can help this project also by using the development version and by reporting any bugs you might encounter.
|
||||||
|
|
||||||
|
You may contribute in several ways like:
|
||||||
|
|
||||||
|
* Creating new features
|
||||||
|
* Fixing bugs
|
||||||
|
* Improving documentation and examples
|
||||||
|
* Translating any document here to your language
|
||||||
|
|
||||||
|
## Table of contents
|
||||||
|
|
||||||
|
* [Contributing](#contributing)
|
||||||
|
* [Developing](#developing)
|
||||||
|
* [Running tests](#running-tests)
|
||||||
|
* [Reporting a bug](#reporting-a-bug)
|
||||||
|
* [Request a feature](#request-a-feature)
|
||||||
|
* [Commit message](#commit-message)
|
||||||
|
* [Code style](#code-style)
|
||||||
|
|
||||||
|
## Developing
|
||||||
|
|
||||||
|
There is only one main source file in the project. It is the [/src/http.js](/src/http.js)
|
||||||
|
The [test/index.spec.js](test/index.spec.js) is for now the only unit test file in the project.
|
||||||
|
|
||||||
|
The `dist` includes the minified version of the source code.
|
||||||
|
|
||||||
|
## Running tests
|
||||||
|
|
||||||
|
Run unit tests using this command:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run test
|
||||||
|
```
|
||||||
|
|
||||||
|
## Reporting a bug
|
||||||
|
|
||||||
|
Use the [GitHub issue tracker](https://github.com/n2geoff/js-lib/issues) to report any bug you find.
|
||||||
|
Bugs description should include:
|
||||||
|
|
||||||
|
* How to reproduce the bug;
|
||||||
|
* Easy to understand title;
|
||||||
|
|
||||||
|
Would be nice to have some code showing how to reproduce the code, you may use [gist](https://gist.github.com) or [Codepen](https://codepen.io) for uploading your example code.
|
||||||
|
|
||||||
|
## Request a feature
|
||||||
|
|
||||||
|
Use the [GitHub issue tracker](https://github.com/n2geoff/http/issues) to request a new feature.
|
||||||
|
|
||||||
|
Keep in mind, this is a pure javascript library
|
||||||
|
|
||||||
|
Feel free to port it to your favorite framework, such as [RiotJS](http://riotjs.com), Angular or VueJs in a new repository.
|
||||||
|
|
||||||
|
## Commit message
|
||||||
|
|
||||||
|
Commit messages should includes GitHub number reference and a imperative easy to understand sentence.
|
||||||
|
|
||||||
|
## Coding style
|
||||||
|
|
||||||
|
If it is supported in all major browsers without transpiling, then please use those JavaScript language features in your code, with one caveat -- readability is king.
|
||||||
|
|
||||||
|
Currently all ES5 and ES6/ES2015 are available.
|
||||||
|
|
||||||
|
This project uses [ESLint](https://eslint.org/) and the [`.eslintrc.json`](.eslintrc.json) is dead-simple, and all you need to followed.
|
||||||
|
|
||||||
|
Thank you for reading this.
|
||||||
|
|
||||||
|
|
||||||
|
Hey, **star** this *repo* and/or share it with your friends.
|
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright (c) 2018
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||||
|
of the Software, and to permit persons to whom the Software is furnished to do
|
||||||
|
so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,83 @@
|
||||||
|
# HTTP
|
||||||
|
|
||||||
|
A tiny http/ajax library.
|
||||||
|
|
||||||
|
Weighs in at less than **300 bytes** gzipped and minified. It is very basic, but contains support for cross-domain requests back to somewhat older browsers (See [Compatibility](#compatibility)).
|
||||||
|
|
||||||
|
> Fork of [nanoajax](https://github.com/yanatan16/nanoajax) to make more encapsulated
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Just include from `/dist`
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script src="/http.min.js"></script>
|
||||||
|
```
|
||||||
|
|
||||||
|
> build from source with: `npm run build`
|
||||||
|
|
||||||
|
## Use
|
||||||
|
|
||||||
|
GET
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
http({url:'/some-get-url'}, function (code, responseText) { ... });
|
||||||
|
```
|
||||||
|
|
||||||
|
POST
|
||||||
|
|
||||||
|
```js
|
||||||
|
http({url: '/some-post-url', method: 'POST', body: 'post=content&args=yaknow'}, function (code, responseText, request) {
|
||||||
|
# code is response code
|
||||||
|
# responseText is response body as a string
|
||||||
|
# request is the xmlhttprequest, which has `getResponseHeader(header)` function
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
Simple and small ajax decorator function for almost any browser. Takes a parameters object and a callback function.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
|
||||||
|
- `url`: string, required
|
||||||
|
- `headers`: object of `{header_name: header_value, ...}`
|
||||||
|
- `body`:
|
||||||
|
+ string (sets content type to 'application/x-www-form-urlencoded' if not set in headers)
|
||||||
|
+ FormData (doesn't set content type so that browser will set as appropriate)
|
||||||
|
- `method`: 'GET', 'POST', etc. Defaults to 'GET' or 'POST' based on body
|
||||||
|
- `cors`: If your using cross-origin, you will need this true for IE8-9 (to use the XMLDomainRequest object, also see [Compatibility](#compatibility))
|
||||||
|
|
||||||
|
The following parameters are passed directly onto the request object:
|
||||||
|
|
||||||
|
**IMPORTANT NOTE**: The caller is responsible for compatibility checking.
|
||||||
|
|
||||||
|
- `responseType`: string, various compatibility, see XHR docs for enum options
|
||||||
|
- `withCredentials`: boolean, IE10+, CORS only
|
||||||
|
- `timeout`: long, ms timeout, IE8+
|
||||||
|
- `onprogress`: callback, IE10+
|
||||||
|
|
||||||
|
Callback function prototype:
|
||||||
|
|
||||||
|
- `statusCode`: integer status or null
|
||||||
|
+ if request errors for some browsers (notably Chrome), this is 0 (and `response` is "Error")
|
||||||
|
- `response`:
|
||||||
|
+ if `responseType` set and supported by browser, this is an object of some type (see docs)
|
||||||
|
+ otherwise if request completed, this is the string text of the response
|
||||||
|
+ if request is aborted, this is `"Abort"`
|
||||||
|
+ if request times out, this is `"Timeout"`
|
||||||
|
+ if request errors before completing (probably a CORS issue), this is `"Error"`
|
||||||
|
- request object
|
||||||
|
|
||||||
|
Returns the request object. So you can call .abort() or other methods
|
||||||
|
|
||||||
|
## Compatibility
|
||||||
|
|
||||||
|
`http` works on android, iOS, IE8+, and all modern browsers, with some (_known_) caveats.
|
||||||
|
|
||||||
|
- Safari is conservative with cookies and will not allow cross-domain cookies to be set from domains that have never been visited by the user.
|
||||||
|
- IE8 and IE9 do not support cookies in cross-domain requests in this library. There are other solutions out there, but this library has chosen small over edge case compatibility.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT](LICENSE)
|
|
@ -0,0 +1 @@
|
||||||
|
(function(root,factory){if(typeof module==="object"&&module.exports){module.exports=factory(root.Module)}else{root.Module=factory(root.Module)}})(this,function(){"use strict";const Module={};return Module});
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"name": "http",
|
||||||
|
"version": "0.4.0",
|
||||||
|
"description": "tiny http lib",
|
||||||
|
"main": "src/http.js",
|
||||||
|
"directories": {
|
||||||
|
"doc": "docs",
|
||||||
|
"lib": "lib",
|
||||||
|
"test": "test"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"lint": "node_modules/.bin/eslint src/http.js",
|
||||||
|
"test": "node_modules/.bin/tape test/*.spec.js"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"library"
|
||||||
|
],
|
||||||
|
"author": "Geoff Doty",
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"eslint": "^5.16.0",
|
||||||
|
"tape": "^4.10.2"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
/* {http} v{version} | MIT | https:// */
|
||||||
|
(function(root, factory) {
|
||||||
|
/* eslint-disable */
|
||||||
|
if (typeof define === 'function' && define.amd) {
|
||||||
|
// AMD
|
||||||
|
define(['http'], factory);
|
||||||
|
} else if(typeof module === 'object' && module.exports) {
|
||||||
|
// CommonJS / NodeJS
|
||||||
|
module.exports = factory();
|
||||||
|
} else {
|
||||||
|
// Global
|
||||||
|
root.http = factory();
|
||||||
|
}
|
||||||
|
|
||||||
|
}(this, function() {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
return function http(params, callback) {
|
||||||
|
// Any variable used more than once is var'd here because
|
||||||
|
// minification will munge the variables whereas it can't munge
|
||||||
|
// the object access.
|
||||||
|
var headers = params.headers || {}
|
||||||
|
, body = params.body
|
||||||
|
, method = params.method || (body ? 'POST' : 'GET')
|
||||||
|
, called = false;
|
||||||
|
|
||||||
|
var req = getRequest(params.cors);
|
||||||
|
|
||||||
|
var reqfields = [
|
||||||
|
'responseType', 'withCredentials', 'timeout', 'onprogress'
|
||||||
|
];
|
||||||
|
|
||||||
|
function getRequest(cors) {
|
||||||
|
if (window.XMLHttpRequest) {
|
||||||
|
return new XMLHttpRequest;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setDefault(obj, key, value) {
|
||||||
|
obj[key] = obj[key] || value;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cb(statusCode, responseText) {
|
||||||
|
return function () {
|
||||||
|
if (!called) {
|
||||||
|
callback(req.status === undefined ? statusCode : req.status,
|
||||||
|
req.status === 0 ? 'Error' : (req.response || req.responseText || responseText),
|
||||||
|
req);
|
||||||
|
called = true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
req.open(method, params.url, true);
|
||||||
|
|
||||||
|
var success = req.onload = cb(200);
|
||||||
|
req.onreadystatechange = function () {
|
||||||
|
if (req.readyState === 4) success();
|
||||||
|
};
|
||||||
|
|
||||||
|
req.onerror = cb(null, 'Error');
|
||||||
|
req.ontimeout = cb(null, 'Timeout');
|
||||||
|
req.onabort = cb(null, 'Abort');
|
||||||
|
|
||||||
|
if (body) {
|
||||||
|
setDefault(headers, 'X-Requested-With', 'XMLHttpRequest');
|
||||||
|
|
||||||
|
if (!window.FormData || !(body instanceof window.FormData)) {
|
||||||
|
setDefault(headers, 'Content-Type', 'application/x-www-form-urlencoded');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0, len = reqfields.length, field; i < len; i++) {
|
||||||
|
field = reqfields[i];
|
||||||
|
if (params[field] !== undefined) {
|
||||||
|
req[field] = params[field];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let field in headers) {
|
||||||
|
req.setRequestHeader(field, headers[field]);
|
||||||
|
}
|
||||||
|
|
||||||
|
req.send(body);
|
||||||
|
|
||||||
|
return req;
|
||||||
|
}
|
||||||
|
}));
|
|
@ -0,0 +1,10 @@
|
||||||
|
const test = require('tape');
|
||||||
|
const index = require('../src/index.js');
|
||||||
|
|
||||||
|
test('Index', function(t) {
|
||||||
|
|
||||||
|
t.test('setup', function(t) {
|
||||||
|
t.ok(true);
|
||||||
|
t.end();
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue