Skip to content

Commit

Permalink
refactor: require context to be an object
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Aug 11, 2024
1 parent 3c37269 commit ed3bd85
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ test("flattened validation errors in execution result from middleware are correc

const expectedResult = {
success: false,
ctx: undefined,
ctx: {},
validationErrors: {
formErrors: [],
fieldErrors: {
Expand Down Expand Up @@ -377,7 +377,7 @@ test("overridden formatted validation errors in execution result from middleware

const expectedResult = {
success: false,
ctx: undefined,
ctx: {},
validationErrors: {
username: {
_errors: ["String must contain at most 3 character(s)"],
Expand Down
6 changes: 3 additions & 3 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function actionBuilder<
ServerError,
MetadataSchema extends Schema | undefined = undefined,
MD = MetadataSchema extends Schema ? Infer<Schema> : undefined,
Ctx = undefined,
Ctx extends object = {},
SF extends (() => Promise<Schema>) | undefined = undefined, // schema function
S extends Schema | undefined = SF extends Function ? Awaited<ReturnType<SF>> : undefined,
const BAS extends readonly Schema[] = [],
Expand Down Expand Up @@ -71,8 +71,8 @@ export function actionBuilder<
utils?: SafeActionUtils<ServerError, MD, Ctx, S, BAS, CVE, CBAVE, Data>
) => {
return async (...clientInputs: unknown[]) => {
let prevCtx: unknown = undefined;
const middlewareResult: MiddlewareResult<ServerError, unknown> = { success: false };
let prevCtx: object = {};
const middlewareResult: MiddlewareResult<ServerError, object> = { success: false };
type PrevResult = SafeActionResult<ServerError, S, BAS, CVE, CBAVE, Data> | undefined;
let prevResult: PrevResult | undefined = undefined;
const parsedInputDatas: any[] = [];
Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export const createSafeActionClient = <
>);

return new SafeActionClient({
middlewareFns: [async ({ next }) => next({ ctx: undefined })],
middlewareFns: [async ({ next }) => next({ ctx: {} })],
handleServerErrorLog,
handleReturnedServerError,
schemaFn: undefined,
bindArgsSchemas: [],
validationAdapter: createOpts?.validationAdapter ?? zodAdapter(), // use zod adapter by default
ctxType: undefined,
ctxType: {},
metadataSchema: (createOpts?.defineMetadataSchema?.() ?? undefined) as MetadataSchema,
metadata: undefined as MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined,
defaultValidationErrorsShape: (createOpts?.defaultValidationErrorsShape ?? "formatted") as ODVES,
Expand Down
32 changes: 23 additions & 9 deletions packages/next-safe-action/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type DVES = "formatted" | "flattened";
export type ServerErrorFunctionUtils<MetadataSchema extends Schema | undefined> = {
clientInput: unknown;
bindArgsClientInputs: unknown[];
ctx: unknown;
ctx: object;
metadata: MetadataSchema extends Schema ? Infer<MetadataSchema> : undefined;
};

Expand Down Expand Up @@ -53,7 +53,7 @@ export type SafeActionResult<
CBAVE = BindArgsValidationErrors<BAS>,
Data = unknown,
// eslint-disable-next-line
NextCtx = unknown,
NextCtx = object,
> = {
data?: Data;
serverError?: ServerError;
Expand Down Expand Up @@ -90,32 +90,46 @@ export type SafeStateActionFn<
* Type of the result of a middleware function. It extends the result of a safe action with
* information about the action execution.
*/
export type MiddlewareResult<ServerError, NextCtx> = SafeActionResult<ServerError, any, any, any, any, any, NextCtx> & {
export type MiddlewareResult<ServerError, NextCtx extends object> = SafeActionResult<
ServerError,
any,
any,
any,
any,
any,
NextCtx
> & {
parsedInput?: unknown;
bindArgsParsedInputs?: unknown[];
ctx?: unknown;
ctx?: object;
success: boolean;
};

/**
* Type of the middleware function passed to a safe action client.
*/
export type MiddlewareFn<ServerError, MD, Ctx, NextCtx> = {
export type MiddlewareFn<ServerError, MD, Ctx extends object, NextCtx extends object> = {
(opts: {
clientInput: unknown;
bindArgsClientInputs: unknown[];
ctx: Ctx;
metadata: MD;
next: {
<NC>(opts: { ctx: NC }): Promise<MiddlewareResult<ServerError, NC>>;
<NC extends object>(opts: { ctx: NC }): Promise<MiddlewareResult<ServerError, NC>>;
};
}): Promise<MiddlewareResult<ServerError, NextCtx>>;
};

/**
* Type of the function that executes server code when defining a new safe action.
*/
export type ServerCodeFn<MD, Ctx, S extends Schema | undefined, BAS extends readonly Schema[], Data> = (args: {
export type ServerCodeFn<
MD,
Ctx extends object,
S extends Schema | undefined,
BAS extends readonly Schema[],
Data,
> = (args: {
parsedInput: S extends Schema ? Infer<S> : undefined;
bindArgsParsedInputs: InferArray<BAS>;
ctx: Ctx;
Expand All @@ -128,7 +142,7 @@ export type ServerCodeFn<MD, Ctx, S extends Schema | undefined, BAS extends read
export type StateServerCodeFn<
ServerError,
MD,
Ctx,
Ctx extends object,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
Expand All @@ -150,7 +164,7 @@ export type StateServerCodeFn<
export type SafeActionUtils<
ServerError,
MD,
Ctx,
Ctx extends object,
S extends Schema | undefined,
BAS extends readonly Schema[],
CVE,
Expand Down
15 changes: 8 additions & 7 deletions packages/next-safe-action/src/safe-action-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SafeActionClient<
ODVES extends DVES | undefined, // override default validation errors shape
MetadataSchema extends Schema | undefined = undefined,
MD = MetadataSchema extends Schema ? Infer<Schema> : undefined,
Ctx = undefined,
Ctx extends object = {},
SF extends (() => Promise<Schema>) | undefined = undefined, // schema function
S extends Schema | undefined = SF extends Function ? Awaited<ReturnType<SF>> : undefined,
const BAS extends readonly Schema[] = [],
Expand All @@ -37,10 +37,10 @@ export class SafeActionClient<
SafeActionClientOpts<ServerError, MetadataSchema, ODVES>["handleReturnedServerError"]
>;
readonly #middlewareFns: MiddlewareFn<ServerError, any, any, any>[];
readonly #ctxType = undefined as Ctx;
readonly #metadataSchema: MetadataSchema;
readonly #metadata: MD;
readonly #schemaFn: SF;
readonly #ctxType: Ctx;
readonly #bindArgsSchemas: BAS;
readonly #validationAdapter: ValidationAdapter;
readonly #handleValidationErrorsShape: HandleValidationErrorsShapeFn<S, CVE>;
Expand Down Expand Up @@ -74,6 +74,7 @@ export class SafeActionClient<
this.#schemaFn = (opts.schemaFn ?? undefined) as SF;
this.#bindArgsSchemas = opts.bindArgsSchemas ?? [];
this.#validationAdapter = opts.validationAdapter;
this.#ctxType = opts.ctxType as unknown as Ctx;
this.#handleValidationErrorsShape = opts.handleValidationErrorsShape;
this.#handleBindArgsValidationErrorsShape = opts.handleBindArgsValidationErrorsShape;
this.#defaultValidationErrorsShape = opts.defaultValidationErrorsShape;
Expand All @@ -86,7 +87,7 @@ export class SafeActionClient<
*
* {@link https://next-safe-action.dev/docs/safe-action-client/instance-methods#use See docs for more information}
*/
use<NextCtx>(middlewareFn: MiddlewareFn<ServerError, MD, Ctx, NextCtx>) {
use<NextCtx extends object>(middlewareFn: MiddlewareFn<ServerError, MD, Ctx, NextCtx>) {
return new SafeActionClient({
middlewareFns: [...this.#middlewareFns, middlewareFn],
handleReturnedServerError: this.#handleReturnedServerError,
Expand All @@ -98,7 +99,7 @@ export class SafeActionClient<
validationAdapter: this.#validationAdapter,
handleValidationErrorsShape: this.#handleValidationErrorsShape,
handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape,
ctxType: undefined as NextCtx,
ctxType: {} as NextCtx,
defaultValidationErrorsShape: this.#defaultValidationErrorsShape,
throwValidationErrors: this.#throwValidationErrors,
});
Expand All @@ -122,7 +123,7 @@ export class SafeActionClient<
validationAdapter: this.#validationAdapter,
handleValidationErrorsShape: this.#handleValidationErrorsShape,
handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape,
ctxType: undefined as Ctx,
ctxType: {} as Ctx,
defaultValidationErrorsShape: this.#defaultValidationErrorsShape,
throwValidationErrors: this.#throwValidationErrors,
});
Expand Down Expand Up @@ -164,7 +165,7 @@ export class SafeActionClient<
handleValidationErrorsShape: (utils?.handleValidationErrorsShape ??
this.#handleValidationErrorsShape) as HandleValidationErrorsShapeFn<AS, OCVE>,
handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape,
ctxType: undefined as Ctx,
ctxType: {} as Ctx,
defaultValidationErrorsShape: this.#defaultValidationErrorsShape,
throwValidationErrors: this.#throwValidationErrors,
});
Expand Down Expand Up @@ -198,7 +199,7 @@ export class SafeActionClient<
handleValidationErrorsShape: this.#handleValidationErrorsShape,
handleBindArgsValidationErrorsShape: (utils?.handleBindArgsValidationErrorsShape ??
this.#handleBindArgsValidationErrorsShape) as HandleBindArgsValidationErrorsShapeFn<OBAS, OCBAVE>,
ctxType: undefined as Ctx,
ctxType: {} as Ctx,
defaultValidationErrorsShape: this.#defaultValidationErrorsShape,
throwValidationErrors: this.#throwValidationErrors,
});
Expand Down

0 comments on commit ed3bd85

Please sign in to comment.