Coder Social home page Coder Social logo

aystore's Introduction

@saber2pr/aystore

Async Store! 异步的 Store!

# from npm
npm install @saber2pr/aystore

# from github
git clone https://github.com/Saber2pr/-saber2pr-aystore.git

API

  1. store.body

返回 store requestListener 处理程序

const store = AyStore()

store.body()

// store.use(store.body())
  1. store.use

注册一个 requestListener

const store = AyStore()

store.use(async ctx => {
  if (ctx.request.url === '/hello') {
    ctx.response.end('hello!')
  }
})
  1. store.context

在 requestListener 间传递的上下文

const store = AyStore({ name: 'aystore' })

store.use(async (ctx, next) => {
  ctx.name // 'aystore'
  store.context // 'aystore'
})
  1. store.request

发起一个请求

const store = AyStore()

store.use(async ctx => {
  if (ctx.request.url === '/hello') {
    ctx.response.end('hello!')
  }
})

store.request({ url: '/hello' }).then(res => {
  console.log(res) // hello!
})

传递参数

const store = AyStore()

store.use(async ctx => {
  if (ctx.request.url === '/hello') {
    ctx.response.end('hello!', ctx.request.body)
  }
})

store.request({ url: '/hello', body: 'world!' }).then(res => {
  console.log(res) // ['hello!', 'world!']
})

协变

Typescript 对函数参数类型默认允许双向协变,但是它不安全! 例如组合两个 AyStore 的 body 会涉及到 对 context 的依赖,如果允许双向协变,它将不会报错!

强烈建议开启:--strictFunctionTypes

{
  "compilerOptions": {
    "strictFunctionTypes": true
  }
}

start

npm install
npm start

npm run dev

Author: saber2pr


develope and test

you should write ts in /src

you should make test in /src/test

export your core in /src/index.ts!

aystore's People

Contributors

saber2pr avatar

Watchers

 avatar  avatar

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.