diff --git a/.changeset/three-students-grin.md b/.changeset/three-students-grin.md
new file mode 100644
index 0000000..f57380b
--- /dev/null
+++ b/.changeset/three-students-grin.md
@@ -0,0 +1,5 @@
+---
+"@dreamkit/site": patch
+---
+
+Update examples
diff --git a/packages/site/src/pages/api/$route/_examples/optional-search-param.tsx b/packages/site/src/pages/api/$route/_examples/optional-search-param.tsx
index 3fb140e..d0a04e4 100644
--- a/packages/site/src/pages/api/$route/_examples/optional-search-param.tsx
+++ b/packages/site/src/pages/api/$route/_examples/optional-search-param.tsx
@@ -4,10 +4,11 @@ import { $route, Link, s } from "dreamkit";
export const homeRoute = $route.path("/").create(() => (
<>
{/* @ts-expect-error */}
- section
+ link without params
+
{/* @ts-expect-error */}
- section with params
+ link with params
>
));
@@ -15,10 +16,4 @@ export const homeRoute = $route.path("/").create(() => (
export const sectionRoute = $route
.path("/section")
.params({ name: s.string().optional() })
- .create(({ params }) => {
- return (
-
{start.result}
- - > - ); +export const start = $api.title("Start").create(async () => { + await new Promise((resolve) => setTimeout(resolve, 1000)); + const id = Date.now(); + if (id % 2) throw new Error("Random error"); + return id; }); + +export default $route + .path("/") + .api({ start }) + .create(({ api }) => { + const start = createAction(api.start); + return ( + <> ++ +
+ + > + ); + }); diff --git a/packages/site/src/pages/api/input/_examples/controlled-checkbox.tsx b/packages/site/src/pages/api/input/_examples/controlled-checkbox.tsx index b1966e3..74a1933 100644 --- a/packages/site/src/pages/api/input/_examples/controlled-checkbox.tsx +++ b/packages/site/src/pages/api/input/_examples/controlled-checkbox.tsx @@ -1,9 +1,13 @@ -// title: Controlled number +// title: Controlled checkbox import { $route, Input } from "dreamkit"; -import { createEffect, createSignal } from "solid-js"; +import { createSignal } from "solid-js"; export default $route.path("/").create(() => { const [bool, setBool] = createSignal(false); - createEffect(() => console.log("bool", bool())); - return ; + return ( + <> +value: {JSON.stringify(bool())}
+ + > + ); }); diff --git a/packages/site/src/pages/api/input/_examples/controlled-file.tsx b/packages/site/src/pages/api/input/_examples/controlled-file.tsx index 0f94256..7e31a7c 100644 --- a/packages/site/src/pages/api/input/_examples/controlled-file.tsx +++ b/packages/site/src/pages/api/input/_examples/controlled-file.tsx @@ -5,5 +5,10 @@ import { createEffect, createSignal } from "solid-js"; export default $route.path("/").create(() => { const [file, setFile] = createSignalvalue: {file()?.name}
+ + > + ); }); diff --git a/packages/site/src/pages/api/input/_examples/controlled-number.tsx b/packages/site/src/pages/api/input/_examples/controlled-number.tsx index 140b7eb..d468af0 100644 --- a/packages/site/src/pages/api/input/_examples/controlled-number.tsx +++ b/packages/site/src/pages/api/input/_examples/controlled-number.tsx @@ -6,12 +6,15 @@ export default $route.path("/").create(() => { const [number, setNumber] = createSignalvalue: {JSON.stringify(number())}
+ { + if (value === null || value < 100) setNumber(value); + }} + /> + > ); }); diff --git a/packages/site/src/pages/api/input/_examples/controlled-text.tsx b/packages/site/src/pages/api/input/_examples/controlled-text.tsx index c5e7615..a6ddd82 100644 --- a/packages/site/src/pages/api/input/_examples/controlled-text.tsx +++ b/packages/site/src/pages/api/input/_examples/controlled-text.tsx @@ -5,6 +5,9 @@ import { createSignal } from "solid-js"; export default $route.path("/").create(() => { const [name, setName] = createSignal(""); return ( - setName(value.toUpperCase())} /> + <> +value: {name()}
+ setName(value.toUpperCase())} /> + > ); }); diff --git a/packages/site/src/pages/api/ioc/_examples/basic-usage.tsx b/packages/site/src/pages/api/ioc/_examples/basic-usage.tsx index 4d9dcf0..b064ddd 100644 --- a/packages/site/src/pages/api/ioc/_examples/basic-usage.tsx +++ b/packages/site/src/pages/api/ioc/_examples/basic-usage.tsx @@ -1,33 +1,31 @@ // title: Basic usage -import { context, IocClass, ServiceClass } from "dreamkit"; +import { $route, context, IocClass } from "dreamkit"; -class Child extends IocClass({}) { - process(value: string) { - return value; - } +abstract class ThirdModel { + abstract fetch(): string; } -class CustomChild extends Child { - override process(value: string): string { - return value.toUpperCase(); +class MyModel extends IocClass({ ThirdModel }) { + fetch() { + return this.thirdModel.fetch(); } } -class Parent extends IocClass({ Child }) { - process(value: string) { - return this.child.process(value); +export default $route.path("/").create(() => { + class ThirdModelImpl extends ThirdModel { + override fetch() { + return "resolved"; + } } -} - -export class TestService extends ServiceClass({ Parent }) { - onStart() { - const value0 = this.parent.process("hello"); - const value1 = new Parent({ child: new Child() }).process("hello"); - const value2 = context - .fork() - .register(Child, { useClass: CustomChild }) - .resolve(Parent) - .process("world"); - console.log([value0, value1, value2]); // ["hello", "WORLD"] - } -} + const manualModel = new MyModel({ thirdModel: new ThirdModelImpl() }); + const autoModel = context + .fork() + .register(ThirdModel, { value: new ThirdModelImpl() }) + .resolve(MyModel); + return ( + <> +{manualModel.fetch()}
+{autoModel.fetch()}
+ > + ); +}); diff --git a/packages/site/src/pages/api/ioc/_examples/optional.tsx b/packages/site/src/pages/api/ioc/_examples/optional.tsx index 7fdcdeb..917d1c8 100644 --- a/packages/site/src/pages/api/ioc/_examples/optional.tsx +++ b/packages/site/src/pages/api/ioc/_examples/optional.tsx @@ -1,28 +1,39 @@ // title: Optional -import { IocClass, iocParam, ServiceClass } from "dreamkit"; +import { context, IocClass, iocParam, ServiceClass } from "dreamkit"; -class Child extends IocClass({}) { - process(value: string) { - return value; +class StringModel { + toUpperCase(value: string) { + return value.toUpperCase(); } } -class Parent extends IocClass({ child: iocParam(Child).optional() }) { - process(value: string) { - return this.child?.process(value); +class MyModel extends IocClass({ + string: iocParam(StringModel).optional(), +}) { + toUpperCase(value: string) { + return this.string?.toUpperCase(value); } } -export class TestService extends ServiceClass({}) { +export class MyService extends ServiceClass({}) { onStart() { - const value1 = new Parent({}).process("hello"); - const value2 = new Parent({ - child: { - process(value) { + const model1 = new MyModel({}); + const model2 = new MyModel({ + string: { + toUpperCase(value) { return value.toUpperCase(); }, }, - }).process("world"); - console.log([value1, value2]); // [undefined, "WORLD"] + }); + const model3 = context + .fork() + .register(StringModel, { useFactory: () => new StringModel() }) + .resolve(MyModel); + + console.log({ + model1: model1.toUpperCase("text"), // undefined + model2: model2.toUpperCase("text"), // "TEXT" + model3: model3.toUpperCase("text"), // "TEXT" + }); } } diff --git a/packages/site/src/pages/api/link/_examples/basic-usage.tsx b/packages/site/src/pages/api/link/_examples/basic-usage.tsx index 72e9871..13ef183 100644 --- a/packages/site/src/pages/api/link/_examples/basic-usage.tsx +++ b/packages/site/src/pages/api/link/_examples/basic-usage.tsx @@ -1,12 +1,12 @@ // title: Basic usage import { $route, Link, s } from "dreamkit"; -export const otherRoute = $route - .path("/other") - .params({ value: s.string() }) +export const sectionRoute = $route + .path("/section") + .params({ name: s.string() }) .create(({ params }) => ( <> -{params.value}
+section: {params.name}
{/* @ts-expect-error */} Go to home > @@ -15,8 +15,8 @@ export const otherRoute = $route export default $route.path("/").create(() => ( <> {/* @ts-expect-error */} - - Go to other + + Go to section one > )); diff --git a/packages/site/src/pages/api/route-path/_examples/basic-usage.tsx b/packages/site/src/pages/api/route-path/_examples/basic-usage.tsx index c7e71d8..fe8f4bf 100644 --- a/packages/site/src/pages/api/route-path/_examples/basic-usage.tsx +++ b/packages/site/src/pages/api/route-path/_examples/basic-usage.tsx @@ -1,12 +1,12 @@ // title: Basic usage import { $route, routePath, s } from "dreamkit"; -export const userRotue = $route +export const userRoute = $route .params({ id: s.number() }) - .path(({ id }) => `/users/${id}`) - .create(({ params }) => <>{params.id}>); + .path(({ id }) => `/user/${id}`) + .create(({ params }) => <>id: {params.id}>); export default $route.path("/").create(() => { // @ts-expect-error - return User 1; + return User 1; }); diff --git a/packages/site/src/pages/api/schema/_examples/validate.tsx b/packages/site/src/pages/api/schema/_examples/validate.tsx index 93470bd..7764252 100644 --- a/packages/site/src/pages/api/schema/_examples/validate.tsx +++ b/packages/site/src/pages/api/schema/_examples/validate.tsx @@ -1,19 +1,18 @@ // title: Validate -import { $route, s, type InferType } from "dreamkit"; +import { $route, Input, s } from "dreamkit"; +import { createSignal } from "solid-js"; -const type = s.object({ - name: s.string().min(3), - enabled: s.bool(), -}); +const type = s.string().min(3).max(5); export default $route.path("/").create(() => { - const value: InferTypevalidate: {JSON.stringify(type.validate(value), null, 2)}
+ ++ {"errors: "} + {JSON.stringify(type.validate(name()), null, 2)} +
> ); }); diff --git a/packages/site/src/pages/api/service/_examples/basic-usage.tsx b/packages/site/src/pages/api/service/_examples/basic-usage.tsx index 57a6b8a..765fad8 100644 --- a/packages/site/src/pages/api/service/_examples/basic-usage.tsx +++ b/packages/site/src/pages/api/service/_examples/basic-usage.tsx @@ -1,13 +1,48 @@ // title: Basic usage -import { $route, ServiceClass } from "dreamkit"; +import { $api, $route, IocContext, kind, ServiceClass } from "dreamkit"; +import { createResource } from "solid-js"; -export class AppService extends ServiceClass({}) { +class CounterModel { + static { + // [important] save a kind to not break `instanceof` during development + kind(this, "CounterModel"); + } + value = 0; +} + +export class CounterService extends ServiceClass({ IocContext }) { onStart() { - console.log("started"); // edit this line and save - return () => console.log("stopped"); + const counter = new CounterModel(); + const interval = setInterval(() => { + counter.value += 1; + }, 1000); + this.iocContext.register(CounterModel, { value: counter }); + return () => { + this.iocContext.unregister(CounterModel); + clearInterval(interval); + }; } } -export const homeRoute = $route.path("/").create(() => { - return <>hello world>; -}); +const fetchCounterValue = $api + .self({ + CounterModel, + }) + .create(function () { + return this.counterModel.value; + }); + +export default $route + .path("/") + .api({ fetchCounterValue }) + .create(({ api }) => { + const [counterValue, { refetch }] = createResource(api.fetchCounterValue); + return ( + <> +value: {counterValue.latest}
++ +
+ > + ); + }); diff --git a/packages/site/src/pages/examples/sqlite.code.tsx b/packages/site/src/pages/examples/sqlite.code.tsx index fb8480f..206db8d 100644 --- a/packages/site/src/pages/examples/sqlite.code.tsx +++ b/packages/site/src/pages/examples/sqlite.code.tsx @@ -47,7 +47,7 @@ export const fetchUsers = $api export const prueba = $route .api({ fetchUsers }) .params(fetchUsers.params) - .path("/users2") + .path("/users") .create(({ api, params, setParams }) => { const [users] = createResource(() => ({ ...params }), api.fetchUsers); return (