Skip to content

Commit

Permalink
refactor: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Fifciu committed Oct 28, 2024
1 parent 2a6cfd4 commit a737a4e
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ describe("[getInitConfig]", () => {
});

it("should return an empty object when apiClient is not defined", async () => {
const params = {} as unknown as LoadInitConfigProps;
const params = {
alokai: {
logger,
},
} as unknown as LoadInitConfigProps;
const result = await getInitConfig(params);

expect(result).toEqual({});
Expand Down
84 changes: 44 additions & 40 deletions packages/middleware/src/apiClientFactory/applyContextToApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { createExtendQuery } from "./createExtendQuery";
import { markExtensionNameHelpers } from "./markExtensionNameHelpers";
import { getLogger, injectMetadata } from "../logger";
import { wrapFnWithErrorBoundary } from "./wrapFnWithErrorBoundary";

const nopBefore = <ARGS>({ args }: BeforeCallParams<any, ARGS>): ARGS => args;
const nopAfter = <RESPONSE>({
Expand Down Expand Up @@ -48,32 +49,6 @@ function injectHandlerMetadata<CONTEXT extends MiddlewareContext>(
});
}

async function injectHandlerErrorMetadata<CONTEXT extends MiddlewareContext>(
fn: Function,
args: any[],
context: CONTEXT,
callName: string
) {
try {
const response = await fn(...args);
return response;
} catch (err) {
const errorBoundary: LogScope = err.errorBoundary || {
type: "endpoint" as const,
functionName: callName,
integrationName: context.integrationTag,
};

if (!err.errorBoundary && markExtensionNameHelpers.has(fn)) {
errorBoundary.extensionName = markExtensionNameHelpers.get(fn);
}

Object.assign(err, { errorBoundary });

throw err;
}
}

/**
* Wraps api methods with context and hooks triggers
*/
Expand All @@ -94,27 +69,51 @@ const applyContextToApi = <
(prev, [callName, fn]) => ({
...prev,
[callName]: (() => {
const newFn = async (...args: Parameters<typeof fn>) => {
const logger = injectHandlerMetadata(context, fn, callName);
/**
* Raw endpoint's handler decorated with:
* - hooks from every extension (hooks.before contains merged beforeCall's,
* hooks.after contains merged afterCall's),
* - logger with metadata about scope of call (covers orchestrated parallely called endpoints),
* - support for building error boundary (covers orchestrated parallely called endpoints),
*/
const handler = async (...args: Parameters<typeof fn>) => {
const transformedArgs = await hooks.before({
callName,
args,
});

const fnWithErrorBoundary = wrapFnWithErrorBoundary(fn, (err) => {
/**
* Handlers can call different handlers, so error could be already bubbling.
* That's why we check for presence of err.errorBoundary at first.
*
* ```ts
* async function myEndpoint(context) {
* const int = await context.getApiClient("some_integration");
* return await int.api.throwError(); // Bubbling from other integration's endpoint
* }
* ```
*/
const errorBoundary: LogScope = err.errorBoundary || {
type: "endpoint" as const,
functionName: callName,
integrationName: context.integrationTag,
...(markExtensionNameHelpers.has(fn)
? { extensionName: markExtensionNameHelpers.get(fn) }
: {}),
};

return errorBoundary;
});
const apiClientContext = {
...context,
extendQuery: createExtendQuery(context),
logger,
};
const response = await injectHandlerErrorMetadata(
fn,
[
{
...apiClientContext,
logger: injectHandlerMetadata(context, fn, callName),
},
...transformedArgs,
],
context,
callName
const response = await fnWithErrorBoundary(
apiClientContext,
...transformedArgs
);

const transformedResponse = await hooks.after({
Expand All @@ -125,13 +124,18 @@ const applyContextToApi = <

return transformedResponse;
};

/**
* Marks decorated handler with name of integration's extension
* from which original handler was defined, if any
*/
if (markExtensionNameHelpers.has(fn)) {
markExtensionNameHelpers.mark(
newFn,
handler,
markExtensionNameHelpers.get(fn)
);
}
return newFn;
return handler;
})(),
}),
{} as any
Expand Down
Loading

0 comments on commit a737a4e

Please sign in to comment.