From e7ef0e5cee6733a147c2a85c142feb0d933e630f Mon Sep 17 00:00:00 2001 From: Jerry Ng Date: Wed, 5 Oct 2022 09:18:43 +0800 Subject: [PATCH] refactor: move URL_CACHE to wrangler.toml --- src/handlers/createShortUrl.js | 2 -- src/middleware/shortUrlCache.js | 3 +-- src/utils/constants.js | 3 --- src/utils/urlKey.js | 3 ++- wrangler.toml | 3 +++ 5 files changed, 6 insertions(+), 8 deletions(-) delete mode 100644 src/utils/constants.js diff --git a/src/handlers/createShortUrl.js b/src/handlers/createShortUrl.js index b1ba7b4..b526927 100644 --- a/src/handlers/createShortUrl.js +++ b/src/handlers/createShortUrl.js @@ -1,4 +1,3 @@ -import { URL_CACHE } from '../utils/constants' import { generateUniqueUrlKey } from '../utils/urlKey' /* @@ -43,7 +42,6 @@ export const createShortUrl = async (request, event) => { }), { headers: { 'Content-Type': 'application/json' } } ) - const cache = await caches.open(URL_CACHE) event.waitUntil( diff --git a/src/middleware/shortUrlCache.js b/src/middleware/shortUrlCache.js index 5595d18..8d8c36a 100644 --- a/src/middleware/shortUrlCache.js +++ b/src/middleware/shortUrlCache.js @@ -1,5 +1,3 @@ -import { URL_CACHE } from '../utils/constants' - /* A cache middleware that checks if `originalUrl` is present in the `URL_CACHE` cache. @@ -16,6 +14,7 @@ export const shortUrlCacheMiddleware = async (request) => { }) } + /* eslint-disable no-undef */ const cache = await caches.open(URL_CACHE) const response = await cache.match(originalUrl) diff --git a/src/utils/constants.js b/src/utils/constants.js deleted file mode 100644 index 58ae666..0000000 --- a/src/utils/constants.js +++ /dev/null @@ -1,3 +0,0 @@ -export const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' - -export const URL_CACHE = 'apiCache' diff --git a/src/utils/urlKey.js b/src/utils/urlKey.js index 310625e..8e58fdb 100644 --- a/src/utils/urlKey.js +++ b/src/utils/urlKey.js @@ -1,5 +1,6 @@ import { customAlphabet } from 'nanoid' -import { ALPHABET } from './constants' + +const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' /* Generate a unique `urlKey` using `nanoid` package. diff --git a/wrangler.toml b/wrangler.toml index b89f275..ec49039 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -9,6 +9,7 @@ kv_namespaces = [ ] [vars] +URL_CACHE = "apiCache" URL_EXPIRATION_TTL = 86400 [dev] @@ -24,6 +25,7 @@ kv_namespaces = [ { binding = "URL_DB", id = "bf98832a3f6e45e18f3c39b6db1d329a" }, ] [env.staging.vars] +URL_CACHE = "apiCache" URL_EXPIRATION_TTL = 86400 @@ -35,4 +37,5 @@ kv_namespaces = [ { binding = "URL_DB", id = "709b8cf5ddf74f739f1535c34ff0634b" }, ] [env.production.vars] +URL_CACHE = "apiCache" URL_EXPIRATION_TTL = 604800