GithubHelp home page GithubHelp logo

arthurbeaulieu / kom.js Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 289 KB

A basic module to perform server calls. KISS, light and library free.

Home Page: https://arthurbeaulieu.github.io/Kom.js/example.html

License: GNU General Public License v3.0

JavaScript 87.35% HTML 12.65%
javascript http server-call es6-module

kom.js's Introduction

Kom.js

License

Kom.js is a JavaScript ES6 component that provides an abstraction to perform HTTP server calls (GET and POST). The caller can format the server response to be a JavaScript object, a string or raw binary data.

With ~2Ko minified, Kom.js is designed to be stable and remain as light as possible. It is meant to be used application wide so you can perform HTTP calls from anywhere in your app.

Get Started

This repository was made to store documentation, test bench and source code. If you want to include this component in your project, you either need the src/Kom.js file if you have an assets bundler in your project, or use the dist/Kom.bundle.js to use the minified component. This minified file is compiled in ES5 JavaScript for compatibility reasons. The unminified file is, in the contrary, bundled in ES6 JavaScript.

Here's an example on how to create a Kom.js component :

/* Import the Js component */
import _Kom from './src/Kom.js';
/* Create a server call abstraction */
const Kom = new _Kom();

Now that Kom.js is instantiated, you can perform server calls using the following methods ;

HTTP GET with a JavaScript object response

Kom.get('url/to/get').then(response => {
  // response is formatted as a JavaScript object
}).catch(error => {
  // HTTP call encountered an unexpected return value
});

HTTP GET with a string response

Kom.getText('url/to/get').then(response => {
  // response is formatted as string
}).catch(error => {
  // HTTP call encountered an unexpected return value
});

HTTP GET with a raw binary data response

Kom.getRaw('url/to/get').then(response => {
  // response is formatted as raw binary data
}).catch(error => {
  // HTTP call encountered an unexpected return value
});

HTTP POST with a JavaScript object response

Kom.post('url/to/post', {
  data: 'MyData',
  ...
}).then(response => {
  // response is formatted as a JavaScript object
}).catch(error => {
  // HTTP call encountered an unexpected return value
});

HTTP POST with a string response

Kom.postText('url/to/post', {
  data: 'MyData',
  ...
}).then(response => {
  // response is formatted as string
}).catch(error => {
  // HTTP call encountered an unexpected return value
});

HTTP POST with a raw binary data response

Kom.postRaw('url/to/post', {
  data: 'MyData',
  ...
}).then(response => {
  // response is formatted as raw binary data
}).catch(error => {
  // HTTP call encountered an unexpected return value
});

You're now good to go! If however you need more information, you can read the online documentation.

Development

If you clone this repository, you can npm install to install development dependencies. This will allow you to build dist file, run the component tests or generate the documentation ;

  • npm run build to generate the minified file in es5 Js ;
  • npm run watch to generate an es6 bundle and watch for changes ;
  • npm run web-server to launch a local development server ;
  • npm run doc to generate documentation ;
  • npm run beforecommit to perform tests, generate doc and bundle the JavaScript.

To avoid CORS when locally loading the example HTML file, run the web server. Please do not use it on a production environment. Unit tests are performed on both Firefox and Chrome ; ensure you have both installed before running tests, otherwise they might fail.

If you have any question or idea, feel free to DM or open an issue (or even a PR, who knows) ! I'll be glad to answer your request.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.