Switching from "name" to "id" in texture object + using zod for woka/list validation

This commit is contained in:
David Négrier
2022-02-25 17:05:34 +01:00
parent da469b64d2
commit 08fffab410
11 changed files with 57 additions and 69 deletions
+2 -1
View File
@@ -53,7 +53,8 @@
"prom-client": "^12.0.0",
"qs": "^6.10.3",
"query-string": "^6.13.3",
"uuidv4": "^6.0.7"
"uuidv4": "^6.0.7",
"zod": "^3.12.0"
},
"devDependencies": {
"@types/circular-json": "^0.4.0",
+21 -32
View File
@@ -1,46 +1,35 @@
import * as tg from "generic-type-guard";
import { z } from "zod";
//The list of all the player textures, both the default models and the partial textures used for customization
export const isWokaTexture = new tg.IsInterface()
.withProperties({
id: tg.isString,
name: tg.isString,
url: tg.isString,
position: tg.isNumber,
})
.withOptionalProperties({
tags: tg.isArray(tg.isString),
tintable: tg.isBoolean,
})
.get();
const wokaTexture = z.object({
id: z.string(),
name: z.string(),
url: z.string(),
tags: z.array(z.string()).optional(),
tintable: z.boolean().optional(),
});
export type WokaTexture = tg.GuardedType<typeof isWokaTexture>;
export type WokaTexture = z.infer<typeof wokaTexture>;
export const isWokaTextureCollection = new tg.IsInterface()
.withProperties({
name: tg.isString,
position: tg.isNumber,
textures: tg.isArray(isWokaTexture),
})
.get();
const wokaTextureCollection = z.object({
name: z.string(),
textures: z.array(wokaTexture),
});
export type WokaTextureCollection = tg.GuardedType<typeof isWokaTextureCollection>;
export type WokaTextureCollection = z.infer<typeof wokaTextureCollection>;
export const isWokaPartType = new tg.IsInterface()
.withProperties({
collections: tg.isArray(isWokaTextureCollection),
})
.withOptionalProperties({
required: tg.isBoolean,
})
.get();
const wokaPartType = z.object({
collections: z.array(wokaTextureCollection),
required: z.boolean().optional(),
});
export type WokaPartType = tg.GuardedType<typeof isWokaPartType>;
export type WokaPartType = z.infer<typeof wokaPartType>;
export const isWokaList = new tg.IsInterface().withStringIndexSignature(isWokaPartType).get();
export const wokaList = z.record(wokaPartType);
export type WokaList = tg.GuardedType<typeof isWokaList>;
export type WokaList = z.infer<typeof wokaList>;
export const wokaPartNames = ["woka", "body", "eyes", "hair", "clothes", "hat", "accessory"];
+4 -7
View File
@@ -1,6 +1,6 @@
import axios from "axios";
import axios, { AxiosResponse } from "axios";
import { ADMIN_API_TOKEN, ADMIN_API_URL } from "../Enum/EnvironmentVariable";
import { isWokaList, WokaList } from "../Enum/PlayerTextures";
import { wokaList, WokaList } from "../Enum/PlayerTextures";
import { WokaServiceInterface } from "./WokaServiceInterface";
class AdminWokaService implements WokaServiceInterface {
@@ -9,7 +9,7 @@ class AdminWokaService implements WokaServiceInterface {
*/
getWokaList(roomUrl: string, token: string): Promise<WokaList | undefined> {
return axios
.get(`${ADMIN_API_URL}/api/woka/list`, {
.get<unknown, AxiosResponse<unknown>>(`${ADMIN_API_URL}/api/woka/list`, {
headers: { Authorization: `${ADMIN_API_TOKEN}` },
params: {
roomUrl,
@@ -17,10 +17,7 @@ class AdminWokaService implements WokaServiceInterface {
},
})
.then((res) => {
if (isWokaList(res.data)) {
throw new Error("Bad response format provided by woka list endpoint");
}
return res.data;
return wokaList.parse(res.data);
})
.catch((err) => {
console.error(`Cannot get woka list from admin API with token: ${token}`, err);
+5
View File
@@ -2834,3 +2834,8 @@ z-schema@^4.2.3:
validator "^13.6.0"
optionalDependencies:
commander "^2.7.1"
zod@^3.12.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.12.0.tgz#84ba9f6bdb7835e2483982d5f52cfffcb6a00346"
integrity sha512-w+mmntgEL4hDDL5NLFdN6Fq2DSzxfmlSoJqiYE1/CApO8EkOCxvJvRYEVf8Vr/lRs3i6gqoiyFM6KRcWqqdBzQ==