-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom cacheable decorator and fix eslint issue
- Loading branch information
Showing
39 changed files
with
448 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
npm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const ONE_MONTH_MS = 30 * 24 * 60 * 60 * 1000; | ||
export const ONE_HOUR_MS = 60 * 60 * 1000; | ||
export const TEN_MINUTES_MS = 10 * 60 * 1000; | ||
export const ONE_MINUTE_MS = 60 * 1000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { RPC } from '@ckb-lumos/rpc'; | ||
|
||
export interface CellDep { | ||
dep_type: string; | ||
out_point: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// eslint-disable-next-line no-restricted-imports | ||
import { CacheableRegisterOptions, Cacheable as _Cacheable } from 'nestjs-cacheable'; | ||
|
||
export interface CustomCacheableRegisterOptions extends CacheableRegisterOptions { | ||
shouldCache?: (result: any) => boolean | Promise<boolean>; | ||
} | ||
|
||
/** | ||
* Cacheable decorator with custom options, based on the original Cacheable decorator from the nestjs-cacheable package. | ||
* Adds a shouldCache option to determine whether the result should be cached. | ||
* | ||
* @example | ||
* @Cacheable({ | ||
* ttl: 1000, | ||
* key: (args: any[]) => args[0], | ||
* shouldCache: (result: any) => result !== null, | ||
* }); | ||
*/ | ||
export function Cacheable(options: CustomCacheableRegisterOptions): MethodDecorator { | ||
return function (_, propertyKey, descriptor) { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
const originalMethod = descriptor.value as unknown as Function; | ||
return { | ||
...descriptor, | ||
value: async function (...args: any[]) { | ||
const returnVal = await originalMethod.apply(this, args); | ||
|
||
const cacheable = options.shouldCache ? await options.shouldCache(returnVal) : true; | ||
if (!cacheable) { | ||
return returnVal; | ||
} | ||
|
||
const fakeDescriptor = { | ||
value: () => returnVal, | ||
}; | ||
return _Cacheable(options)(this, propertyKey, fakeDescriptor); | ||
} as any, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { FieldMiddleware, MiddlewareContext, NextFn } from '@nestjs/graphql'; | ||
|
||
export const fieldPerformanceMiddleware: FieldMiddleware = async ( | ||
ctx: MiddlewareContext, | ||
next: NextFn, | ||
) => { | ||
const now = performance.now(); | ||
const value = await next(); | ||
|
||
const executionTime = performance.now() - now; | ||
if (executionTime > 100) { | ||
const { path } = ctx.info; | ||
logger.debug(`[${path.typename}.${path.key}]: ${executionTime}ms`); | ||
} | ||
return value; | ||
}; | ||
|
||
const logger = new Logger(fieldPerformanceMiddleware.name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.