Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Programmatic | ||
|
||
This example shows programmatic usage of Akte. This is helpful for running Akte in various environments, including serverless. | ||
|
||
Peek inside `programmatic.ts` to see some example usage of Akte API. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { defineAkteApp, defineAkteFile, defineAkteFiles } from "akte"; | ||
|
||
type GlobalData = { siteDescription: string }; | ||
|
||
// Unique file | ||
const index = defineAkteFile<GlobalData>().from({ | ||
path: "/", | ||
data() { | ||
// We assume those are sourced one way or another | ||
const posts = { | ||
"/posts/foo": "foo", | ||
"/posts/bar": "bar", | ||
"/posts/baz": "bar", | ||
}; | ||
|
||
return { posts }; | ||
}, | ||
render(context) { | ||
const posts = Object.entries(context.data.posts).map( | ||
([href, title]) => /* html */ `<li><a href="${href}">${title}</a></li>`, | ||
); | ||
|
||
return /* html */ `<main> | ||
<h1>basic typescript</h1> | ||
<p>${context.globalData.siteDescription}</p> | ||
<ul> | ||
${posts.join("\n")} | ||
</ul> | ||
</main> | ||
`; | ||
}, | ||
}); | ||
|
||
// Multiple files | ||
const posts = defineAkteFiles<GlobalData>().from({ | ||
path: "/posts/:slug", | ||
bulkData() { | ||
// We assume those are sourced one way or another | ||
const posts = { | ||
"/posts/foo": { | ||
title: "foo", | ||
body: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod, dignissimos enim qui cupiditate provident cumque distinctio id reiciendis quia consectetur fugiat dolorem mollitia laborum libero natus et, vero voluptatibus dolorum?", | ||
}, | ||
"/posts/bar": { | ||
title: "bar", | ||
body: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod, dignissimos enim qui cupiditate provident cumque distinctio id reiciendis quia consectetur fugiat dolorem mollitia laborum libero natus et, vero voluptatibus dolorum?", | ||
}, | ||
"/posts/baz": { | ||
title: "baz", | ||
body: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Quod, dignissimos enim qui cupiditate provident cumque distinctio id reiciendis quia consectetur fugiat dolorem mollitia laborum libero natus et, vero voluptatibus dolorum?", | ||
}, | ||
}; | ||
|
||
return posts; | ||
}, | ||
render(context) { | ||
return /* html */ `<main> | ||
<h1>${context.data.title}</h1> | ||
<p>${context.data.body}</p> | ||
</main>`; | ||
}, | ||
}); | ||
|
||
export const app = defineAkteApp({ | ||
files: [index, posts], | ||
globalData: () => { | ||
return { | ||
siteDescription: "A really simple website", | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "akte.examples.programmatic", | ||
"version": "0.0.0", | ||
"private": true, | ||
"type": "module", | ||
"devDependencies": { | ||
"akte": "^0.0.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { app } from "./akte.app"; | ||
|
||
// Renders all files and returns them. | ||
const files = await app.renderAll(); | ||
|
||
// Renders all files and returns them. | ||
await app.writeAll({ files, outDir: "my-out-dir" }); | ||
|
||
// Renders and writes all files to the config output directory. | ||
await app.buildAll(); | ||
|
||
// Looks up the Akte file responsible for rendering the given path. | ||
const match = app.lookup("/foo"); | ||
|
||
// Renders a match from `app.lookup()` | ||
const file = await app.render(match); |