Skip to content

Commit

Permalink
refactor: move util types to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 2, 2024
1 parent 9f7d293 commit 1dac2a1
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 49 deletions.
12 changes: 2 additions & 10 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@ import type {
ServerCodeFn,
StateServerCodeFn,
} from "./index.types";
import type { InferArray, InferInArray } from "./utils";
import {
ActionMetadataError,
DEFAULT_SERVER_ERROR_MESSAGE,
isError,
zodValidate,
type Infer,
type InferIn,
type Schema,
} from "./utils";
import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError, zodValidate } from "./utils";
import type { Infer, InferArray, InferIn, InferInArray, Schema } from "./utils.types";
import { ActionValidationError, buildValidationErrors } from "./validation-errors";
import type {
BindArgsValidationErrors,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/hooks-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from "react";
import {} from "react/experimental";
import type {} from "zod";
import type { HookActionStatus, HookCallbacks, HookResult } from "./hooks.types";
import type { InferIn, Schema } from "./utils";
import type { InferIn, Schema } from "./utils.types";

export const getActionStatus = <
ServerError,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {} from "react/experimental";
import type {} from "zod";
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks } from "./hooks-utils";
import type { HookCallbacks, HookResult, HookSafeActionFn } from "./hooks.types";
import type { InferIn, Schema } from "./utils";
import { isError } from "./utils";
import type { InferIn, Schema } from "./utils.types";

// HOOKS

Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/hooks.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SafeActionResult } from "./index.types";
import type { InferIn, MaybePromise, Prettify, Schema } from "./utils";
import type { InferIn, MaybePromise, Prettify, Schema } from "./utils.types";

/**
* Type of `result` object returned by `useAction`, `useOptimisticAction` and `useStateAction` hooks.
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DVES, SafeActionClientOpts } from "./index.types";
import { SafeActionClient } from "./safe-action-client";
import type { Infer, Schema } from "./utils";
import { DEFAULT_SERVER_ERROR_MESSAGE } from "./utils";
import type { Infer, Schema } from "./utils.types";
import {
flattenBindArgsValidationErrors,
flattenValidationErrors,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Infer, InferArray, InferIn, InferInArray, MaybePromise, Prettify, Schema } from "./utils";
import type { Infer, InferArray, InferIn, InferInArray, MaybePromise, Prettify, Schema } from "./utils.types";
import type { BindArgsValidationErrors, ValidationErrors } from "./validation-errors.types";

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/safe-action-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
ServerCodeFn,
StateServerCodeFn,
} from "./index.types";
import type { Infer, Schema } from "./utils";
import type { Infer, Schema } from "./utils.types";
import type {
BindArgsValidationErrors,
FlattenedBindArgsValidationErrors,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/stateful-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {} from "react/experimental";
import type {} from "zod";
import { getActionShorthandStatusObject, getActionStatus, useActionCallbacks } from "./hooks-utils";
import type { HookCallbacks, HookSafeStateActionFn } from "./hooks.types";
import type { InferIn, Schema } from "./utils";
import type { InferIn, Schema } from "./utils.types";
/**
* 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).
* @param safeActionFn The action function
Expand Down
31 changes: 1 addition & 30 deletions packages/next-safe-action/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
import type { z } from "zod";
import type { Infer, Schema } from "./utils.types";

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

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

// UTIL TYPES

// Takes an object type and makes it more readable.
export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};

// Returns type or promise of type.
export type MaybePromise<T> = Promise<T> | T;

// Schema type.
export type Schema = z.ZodType;

// Infers output schema type.
export type Infer<S extends Schema> = z.infer<S>;

// Infers input schema type.
export type InferIn<S extends Schema> = z.input<S>;

// Infers output schema type in array of schemas.
export type InferArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: Infer<BAS[K]>;
};

// Infers input schema type in array of schemas.
export type InferInArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: InferIn<BAS[K]>;
};

// Validate with Zod.
export async function zodValidate<S extends Schema>(s: S, data: unknown) {
const result = await s.safeParseAsync(data);
Expand Down
28 changes: 28 additions & 0 deletions packages/next-safe-action/src/utils.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { z } from "zod";

// Takes an object type and makes it more readable.
export type Prettify<T> = {
[K in keyof T]: T[K];
} & {};

// Returns type or promise of type.
export type MaybePromise<T> = Promise<T> | T;

// Schema type.
export type Schema = z.ZodType;

// Infers output schema type.
export type Infer<S extends Schema> = z.infer<S>;

// Infers input schema type.
export type InferIn<S extends Schema> = z.input<S>;

// Infers output schema type in array of schemas.
export type InferArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: Infer<BAS[K]>;
};

// Infers input schema type in array of schemas.
export type InferInArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: InferIn<BAS[K]>;
};
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/validation-errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */

import type { Schema } from "./utils";
import type { Schema } from "./utils.types";
import type {
FlattenedBindArgsValidationErrors,
FlattenedValidationErrors,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-safe-action/src/validation-errors.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Infer, Prettify, Schema } from "./utils";
import type { Infer, Prettify, Schema } from "./utils.types";

export type ValidationIssue = {
message: string;
Expand Down

0 comments on commit 1dac2a1

Please sign in to comment.