-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from stone-lyl/main
feat: Add Zod schema for request method validation in `WorkspaceApiClient`
- Loading branch information
Showing
34 changed files
with
398 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,174 @@ | ||
import { RequestObserverType } from './InputObserveConfig'; | ||
import { NotifyObserversCallback } from './NotifyObserversCallback'; | ||
import { LinkCount, LinkId } from './Link'; | ||
import { NodeStatus } from '../Executor'; | ||
import { NotifyObserversCallback, NotifyObserversCallbackSchema } from './NotifyObserversCallback'; | ||
import { LinkId } from './Link'; | ||
import { NodeId } from './Node'; | ||
import { z } from 'zod'; | ||
|
||
export type ObserveLinkItems = { | ||
type: RequestObserverType.observeLinkItems, | ||
linkIds: LinkId[], | ||
onReceive: NotifyObserversCallback, | ||
observerId: string, | ||
direction?: 'pull' | 'push', | ||
onlyFirstNItems?: number, | ||
throttleMs?: number, | ||
msgId?: string; | ||
} | ||
|
||
export interface LinkCountInfo { | ||
count: LinkCount; | ||
linkId: LinkId; | ||
} | ||
|
||
export type ObserveLinkCounts = { | ||
type: RequestObserverType.observeLinkCounts, | ||
linkIds: LinkId[], | ||
observerId: string, | ||
throttleMs?: number, | ||
msgId?: string, | ||
onReceive: (params: { | ||
links: LinkCountInfo[], | ||
}) => void, | ||
} | ||
|
||
export type ObserveNodeStatus = { | ||
type: RequestObserverType.observeNodeStatus, | ||
nodeIds: NodeId[], | ||
observerId: string, | ||
throttleMs?: number, | ||
msgId?: string, | ||
onReceive: (data: { | ||
nodes: {nodeId: NodeId, status: Omit<NodeStatus, 'AVAILABLE'>}[], | ||
}) => void, | ||
} | ||
|
||
export type ObserveLinkUpdate = { | ||
type: RequestObserverType.observeLinkUpdate, | ||
linkIds: LinkId[], | ||
observerId: string, | ||
onReceive: (linkIds: LinkId[]) => void, | ||
throttleMs?: number, | ||
msgId?: string, | ||
limit?: number, | ||
offset?: number, | ||
} | ||
|
||
export type CancelObservation = { | ||
type: RequestObserverType.cancelObservation, | ||
observerId: string, | ||
msgId?: string | ||
} | ||
|
||
export type ExecutionObserver = ObserveLinkItems | ObserveLinkCounts | CancelObservation | ObserveLinkUpdate | ObserveNodeStatus; | ||
export const LinkCountInfoSchema = z.object({ | ||
count: z.number({ | ||
required_error: 'count is required', | ||
invalid_type_error: 'count must be a number' | ||
}), | ||
linkId: z.string({ | ||
required_error: 'linkId is required', | ||
invalid_type_error: 'linkId must be a string' | ||
}) as z.ZodType<LinkId>, | ||
}) | ||
|
||
export type LinkCountInfo = z.input<typeof LinkCountInfoSchema>; | ||
|
||
export const NodesStatusInfoSchema = z.object({ | ||
nodeId: z.string({ | ||
required_error: 'nodeId is required', | ||
invalid_type_error: 'nodeId must be a string' | ||
}), | ||
status: z.enum(['BUSY', 'COMPLETE'], { | ||
required_error: 'status is required', | ||
invalid_type_error: 'status must be either BUSY or COMPLETE' | ||
}), | ||
}) | ||
|
||
export type NodesStatusInfo = z.input<typeof NodesStatusInfoSchema>; | ||
|
||
export const ObserveLinkItemsSchema = z.object({ | ||
linkIds: z.array(z.string({ | ||
required_error: 'linkIds is required', | ||
invalid_type_error: 'linkIds must be an array' | ||
}) as z.ZodType<LinkId>), | ||
type: z.literal(RequestObserverType.observeLinkItems, { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be observeLinkItems' | ||
}), | ||
observerId: z.string({ | ||
required_error: 'observerId is required', | ||
invalid_type_error: 'observerId must be a string' | ||
}), | ||
direction: z.enum(['pull', 'push'], { | ||
invalid_type_error: 'direction must be either pull or push' | ||
}).optional(), | ||
throttleMs: z.number({ | ||
invalid_type_error: 'throttleMs must be a number' | ||
}).optional(), | ||
msgId: z.string({ | ||
invalid_type_error: 'msgId must be a string' | ||
}).optional(), | ||
onReceive: NotifyObserversCallbackSchema as z.ZodType<NotifyObserversCallback>, | ||
}) | ||
|
||
export type ObserveLinkItems = z.input<typeof ObserveLinkItemsSchema>; | ||
|
||
export const ObserveLinkCountsSchema = z.object({ | ||
type: z.literal(RequestObserverType.observeLinkCounts, { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be observeLinkCounts' | ||
}), | ||
linkIds: z.array(z.string({ | ||
required_error: 'linkIds is required', | ||
invalid_type_error: 'linkIds must be a string' | ||
}) as z.ZodType<LinkId>), | ||
observerId: z.string({ | ||
required_error: 'observerId is required', | ||
invalid_type_error: 'observerId must be a string' | ||
}), | ||
throttleMs: z.number({ | ||
invalid_type_error: 'throttleMs must be a number' | ||
}).optional(), | ||
msgId: z.string({ | ||
invalid_type_error: 'msgId must be a string' | ||
}).optional(), | ||
onReceive: z.function() | ||
.args(z.object({ | ||
links: z.array(LinkCountInfoSchema) | ||
})) | ||
.returns(z.void()), | ||
}); | ||
|
||
export type ObserveLinkCounts = z.input<typeof ObserveLinkCountsSchema>; | ||
|
||
export const ObserveNodeStatusSchema = z.object({ | ||
type: z.literal(RequestObserverType.observeNodeStatus, { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be observeNodeStatus' | ||
}), | ||
nodeIds: z.array(z.string({ | ||
required_error: 'nodeIds is required', | ||
invalid_type_error: 'nodeIds must be a string' | ||
}).transform(nodeId => nodeId as NodeId)), | ||
observerId: z.string({ | ||
required_error: 'observerId is required', | ||
invalid_type_error: 'observerId must be a string' | ||
}), | ||
throttleMs: z.number({ | ||
invalid_type_error: 'throttleMs must be a number' | ||
}).optional(), | ||
msgId: z.string({ | ||
invalid_type_error: 'msgId must be a string' | ||
}).optional(), | ||
onReceive: z.function() | ||
.args(z.object({ | ||
nodes: z.array(NodesStatusInfoSchema) | ||
})) | ||
.returns(z.void()), | ||
}); | ||
|
||
export type ObserveNodeStatus = z.input<typeof ObserveNodeStatusSchema>; | ||
|
||
export const ObserveLinkUpdateSchema = z.object({ | ||
type: z.literal(RequestObserverType.observeLinkUpdate, { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be observeLinkUpdate' | ||
}), | ||
linkIds: z.array(z.string({ | ||
required_error: 'linkIds is required', | ||
invalid_type_error: 'linkIds must be a string' | ||
}) as z.ZodType<LinkId>), | ||
observerId: z.string({ | ||
required_error: 'observerId is required', | ||
invalid_type_error: 'observerId must be a string' | ||
}), | ||
throttleMs: z.number({ | ||
invalid_type_error: 'throttleMs must be a number' | ||
}).optional(), | ||
msgId: z.string({ | ||
invalid_type_error: 'msgId must be a string' | ||
}).optional(), | ||
limit: z.number({ | ||
invalid_type_error: 'limit must be a number' | ||
}).optional(), | ||
offset: z.number({ | ||
invalid_type_error: 'offset must be a number' | ||
}).optional(), | ||
onReceive: z.function() | ||
.args(z.array( | ||
z.string().transform(linkId => linkId as LinkId).optional() | ||
)) | ||
.returns(z.void()) | ||
}); | ||
|
||
export type ObserveLinkUpdate = z.input<typeof ObserveLinkUpdateSchema>; | ||
|
||
export const CancelObservationSchema = z.object({ | ||
type: z.literal(RequestObserverType.cancelObservation, { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be cancelObservation' | ||
}), | ||
observerId: z.string({ | ||
required_error: 'observerId is required', | ||
invalid_type_error: 'observerId must be a string' | ||
}), | ||
msgId: z.string({ | ||
invalid_type_error: 'msgId must be a string' | ||
}).optional(), | ||
}); | ||
|
||
export type CancelObservation = z.input<typeof CancelObservationSchema>; | ||
|
||
export const ExecutionObserverSchema = z.discriminatedUnion('type', [ | ||
ObserveLinkItemsSchema, | ||
ObserveLinkCountsSchema, | ||
ObserveLinkUpdateSchema, | ||
ObserveNodeStatusSchema, | ||
CancelObservationSchema | ||
]); | ||
|
||
export type ExecutionObserver = z.input<typeof ExecutionObserverSchema>; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { ItemValue } from './ItemValue'; | ||
import { LinkId } from './Link'; | ||
import { z } from 'zod'; | ||
|
||
export type LinkItems = Record<LinkId, ItemValue[]> | ||
|
||
export const LinkItemsSchema = z.record(z.string() as z.ZodType<LinkId>, z.array(z.object({}) as z.ZodType<ItemValue>)); | ||
|
||
/** | ||
* @schema GetDataFromStorageParams | ||
*/ | ||
export const GetDataFromStorageParamsSchema = z.object({ | ||
type: z.literal('getDataFromStorage', { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be getDataFromStorage' | ||
}), | ||
linkId: z.string({ | ||
required_error: 'linkId is required', | ||
invalid_type_error: 'linkId must be a string' | ||
}) as z.ZodType<LinkId>, | ||
msgId: z.string({ | ||
invalid_type_error: 'msgId must be a string' | ||
}).optional(), | ||
offset: z.number({ | ||
invalid_type_error: 'offset must be a number' | ||
}).optional(), | ||
limit: z.number({ | ||
invalid_type_error: 'limit must be a number' | ||
}).optional(), | ||
}); | ||
|
||
export type GetDataFromStorageParams = z.input<typeof GetDataFromStorageParamsSchema>; | ||
|
||
export const GetDataFromStorageResponseSchema = GetDataFromStorageParamsSchema.extend({ | ||
data: LinkItemsSchema, | ||
}); | ||
|
||
export type GetDataFromStorageResponse = z.input<typeof GetDataFromStorageResponseSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
import { z } from 'zod'; | ||
import { RequestObserverType } from './InputObserveConfig'; | ||
import { ItemValue } from './ItemValue'; | ||
import { LinkId } from './Link'; | ||
|
||
export type LinkItemsParam = { | ||
type: RequestObserverType.observeLinkItems; | ||
linkId: LinkId; | ||
items: ItemValue[]; | ||
} | ||
export const LinkItemsParamSchema = z.object({ | ||
type: z.literal(RequestObserverType.observeLinkItems, { | ||
required_error: 'type is required', | ||
invalid_type_error: 'type must be observeLinkItems' | ||
}), | ||
linkId: z.string({ | ||
required_error: 'linkId is required', | ||
invalid_type_error: 'linkId must be a string' | ||
}) as z.ZodType<string>, | ||
items: z.array(z.object({}) as z.ZodType<ItemValue>), | ||
}); | ||
|
||
export type LinkItemsParam = z.input<typeof LinkItemsParamSchema>; |
Oops, something went wrong.