All Projects → otiai10 → chomex

otiai10 / chomex

Licence: MIT License
Chrome Extension Messaging Routing Kit / Promisify Chrome Messaging / LocalStorage Object Mapper

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to chomex

doasync
Promisify functions and objects immutably
Stars: ✭ 27 (-34.15%)
Mutual labels:  promise, promisify
ngx-localstorage
An Angular wrapper for localstorage/sessionstorage access.
Stars: ✭ 27 (-34.15%)
Mutual labels:  promise, localstorage
Rapid.js
An ORM-like Interface and a Router For Your API Requests
Stars: ✭ 700 (+1607.32%)
Mutual labels:  router, promise
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (-19.51%)
Mutual labels:  promise, promisify
Flowa
🔥Service level control flow for Node.js
Stars: ✭ 66 (+60.98%)
Mutual labels:  router, promise
organiser
An organic web framework for organized web servers.
Stars: ✭ 58 (+41.46%)
Mutual labels:  router, promise
GitHubSearch
GitHub iOS client with minimum third-party dependencies.
Stars: ✭ 34 (-17.07%)
Mutual labels:  router
react-native-boilerplate
Ready-made structure of your next React Native application within a few minutes.
Stars: ✭ 36 (-12.2%)
Mutual labels:  router
koa-routeify
the next router for koajs.
Stars: ✭ 12 (-70.73%)
Mutual labels:  router
Actuate
One line easy actuation of CSS animation sequences
Stars: ✭ 42 (+2.44%)
Mutual labels:  promise
replace-in-files
Replace text in one or more files or globs.
Stars: ✭ 21 (-48.78%)
Mutual labels:  promise
wolfpacs
WolfPACS is an DICOM load balancer written in Erlang.
Stars: ✭ 1 (-97.56%)
Mutual labels:  router
yarr
A React router library enabling the render-as-you-fetch concurrent UI pattern.
Stars: ✭ 97 (+136.59%)
Mutual labels:  router
r5r
ipeagit.github.io/r5r/
Stars: ✭ 90 (+119.51%)
Mutual labels:  router
purescript-promises
An alternative effect monad for PureScript.
Stars: ✭ 23 (-43.9%)
Mutual labels:  promise
lifx-http-api
💡 Thin wrapper around the Lifx HTTP API (http://api.developer.lifx.com/)
Stars: ✭ 17 (-58.54%)
Mutual labels:  promise
TelegramStorageParser
Library to decrypt and parse Telegram Desktop's storage
Stars: ✭ 38 (-7.32%)
Mutual labels:  localstorage
fritz-box
📦 Promise-based JavaScript FRITZ!Box API.
Stars: ✭ 14 (-65.85%)
Mutual labels:  router
yew-router
Router extension to yew
Stars: ✭ 27 (-34.15%)
Mutual labels:  router
queue-promise
A simple, dependency-free library for concurrent promise-based queues. Comes with with concurrency and timeout control.
Stars: ✭ 56 (+36.59%)
Mutual labels:  promise

chomex

Latest Stable Version NPM Downloads Node.js CI codecov

Chrome Extension Messaging Routing Kit.

  • Router to handle onMessage with routes expression
  • Client to Promisify sendMessage
  • Model to access to localStorage like ActiveRecord
  • Types to define data schema of Model

Installation

npm install chomex

Why?

.onMessage like a server routing

👎 Dispatching message inside addListener function makes my code messy and unreeadable.

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  switch(message.action) {
  case "/users/get":
    GetUser.apply(sender, [message, sendResponse]);
    break;
  default:
    NotFound.apply(sender, [message, sendResponse]);
  }
  return true;
});

👍 chomex.Router makes it more claen and readable.

const router = new chomex.Router();
router.on("/users/get", GetUser);
chrome.runtime.onMessage.addListener(router.listener());

Happy 🤗

.sendMessage like a fetch client

👎 Handling the response of sendMessage by callback makes my code messy and unreadable.

chrome.runtime.sendMessage({action:"/users/get",id:123}, (response) => {
  if (response.status == 200) {
    alert("User: " + response.user.name);
  } else {
    console.log("Error:", response);
  }
});

👍 chomex.Client makes it clean and readable by handling response with Promise.

const client = new chomex.Client(chrome.runtime);
const response = await client.message("/users/get", {id:123});
alert("User: " + response.data.user.name);

Happy 🤗

Examples

NOTE: These examples are using async/await on top-level. I believe you are familiar with asyc/await.

background.js as a server

import {Router, Model, Types} from 'chomex';

// Define your model
class User extends Model {
  static schema = {
    name: Types.string.isRequired,
    age:  Types.number,
  }
}

const router = new Router();

// Define your routes
router.on("/users/create", message => {
  const obj = message.user;
  const user = User.new(obj).save();
  return user;
});

router.on("/users/get", message => {
  const userId = message.id;
  const user = User.find(userId);
  if (!user) {
    return {status:404,error:"not found"};
  }
  // You can also return async Promise
  return Promise.resolve(user);
});

// Of course, you can separate files
// in which controller functions are defined.
import {UserDelete} from "./Controllers/Users";
router.on("/users/delete", UserDelete);

// Don't forget to add listener to chrome modules.
chrome.runtime.onMessage.addListener(router.listener());

content_script.js as a client

import {Client} from 'chomex';

const client = new Client(chrome.runtime);

// it sends message to "/users/get" route.
const user = {name: 'otiai10', age: 30};
const response = await client.message('/users/create', {user});
console.log("Created!", response.data);

const {data: user} = await client.message('/users/get', {id: 12345});
console.log("Found:", res.data);

Customize Router for other listeners

You can also customize resolver for routing. It's helpful when you want to make routings for EventListener modules on chrome, such as chrome.notifications.onClicked, chrome.webRequest.onBeforeRequest or so.

// Resolver rule, which resolve given "id" to routing name.
const resolve = (id) => {
  const prefix = id.split(".")[0];
  return {name: prefix};
};

const router = new Router(resolve);
// You see, this controller is invoked when
// a notification with ID "quest.xxxx" is clicked.
router.on('quest', NotificaionOnClickController.Quest);

chrome.notifications.onClicked.addListener(router.listener());

For more information

Reference Projects

Projects using chomex

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].