diff --git a/core/server/mod.ts b/core/server/mod.ts index e9f800455..345c7c37c 100644 --- a/core/server/mod.ts +++ b/core/server/mod.ts @@ -78,7 +78,7 @@ const createResponse = ( }; export default class Server implements Fastro { - constructor(options?: Map) { + constructor(options?: Record) { this.serverOptions = options ?? {}; this.#handler = this.#createHandler(); this.#addPropsEndpoint(); @@ -510,6 +510,7 @@ if (root) fetchProps(root); ctx.url = url; ctx.server = this; ctx.kv = this.serverOptions["kv"]; + ctx.store = this.store; return ctx; }; diff --git a/deno.json b/deno.json index ba8e9c8d4..f9b147c03 100644 --- a/deno.json +++ b/deno.json @@ -45,10 +45,11 @@ "params_query": "deno run -A examples/params_query.ts", "static_file_string": "deno run -A examples/static_file_string.ts", "static_file_image": "deno run -A examples/static_file_image.ts", - "markdown_middleware": "GITHUB_CLIENT_ID=abe04d0d76dd2b169380 GITHUB_CLIENT_SECRET=e9a16ec40a87c8a6d1f4eff3ea955248059b67ee deno run -A --unstable-kv examples/markdown_middleware.ts", + "markdown_middleware": "deno run -A --env --unstable-kv examples/markdown_middleware.ts", "group": "deno run -A examples/group.ts", "app_middleware": "deno run -A examples/app_middleware.ts", "route_middleware": "deno run -A examples/route_middleware.ts", - "server_rendering": "deno run -A examples/server_rendering.tsx" + "server_rendering": "deno run -A examples/server_rendering.tsx", + "store": "deno run -A --env examples/store.ts" } } diff --git a/examples/store.ts b/examples/store.ts new file mode 100644 index 000000000..8a9d5ac4a --- /dev/null +++ b/examples/store.ts @@ -0,0 +1,53 @@ +import fastro, { Context, HttpRequest } from "@app/mod.ts"; + +const f = new fastro(); + +// set default value for the store +f.store.set("hello", "hello world"); + +// update default value +f.post( + "/", + (_req: HttpRequest, ctx: Context) => { + ctx.store.set("hello", "hello world v2"); + return ctx.send("Helo world", 200); + }, +); + +// update default value with TTL +f.post( + "/ttl", + (_req: HttpRequest, ctx: Context) => { + ctx.store.set("hello", "world", 1000); + return ctx.send("ttl", 200); + }, +); + +// save store to github +f.post( + "/commit", + async (_req: HttpRequest, ctx: Context) => { + await ctx.store.commit(); + return ctx.send("commit", 200); + }, +); + +// destroy file +f.post( + "/destroy", + async (_req: HttpRequest, ctx: Context) => { + await ctx.store.destroy(); + return ctx.send("destroy", 200); + }, +); + +// get the value +f.get( + "/", + async (_req: HttpRequest, ctx: Context) => { + const res = await ctx.store.get("hello"); + return Response.json({ value: res }); + }, +); + +await f.serve();