From ed3bd8544212ce1585e82804d4b0a68e11e13005 Mon Sep 17 00:00:00 2001 From: Edoardo Ranghieri Date: Sun, 11 Aug 2024 16:46:57 +0200 Subject: [PATCH] refactor: require context to be an object --- .../src/__tests__/middleware.test.ts | 4 +-- .../next-safe-action/src/action-builder.ts | 6 ++-- packages/next-safe-action/src/index.ts | 4 +-- packages/next-safe-action/src/index.types.ts | 32 +++++++++++++------ .../src/safe-action-client.ts | 15 +++++---- 5 files changed, 38 insertions(+), 23 deletions(-) diff --git a/packages/next-safe-action/src/__tests__/middleware.test.ts b/packages/next-safe-action/src/__tests__/middleware.test.ts index 60ba0ae5..6b1f02a1 100644 --- a/packages/next-safe-action/src/__tests__/middleware.test.ts +++ b/packages/next-safe-action/src/__tests__/middleware.test.ts @@ -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: { @@ -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)"], diff --git a/packages/next-safe-action/src/action-builder.ts b/packages/next-safe-action/src/action-builder.ts index ebd9ad9c..f5efb77c 100644 --- a/packages/next-safe-action/src/action-builder.ts +++ b/packages/next-safe-action/src/action-builder.ts @@ -26,7 +26,7 @@ export function actionBuilder< ServerError, MetadataSchema extends Schema | undefined = undefined, MD = MetadataSchema extends Schema ? Infer : undefined, - Ctx = undefined, + Ctx extends object = {}, SF extends (() => Promise) | undefined = undefined, // schema function S extends Schema | undefined = SF extends Function ? Awaited> : undefined, const BAS extends readonly Schema[] = [], @@ -71,8 +71,8 @@ export function actionBuilder< utils?: SafeActionUtils ) => { return async (...clientInputs: unknown[]) => { - let prevCtx: unknown = undefined; - const middlewareResult: MiddlewareResult = { success: false }; + let prevCtx: object = {}; + const middlewareResult: MiddlewareResult = { success: false }; type PrevResult = SafeActionResult | undefined; let prevResult: PrevResult | undefined = undefined; const parsedInputDatas: any[] = []; diff --git a/packages/next-safe-action/src/index.ts b/packages/next-safe-action/src/index.ts index 033c4368..7c6756c1 100644 --- a/packages/next-safe-action/src/index.ts +++ b/packages/next-safe-action/src/index.ts @@ -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 : undefined, defaultValidationErrorsShape: (createOpts?.defaultValidationErrorsShape ?? "formatted") as ODVES, diff --git a/packages/next-safe-action/src/index.types.ts b/packages/next-safe-action/src/index.types.ts index 73c41389..ede2f975 100644 --- a/packages/next-safe-action/src/index.types.ts +++ b/packages/next-safe-action/src/index.types.ts @@ -14,7 +14,7 @@ export type DVES = "formatted" | "flattened"; export type ServerErrorFunctionUtils = { clientInput: unknown; bindArgsClientInputs: unknown[]; - ctx: unknown; + ctx: object; metadata: MetadataSchema extends Schema ? Infer : undefined; }; @@ -53,7 +53,7 @@ export type SafeActionResult< CBAVE = BindArgsValidationErrors, Data = unknown, // eslint-disable-next-line - NextCtx = unknown, + NextCtx = object, > = { data?: Data; serverError?: ServerError; @@ -90,24 +90,32 @@ 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 = SafeActionResult & { +export type MiddlewareResult = 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 = { +export type MiddlewareFn = { (opts: { clientInput: unknown; bindArgsClientInputs: unknown[]; ctx: Ctx; metadata: MD; next: { - (opts: { ctx: NC }): Promise>; + (opts: { ctx: NC }): Promise>; }; }): Promise>; }; @@ -115,7 +123,13 @@ export type MiddlewareFn = { /** * Type of the function that executes server code when defining a new safe action. */ -export type ServerCodeFn = (args: { +export type ServerCodeFn< + MD, + Ctx extends object, + S extends Schema | undefined, + BAS extends readonly Schema[], + Data, +> = (args: { parsedInput: S extends Schema ? Infer : undefined; bindArgsParsedInputs: InferArray; ctx: Ctx; @@ -128,7 +142,7 @@ export type ServerCodeFn : undefined, - Ctx = undefined, + Ctx extends object = {}, SF extends (() => Promise) | undefined = undefined, // schema function S extends Schema | undefined = SF extends Function ? Awaited> : undefined, const BAS extends readonly Schema[] = [], @@ -37,10 +37,10 @@ export class SafeActionClient< SafeActionClientOpts["handleReturnedServerError"] >; readonly #middlewareFns: MiddlewareFn[]; - 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; @@ -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; @@ -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(middlewareFn: MiddlewareFn) { + use(middlewareFn: MiddlewareFn) { return new SafeActionClient({ middlewareFns: [...this.#middlewareFns, middlewareFn], handleReturnedServerError: this.#handleReturnedServerError, @@ -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, }); @@ -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, }); @@ -164,7 +165,7 @@ export class SafeActionClient< handleValidationErrorsShape: (utils?.handleValidationErrorsShape ?? this.#handleValidationErrorsShape) as HandleValidationErrorsShapeFn, handleBindArgsValidationErrorsShape: this.#handleBindArgsValidationErrorsShape, - ctxType: undefined as Ctx, + ctxType: {} as Ctx, defaultValidationErrorsShape: this.#defaultValidationErrorsShape, throwValidationErrors: this.#throwValidationErrors, }); @@ -198,7 +199,7 @@ export class SafeActionClient< handleValidationErrorsShape: this.#handleValidationErrorsShape, handleBindArgsValidationErrorsShape: (utils?.handleBindArgsValidationErrorsShape ?? this.#handleBindArgsValidationErrorsShape) as HandleBindArgsValidationErrorsShapeFn, - ctxType: undefined as Ctx, + ctxType: {} as Ctx, defaultValidationErrorsShape: this.#defaultValidationErrorsShape, throwValidationErrors: this.#throwValidationErrors, });