@@ -25,12 +25,11 @@ const router = ddmushi.init({
2525 }
2626});
2727
28- function createCollection = router.collection;
29- function operation = router.operation;
28+ const { collection, operation } = router ;
3029
3130// Define your API operations
32- export const api = createCollection ({
33- users: createCollection ({
31+ export const api = collection ({
32+ users: collection ({
3433 list: operation .query <User []>(
3534 async ({ opts : { ctx } }) => {
3635 const response = await fetch (` ${ctx .apiUrl }/users ` , {
@@ -44,7 +43,10 @@ export const api = createCollection({
4443 async ({ opts : { ctx }, input }) => {
4544 const response = await fetch (` ${ctx .apiUrl }/users ` , {
4645 method: ' POST' ,
47- headers: { Authorization: ` Bearer ${ctx .token } ` },
46+ headers: {
47+ Authorization: ` Bearer ${ctx .token } ` ,
48+ ' Content-Type' : ' application/json'
49+ },
4850 body: JSON .stringify (input )
4951 });
5052 return response .json ();
@@ -75,6 +77,60 @@ function UsersList() {
7577- 🚀 ** DX Focused** - tRPC-like developer experience for REST APIs
7678- 🪶 ** Lightweight** - Minimal runtime overhead
7779- ♾️ ** Infinite Queries** - Built-in support for pagination and infinite scrolling
80+ - 🔌 ** Middleware Support** - Composable middleware for cross-cutting concerns
81+ - ✅ ** Built-in Validation** - Standard Schema integration for input/output validation
82+ - 🎯 ** Flexible Operations** - Chainable operation builders with enhanced customization
83+
84+ ## Middleware
85+
86+ ddmushi supports composable middleware for cross-cutting concerns like authentication, logging, caching, and error handling:
87+
88+ ``` typescript
89+ import { ddmushi } from ' ddmushi' ;
90+
91+ // Create authentication middleware
92+ const authMiddleware = async ({ ctx , next }) => {
93+ if (! ctx .token ) {
94+ throw new Error (' Authentication required' );
95+ }
96+
97+ // Add user info to context
98+ const userInfo = await getUserInfo (ctx .token );
99+ return next ({ ... ctx , user: userInfo });
100+ };
101+
102+ // Create logging middleware
103+ const loggingMiddleware = async ({ ctx , next }) => {
104+ const start = Date .now ();
105+ console .log (' Operation started' );
106+
107+ try {
108+ const result = await next (ctx );
109+ console .log (` Operation completed in ${Date .now () - start }ms ` );
110+ return result ;
111+ } catch (error ) {
112+ console .error (` Operation failed in ${Date .now () - start }ms: ` , error );
113+ throw error ;
114+ }
115+ };
116+
117+ const router = ddmushi .init ({
118+ ctx: { token: ' your-token' }
119+ });
120+
121+ // Apply middleware to operations
122+ const api = router .collection ({
123+ users: router .collection ({
124+ list: router .operation
125+ .use (authMiddleware )
126+ .use (loggingMiddleware )
127+ .query <User []>(async ({ opts : { ctx } }) => {
128+ // ctx now includes user info from middleware
129+ return fetchUsers (ctx .user .id );
130+ })
131+ })
132+ });
133+ ```
78134
79135## Development
80136
0 commit comments