@@ -46,10 +46,35 @@ npm i @chubbyts/chubbyts-undici-api@^2.0.0
4646
4747### Handler
4848
49- #### my-model.ts
50-
5149``` ts
5250import { z } from ' zod' ;
51+ import { createEncoder } from ' @chubbyts/chubbyts-decode-encode/dist/encoder' ;
52+ import { createJsonTypeEncoder }
53+ from ' @chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder' ;
54+ import { ServerRequest } from ' @chubbyts/chubbyts-undici-server/dist/server' ;
55+ import { createDecoder } from ' @chubbyts/chubbyts-decode-encode/dist/decoder' ;
56+ import { createJsonTypeDecoder }
57+ from ' @chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder' ;
58+ import { createListHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/list' ;
59+ import type {
60+ FindModelById ,
61+ PersistModel ,
62+ RemoveModel ,
63+ ResolveModelList ,
64+ } from ' @chubbyts/chubbyts-undici-api/dist/repository' ;
65+ import { createCreateHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/create' ;
66+ import { createReadHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/read' ;
67+ import { createUpdateHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/update' ;
68+ import { createDeleteHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/delete' ;
69+ import {
70+ numberSchema ,
71+ sortSchema ,
72+ stringSchema ,
73+ createEnrichedModelListSchema ,
74+ createModelSchema ,
75+ createModelListSchema ,
76+ createEnrichedModelSchema ,
77+ } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
5378import type {
5479 EnrichedModel ,
5580 EnrichedModelList ,
@@ -62,22 +87,13 @@ import type {
6287 ModelListSchema ,
6388 ModelSchema ,
6489} from ' @chubbyts/chubbyts-undici-api/dist/model' ;
65- import {
66- numberSchema ,
67- sortSchema ,
68- stringSchema ,
69- createEnrichedModelListSchema ,
70- createModelSchema ,
71- createModelListSchema ,
72- createEnrichedModelSchema ,
73- } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
7490
75- export const inputMyModelSchema = z
76- .object ({ name: stringSchema , value: stringSchema })
77- .strict ();
91+ export const inputMyModelSchema = z .object ({
92+ name: stringSchema ,
93+ value: stringSchema ,
94+ }).strict ();
7895
7996export type InputMyModelSchema = typeof inputMyModelSchema ;
80-
8197export type InputMyModel = InputModel <InputMyModelSchema >;
8298
8399export const inputMyModelListSchema = z
@@ -95,15 +111,11 @@ export type InputMyModelList = InputModelList<InputMyModelListSchema>;
95111
96112export type MyModelSchema = ModelSchema <InputMyModelSchema >;
97113
98- export const myModelSchema: MyModelSchema =
99- createModelSchema (inputMyModelSchema );
114+ export const myModelSchema: MyModelSchema = createModelSchema (inputMyModelSchema );
100115
101116export type MyModel = Model <InputMyModelSchema >;
102117
103- export type MyModelListSchema = ModelListSchema <
104- InputMyModelSchema ,
105- InputMyModelListSchema
106- >;
118+ export type MyModelListSchema = ModelListSchema <InputMyModelSchema , InputMyModelListSchema >;
107119
108120export const myModelListSchema: MyModelListSchema = createModelListSchema (
109121 inputMyModelSchema ,
@@ -114,48 +126,52 @@ export type MyModelList = ModelList<InputMyModelSchema, InputMyModelListSchema>;
114126
115127export type EnrichedMyModelSchema = EnrichedModelSchema <InputMyModelSchema >;
116128
117- export const enrichedMyModelSchema: EnrichedMyModelSchema =
118- createEnrichedModelSchema (inputMyModelSchema );
129+ export const enrichedMyModelSchema: EnrichedMyModelSchema = createEnrichedModelSchema (
130+ inputMyModelSchema ,
131+ );
119132
120133export type EnrichedMyModel = EnrichedModel <InputMyModelSchema >;
121134
122135export type EnrichedMyModelListSchema = EnrichedModelListSchema <
123136 InputMyModelSchema ,
124- InputMyModelListSchema
137+ InputMyModelListSchema ,
125138>;
126139
127140export const enrichedMyModelListSchema: EnrichedMyModelListSchema =
128- createEnrichedModelListSchema (inputMyModelSchema , inputMyModelListSchema );
141+ createEnrichedModelListSchema (
142+ inputMyModelSchema ,
143+ inputMyModelListSchema ,
144+ );
129145
130146export type EnrichedMyModelList = EnrichedModelList <
131147 InputMyModelSchema ,
132- InputMyModelListSchema
148+ InputMyModelListSchema ,
133149>;
134- ```
135150
136- #### my-list-handler.ts
151+ // decoder / encoder
137152
138- ``` ts
139- import { createEncoder } from ' @chubbyts/chubbyts-decode-encode/dist/encoder/encoder' ;
140- import { createJsonTypeEncoder }
141- from ' @chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder' ;
142- import type { InputMyModelListSchema , InputMyModelSchema } from ' ./my-model.js' ;
143- import {
144- enrichedMyModelListSchema ,
145- inputMyModelListSchema ,
146- } from ' ./my-model.js' ;
147- import { ResolveModelList } from ' @chubbyts/chubbyts-undici-api/dist/repository' ;
148- import { InputModelList , ModelList } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
149- import { createListHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/list' ;
150- import { ServerRequest } from ' @chubbyts/chubbyts-undici-server/dist/server' ;
153+ const decoder = createDecoder ([createJsonTypeDecoder ()]);
154+ const encoder = createEncoder ([createJsonTypeEncoder ()]);
151155
152- const resolveModelList: ResolveModelList <
153- InputMyModelSchema ,
154- InputMyModelListSchema
155- > = (
156+ // repository
157+
158+ const resolveModelList: ResolveModelList <InputMyModelSchema , InputMyModelListSchema > = (
156159 modelList : InputModelList <InputMyModelListSchema >,
157160): Promise <ModelList <InputMyModelSchema >> => {};
158- const encoder = createEncoder ([createJsonTypeEncoder ()]);
161+
162+ const findModelById: FindModelById <InputMyModelSchema > = async (
163+ id : string ,
164+ ): Promise <Model <InputMyModelSchema > | undefined > => {};
165+
166+ const persistModel: PersistModel <InputMyModelSchema > = (
167+ model : Model <InputMyModelSchema >,
168+ ): Promise <Model <InputMyModelSchema >> => {};
169+
170+ const removeModel: RemoveModel <InputMyModelSchema > = (
171+ model : Model <InputMyModelSchema >,
172+ ): Promise <void > => {};
173+
174+ // handler
159175
160176const listHandler = createListHandler (
161177 inputMyModelListSchema ,
@@ -165,106 +181,42 @@ const listHandler = createListHandler(
165181);
166182
167183(async () => {
168- const serverRequest = new ServerRequest (' http://localhost:8080/api/pets' , {method: ' GET' });
184+ const serverRequest = new ServerRequest (
185+ ' http://localhost:8080/api/pets' ,
186+ { method: ' GET' },
187+ );
169188 const response = await listHandler (serverRequest );
170189})();
171- ```
172-
173- #### my-create-handler.ts
174-
175- ``` ts
176- import { createDecoder } from ' @chubbyts/chubbyts-decode-encode/dist/decoder/decoder' ;
177- import { createJsonTypeDecoder } from ' @chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder' ;
178- import { createEncoder } from ' @chubbyts/chubbyts-decode-encode/dist/encoder/encoder' ;
179- import { createJsonTypeEncoder } from ' @chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder' ;
180- import type { InputMyModelSchema } from ' ./my-model.js' ;
181- import { enrichedMyModelSchema , inputMyModelSchema } from ' ./my-model.js' ;
182- import type { PersistModel } from ' @chubbyts/chubbyts-undici-api/dist/repository' ;
183- import type { Model } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
184- import { v7 } from ' uuid' ;
185- import { createCreateHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/create' ;
186- import { ServerRequest } from ' @chubbyts/chubbyts-undici-server/dist/server' ;
187-
188- const decoder = createDecoder ([createJsonTypeDecoder ()]);
189- const persistModel: PersistModel <InputMyModelSchema > = (
190- model : Model <InputMyModelSchema >,
191- ): Promise <Model <InputMyModelSchema >> => {};
192- const encoder = createEncoder ([createJsonTypeEncoder ()]);
193190
194191const createHandler = createCreateHandler (
195192 decoder ,
196193 inputMyModelSchema ,
197194 persistModel ,
198195 enrichedMyModelSchema ,
199196 encoder ,
200- v7 , // if not provided v4 is used
201197);
202198
203199(async () => {
204- const serverRequest = new ServerRequest (' http://localhost:8080/api/pets' , {method: ' POST' });
200+ const serverRequest = new ServerRequest (
201+ ' http://localhost:8080/api/pets' ,
202+ { method: ' POST' },
203+ );
205204 const response = await createHandler (serverRequest );
206205})();
207- ```
208206
209- #### my-read-handler.ts
210-
211- ``` ts
212- import { createEncoder } from ' @chubbyts/chubbyts-decode-encode/dist/encoder/encoder' ;
213- import { createJsonTypeEncoder }
214- from ' @chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder' ;
215- import type { InputMyModel } from ' ./my-model.js' ;
216- import { enrichedMyModelSchema } from ' ./my-model.js' ;
217- import type { FindModelById } from ' @chubbyts/chubbyts-undici-api/dist/repository' ;
218- import type { Model } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
219- import { createReadHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/read' ;
220- import { ServerRequest } from ' @chubbyts/chubbyts-undici-server/dist/server' ;
221-
222- const findModelById: FindModelById <InputMyModel > =
223- async (id : string ): Promise <Model <InputMyModel > | undefined > => {};
224- const encoder = createEncoder ([createJsonTypeEncoder ()]);
225-
226- const readHandler = createReadHandler <InputMyModel >(
207+ const readHandler = createReadHandler <InputMyModelSchema >(
227208 findModelById ,
228209 enrichedMyModelSchema ,
229- encoder
210+ encoder ,
230211);
231212
232213(async () => {
233214 const serverRequest = new ServerRequest (
234215 ' http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d' ,
235- { method: ' GET' }
216+ { method: ' GET' },
236217 );
237218 const response = await readHandler (serverRequest );
238219})();
239- ```
240-
241- #### my-update-handler.ts
242-
243- ``` ts
244- import { createDecoder } from ' @chubbyts/chubbyts-decode-encode/dist/decoder/decoder' ;
245- import { createJsonTypeDecoder }
246- from ' @chubbyts/chubbyts-decode-encode/dist/decoder/json-type-decoder' ;
247- import { createEncoder } from ' @chubbyts/chubbyts-decode-encode/dist/encoder/encoder' ;
248- import { createJsonTypeEncoder }
249- from ' @chubbyts/chubbyts-decode-encode/dist/encoder/json-type-encoder' ;
250- import type { InputMyModelSchema } from ' ./my-model.js' ;
251- import { enrichedMyModelSchema , inputMyModelSchema } from ' ./my-model.js' ;
252- import type {
253- FindModelById ,
254- PersistModel ,
255- } from ' @chubbyts/chubbyts-undici-api/dist/repository' ;
256- import type { Model } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
257- import { createUpdateHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/update' ;
258- import { ServerRequest } from ' @chubbyts/chubbyts-undici-server/dist/server' ;
259-
260- const findModelById: FindModelById <InputMyModelSchema > = async (
261- id : string ,
262- ): Promise <Model <InputMyModelSchema > | undefined > => {};
263- const decoder = createDecoder ([createJsonTypeDecoder ()]);
264- const persistModel: PersistModel <InputMyModelSchema > = (
265- model : Model <InputMyModelSchema >,
266- ): Promise <Model <InputMyModelSchema >> => {};
267- const encoder = createEncoder ([createJsonTypeEncoder ()]);
268220
269221const updateHandler = createUpdateHandler (
270222 findModelById ,
@@ -278,40 +230,20 @@ const updateHandler = createUpdateHandler(
278230(async () => {
279231 const serverRequest = new ServerRequest (
280232 ' http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d' ,
281- { method: ' PUT' }
233+ { method: ' PUT' },
282234 );
283235 const response = await updateHandler (serverRequest );
284236})();
285- ```
286-
287- #### my-delete-handler.ts
288-
289- ``` ts
290- import type { InputMyModelSchema } from ' ./my-model.js' ;
291- import {
292- FindModelById ,
293- RemoveModel ,
294- } from ' @chubbyts/chubbyts-undici-api/dist/repository' ;
295- import { Model } from ' @chubbyts/chubbyts-undici-api/dist/model' ;
296- import { createDeleteHandler } from ' @chubbyts/chubbyts-undici-api/dist/handler/delete' ;
297- import { ServerRequest } from ' @chubbyts/chubbyts-undici-server/dist/server' ;
298-
299- const findModelById: FindModelById <InputMyModelSchema > = async (
300- id : string ,
301- ): Promise <Model <InputMyModelSchema > | undefined > => {};
302- const removeModel: RemoveModel <InputMyModelSchema > = (
303- model : Model <InputMyModelSchema >,
304- ): Promise <void > => {};
305237
306- const deleteHandler = createDeleteHandler (
238+ const deleteHandler = createDeleteHandler < InputMyModelSchema > (
307239 findModelById ,
308240 removeModel ,
309241);
310242
311243(async () => {
312244 const serverRequest = new ServerRequest (
313245 ' http://localhost:8080/api/pets/8ba9661b-ba7f-436b-bd25-c0606f911f7d' ,
314- { method: ' DELETE' }
246+ { method: ' DELETE' },
315247 );
316248 const response = await deleteHandler (serverRequest );
317249})();
0 commit comments