How to use the @plumier/core.ActionResult.fromContext function in @plumier/core

To help you get started, we’ve selected a few @plumier/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github plumier / plumier / packages / serve-static / src / index.ts View on Github external
async execute(invocation: Readonly): Promise {
        if(this.option.rootPath && !this.option.rootPath.startsWith("/"))
            this.option.rootPath = "/" + this.option.rootPath
        const rootPath = this.option.rootPath || ""
        //no route = no controller = possibly static file request
        if (!invocation.ctx.route && invocation.ctx.path.toLowerCase().startsWith(rootPath.toLowerCase())) {
            //check if not in root path then proceed
            //execute action result directly here, so if file not found it will possible to chain with next middleware
            try {
                const path = invocation.ctx.path.substr(rootPath.length)
                const result = new FileActionResult(path, this.option)
                await result.execute(invocation.ctx)
                return ActionResult.fromContext(invocation.ctx)
            }
            catch (e) {
                if (e.status && e.status === 404)
                    return invocation.proceed()
                else
                    throw e
            }
        }
        else
            return invocation.proceed()
    }
}