133 lines
4.4 KiB
TypeScript
133 lines
4.4 KiB
TypeScript
import { z } from 'zod';
|
|
import { researchActorSchema } from '@stock-bot/stock-config/research';
|
|
import {
|
|
walkForwardHashSchema as hash,
|
|
invalidWalkForwardResponse as invalid,
|
|
parseWalkForwardDetail,
|
|
parseWalkForwardPreview,
|
|
sameWalkForwardInput as same,
|
|
walkForwardSummarySchema as summary,
|
|
walkForwardIdSchema as uuid,
|
|
WalkForwardApiError,
|
|
} from '@stock-bot/stock-config/research-responses';
|
|
import {
|
|
type WalkForwardFinalizationInput,
|
|
type WalkForwardInput,
|
|
type WalkForwardSubmission,
|
|
type WalkForwardSummary,
|
|
} from '@stock-bot/stock-config/walk-forward';
|
|
import { API_BASE_URL } from '@/lib/api';
|
|
import { requestJson } from '@/lib/requestJson';
|
|
|
|
export {
|
|
WalkForwardApiError,
|
|
parseWalkForwardDetail,
|
|
parseWalkForwardPreview,
|
|
type WalkForwardDetailView,
|
|
type WalkForwardPreviewView,
|
|
} from '@stock-bot/stock-config/research-responses';
|
|
|
|
const envelope = (body: unknown, key: string) => z.record(z.unknown()).parse(body)[key];
|
|
function request<T>(
|
|
path: string,
|
|
init: RequestInit,
|
|
status: number,
|
|
parse: (body: unknown) => T
|
|
): Promise<T> {
|
|
return requestJson(
|
|
`${API_BASE_URL}/api/v2/experiments/walk-forward${path}`,
|
|
init,
|
|
status,
|
|
parse,
|
|
{
|
|
create: (message, code) => new WalkForwardApiError(message, code),
|
|
invalid,
|
|
lostMessage:
|
|
'The walk-forward response was lost. Retry the same request to recover its receipt.',
|
|
failureLabel: 'Walk-forward request failed',
|
|
}
|
|
);
|
|
}
|
|
|
|
const post = (body: unknown, key?: string): RequestInit => ({
|
|
method: 'POST',
|
|
body: JSON.stringify(body),
|
|
headers: { 'Content-Type': 'application/json', ...(key ? { 'Idempotency-Key': key } : {}) },
|
|
});
|
|
export const walkForwardApi = {
|
|
preview: (input: WalkForwardInput, signal?: AbortSignal) =>
|
|
request('/preview', { ...post(input), signal }, 200, body =>
|
|
parseWalkForwardPreview(envelope(body, 'preview'), input)
|
|
),
|
|
list: (backtestId?: string, signal?: AbortSignal) =>
|
|
request(
|
|
backtestId ? `?backtestId=${encodeURIComponent(backtestId)}` : '',
|
|
{ signal },
|
|
200,
|
|
body => {
|
|
const rows = z.array(summary).parse(envelope(body, 'walkForwards'));
|
|
if (
|
|
new Set(rows.map(item => item.id)).size !== rows.length ||
|
|
(backtestId && rows.some(item => item.backtestId !== backtestId))
|
|
) {
|
|
throw invalid();
|
|
}
|
|
return rows as WalkForwardSummary[];
|
|
}
|
|
),
|
|
get: (id: string, signal?: AbortSignal) =>
|
|
request(`/${encodeURIComponent(id)}`, { signal }, 200, body => {
|
|
const value = parseWalkForwardDetail(envelope(body, 'walkForward'));
|
|
if (value.id !== id) {
|
|
throw invalid();
|
|
}
|
|
return value;
|
|
}),
|
|
submit: (input: WalkForwardSubmission, key: string) =>
|
|
request('', post(input, key), 202, body => {
|
|
const value = parseWalkForwardDetail(envelope(body, 'walkForward'));
|
|
const { previewHash, ...original } = input;
|
|
if (value.previewHash !== previewHash || !same(value.preview.input, original)) {
|
|
throw invalid();
|
|
}
|
|
return value;
|
|
}),
|
|
finalize: (id: string, input: WalkForwardFinalizationInput, key: string) =>
|
|
request(`/${encodeURIComponent(id)}/finalize`, post(input, key), 202, body => {
|
|
const accepted = z
|
|
.object({
|
|
id: uuid,
|
|
walkForwardId: uuid,
|
|
operation: z.literal('finalize-walk-forward-report'),
|
|
previewHash: hash,
|
|
reportHash: hash,
|
|
createdAt: z.string().min(1),
|
|
principal: researchActorSchema,
|
|
})
|
|
.parse(envelope(body, 'receipt'));
|
|
const value = parseWalkForwardDetail(envelope(body, 'walkForward')),
|
|
saved = value.report;
|
|
if (
|
|
value.id !== id ||
|
|
accepted.walkForwardId !== id ||
|
|
value.previewHash !== input.previewHash ||
|
|
accepted.previewHash !== input.previewHash ||
|
|
!saved ||
|
|
accepted.id !== saved.id ||
|
|
accepted.reportHash !== saved.reportHash ||
|
|
accepted.createdAt !== saved.createdAt ||
|
|
accepted.principal !== saved.principal
|
|
) {
|
|
throw invalid();
|
|
}
|
|
return { receipt: accepted, walkForward: value };
|
|
}),
|
|
cancel: (id: string) =>
|
|
request(`/${encodeURIComponent(id)}/cancel`, post({}), 200, body => {
|
|
const value = parseWalkForwardDetail(envelope(body, 'walkForward'));
|
|
if (value.id !== id || !value.cancelRequested) {
|
|
throw invalid();
|
|
}
|
|
return value;
|
|
}),
|
|
};
|