@stable-io/cctp-sdk-definitions
Version:
Definitions for the CCTP SDK
24 lines • 1.03 kB
JavaScript
// Copyright (c) 2025 Stable Technologies Inc
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
import { duration } from "./constants/index.js";
const defaultApiOptions = {
method: "GET",
headers: { "Content-Type": "application/json" },
};
const defaultCacheTtl = duration(10, "sec");
const apiCache = new Map();
export const fetchApiResponse = async (endpoint, ttl = defaultCacheTtl) => {
const now = Date.now();
const cached = apiCache.get(endpoint);
if (cached && now - cached.timestamp <= ttl.toUnit("msec").floor()) {
return { status: cached.status, value: cached.value };
}
const response = await fetch(endpoint, defaultApiOptions);
const status = response.status;
const value = await response.json();
apiCache.set(endpoint, { status, value, timestamp: now });
return { status, value };
};
//# sourceMappingURL=api.js.map