From b6822c6bfc9bfba86dfea9b924f2e7d08b7f8dd7 Mon Sep 17 00:00:00 2001 From: Mahesh Date: Sat, 7 Sep 2024 17:14:49 +0530 Subject: [PATCH] perf: support type suggestions for hono context object --- src/context.d.ts | 43 ++++++++++++++++++++++++++++++++++ src/middlewares/cache/index.ts | 10 ++++---- 2 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/context.d.ts diff --git a/src/context.d.ts b/src/context.d.ts new file mode 100644 index 000000000..1549f9231 --- /dev/null +++ b/src/context.d.ts @@ -0,0 +1,43 @@ +export * from 'hono'; + +enum CACHE_STATUS { + HIT = 'HIT', + SEMANTIC_HIT = 'SEMANTIC HIT', + MISS = 'MISS', + SEMANTIC_MISS = 'SEMANTIC MISS', + REFRESH = 'REFRESH', + DISABLED = 'DISABLED', +} + +declare module 'hono' { + interface ContextVariableMap { + hooksManager: import('./middlewares/hooks').HooksManager; + executeHooks: ContextVariableMap['hooksManager']['executeHooks']; + getFromCache: ( + env: any, + requestHeaders: any, + requestBody: any, + url: string, + organisationId: string, + cacheMode: string, + cacheMaxAge?: string | number | null + ) => Promise<[any, CACHE_STATUS, any]>; + requestOptions: [ + { + providerOptions: { + provider: string; + requestURL: string; + rubeusURL: string; + [key: string]: any; + }; + requestParams: any; + response: Response; + cacheStatus: CACHE_STATUS; + cacheKey: string; + cacheMode: string; + // intentionally undefined + cacheMaxAge: string | number | null | undefined; + }, + ]; + } +} diff --git a/src/middlewares/cache/index.ts b/src/middlewares/cache/index.ts index 127660e72..084e62ac9 100644 --- a/src/middlewares/cache/index.ts +++ b/src/middlewares/cache/index.ts @@ -100,14 +100,14 @@ export const memoryCache = () => { Array.isArray(requestOptions) && requestOptions.length > 0 ) { - requestOptions = requestOptions[0]; - if (requestOptions.cacheMode === 'simple') { + const _requestOptions = requestOptions[0]; + if (_requestOptions.cacheMode === 'simple') { await putInCache( null, null, - requestOptions.requestParams, - await requestOptions.response.json(), - requestOptions.providerOptions.rubeusURL, + _requestOptions.requestParams, + await _requestOptions.response.json(), + _requestOptions.providerOptions.rubeusURL, '', null, null