upd core
- added zod integration (validation) for testing
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { rpc } from './rpc'
|
||||
import { Middleware } from './middleware'
|
||||
import { Validation } from './validation'
|
||||
import type * as T from '../types'
|
||||
|
||||
/** Client-side interactions */
|
||||
@@ -10,6 +11,7 @@ export class Client {
|
||||
* @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
|
||||
* @param [options.middlewares] - Middleware functions to be checked before the callback executes
|
||||
* @returns {Client} The current client instance, enabling method chaining
|
||||
*
|
||||
@@ -44,6 +46,7 @@ export class Client {
|
||||
eventName: EventName,
|
||||
callback: T.RageFW_ClientCallback<EventName>,
|
||||
options?: {
|
||||
validation?: T.RageFW_ValidationOptions
|
||||
middlewares?: T.RageFW_MiddlewareOptions<EventName>
|
||||
},
|
||||
): Client {
|
||||
@@ -52,9 +55,22 @@ export class Client {
|
||||
ReturnType<typeof callback> | Promise<unknown>,
|
||||
EventName
|
||||
>(eventName, async (...data) => {
|
||||
if (!options?.middlewares) return await callback(...data)
|
||||
if (!options?.middlewares && !options?.validation)
|
||||
return await callback(...data)
|
||||
|
||||
await Middleware.process(options.middlewares, callback, data)
|
||||
const validationSuccess = Validation.process(
|
||||
data,
|
||||
options?.validation,
|
||||
)
|
||||
if (!validationSuccess) return
|
||||
|
||||
const middlewaresSuccess = await Middleware.process(
|
||||
data,
|
||||
options?.middlewares,
|
||||
)
|
||||
if (!middlewaresSuccess) return
|
||||
|
||||
return await callback(...data)
|
||||
})
|
||||
|
||||
return this
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from './client'
|
||||
export * from './logger'
|
||||
export * from './middleware'
|
||||
export * from './player'
|
||||
export * from './rpc'
|
||||
|
||||
@@ -4,8 +4,8 @@ export class Middleware {
|
||||
constructor() {}
|
||||
|
||||
private static async execute<EventName extends T.RageFW_ClientEvent>(
|
||||
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
|
||||
args: T.RageFW_ClientArgs<EventName>,
|
||||
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
|
||||
): Promise<T.RageFW_MiddlewareResponseInternal> {
|
||||
for (let i = 0; i < middlewares.length; i++) {
|
||||
const result = await middlewares[i](...args)
|
||||
@@ -22,25 +22,25 @@ export class Middleware {
|
||||
}
|
||||
|
||||
public static async process<EventName extends T.RageFW_ClientEvent>(
|
||||
middlewareOptions: T.RageFW_MiddlewareOptions<EventName>,
|
||||
callback: T.RageFW_ClientCallback<EventName>,
|
||||
args: T.RageFW_ClientArgs<EventName>,
|
||||
) {
|
||||
middlewareOptions?: T.RageFW_MiddlewareOptions<EventName>,
|
||||
): Promise<boolean> {
|
||||
if (!middlewareOptions) return true
|
||||
|
||||
if (Array.isArray(middlewareOptions)) {
|
||||
const middlewaresResponse = await Middleware.execute(
|
||||
middlewareOptions,
|
||||
args,
|
||||
middlewareOptions,
|
||||
)
|
||||
|
||||
if (middlewaresResponse.success) return await callback(...args)
|
||||
return middlewaresResponse.success
|
||||
} else {
|
||||
const middlewaresResponse = await Middleware.execute(
|
||||
middlewareOptions.executables,
|
||||
args,
|
||||
middlewareOptions.executables,
|
||||
)
|
||||
|
||||
if (middlewaresResponse.success) {
|
||||
return await callback(...args)
|
||||
return true
|
||||
} else {
|
||||
middlewareOptions.onError(
|
||||
middlewaresResponse.message ??
|
||||
@@ -48,6 +48,7 @@ export class Middleware {
|
||||
middlewaresResponse.id +
|
||||
' failed',
|
||||
)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import type * as T from '../types'
|
||||
|
||||
export class Validation {
|
||||
constructor() {}
|
||||
|
||||
public static process<EventName extends T.RageFW_ClientEvent>(
|
||||
args: T.RageFW_ClientArgs<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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,3 +2,4 @@ export * from './browser'
|
||||
export * from './client'
|
||||
export * from './middleware'
|
||||
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