Skip to content

Commit

Permalink
Include detailsResponse in schema data
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Sep 7, 2023
1 parent 147889a commit 8085c12
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/routes/pull-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,14 @@ export async function pullSchema(
JSON.stringify(responseContext, null, "\t"),
)
if (activeResponse.schemaId) {
const detailsResponse = responseContext.detailsResponse
await env.SCHEMAS.put(
activeResponse.schemaId,
JSON.stringify({ activeResponse, modelResponse }, null, "\t"),
JSON.stringify(
{ activeResponse, modelResponse, detailsResponse },
null,
"\t",
),
)
} else {
errors.push("Schema ID not found in activeResponse")
Expand Down
36 changes: 35 additions & 1 deletion src/routes/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { DeviceActiveResponse } from "../api/device-types"
import { ObjectType } from "../types"
import { Env } from "./common"
import { checkAuth, Env } from "./common"

type ProductType = {
activeResponse: DeviceActiveResponse
modelResponse: ObjectType
detailsResponse: ObjectType
}

export async function schemas(
request: Request,
Expand All @@ -8,6 +15,33 @@ export async function schemas(
): Promise<ObjectType | null | string[] | Response> {
const url = new URL(request.url)

if (url.pathname.split("/")[2] === "rebuild") {
let auth
if ((auth = checkAuth(request, env, true))) return auth

const result = []
const productKeys = (await env.PRODUCTS.list()).keys.map((k) => k.name)
for (const productKey of productKeys) {
const product = await env.PRODUCTS.get<ProductType>(
productKey,
"json",
)
if (!product) continue
const { activeResponse, modelResponse, detailsResponse } = product
result.push(activeResponse.schemaId)
await env.SCHEMAS.put(
activeResponse.schemaId,
JSON.stringify(
{ activeResponse, modelResponse, detailsResponse },
null,
"\t",
),
)
}

return Response.json(result)
}

if (request.method == "GET") {
let schemaKey: string
if ((schemaKey = url.pathname.split("/")[2])) {
Expand Down

0 comments on commit 8085c12

Please sign in to comment.