Skip to content

Commit 1dac2a1

Browse files
committed
refactor: move util types to separate file
1 parent 9f7d293 commit 1dac2a1

File tree

12 files changed

+40
-49
lines changed

12 files changed

+40
-49
lines changed

packages/next-safe-action/src/action-builder.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,8 @@ import type {
1212
ServerCodeFn,
1313
StateServerCodeFn,
1414
} from "./index.types";
15-
import type { InferArray, InferInArray } from "./utils";
16-
import {
17-
ActionMetadataError,
18-
DEFAULT_SERVER_ERROR_MESSAGE,
19-
isError,
20-
zodValidate,
21-
type Infer,
22-
type InferIn,
23-
type Schema,
24-
} from "./utils";
15+
import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError, zodValidate } from "./utils";
16+
import type { Infer, InferArray, InferIn, InferInArray, Schema } from "./utils.types";
2517
import { ActionValidationError, buildValidationErrors } from "./validation-errors";
2618
import type {
2719
BindArgsValidationErrors,

packages/next-safe-action/src/hooks-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import {} from "react/experimental";
33
import type {} from "zod";
44
import type { HookActionStatus, HookCallbacks, HookResult } from "./hooks.types";
5-
import type { InferIn, Schema } from "./utils";
5+
import type { InferIn, Schema } from "./utils.types";
66

77
export const getActionStatus = <
88
ServerError,

packages/next-safe-action/src/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import {} from "react/experimental";
88
import type {} from "zod";
99
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks } from "./hooks-utils";
1010
import type { HookCallbacks, HookResult, HookSafeActionFn } from "./hooks.types";
11-
import type { InferIn, Schema } from "./utils";
1211
import { isError } from "./utils";
12+
import type { InferIn, Schema } from "./utils.types";
1313

1414
// HOOKS
1515

packages/next-safe-action/src/hooks.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SafeActionResult } from "./index.types";
2-
import type { InferIn, MaybePromise, Prettify, Schema } from "./utils";
2+
import type { InferIn, MaybePromise, Prettify, Schema } from "./utils.types";
33

44
/**
55
* Type of `result` object returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.

packages/next-safe-action/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { DVES, SafeActionClientOpts } from "./index.types";
22
import { SafeActionClient } from "./safe-action-client";
3-
import type { Infer, Schema } from "./utils";
43
import { DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
4+
import type { Infer, Schema } from "./utils.types";
55
import {
66
flattenBindArgsValidationErrors,
77
flattenValidationErrors,

packages/next-safe-action/src/index.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Infer, InferArray, InferIn, InferInArray, MaybePromise, Prettify, Schema } from "./utils";
1+
import type { Infer, InferArray, InferIn, InferInArray, MaybePromise, Prettify, Schema } from "./utils.types";
22
import type { BindArgsValidationErrors, ValidationErrors } from "./validation-errors.types";
33

44
/**

packages/next-safe-action/src/safe-action-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
ServerCodeFn,
99
StateServerCodeFn,
1010
} from "./index.types";
11-
import type { Infer, Schema } from "./utils";
11+
import type { Infer, Schema } from "./utils.types";
1212
import type {
1313
BindArgsValidationErrors,
1414
FlattenedBindArgsValidationErrors,

packages/next-safe-action/src/stateful-hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {} from "react/experimental";
66
import type {} from "zod";
77
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks } from "./hooks-utils";
88
import type { HookCallbacks, HookSafeStateActionFn } from "./hooks.types";
9-
import type { InferIn, Schema } from "./utils";
9+
import type { InferIn, Schema } from "./utils.types";
1010
/**
1111
* Use the stateful action from a Client Component via hook. Used for actions defined with [`stateAction`](https://next-safe-action.dev/docs/safe-action-client/instance-methods#action--stateaction).
1212
* @param safeActionFn The action function

packages/next-safe-action/src/utils.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,9 @@
1-
import type { z } from "zod";
1+
import type { Infer, Schema } from "./utils.types";
22

33
export const DEFAULT_SERVER_ERROR_MESSAGE = "Something went wrong while executing the operation.";
44

55
export const isError = (error: unknown): error is Error => error instanceof Error;
66

7-
// UTIL TYPES
8-
9-
// Takes an object type and makes it more readable.
10-
export type Prettify<T> = {
11-
[K in keyof T]: T[K];
12-
} & {};
13-
14-
// Returns type or promise of type.
15-
export type MaybePromise<T> = Promise<T> | T;
16-
17-
// Schema type.
18-
export type Schema = z.ZodType;
19-
20-
// Infers output schema type.
21-
export type Infer<S extends Schema> = z.infer<S>;
22-
23-
// Infers input schema type.
24-
export type InferIn<S extends Schema> = z.input<S>;
25-
26-
// Infers output schema type in array of schemas.
27-
export type InferArray<BAS extends readonly Schema[]> = {
28-
[K in keyof BAS]: Infer<BAS[K]>;
29-
};
30-
31-
// Infers input schema type in array of schemas.
32-
export type InferInArray<BAS extends readonly Schema[]> = {
33-
[K in keyof BAS]: InferIn<BAS[K]>;
34-
};
35-
367
// Validate with Zod.
378
export async function zodValidate<S extends Schema>(s: S, data: unknown) {
389
const result = await s.safeParseAsync(data);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { z } from "zod";
2+
3+
// Takes an object type and makes it more readable.
4+
export type Prettify<T> = {
5+
[K in keyof T]: T[K];
6+
} & {};
7+
8+
// Returns type or promise of type.
9+
export type MaybePromise<T> = Promise<T> | T;
10+
11+
// Schema type.
12+
export type Schema = z.ZodType;
13+
14+
// Infers output schema type.
15+
export type Infer<S extends Schema> = z.infer<S>;
16+
17+
// Infers input schema type.
18+
export type InferIn<S extends Schema> = z.input<S>;
19+
20+
// Infers output schema type in array of schemas.
21+
export type InferArray<BAS extends readonly Schema[]> = {
22+
[K in keyof BAS]: Infer<BAS[K]>;
23+
};
24+
25+
// Infers input schema type in array of schemas.
26+
export type InferInArray<BAS extends readonly Schema[]> = {
27+
[K in keyof BAS]: InferIn<BAS[K]>;
28+
};

0 commit comments

Comments
 (0)