Skip to content

Latest commit

 

History

History
188 lines (133 loc) · 4.88 KB

README.md

File metadata and controls

188 lines (133 loc) · 4.88 KB

http v1.0.2

http is a unified Javascript RequireJS/CommonJS module for the browser or Wakanda Server (SSJS) which provides basic http constants and utilities.

Contents

Dependencies

Script Files

  • http.js - Fully commented script. Update to contribute.
  • http.min.js - Minimized script. For normal use.
  • http.no-md.js - Commented script without markdown comments. Use for debugging.

Constants

HEADER_FIELD_NAMES (Alias: HFN)

Standard and common custom HTTP header field names. See the module code for specific values.

Examples:

xhr.setRequestHeader(http.HEADER_FIELD_NAMES.CONTENT_TYPE, "text/plain");	

STATUS_CODES

The HTTP protocol status codes. See the module code for specific values.

Examples:

http.STATUS_CODES.OK; // 200

Module Functions

isAuthErrorResponse (xhr)

Determines if the status within xhr indicates that an authentication error (401) occurred.

Arguments

  • xhr - An XMLHttpRequest object.

Return Value

  • Returns true if xhr indicates an authentication error and returns false otherwise.

Examples:

....
xhr.send();

if (http.isAuthErrorResponse(xhr)) {
    // Reauthorize and try again.
}

isErrorResponse (xhr)

Determines if the status within xhr indicates that an error occurred.

Arguments

  • xhr - An XMLHttpRequest object.

Return Value

  • Returns true if xhr indicates an error and returns false otherwise.

Examples:

....
xhr.send();

if (http.isErrorResponse(xhr)) {
    // Handle the error.
}

responseHeadersToObject (xhr)

Converts the response headers in xhr into a javascript object where the response fields become the object's properties with their corresponding values.

Arguments

  • xhr - An XMLHttpRequest object.

Return Value

  • Returns the response headers within xhr as a javascript object.

Examples:

....
xhr.send();

headers = http.responseHeadersToObject(xhr);

// headers ==> {
//                  "Content-Length": 583727,
//                  "Content-Type": "text/html; charset=utf-8",
//                  "Last-Modified": "Tue, 15 Nov 1994 12:45:26 +0000"
//             }

statusText (xhr)

Returns a textual representation of the status within xhr.

Arguments

  • xhr - An XMLHttpRequest object.

Return Value

  • See description.

Examples:

....
xhr.send();

http.statusText(xhr); // "200 OK"

Contributions

If you contribute to this library, just modify http.js and send a pull request. Please remember to update the markdown if the public interface changes.

License

Licensed under MIT.

Copyright (C) 2013 Jeff Grann jeff@successware.net

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.