1.0.9 • Published 5 years ago

mares-api-router v1.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

mares-api-router

mares presentation layer에서 사용하는 router class 입니다. 반드시 라우팅 전에 해당 클래스를 상속받아서 사용합니다.

Installation

npm install --save mares-api-router

Example

GetRouter, PostRouter, DelRouter, PutRouter 가 있으며 사용법은 동일합니다. 먼저 sg-openapi-validator 모듈의 validator 미들웨어를 use 시켜줍니다.

const validator = require('sg-openapi-validator')
app.use(validator())

이후에 다음과 같이 클래스를 정의합니다.

// get.js
const GetRouter = require('mares-api-router').GetRouter;
module.exports = class extends GetRouter {

    constructor(url) {
        super(url);
    }

    fetcher() {
        return async (req, res, next) => {
            try {
                next();
            } catch (e) {
                next(e);
            }
        };
    }

    responder() {
        return async (req, res) => {
            res.success({
                row: req.receiver.getObject()
            })
        }
    }
}

마지막으로 express의 router를 이용하여 미들웨어 호출 스택을 정의합니다.

const router = require('express').Router()
const GET = require('./get.js')
const gets = new GET('url')

router.post(
	post.getUrl(), // mares-api-router에서 생성자에서 넘긴 url을 가져옵니다.
	post.validator(), // mares-api-router의 기본 미들웨어입니다. 
	post.fetcher(),
	post.responder()
)

해당 미들웨어는 api 요청시 들어온 parameter, body 데이터의 유효성을 검사합니다. 반드시 sg-openapi-validator 모듈의 validator 미들웨어를 먼저 use 시켜줘야 합니다. 해당 모듈의 대한 자세한 설명은 문서를 참고하시기 바랍니다.

최종적으로 mares-api-router의 validator 미들웨어에서는 1. 스트링 형태로만 들어온 데이터 타입을 스펙 정의에 맞게 알맞게 변환해주고 2. query 필드의 유효성 검증 3. body 필드의 유효성 검증

3가지를 수행합니다.

즉 validator 에서는 내부적으로 sg-openapi-validator의 바인딩된 미들웨어를 사용합니다.

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago