Socket
Book a DemoInstallSign in
Socket

@saber2pr/aystore

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@saber2pr/aystore

> Async Store! 异步的 Store!

0.0.4
latest
Source
npmnpm
Version published
Weekly downloads
4
Maintainers
1
Weekly downloads
 
Created
Source

@saber2pr/aystore

Async Store! 异步的 Store!

# from npm
npm install @saber2pr/aystore

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

它用来干什么?

它是浏览器端的虚拟 server 程序

发起 http 请求,你将使用组合中间件的机制来封装接口!

它作为代理去请求后端接口.

API

  • store.body

返回 store requestListener 处理程序

const store = AyStore()

store.body()

// store.use(store.body())
  • store.use

注册一个 requestListener

const store = AyStore()

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

在 requestListener 间传递的上下文

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

store.use(async (ctx, next) => {
  ctx.name // 'aystore'
  store.context // 'aystore'
})
  • 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!

Keywords

store

FAQs

Package last updated on 03 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.