From c92a96d1fa723fdacc3056c4a22ff87ac1066bdd Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Wed, 22 May 2024 10:39:56 +0900 Subject: [PATCH] feat(REST API): Get cached time of `Response` --- rest/getCachedAt.ts | 12 ++++++++++++ rest/mod.ts | 1 + 2 files changed, 13 insertions(+) create mode 100644 rest/getCachedAt.ts diff --git a/rest/getCachedAt.ts b/rest/getCachedAt.ts new file mode 100644 index 0000000..0dd5dbf --- /dev/null +++ b/rest/getCachedAt.ts @@ -0,0 +1,12 @@ +/** ServiceWorkerにcacheされた日時を得る + * + * cacheされたResponseでなければ`undefined`を返す + * + * @param res Response to check the chached date + * @return cached date (as UNIX timestamp) or `undefined` + */ +export const getCachedAt = (res: Response): number | undefined => { + const cachedAt = res.headers.get("x-serviceworker-cached"); + if (!cachedAt) return; + return parseInt(cachedAt); +}; diff --git a/rest/mod.ts b/rest/mod.ts index 610f070..916fa9a 100644 --- a/rest/mod.ts +++ b/rest/mod.ts @@ -16,3 +16,4 @@ export * from "./error.ts"; export * from "./getCodeBlocks.ts"; export * from "./getCodeBlock.ts"; export * from "./uploadToGCS.ts"; +export * from "./getCachedAt.ts";