镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content
MupliPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

API module for Mupli

Description

Module to setup Rest api for multiple projects without boilerplate code. add

Specs

  • module name : api
  • path prefix: /api
  • supported files : js, json

project project

/config/apps.json

{
    "customProjectName": {
        "hosts": ["localhost", 
            // "support multi domains", "my-local", 
        ],
        "modules": ["api",
        //some other if needed "page" ,"store", "react", "users", "security", "security-auth", "aws", "files", "services", "products", "media", "cron"
        ]
    },
}

/app/app.js

import {Mupli} from "mupli-core"
import {ApiModule} from "mupli-api"


Mupli.init()
    .module(new ApiModule())
    .listen(3000)

/app/customProjectName/api ./someFile.js

import {isMethod} from "mupli-middlewares"

// -- /api/someFile
export function init(ctx) {
    return "init OK"

}
// -- /api/someFile/super 
export async function super(ctx) {
    return ctx.res.status(201).json({
        message: "super accepted"
    })
}
// -- /api/someFile/doSomething 
export const doSomething = [
    //... middlewares if needed
    isAuthenticated(),
    isMethod("post"),
    hasRole("POSTMAN"),
    hasHeader("header"),
    isResourceOwner(),
    // isFileUpload()    

   controllerAction
]

const controllerAction =  async (ctx) => {
    //..your logic here 

    return {
        message: "post test"
    } // 200 OK
}

Got to:

  • GET localhost:3000/api/someFile
  • GET localhost:3000/api/someFile/super
  • POST localhost:3000/api/someFile/doSomething

other example

Example with mupli-middleware and executeOn to handle POST,GET,PATCH,DELETE methods for one uri.

file: project/api/users.js

// path - /api/users/test
export const test = [
    isAuthenticated(),
    async (ctx) => ctx.res.ok().text("OK"),
]

// path - /api/users
export const init = [
    isAuthenticated(),
    executeOn(isMethod("get"), findUsers),
    executeOn(isMethod("post"), [ validateAjv(User.SCHEMA), createUser ]),
    executeOn(isMethod("put"),  [ validateAjv(User.SCHEMA), updateUser ]),
    executeOn(isMethod("patch"), partialUserUpdate),
    (ctx) => ctx.res.notFound(),
];

Note: It is encuraged to create own middlewares for your needs for example:

  • isRequest(method, mandatoryHeaders... )
  • loadProductById...
  • isOauth
  • hasProduct
  • isJsonFormatPatch..

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages