|
1 | 1 | // @ts-check |
2 | | -import { isExtensionEndpointHandler } from "../helpers"; |
3 | | -import { createExtendQuery } from "./createExtendQuery"; |
4 | 2 | import { |
5 | 3 | AfterCallParams, |
6 | 4 | ApiMethods, |
7 | 5 | ApplyingContextHooks, |
8 | 6 | BeforeCallParams, |
9 | 7 | MiddlewareContext, |
10 | 8 | } from "../types"; |
11 | | -import { getLogger } from "../logger"; |
| 9 | +import { |
| 10 | + ExtensionEndpointMethodDecorator, |
| 11 | + PurgeHookNameMethodDecorator, |
| 12 | + MethodDecoratorManager, |
| 13 | + MissingScopeMethodDecorator, |
| 14 | + OrchiestratedMethodDecorator, |
| 15 | + ScopeTypeMethodDecorator, |
| 16 | +} from "./methodDecorator"; |
12 | 17 |
|
13 | 18 | const nopBefore = <ARGS>({ args }: BeforeCallParams<any, ARGS>): ARGS => args; |
14 | 19 | const nopAfter = <RESPONSE>({ |
@@ -39,87 +44,27 @@ const applyContextToApi = < |
39 | 44 | (prev, [callName, fn]) => ({ |
40 | 45 | ...prev, |
41 | 46 | [callName]: async (...args: Parameters<typeof fn>) => { |
42 | | - const hasMetadataScope = checkMetadataScope(context); |
43 | | - if (!hasMetadataScope) { |
44 | | - const logger = getLogger(context.res); |
45 | | - logger.warning( |
46 | | - `Alokai's metadata object is missing in the context under 'res.locals.alokai'. |
47 | | - This could indicate that the extension's scope or metadata has not been properly initialized. |
48 | | - Without this metadata, certain custom API client functionalities may not work as expected, |
49 | | - including tracking of extension-specific actions or data. |
50 | | - Please ensure that the Alokai metadata object is correctly set up in 'res.locals.alokai' before proceeding with the request. |
51 | | - |
52 | | - Steps to troubleshoot: |
53 | | - 1. Verify if content of res.locals hasn't been overwritten, instead of extended. |
54 | | - 2. If you're unsure, please consult Alokai's team for further assistance or review the source code implementation. |
55 | | -
|
56 | | - Call Name: ${callName} |
57 | | - Function Name: ${fn.name || "Unnamed function"}` |
58 | | - ); |
59 | | - } |
60 | | - const extendQuery = createExtendQuery(context); |
61 | | - let tmpIntegrationName = ""; |
62 | | - let tmpFnName = ""; |
63 | | - if (hasMetadataScope) { |
64 | | - context.res.locals.alokai.metadata.scope.type = "requestHook"; |
65 | | - tmpIntegrationName = |
66 | | - context.res.locals.alokai.metadata.scope.integrationName; |
67 | | - context.res.locals.alokai.metadata.scope.integrationName = |
68 | | - context.integrationKey; |
69 | | - |
70 | | - tmpFnName = context.res.locals.alokai.metadata.scope.functionName; |
71 | | - context.res.locals.alokai.metadata.scope.functionName = callName; |
72 | | - } |
73 | | - const transformedArgs = await hooks.before({ callName, args }); |
74 | | - const apiClientContext = { ...context, extendQuery }; |
75 | | - if (isExtensionEndpointHandler(fn)) { |
76 | | - context.res.locals.alokai.metadata.scope.extensionName = |
77 | | - fn._extensionName; |
78 | | - } |
79 | | - let tmp = { |
80 | | - extensionName: null, |
81 | | - functionName: null, |
82 | | - }; |
83 | | - const isTmpSet = (tmp) => !!tmp.functionName; // as we never set fn name to nullish |
84 | | - if (hasMetadataScope) { |
85 | | - context.res.locals.alokai.metadata.scope.type = "endpoint"; |
86 | | - context.res.locals.alokai.metadata.scope.hookName = undefined; |
87 | | - if (!(fn as any)._extensionName) { |
88 | | - tmp = { |
89 | | - extensionName: |
90 | | - context.res.locals.alokai.metadata.scope.extensionName, |
91 | | - functionName: |
92 | | - context.res.locals.alokai.metadata.scope.functionName, |
93 | | - }; |
94 | | - context.res.locals.alokai.metadata.scope.extensionName = undefined; |
95 | | - context.res.locals.alokai.metadata.scope.functionName = callName; |
96 | | - // console.log(`wywolam ${fn.name} lub ${callName}`, { ...tmp }); |
97 | | - } |
98 | | - } |
99 | | - const response = await fn(apiClientContext, ...transformedArgs); |
100 | | - if (hasMetadataScope) { |
101 | | - if (isTmpSet(tmp)) { |
102 | | - context.res.locals.alokai.metadata.scope.extensionName = |
103 | | - tmp.extensionName; |
104 | | - context.res.locals.alokai.metadata.scope.functionName = |
105 | | - tmp.functionName; |
106 | | - } |
107 | | - // console.log("co ustawiam?: ", { ...tmp }); |
108 | | - context.res.locals.alokai.metadata.scope.type = "requestHook"; |
109 | | - } |
110 | | - const transformedResponse = await hooks.after({ |
| 47 | + const methodDecoratorManager = new MethodDecoratorManager( |
| 48 | + context, |
| 49 | + hooks, |
111 | 50 | callName, |
112 | | - args, |
113 | | - response, |
114 | | - }); |
| 51 | + fn |
| 52 | + ); |
| 53 | + |
| 54 | + const hasMetadataScope = checkMetadataScope(context); |
| 55 | + const decoratorsToAdd = hasMetadataScope |
| 56 | + ? [ |
| 57 | + new ScopeTypeMethodDecorator(context), |
| 58 | + new PurgeHookNameMethodDecorator(context), |
| 59 | + new ExtensionEndpointMethodDecorator(context, fn), |
| 60 | + new OrchiestratedMethodDecorator(context, callName, fn), |
| 61 | + ] |
| 62 | + : [new MissingScopeMethodDecorator(context, callName, fn.name)]; |
| 63 | + methodDecoratorManager.addDecorator(...decoratorsToAdd); |
115 | 64 |
|
116 | | - if (hasMetadataScope) { |
117 | | - context.res.locals.alokai.metadata.scope.integrationName = |
118 | | - tmpIntegrationName; |
119 | | - context.res.locals.alokai.metadata.scope.functionName = tmpFnName; |
120 | | - } |
| 65 | + const method = methodDecoratorManager.prepare<typeof args>(); |
121 | 66 |
|
122 | | - return transformedResponse; |
| 67 | + return await method(args); |
123 | 68 | }, |
124 | 69 | }), |
125 | 70 | {} as any |
|
0 commit comments