@kenote/api-proxy
TypeScript icon, indicating that this package has built-in type declarations

1.0.46 • Public • Published

@kenote/api-proxy

Automation API proxy module based on the Kenote.js framework.

NPM Version NPM Downloads Gratipay

Usage

import { Context, Controller, Get, NextHandler, Post } from '@kenote/core'
import { getEntrance, getProxyResponse } from '@kenote/api-proxy'
import * as service from '../service'

@Controller('/')
export default class MainController {

  @Get('/:channel/:pathLabel?/:tag?')
  @Post('/:channel/:pathLabel?/:tag?')
  async handler (ctx: Context, next: NextHandler) {
    let { channel, pathLabel } = ctx.params
    let sandbox: Record<string, unknown> = { service }
    try {
      let { 
        payload, 
        notFound, 
        isUser, 
        entrance, 
        setting, 
        authenticationState, 
        serviceModules 
      } = await getEntrance({ channel, pathLabel, getUser, sandbox })(ctx, 'channels')
      if (notFound) return ctx.status(404)
      if (authenticationState?.type === 'jwt' && isUser === 'Unauthorized') {
        return await ctx.status(401).send('Unauthorized')
      }
      let [ type, result ] = await getProxyResponse(entrance, payload)({ serviceModules, setting, logger: console, ctx })
      if (entrance?.native) {
        ctx.setHeader('content-type', type)
        return ctx.send(result)
      }
      return ctx.json(result)
    } catch (error) {
      if (error?.code >= 1000) {
        ctx.json({ error: error.message })
      }
      else {
        return next(error)
      }
    }
  }
}

MIT License.

Readme

Keywords

Package Sidebar

Install

npm i @kenote/api-proxy

Weekly Downloads

4

Version

1.0.46

License

MIT

Unpacked Size

146 kB

Total Files

48

Last publish

Collaborators

  • thondery