Giter Site home page Giter Site logo

injector's Introduction

honkjs/injector

Injects services into functions passed to honk. Very similar functionality to redux-thunk.

import Honk from '@honkjs/honk';
import injector from '@honkjs/injector';
import api from 'mycoolapi';

const honk = new Honk()
  // add injector to the middleware pipeline
  .use(injector({ api })).honk;

function getSomething(name) {
  return function({ api }) {
    // returns a promise
    return api.fetchSomething(name);
  };
}

honk(getSomething('bob')).then((results) => console.log(results));

Injector always returns the results of the function passed in.

"Dependency injection" is handled using js object deconstruction.

// no deconstruction
function boringThunk(name) {
  return function(services) {
    return services.api.getSomething(name);
  };
}

// deconstructed
function coolThunk(name) {
  return function({ api }) {
    return api.getSomething(name);
  };
}

honk(boringThunk('Bob'));
honk(coolThunk('George'));

There is no dependency resolution, or anything fancy like that. Injector is built with the simple goal of allowing you to easily access application services from anywhere honk is available.

Type safety

If you're using typescript, you likely want some type safety on the services object. There are a couple ways to achieve this. Which way you use is totally a matter of preference.

Union type

type MyHonkAppServices = {
  api: MyApi;
} & IHonkServices;

// this creates a new type combining the IHonkServices with your own
// { api: MyApi, honk: IHonk }

function coolThunk(name) {
  return function({ api }: MyHonkAppServices) {
    return api.getSomething(name);
  };
}

Declaration merging

Similar to how IHonk can be overloaded, you can use declaration merging.

declare module '@honkjs/injector' {
  interface IHonkServices {
    api: MyApi;
  }
}

// The standard IHonkServices type will now be:
// { api: MyApi, honk: IHonk }

function coolThunk(name) {
  return function({ api }: IHonkServices) {
    return api.getSomething(name);
  };
}

Manually

type MyHonkAppServices = {
  honk: IHonk;
  api: MyApi;
}

function coolThunk(name) {
  return function({ api }: MyHonkAppServices) {
    return api.getSomething(name);
  };
}

injector's People

Contributors

decoy avatar greenkeeper[bot] avatar

Watchers

 avatar  avatar

injector's Issues

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

An in-range update of @types/jest is breaking the build 🚨

The devDependency @types/jest was updated from 24.0.9 to 24.0.10.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of ts-jest is breaking the build 🚨

The devDependency ts-jest was updated from 24.0.0 to 24.0.1.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

ts-jest is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Commits

The new version differs by 23 commits.

  • b43b3c1 chore(release): 24.0.1
  • 2d91a37 chore: update package-lock
  • 485d3f7 build(deps): bump semver from 5.6.0 to 5.7.0 (#1043)
  • 2bd2534 build(deps-dev): bump @types/node from 10.14.3 to 10.14.4 (#1041)
  • bdba560 build(deps-dev): bump @types/node from 10.14.2 to 10.14.3 (#1038)
  • 08766bf build(deps-dev): bump @types/node from 10.14.1 to 10.14.2 (#1036)
  • 5f92fd2 build(deps-dev): bump js-yaml from 3.12.2 to 3.13.0 (#1034)
  • a9c79e9 build(deps-dev): bump @types/yargs from 12.0.9 to 12.0.10 (#1032)
  • 245ab29 build(deps-dev): bump eslint from 5.15.2 to 5.15.3 (#1031)
  • 4e72e59 build(deps-dev): bump eslint from 5.15.1 to 5.15.2 (#1030)
  • fb7dd55 feat(config): specify package.json location (#823) (#1013)
  • 279edcd build(deps-dev): bump tslint from 5.13.1 to 5.14.0 (#1028)
  • 8b93228 build(deps-dev): bump @types/node from 10.12.30 to 10.14.1 (#1027)
  • b825c7f build(deps-dev): bump @types/lodash.memoize from 4.1.4 to 4.1.6 (#1014)
  • 6f0ab80 build(deps-dev): bump @types/lodash.merge from 4.6.5 to 4.6.6 (#1015)

There are 23 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

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.