Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/forthedamn/koa-context-loader

load anything to koa context
https://github.com/forthedamn/koa-context-loader

context file-loader koa koa-context koa-context-loader

Last synced: 21 days ago
JSON representation

load anything to koa context

Awesome Lists containing this project

README

        

# koa-context-loader
[![NPM version][npm-image]][npm-url]
[![build status](https://img.shields.io/travis/forthedamn/koa-context-loader.svg)](https://travis-ci.org/forthedamn/koa-context-loader)
[![code coverage](https://img.shields.io/codecov/c/github/forthedamn/koa-context-loader.svg)](https://codecov.io/gh/forthedamn/koa-context-loader)
[![license](https://img.shields.io/github/license/forthedamn/koa-context-loader.svg)](LICENSE)

[npm-image]: https://img.shields.io/npm/v/koa-context-loader.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-context-loader

> load file module into app.context

## Install

```sh
npm install koa-context-loader

yarn add koa-context-loader
```

## Usage

```js
// ./service/user.js

module.exports = {
getUserId() {
return 123;
}
}

// ./app.js
const Koa = require('koa');
const app = new Koa();
const loader = require('koa-context-loader');
const path = require('path');

const options = {
service: path.resolve(__dirname, './service'),
alias: 'path'
}

loader(app.context, options)

app.use((ctx, next) => {
// ctx[alias][filename][export_method]
const id = ctx.service.user.getUserId(); // id:123
});

```

### API

#### options {alias: keyof options, path: string}

```
options = {
alias: path
}
```

> path must be absolute