Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
improve EnhanceApiContext and EnhanceServerContext
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Aug 4, 2023
1 parent 9becf4a commit a5439f4
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log (@egomobile/next-utils)

## 0.6.0

- improve `EnhanceApiContext` and `EnhanceServerContext` using execution context objects instead

## 0.5.0

- add `options` argument to `EnhanceApiContext` and `EnhanceServerContext`
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@egomobile/next-utils",
"version": "0.5.0",
"version": "0.6.0",
"description": "Handy utils and extensions for Next.js",
"main": "lib/index.js",
"engines": {
Expand Down
37 changes: 30 additions & 7 deletions src/middlewares/withApiProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@ import { asError } from "../utils/internal/asError";
/**
* Function that enhances an API context.
*
* @param {TContext} context The context to anhance.
* @param {Nullable<Partial<IWithApiPropsOptions<TContext, TResponse>>>} options The underlying options.
*
* @returns {false|void|PromiseLike<void|false>} The `false`, the execution will be stopped.
* @param {IEnhanceApiContextContext<TContext, TResponse>} context The execution context.
*/
export type EnhanceApiContext<TContext = IGetApiPropsActionContext<any>, TResponse = any> =
(context: TContext, options: Nullable<Partial<IWithApiPropsOptions<TContext, TResponse>>>) => false | void | PromiseLike<void | false>;
(context: IEnhanceApiContextExecutionContext<TContext, TResponse>) => void | PromiseLike<void>;

/**
* An action for a `getProps` value of an `IWithApiPropsOptions` instance.
Expand Down Expand Up @@ -76,6 +73,26 @@ export interface ICreateWithApiPropsOptions<TContext = IGetApiPropsActionContext
onError?: Nilable<ServerErrorHandler>;
}

/**
* Context for an `EnhanceApiContext` function.
*/
export interface IEnhanceApiContextExecutionContext<TContext = IGetApiPropsActionContext<any>, TResponse = any> {
/**
* The context to enhance.
*/
context: TContext;
/**
* The options.
*/
options: Nullable<Partial<IWithApiPropsOptions<TContext, TResponse>>>;
/**
* Gets or sets if the execution should be stopped or not.
*
* @default `false`
*/
shouldStop: boolean;
}

/**
* Context for a `GetApiPropsAction` action.
*/
Expand Down Expand Up @@ -211,8 +228,14 @@ export function createWithApiProps<TContext = IGetApiPropsActionContext<any>>(
...options
} : null;

const shouldStop = ((await enhanceContext?.(getPropsContext as unknown as TContext, copyOfOptions)) as any) === false;
if (shouldStop) {
const enhanceExecCtx: IEnhanceApiContextExecutionContext<TContext, any> = {
"context": getPropsContext as any,
"options": copyOfOptions as any,
"shouldStop": false
};

await enhanceContext?.(enhanceExecCtx);
if (enhanceExecCtx.shouldStop) {
return;
}

Expand Down
44 changes: 37 additions & 7 deletions src/middlewares/withServerProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ import { wrapServerHandler } from "../utils/internal/wrapServerHandler";
* Function that enhances a server context.
*
* @param {TContext} context The context to anhance.
*
* @returns {false|void|PromiseLike<void|false>} The `false`, the execution will be stopped.
*/
export type EnhanceServerContext<TContext = IWithServerPropsActionContext, TResponse = any> =
(context: TContext, options: Nullable<Partial<IWithServerPropsOptions>>) => false | void | PromiseLike<void | false>;
export type EnhanceServerContext<TContext = IWithServerPropsActionContext> =
(context: IEnhanceServerContextExecutionContext<TContext>) => void | PromiseLike<void>;

/**
* Options for `createWithServerProps()` function.
Expand All @@ -44,6 +42,30 @@ export interface ICreateWithServerPropsOptions<TContext = IWithServerPropsAction
onError?: Nilable<ServerErrorHandler>;
}

/**
* Context for an `EnhanceServerContext` function.
*/
export interface IEnhanceServerContextExecutionContext<TContext = IWithServerPropsActionContext> {
/**
* The context to enhance.
*/
context: TContext;
/**
* The options.
*/
options: Nullable<Partial<IWithServerPropsOptions>>;
/**
* The props to return if execution will be stopped.
*/
props: Record<string, any>;
/**
* Gets or sets if the execution should be stopped or not.
*
* @default `false`
*/
shouldStop: boolean;
}

/**
* A context for a `WithServerPropsAction` action.
*/
Expand Down Expand Up @@ -126,10 +148,18 @@ export function createWithServerProps<TContext = IWithServerPropsActionContext>(
...options
} : null;

const shouldStop = ((await enhanceContext?.(actionContext as unknown as TContext, copyOfOptions)) as any) === false;
if (shouldStop) {
const enhanceExecCtx: IEnhanceServerContextExecutionContext<TContext> = {
"context": actionContext as any,
"options": copyOfOptions as any,
"props": {},
"shouldStop": false
};

await enhanceContext?.(enhanceExecCtx);

if (enhanceExecCtx.shouldStop) {
return {
"props": {}
"props": enhanceExecCtx.props ?? {}
};
}

Expand Down

0 comments on commit a5439f4

Please sign in to comment.