Skip to content

Commit

Permalink
fix: exported class expression ts error 4094 (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan authored Apr 7, 2024
1 parent 04c080d commit f36392a
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ import {
import type { ValidationErrors } from "./validation-errors.types";

class SafeActionClient<const ServerError, const Ctx = null> {
private readonly handleServerErrorLog: NonNullable<
readonly #handleServerErrorLog: NonNullable<
SafeActionClientOpts<ServerError>["handleServerErrorLog"]
>;
private readonly handleReturnedServerError: NonNullable<
readonly #handleReturnedServerError: NonNullable<
SafeActionClientOpts<ServerError>["handleReturnedServerError"]
>;

private middlewareFns: MiddlewareFn<ServerError, any, any, any>[];
private _metadata: ActionMetadata = {};
#middlewareFns: MiddlewareFn<ServerError, any, any, any>[];
#metadata: ActionMetadata = {};

constructor(
opts: { middlewareFns: MiddlewareFn<ServerError, any, any, any>[] } & Required<
SafeActionClientOpts<ServerError>
>
) {
this.middlewareFns = opts.middlewareFns;
this.handleServerErrorLog = opts.handleServerErrorLog;
this.handleReturnedServerError = opts.handleReturnedServerError;
this.#middlewareFns = opts.middlewareFns;
this.#handleServerErrorLog = opts.handleServerErrorLog;
this.#handleReturnedServerError = opts.handleReturnedServerError;
}

/**
Expand All @@ -47,9 +47,9 @@ class SafeActionClient<const ServerError, const Ctx = null> {
*/
public clone() {
return new SafeActionClient<ServerError, Ctx>({
handleReturnedServerError: this.handleReturnedServerError,
handleServerErrorLog: this.handleServerErrorLog,
middlewareFns: [...this.middlewareFns], // copy the middleware stack so we don't mutate it
handleReturnedServerError: this.#handleReturnedServerError,
handleServerErrorLog: this.#handleServerErrorLog,
middlewareFns: [...this.#middlewareFns], // copy the middleware stack so we don't mutate it
});
}

Expand All @@ -61,12 +61,12 @@ class SafeActionClient<const ServerError, const Ctx = null> {
public use<const ClientInput, const NextCtx>(
middlewareFn: MiddlewareFn<ServerError, ClientInput, Ctx, NextCtx>
) {
this.middlewareFns.push(middlewareFn);
this.#middlewareFns.push(middlewareFn);

return new SafeActionClient<ServerError, NextCtx>({
middlewareFns: this.middlewareFns,
handleReturnedServerError: this.handleReturnedServerError,
handleServerErrorLog: this.handleServerErrorLog,
middlewareFns: this.#middlewareFns,
handleReturnedServerError: this.#handleReturnedServerError,
handleServerErrorLog: this.#handleServerErrorLog,
});
}

Expand All @@ -76,7 +76,7 @@ class SafeActionClient<const ServerError, const Ctx = null> {
* @returns {Function} Define a new action
*/
public metadata(data: ActionMetadata) {
this._metadata = data;
this.#metadata = data;

return {
schema: this.schema.bind(this),
Expand Down Expand Up @@ -107,7 +107,7 @@ class SafeActionClient<const ServerError, const Ctx = null> {

// Execute the middleware stack.
const executeMiddlewareChain = async (idx = 0) => {
const currentFn = classThis.middlewareFns[idx];
const currentFn = classThis.#middlewareFns[idx];

middlewareResult.ctx = prevCtx;

Expand All @@ -116,7 +116,7 @@ class SafeActionClient<const ServerError, const Ctx = null> {
await currentFn({
clientInput, // pass raw client input
ctx: prevCtx,
metadata: classThis._metadata,
metadata: classThis.#metadata,
next: async ({ ctx }) => {
prevCtx = ctx;
await executeMiddlewareChain(idx + 1);
Expand All @@ -134,7 +134,7 @@ class SafeActionClient<const ServerError, const Ctx = null> {
const data =
(await serverCodeFn(parsedInput.data, {
ctx: prevCtx,
metadata: classThis._metadata,
metadata: classThis.#metadata,
})) ?? null;
middlewareResult.success = true;
middlewareResult.data = data;
Expand All @@ -159,10 +159,10 @@ class SafeActionClient<const ServerError, const Ctx = null> {
// the default message.
const error = isError(e) ? e : new Error(DEFAULT_SERVER_ERROR_MESSAGE);

await Promise.resolve(classThis.handleServerErrorLog(error));
await Promise.resolve(classThis.#handleServerErrorLog(error));

middlewareResult.serverError = await Promise.resolve(
classThis.handleReturnedServerError(error)
classThis.#handleReturnedServerError(error)
);
}
};
Expand Down

0 comments on commit f36392a

Please sign in to comment.