| 14 | import { getVariant } from './utils/cache-utils'; |
| 15 | |
| 16 | export class ISRHandler { |
| 17 | protected cache!: CacheHandler; |
| 18 | protected cacheGeneration!: CacheGeneration; |
| 19 | protected logger: ISRLogger; |
| 20 | |
| 21 | constructor(protected isrConfig: ISRHandlerConfig) { |
| 22 | if (!isrConfig) { |
| 23 | throw new Error('Provide ISRHandlerConfig!'); |
| 24 | } |
| 25 | |
| 26 | this.logger = new ISRLogger(this.isrConfig?.enableLogging || false); |
| 27 | // if skipCachingOnHttpError is not provided it will default to true |
| 28 | isrConfig.skipCachingOnHttpError = |
| 29 | isrConfig.skipCachingOnHttpError !== false; |
| 30 | // if buildId is not provided it will default to null |
| 31 | isrConfig.buildId = isrConfig.buildId || null; |
| 32 | // if invalidateSecretToken is not provided it will default to null |
| 33 | isrConfig.invalidateSecretToken = isrConfig.invalidateSecretToken || null; |
| 34 | |
| 35 | if (isrConfig.cache && isrConfig.cache instanceof CacheHandler) { |
| 36 | this.logger.log('Using custom cache handler!'); |
| 37 | this.cache = isrConfig.cache; |
| 38 | } else { |
| 39 | this.logger.log('Using in memory cache handler!'); |
| 40 | this.cache = new InMemoryCacheHandler(); |
| 41 | } |
| 42 | |
| 43 | this.cacheGeneration = new CacheGeneration( |
| 44 | this.isrConfig, |
| 45 | this.cache, |
| 46 | this.logger, |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | async invalidate( |
| 51 | req: Request, |
| 52 | res: Response, |
| 53 | config?: InvalidateConfig, |
| 54 | ): Promise<Response> { |
| 55 | const { token, urlsToInvalidate } = extractDataFromBody(req); |
| 56 | |
| 57 | if (token !== this.isrConfig.invalidateSecretToken) { |
| 58 | return res.json({ |
| 59 | status: 'error', |
| 60 | message: 'Your secret token is wrong!!!', |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | if (!urlsToInvalidate || !urlsToInvalidate.length) { |
| 65 | return res.json({ |
| 66 | status: 'error', |
| 67 | message: 'Please add `urlsToInvalidate` in the payload!', |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | const notInCache: string[] = []; |
| 72 | const urlWithErrors: Record<string, string[]> = {}; |
| 73 |
nothing calls this directly
no outgoing calls
no test coverage detected