Skip to content

Commit

Permalink
Merge pull request #33 from janglad/bug/type-bugs-docs
Browse files Browse the repository at this point in the history
update doc, add and fix exposed types
  • Loading branch information
janglad authored Sep 6, 2024
2 parents 92e7219 + c8f8132 commit 2b5f854
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 114 deletions.
8 changes: 8 additions & 0 deletions .changeset/nervous-chicken-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"safe-fn": patch
---

- Add InferActionErrError and InferActionOkData types
- Add InferAsyncOkData and InferAsyncErrError types
- Rename some types for clarity
- Improve type docs
29 changes: 28 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,31 @@ coverage
sandbox.ts
.turbo
dist/
.env
.env


#From Fumadocs
# generated content
.contentlayer
.content-collections
.source

# test & build
.next
.next
/out/
/build
*.tsbuildinfo

# misc
*.pem
/.pnp
.pnp.js
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# others
.env*.local
.vercel
next-env.d.ts
2 changes: 1 addition & 1 deletion packages/safe-fn-react/src/useServerAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const useServerAction = <TAction extends AnySafeFnAction>(
): UseServerActionReturn<TAction> => {
type ActionArgs = InferSafeFnActionArgs<TAction>;
/** Original `ActionResult<T,E>` */
type ActionActionResult = InferSafeFnActionReturn<TAction>;
type ActionActionResult = Awaited<InferSafeFnActionReturn<TAction>>;
/** Converted `ActionResult<T,E>` -\> `Result<T,E>` to be returned to the user */
type ActionResult = ActionResultToResult<ActionActionResult>;

Expand Down
30 changes: 19 additions & 11 deletions packages/safe-fn/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { Err, Ok, Result } from "./result";
import {
type ActionResult,
type ActionResultToResult,
actionResultToResult,
err,
ok,
type ActionErr,
type ActionOk,
type ActionResult,
type ActionResultToResult,
type InferActionErrError,
type InferActionOkData,
} from "./result";
import { SafeFnBuilder } from "./safe-fn-builder";

Expand All @@ -14,14 +18,14 @@ import type {
AnySafeFnThrownHandler,
InferInputSchema,
InferOutputSchema,
InferReturn,
InferReturnData,
InferReturnError,
InferRunArgs,
InferSafeFnActionArgs,
InferSafeFnActionError,
InferSafeFnActionOkData,
InferSafeFnActionReturn,
InferSafeFnArgs,
InferSafeFnErrError,
InferSafeFnOkData,
InferSafeFnReturn,
InferUnparsedInput,
SafeFnAction,
SafeFnActionArgs,
Expand All @@ -36,30 +40,34 @@ import type {

export { actionResultToResult, err, ok, SafeFnBuilder as SafeFn };
export type {
ActionErr,
ActionOk,
ActionResult,
ActionResultToResult,
AnyRunnableSafeFn,
AnySafeFnAction,
AnySafeFnThrownHandler,
Err,
InferActionErrError,
InferActionOkData,
InferInputSchema,
InferOutputSchema,
InferReturn,
InferReturnData,
InferReturnError,
InferRunArgs,
InferSafeFnActionArgs,
InferSafeFnActionError,
InferSafeFnActionOkData,
InferSafeFnActionReturn,
InferSafeFnArgs,
InferSafeFnErrError,
InferSafeFnOkData,
InferSafeFnReturn,
InferUnparsedInput,
Ok,
Result,
SafeFnAction,
SafeFnActionArgs,
SafeFnActionReturn,
SafeFnAsyncGeneratorHandlerFn,
SafeFnRegularHandlerFn as SafeFnHandlerFn,
SafeFnRegularHandlerFn,
SafeFnReturn,
SafeFnReturnData,
SafeFnReturnError,
Expand Down
9 changes: 8 additions & 1 deletion packages/safe-fn/src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ export type AnyResult = Result<any, any>;

export type Ok<TData, TError = never> = NT.Ok<TData, TError>;
export type InferOkData<T> = T extends Result<infer TData, any> ? TData : never;
export type InferAsyncOkData<T> =
T extends ResultAsync<infer TData, any> ? TData : never;

export const ok = <const TData>(data: TData): Ok<TData, never> => NT.ok(data);

export type Err<TData = never, TError = unknown> = NT.Err<TData, TError>;
export type InferErrError<T> =
T extends Result<any, infer TError> ? TError : never;
export type InferAsyncErrError<T> =
T extends ResultAsync<any, infer TError> ? TError : never;

export type AnyErr = Err<never, any>;
export const err = <const TError>(error: TError): Err<never, TError> =>
Expand Down Expand Up @@ -43,13 +47,16 @@ export type ActionOk<T> = {
value: T;
};
export const actionOk = <T>(value: T): ActionOk<T> => ({ ok: true, value });
export type InferActionOkData<T> =
T extends ActionOk<infer TData> ? TData : never;

export type ActionErr<E> = {
ok: false;
error: E;
};
export const actionErr = <E>(error: E): ActionErr<E> => ({ ok: false, error });

export type InferActionErrError<T> =
T extends ActionErr<infer TError> ? TError : never;
/**
* Converts a `ResultAsync<T,E>` to a `Promise<Result<T,E>>`.
*/
Expand Down
Loading

0 comments on commit 2b5f854

Please sign in to comment.