upd core
- added zod integration (validation) for testing
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
import { Helper } from './helper'
|
||||
import { rpc } from './rpc'
|
||||
import { Helper } from './helper'
|
||||
import { Validation } from './validation'
|
||||
import type * as T from '../types'
|
||||
import {
|
||||
RageFW_BrowserEvent,
|
||||
RageFW_ClientEvent,
|
||||
RageFW_ServerEvent,
|
||||
} from '../types'
|
||||
|
||||
/** Browser-side interactions */
|
||||
export class Browser extends Helper {
|
||||
@@ -34,6 +30,8 @@ export class Browser extends Helper {
|
||||
*
|
||||
* @param eventName - The name of the event to register
|
||||
* @param callback - The callback function to be executed when the event is triggered
|
||||
* @param [options] - Optional settings for callback execution
|
||||
* @param [options.validation] - Validation schema to be checked before the callback executes
|
||||
* @returns {Browser} The current browser instance, enabling method chaining
|
||||
*
|
||||
* @example
|
||||
@@ -47,14 +45,27 @@ export class Browser extends Helper {
|
||||
public register<EventName extends T.RageFW_BrowserEvent>(
|
||||
eventName: EventName,
|
||||
callback: T.RageFW_BrowserCallback<EventName>,
|
||||
options?: {
|
||||
validation?: T.RageFW_ValidationOptions
|
||||
},
|
||||
): Browser {
|
||||
this.log_('register', eventName, callback)
|
||||
|
||||
rpc.register<
|
||||
Parameters<typeof callback>,
|
||||
ReturnType<typeof callback>,
|
||||
ReturnType<typeof callback> | Promise<unknown>,
|
||||
EventName
|
||||
>(eventName, async (...data) => await callback(...data))
|
||||
>(eventName, async (...data) => {
|
||||
if (!options?.validation) return await callback(...data)
|
||||
|
||||
const validationSuccess = Validation.process(
|
||||
data,
|
||||
options?.validation,
|
||||
)
|
||||
if (!validationSuccess) return
|
||||
|
||||
return await callback(...data)
|
||||
})
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import type * as T from '../types'
|
||||
|
||||
export class Validation {
|
||||
constructor() {}
|
||||
|
||||
public static process<EventName extends T.RageFW_BrowserEvent>(
|
||||
args: T.RageFW_BrowserArgs<EventName>,
|
||||
validationOptions?: T.RageFW_ValidationOptions,
|
||||
): boolean {
|
||||
if (!validationOptions) return true
|
||||
|
||||
if ('schema' in validationOptions) {
|
||||
const validationResponse = validationOptions.schema.safeParse(args)
|
||||
|
||||
if (validationResponse.success) {
|
||||
return true
|
||||
} else {
|
||||
validationOptions.onError(validationResponse.error)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
const validationResponse = validationOptions.safeParse(args)
|
||||
return validationResponse.success
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './browser'
|
||||
export * from './client'
|
||||
export * from './server'
|
||||
export * from './validation'
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { z } from 'zod'
|
||||
|
||||
export type RageFW_ValidationSchema = z.ZodTuple<any, any> | z.ZodArray<any>
|
||||
export type RageFW_ValidationSchemaExtended = {
|
||||
schema: RageFW_ValidationSchema
|
||||
onError: (error: z.ZodError) => unknown
|
||||
}
|
||||
|
||||
export type RageFW_ValidationOptions =
|
||||
| RageFW_ValidationSchema
|
||||
| RageFW_ValidationSchemaExtended
|
||||
@@ -4,7 +4,6 @@ export default defineConfig({
|
||||
entry: ['src/index.ts'],
|
||||
outDir: './dist',
|
||||
format: ['cjs'],
|
||||
noExternal: ['rage-rpc'],
|
||||
experimentalDts: true,
|
||||
splitting: false,
|
||||
sourcemap: false,
|
||||
|
||||
Reference in New Issue
Block a user