-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve api typing #129
Improve api typing #129
Changes from 6 commits
b31c2a5
d786e25
0a188e6
e32695f
4f25615
580d1ed
fb9f5de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
import mcache from "memory-cache"; | ||
|
||
export default function MemoryCacheEngine() {} | ||
|
||
/** | ||
* Used to retrieve data from memcache | ||
* @param {*} key | ||
*/ | ||
MemoryCacheEngine.prototype.getFromCache = (key) => { | ||
const cachedBody = mcache.get(key); | ||
if (cachedBody) { | ||
return cachedBody; | ||
export default class MemoryCacheEngine { | ||
/** | ||
* Used to retrieve data from memcache | ||
* @param {*} key | ||
*/ | ||
getFromCache(key: string) { | ||
const cachedBody = mcache.get(key); | ||
if (cachedBody) { | ||
return cachedBody; | ||
} | ||
return false; | ||
} | ||
return false; | ||
}; | ||
|
||
/** | ||
* Used to store data in a memcache | ||
* @param {*} key | ||
* @param {*} data | ||
* @param {*} duration | ||
*/ | ||
MemoryCacheEngine.prototype.storeInCache = (key, data, duration) => { | ||
mcache.put(key, data, duration); | ||
}; | ||
/** | ||
* Used to delete all keys in a memcache | ||
*/ | ||
MemoryCacheEngine.prototype.clearAllKeyInCache = () => { | ||
mcache.clear(); | ||
}; | ||
/** | ||
* Used to delete specific key from memcache | ||
* @param {*} key | ||
*/ | ||
MemoryCacheEngine.prototype.clearAllKeyInCache = (key) => { | ||
mcache.del(key); | ||
}; | ||
/** | ||
* Used to store data in a memcache | ||
* @param {*} key | ||
* @param {*} data | ||
* @param {*} duration | ||
*/ | ||
storeInCache<T>(key: string, data: T, duration?: number) { | ||
mcache.put(key, data, duration); | ||
} | ||
/** | ||
* Used to delete all keys in a memcache | ||
*/ | ||
clearAllKeyInCache() { | ||
mcache.clear(); | ||
} | ||
/** | ||
* Used to delete specific key from memcache | ||
* @param {*} key | ||
*/ | ||
clearKeyInCache(key: string) { | ||
mcache.del(key); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,13 @@ | ||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi"; | ||
import { getGraphData } from "@src/services/db/statsService"; | ||
|
||
const authorizedDataNames = [ | ||
"dailyUAktSpent", | ||
"dailyUUsdcSpent", | ||
"dailyUUsdSpent", | ||
"dailyLeaseCount", | ||
"totalUAktSpent", | ||
"totalUUsdcSpent", | ||
"totalUUsdSpent", | ||
"activeLeaseCount", | ||
"totalLeaseCount", | ||
"activeCPU", | ||
"activeGPU", | ||
"activeMemory", | ||
"activeStorage" | ||
]; | ||
import { AuthorizedGraphDataNames, getGraphData, isValidGraphDataName } from "@src/services/db/statsService"; | ||
|
||
const route = createRoute({ | ||
method: "get", | ||
path: "/graph-data/{dataName}", | ||
tags: ["Analytics"], | ||
request: { | ||
params: z.object({ | ||
dataName: z.string().openapi({ example: "dailyUAktSpent", enum: authorizedDataNames }) | ||
dataName: z.string().openapi({ example: "dailyUAktSpent", enum: Array.from(AuthorizedGraphDataNames) }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
AuthorizedGraphDataNames as unknown as string[] However I'd just created a dedicated type in the source instead of deriving type from an array. Yes, some duplication but ts would enforce to follow and it works consistently without hacks type AuthorizedGraphDataName = (
"dailyUAktSpent" |
"dailyUUsdcSpent" |
"dailyUUsdSpent" |
"dailyLeaseCount" |
"totalUAktSpent" |
"totalUUsdcSpent" |
"totalUUsdSpent" |
"activeLeaseCount" |
"totalLeaseCount" |
"activeCPU" |
"activeGPU" |
"activeMemory" |
"activeStorage"
)
export const authorizedGraphDataNames: AuthorizedGraphDataName[] = [
"dailyUAktSpent",
"dailyUUsdcSpent",
"dailyUUsdSpent",
"dailyLeaseCount",
"totalUAktSpent",
"totalUUsdcSpent",
"totalUUsdSpent",
"activeLeaseCount",
"totalLeaseCount",
"activeCPU",
"activeGPU",
"activeMemory",
"activeStorage"
]; |
||
}) | ||
}, | ||
responses: { | ||
|
@@ -53,9 +37,9 @@ const route = createRoute({ | |
export default new OpenAPIHono().openapi(route, async (c) => { | ||
const dataName = c.req.valid("param").dataName; | ||
|
||
if (!authorizedDataNames.includes(dataName)) { | ||
if (!isValidGraphDataName(dataName)) { | ||
console.log("Rejected graph request: " + dataName); | ||
return c.text("Graph not found", 404); | ||
return c.text("Graph not found: " + dataName, 404); | ||
} | ||
|
||
const graphData = await getGraphData(dataName); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it needed to be this flexible? if you skip this type it's implicitly typed as object with specified keys and values. It seems that what really needed here is typed env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the type and added a typeguard instead. But yeah ideally we would have env var typing & validation.