From 8085c12f0623851380c3b5ea42a67a1079d15c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Thu, 7 Sep 2023 16:36:47 +0200 Subject: [PATCH] Include detailsResponse in schema data --- src/routes/pull-schema.ts | 7 ++++++- src/routes/schemas.ts | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/routes/pull-schema.ts b/src/routes/pull-schema.ts index 42a8712..a8ee04d 100644 --- a/src/routes/pull-schema.ts +++ b/src/routes/pull-schema.ts @@ -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") diff --git a/src/routes/schemas.ts b/src/routes/schemas.ts index 14142a6..46544a6 100644 --- a/src/routes/schemas.ts +++ b/src/routes/schemas.ts @@ -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, @@ -8,6 +15,33 @@ export async function schemas( ): Promise { 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( + 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])) {