File size: 4,002 Bytes
4e23b01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import { z } from 'zod';
import { okEnvelope } from '../envelope';
import { defineRoute } from '../middleware/defineRoute';
import { IGuiStoreService } from '../services/guiStore/guiStore';
import { ErrorCode } from '../protocol/error-codes';
import {
guiStoreGetItemQuerySchema,
guiStoreGetItemResponseSchema,
guiStoreLengthResponseSchema,
guiStoreRemoveItemBodySchema,
guiStoreSetItemBodySchema,
} from '../protocol/rest-guiStore';
interface GuiStoreRouteHost {
get(
path: string,
options: { schema?: Record<string, unknown> },
handler: (
req: { id: string; query?: unknown },
reply: { send(payload: unknown): void },
) => Promise<void> | void,
): unknown;
post(
path: string,
options: { schema?: Record<string, unknown> },
handler: (
req: { id: string; body?: unknown },
reply: { send(payload: unknown): void },
) => Promise<void> | void,
): unknown;
}
export function registerGuiStoreRoutes(app: GuiStoreRouteHost, store: IGuiStoreService): void {
const getItemRoute = defineRoute(
{
method: 'GET',
path: '/gui/store/getItem',
querystring: guiStoreGetItemQuerySchema,
success: { data: guiStoreGetItemResponseSchema },
errors: { [ErrorCode.VALIDATION_FAILED]: {} },
description: 'Read a value by key (mirrors localStorage.getItem).',
tags: ['gui-store'],
},
async (req, reply) => {
const value = await store.getItem((req.query as { key: string }).key);
reply.send(okEnvelope({ value }, req.id));
},
);
app.get(
getItemRoute.path,
getItemRoute.options,
getItemRoute.handler as Parameters<GuiStoreRouteHost['get']>[2],
);
const setItemRoute = defineRoute(
{
method: 'POST',
path: '/gui/store/setItem',
body: guiStoreSetItemBodySchema,
success: { data: z.null() },
errors: { [ErrorCode.VALIDATION_FAILED]: {} },
description: 'Write a value by key (mirrors localStorage.setItem).',
tags: ['gui-store'],
},
async (req, reply) => {
const body = req.body as { key: string; value: string };
await store.setItem(body.key, body.value);
reply.send(okEnvelope(null, req.id));
},
);
app.post(
setItemRoute.path,
setItemRoute.options,
setItemRoute.handler as Parameters<GuiStoreRouteHost['post']>[2],
);
const removeItemRoute = defineRoute(
{
method: 'POST',
path: '/gui/store/removeItem',
body: guiStoreRemoveItemBodySchema,
success: { data: z.null() },
errors: { [ErrorCode.VALIDATION_FAILED]: {} },
description: 'Delete a value by key (mirrors localStorage.removeItem).',
tags: ['gui-store'],
},
async (req, reply) => {
await store.removeItem((req.body as { key: string }).key);
reply.send(okEnvelope(null, req.id));
},
);
app.post(
removeItemRoute.path,
removeItemRoute.options,
removeItemRoute.handler as Parameters<GuiStoreRouteHost['post']>[2],
);
const clearRoute = defineRoute(
{
method: 'POST',
path: '/gui/store/clear',
success: { data: z.null() },
description: 'Delete all values (mirrors localStorage.clear).',
tags: ['gui-store'],
},
async (_req, reply) => {
await store.clear();
reply.send(okEnvelope(null, _req.id));
},
);
app.post(
clearRoute.path,
clearRoute.options,
clearRoute.handler as Parameters<GuiStoreRouteHost['post']>[2],
);
const lengthRoute = defineRoute(
{
method: 'GET',
path: '/gui/store/length',
success: { data: guiStoreLengthResponseSchema },
description: 'Number of stored keys (mirrors localStorage.length).',
tags: ['gui-store'],
},
async (req, reply) => {
const length = await store.length();
reply.send(okEnvelope({ length }, req.id));
},
);
app.get(
lengthRoute.path,
lengthRoute.options,
lengthRoute.handler as Parameters<GuiStoreRouteHost['get']>[2],
);
}
|