Skip to content

Latest commit

 

History

History
103 lines (79 loc) · 2.37 KB

README.md

File metadata and controls

103 lines (79 loc) · 2.37 KB

store-helper

Upload Store's Helper.

NPM Version NPM Downloads Build Status Gratipay

Installation

$ npm install kenote-store-helper
# Or
$ yarn add kenote-store-helper

Usages

app.ts

import * as http from 'http'
import * as express from 'express'
import * as bodyParser from 'body-parser'
import { StroeOptions } from 'kenote-store-helper'

const store: StroeOptions = {
  type: 'local',
  max_size: '2MB',
  root_dir: 'uploadfiles',
  root_url: 'http://localhost',
  mime_type: ['image/png', 'image/jpeg']
}

const app: express.Application = express()
app.use(bodyParser.json({ limit: '1mb' }))
app.use(bodyParser.urlencoded({ extended: true, limit: '1mb' }))

app.post('/upload', async (req, res, next) => {
  try {
    let result: ProxyResult = await new IStore({ request: req, options: store }).asyncSave(ErrorInfo)
    return res.json(result)
  } catch (error) {
    return res.json(error)
  }
})

http.createServer(app).listen(3000)

function ErrorInfo (code: number, opts?: any) {
  return {
    code,
    message: opts && opts.join(',')
  }
}

store

import { Store, Connect, localProxy } from 'kenote-store-helper'

@Connect({
  proxys: {
    local: new localProxy()
  },
  errors: {
    mimetype: 1001,
    limit: 1002
  }
})
export default class IStore extends Store {}

Custom Proxy

store_proxys/qn.ts

import { IProxy, ProxyOptions, ProxyResult } from 'kenote-store-helper'

export default class Proxy implements IProxy {

  public upload (file: NodeJS.ReadableStream, options: ProxyOptions, done: (err: any, doc: ProxyResult) => void): void {
    // ---
    
  }
}

License

this repo is released under the MIT License.