-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(middleware): move
createMiddleware
function to separate file
- Loading branch information
Showing
3 changed files
with
23 additions
and
23 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
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,21 @@ | ||
import type { MiddlewareFn } from "./index.types"; | ||
|
||
/** | ||
* Creates a standalone middleware function. It accepts a generic object with optional `serverError`, `ctx` and `metadata` | ||
* properties, if you need one or all of them to be typed. The type for each property that is passed as generic is the | ||
* **minimum** shape required to define the middleware function, but it can also be larger than that. | ||
* | ||
* {@link https://next-safe-action.dev/docs/safe-action-client/middleware#create-standalone-middleware-with-createmiddleware See docs for more information} | ||
*/ | ||
export const createMiddleware = <BaseData extends { serverError?: any; ctx?: object; metadata?: any }>() => { | ||
return { | ||
define: <NextCtx extends object>( | ||
middlewareFn: MiddlewareFn< | ||
BaseData extends { serverError: infer SE } ? SE : any, | ||
BaseData extends { metadata: infer MD } ? MD : any, | ||
BaseData extends { ctx: infer Ctx extends object } ? Ctx : object, | ||
NextCtx | ||
> | ||
) => middlewareFn, | ||
}; | ||
}; |
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