Skip to content

Commit

Permalink
fix(types): handle result undefined value at the action level
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jun 12, 2024
1 parent 16e5693 commit a9a2c8c
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const statefulAction = action
await new Promise((res) => setTimeout(res, 1000));

return {
prevName: prevResult?.data?.newName,
prevName: prevResult.data?.newName,
newName: parsedInput.name,
};
});
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/hooks.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type HookResult<
CVE,
CBAVE,
Data,
> = NonNullable<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> & {
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
fetchError?: string;
};

Expand Down
18 changes: 8 additions & 10 deletions packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,19 @@ export type SafeActionResult<
Data = unknown,
// eslint-disable-next-line
NextCtx = unknown,
> =
| {
data?: Data;
serverError?: ServerError;
validationErrors?: CVE;
bindArgsValidationErrors?: CBAVE;
}
| undefined;
> = {
data?: Data;
serverError?: ServerError;
validationErrors?: CVE;
bindArgsValidationErrors?: CBAVE;
};

/**
* Type of the function called from components with type safe input data.
*/
export type SafeActionFn<ServerError, S extends Schema | undefined, BAS extends readonly Schema[], CVE, CBAVE, Data> = (
...clientInputs: [...bindArgsInputs: InferInArray<BAS>, input: S extends Schema ? InferIn<S> : void]
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;

/**
* Type of the stateful function called from components with type safe input data.
Expand All @@ -66,7 +64,7 @@ export type SafeStateActionFn<
prevResult: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>,
input: S extends Schema ? InferIn<S> : void,
]
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;

/**
* Type of the result of a middleware function. It extends the result of a safe action with
Expand Down
2 changes: 1 addition & 1 deletion website/docs/execution/hooks/usestateaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const statefulAction = actionClient
await new Promise((res) => setTimeout(res, 1000));

return {
prevName: prevResult?.data?.newName,
prevName: prevResult.data?.newName,
newName: parsedInput.name,
};
});
Expand Down
10 changes: 4 additions & 6 deletions website/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@ export type SafeActionResult<
Data = unknown,
// eslint-disable-next-line
NextCtx = unknown,
> =
| {
> = {
data?: Data;
serverError?: ServerError;
validationErrors?: CVE;
bindArgsValidationErrors?: CBAVE;
};
| undefined;
```

### `SafeActionFn`
Expand All @@ -70,7 +68,7 @@ export type SafeActionFn<
Data
> = (
...clientInputs: [...bindArgsInputs: InferInArray<BAS>, input: S extends Schema ? InferIn<S> : void]
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;
```

### `SafeStateActionFn`
Expand All @@ -91,7 +89,7 @@ export type SafeStateActionFn<
prevResult: Prettify<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>,
input: S extends Schema ? InferIn<S> : void,
]
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>>;
) => Promise<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined>;
```

### `MiddlewareResult`
Expand Down Expand Up @@ -284,7 +282,7 @@ export type HookResult<
CVE,
CBAVE,
Data,
> = NonNullable<SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data>> & {
> = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> & {
fetchError?: string;
};
```
Expand Down

0 comments on commit a9a2c8c

Please sign in to comment.