Merged in feat/common-package (pull request #2333)
feat: Add common package * Add isEdge, safeTry and dataCache to new common package * Add eslint and move prettier config * Fix yarn lock * Clean up tests * Add lint-staged config to common * Add missing dependencies Approved-by: Joakim Jäderberg
This commit is contained in:
32
packages/common/dataCache/DistributedCache/set.ts
Normal file
32
packages/common/dataCache/DistributedCache/set.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
|
||||
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
||||
|
||||
import { type CacheTime, getCacheTimeInSeconds } from "../Cache"
|
||||
import { API_KEY } from "./client"
|
||||
import { getCacheEndpoint } from "./endpoints"
|
||||
|
||||
export async function set<T>(key: string, value: T, ttl: CacheTime) {
|
||||
const [response, error] = await safeTry(
|
||||
fetch(getCacheEndpoint(key), {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-api-key": API_KEY,
|
||||
},
|
||||
body: JSON.stringify({ data: value, ttl: getCacheTimeInSeconds(ttl) }),
|
||||
cache: "no-cache",
|
||||
signal: AbortSignal.timeout(3_000),
|
||||
})
|
||||
)
|
||||
|
||||
if (!response || !response.ok || error) {
|
||||
Sentry.captureException(error ?? new Error("Unable to SET cachekey"), {
|
||||
extra: {
|
||||
cacheKey: key,
|
||||
statusCode: response?.status,
|
||||
statusText: response?.statusText,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user