fix: mustache middleware (#74)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix: mustache middleware * fix error message
authored by Yusuke Wada · committed by GitHub · parent a1f29da · Browse files · 3 files·+15 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+15 added−14 removed
- tests:+2 added−2 removed
- other:+13 added−12 removed
Provenance
- Commit
5882520- Tree
2586a54- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
a1f29da
3 files changed+15 added−14 removed
modified:src/middleware/mustache/mustache.test.ts+2 added-2 removed
20 unmodified lines212223242525262728282930313214 unmodified lines20 unmodified lines }, },})
describe.skip('Mustache by Middleware', () => {describe('Mustache by Middleware', () => { const app = new Hono()
// app.use('*', Middleware.mustache()) app.use('*', Middleware.mustache()) app.get('/', (c) => { return c.render( 'index', { title: 'Hono!' },14 unmodified linesmodified:src/middleware/mustache/mustache.ts+11 added-10 removed
4 unmodified lines56789109101112131415121314151616171819202122232224252627282930313231333435363736383940412 unmodified lines4 unmodified lines
type Partials = { [file: string]: string }
export const mustache = () => { return async (c: Context, next: Function) => { let Mustache: any let Mustache: any try { Mustache = require('mustache') } catch { // Do nothing. }
try { Mustache = await import('mustache') } catch (e) { console.error(`Mustache is not found! ${e}`) throw new Error(`${e}`) return async (c: Context, next: Function) => { if (!Mustache) { throw new Error('If you want to use Mustache Middleware, install mustache module.') }
c.render = async (filename, view = {}, options?) => { const content = await getContentFromKVAsset(`${filename}${EXTENSION}`) if (!content) { throw new Error(`Template "${filename}${EXTENSION}" is not found`) throw new Error(`Template "${filename}${EXTENSION}" is not found or blank.`) }
const partialArgs: { [name: string]: string } = {} if (options) { const partials = options as Partials for (const key of Object.keys(partials)) { const partialContent = await getContentFromKVAsset(`${partials[key]}${EXTENSION}`) if (!partialContent) { throw new Error(`Partial Template "${partials[key]}${EXTENSION}" is not found`) throw new Error(`Partial Template "${partials[key]}${EXTENSION}" is not found or blank.`) } partialArgs[key] = partialContent } }
const output = Mustache.render(content, view, partialArgs) return c.html(output) } await next()2 unmodified linesmodified:src/middleware.ts+2 added-2 removed
3 unmodified lines456788910111213141516171818193 unmodified linesimport { basicAuth } from './middleware/basic-auth/basic-auth'import { bodyParse } from './middleware/body-parse/body-parse'import { cors } from './middleware/cors/cors'import { cookie } from './middleware/cookie/cookie'//import { mustache } from './middleware/mustache/mustache'import { mustache } from './middleware/mustache/mustache'
export class Middleware { static default = defaultMiddleware static poweredBy = poweredBy static logger = logger static basicAuth = basicAuth static bodyParse = bodyParse static cors = cors static cookie = cookie //static mustache = mustache static mustache = mustache}