# statenext-api

> statenext is web server builder.

Latest version **1.2.2** (published 2020-05-27) · ISC license · 0 weekly downloads

## Install

```sh
npm install statenext-api
pnpm add statenext-api
yarn add statenext-api
bun add statenext-api
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.2 |
| Published | 2020-05-27 |
| First published | 2020-05-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 108.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | tanamettpk |
| Maintainers | tanamet.tpk |

## Links

- npm: https://www.npmjs.com/package/statenext-api
- Repository: https://github.com/TanametTpk/statenext-api
- Homepage: https://github.com/TanametTpk/statenext-api#readme
- Issues: https://github.com/TanametTpk/statenext-api/issues
- npm.io page: https://npm.io/package/statenext-api

## Dependencies (4)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [express](https://npm.io/package/express.md) ^4.17.1
- [socket.io](https://npm.io/package/socket.io.md) ^2.3.0

## Recent versions

- 1.2.2 (latest) — 2020-05-27
- 1.2.1 — 2020-05-27
- 1.2.0 — 2020-05-27
- 1.1.0 — 2020-05-27
- 1.0.1 — 2020-05-20
- 1.0.0 — 2020-05-20

## README

# What is statenext-api ? | statenext-api คืออะไร ?

This is package that help you create API server with typescript faster.
นี้คือ package ที่จะช่วยให้สร้าง API server ได้เร็วขึ้นด้วย typescript 

# What can statenext-api do ? | statenext-api ทำอะไรได้บ้าง​ ?
statenext-api is build on top of Express, I try to make developer write less code and more reuse.
statenext-api สร้างต่อยอดจาก Express, เราพยายามจะทำให้ developer เขียน code น้อยลง แต่ reuse ได้เยอะขึ้น

## features
- meta-data based | ใช้ meta-data เป็นหลัก
- one service all trasportation | สร้าง service เดียวใช้ได้ทุกรูปแบบ

# Installation

`npm i --save statenext-api`

then import

```
import ServerBuilder, {SNRequest} from 'statenext-api'
import { Router, SocketBoardcastPayload } from 'statenext-api/lib/core/SystemManagement';

let routes:Router = {
    index: {
        path: "/",
        method: "get",
        middlewares: [],
        controller: "xs",
        action: "get",
        socket:{
            event_name: "get-xs",
        }
    },
    api: {
        hello: {
            path: "/",
            method: "get",
            middlewares: [],
            controller: "xs",
            action: "get",
        }
    }
}


let services = {
    xs:{
        get: async (req:SNRequest) => {

            req._receivers = [req.body.to]
            return ["hello world", req.body.message];
            
        }
    },

}

let server = new ServerBuilder()

server.setup({
    // services,
}).server.listen(8080, () => {
    console.log("listen on 8080")
})

```

# Usages

use path create routes

### example project structure

- server.ts
- /routes
    - index.ts
    - /api
        - hello.ts
- /services
    - /xs
        - get.ts

### routes example
```
export default {
    path: "/",
    method: "get",
    middlewares: [],
    controller: "xs",
    action: "get",
    socket:{
        event_name: "get-xs",
        boardcast:{
            type: "personal",
            event_name: "new-xs"
        }
    }
}
```

### service example
```
import { SNRequest } from "statenext-api";

export default async (req:SNRequest) => {

    req._receivers = [req.body.to]
    return ["hello world", req.body.message];
            
}
```

### server example
```
import ServerBuilder from 'statenext-api'

let server = new ServerBuilder()

server.usePath(__dirname + '/routes', __dirname + '/services')

server.setup({
    // services,
}).server.listen(8080, () => {
    console.log("listen on 8080")
})

```

# CORS

you can allow cors by env like
คุณสามารถ allow cors ได้เพียงแค่ใช้ env

```
ALLOW_ORIGIN=http://localhost
```
or multiple origins
ถ้าจะใช้หลายๆอันก็ได้

```
ALLOW_ORIGIN=<domain1>
ALLOW_ORIGIN0=<domain2>
ALLOW_ORIGIN1=<domain3>
ALLOW_ORIGIN2=<domain4>
ALLOW_ORIGIN3=<domain5>
```

---
_Source: https://npm.io/package/statenext-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
