66 lines
1.6 KiB
TypeScript
66 lines
1.6 KiB
TypeScript
////
|
|
// Helpers for writing NOSE webapps & cli commands
|
|
//
|
|
// Be *very careful* modifying this file, as people's scripts and www's will depend
|
|
// on the API. We need to eventually version it and provide backwards compat.
|
|
//
|
|
// Access them in your command or webapp:
|
|
// import { css } from "@nose"
|
|
|
|
import { Hono } from "hono"
|
|
import { type Handler, toResponse } from "./webapp"
|
|
|
|
//
|
|
// command helpers
|
|
//
|
|
|
|
// (none for now)
|
|
|
|
|
|
|
|
//
|
|
// webapp helpers
|
|
//
|
|
|
|
const transpiler = new Bun.Transpiler({ loader: 'tsx' })
|
|
|
|
export function css(strings: TemplateStringsArray, ...values: any[]) {
|
|
return <style dangerouslySetInnerHTML={
|
|
{
|
|
__html: strings.reduce((result, str, i) => {
|
|
return result + str + (values[i] || '')
|
|
}, '')
|
|
}
|
|
} />
|
|
}
|
|
|
|
export function js(strings: TemplateStringsArray, ...values: any[]) {
|
|
return <script dangerouslySetInnerHTML={
|
|
{
|
|
__html: strings.reduce((result, str, i) => {
|
|
return transpiler.transformSync(result + str + (values[i] || ''))
|
|
}, '')
|
|
}
|
|
} />
|
|
}
|
|
|
|
// for defining routes in your NOSE webapp
|
|
// example:
|
|
// export default routes({
|
|
// "GET /": index,
|
|
// "GET /pets": pets
|
|
// })
|
|
export function routes(def: Record<string, Handler>): Hono {
|
|
const app = new Hono
|
|
|
|
for (const key in def) {
|
|
const parts = key.split(" ") // GET /path
|
|
const method = parts[0] || "GET"
|
|
const path = parts[1] || "/"
|
|
|
|
//@ts-ignore
|
|
app.on(method, path, async c => toResponse(await def[key](c)))
|
|
}
|
|
|
|
return app
|
|
} |