Compare commits
No commits in common. "fd9f1cab1859feef68f47fab71ad8da17f1b9fa0" and "495217ecd5bd992890a4bcae23910807faf263e1" have entirely different histories.
fd9f1cab18
...
495217ecd5
Binary file not shown.
@ -8,15 +8,11 @@
|
|||||||
"readme.md",
|
"readme.md",
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
],
|
],
|
||||||
"exports": {
|
|
||||||
".": "./dist/index.js"
|
|
||||||
},
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsup"
|
"build": "tsup"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@entityseven/rage-fw-rpc": "0.2.5",
|
"@entityseven/rage-fw-rpc": "0.2.5"
|
||||||
"zod": "^3.23.8"
|
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@entityseven/rage-fw-shared-types": "0.2.0",
|
"@entityseven/rage-fw-shared-types": "0.2.0",
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
import { rpc } from './rpc'
|
|
||||||
import { Helper } from './helper'
|
import { Helper } from './helper'
|
||||||
import { Validation } from './validation'
|
import { rpc } from './rpc'
|
||||||
import type * as T from '../types'
|
import type * as T from '../types'
|
||||||
|
import {
|
||||||
|
RageFW_BrowserEvent,
|
||||||
|
RageFW_ClientEvent,
|
||||||
|
RageFW_ServerEvent,
|
||||||
|
} from '../types'
|
||||||
|
|
||||||
/** Browser-side interactions */
|
/** Browser-side interactions */
|
||||||
export class Browser extends Helper {
|
export class Browser extends Helper {
|
||||||
@ -30,8 +34,6 @@ export class Browser extends Helper {
|
|||||||
*
|
*
|
||||||
* @param eventName - The name of the event to register
|
* @param eventName - The name of the event to register
|
||||||
* @param callback - The callback function to be executed when the event is triggered
|
* @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
|
* @returns {Browser} The current browser instance, enabling method chaining
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
@ -45,27 +47,14 @@ export class Browser extends Helper {
|
|||||||
public register<EventName extends T.RageFW_BrowserEvent>(
|
public register<EventName extends T.RageFW_BrowserEvent>(
|
||||||
eventName: EventName,
|
eventName: EventName,
|
||||||
callback: T.RageFW_BrowserCallback<EventName>,
|
callback: T.RageFW_BrowserCallback<EventName>,
|
||||||
options?: {
|
|
||||||
validation?: T.RageFW_ValidationOptions
|
|
||||||
},
|
|
||||||
): Browser {
|
): Browser {
|
||||||
this.log_('register', eventName, callback)
|
this.log_('register', eventName, callback)
|
||||||
|
|
||||||
rpc.register<
|
rpc.register<
|
||||||
Parameters<typeof callback>,
|
Parameters<typeof callback>,
|
||||||
ReturnType<typeof callback> | Promise<unknown>,
|
ReturnType<typeof callback>,
|
||||||
EventName
|
EventName
|
||||||
>(eventName, async (...data) => {
|
>(eventName, async (...data) => await callback(...data))
|
||||||
if (!options?.validation) return await callback(...data)
|
|
||||||
|
|
||||||
const validationSuccess = Validation.process(
|
|
||||||
data,
|
|
||||||
options?.validation,
|
|
||||||
)
|
|
||||||
if (!validationSuccess) return
|
|
||||||
|
|
||||||
return await callback(...data)
|
|
||||||
})
|
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
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,4 +1,3 @@
|
|||||||
export * from './browser'
|
export * from './browser'
|
||||||
export * from './client'
|
export * from './client'
|
||||||
export * from './server'
|
export * from './server'
|
||||||
export * from './validation'
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
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,6 +4,7 @@ export default defineConfig({
|
|||||||
entry: ['src/index.ts'],
|
entry: ['src/index.ts'],
|
||||||
outDir: './dist',
|
outDir: './dist',
|
||||||
format: ['cjs'],
|
format: ['cjs'],
|
||||||
|
noExternal: ['rage-rpc'],
|
||||||
experimentalDts: true,
|
experimentalDts: true,
|
||||||
splitting: false,
|
splitting: false,
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
|
BIN
client/bun.lockb
BIN
client/bun.lockb
Binary file not shown.
@ -1,50 +1,38 @@
|
|||||||
{
|
{
|
||||||
"name": "@entityseven/rage-fw-client",
|
"name": "@entityseven/rage-fw-client",
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/src/index.d.ts",
|
"types": "dist/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"dist/**/*",
|
"dist/**/*",
|
||||||
"readme.md",
|
"readme.md",
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
],
|
],
|
||||||
"exports": {
|
"scripts": {
|
||||||
".": "./dist/index.js"
|
"build": "tsup"
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsup"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@entityseven/rage-fw-rpc": "0.2.5",
|
|
||||||
"zod": "^3.23.8"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"@entityseven/rage-fw-shared-types": "0.2.0",
|
|
||||||
"@ragempcommunity/types-client": "^2.1.8"
|
|
||||||
},
|
|
||||||
"description": "Package used on a client-side of your Rage:MP Server",
|
|
||||||
"keywords": [
|
|
||||||
"rage-fw-client",
|
|
||||||
"ragefw",
|
|
||||||
"rage-fw",
|
|
||||||
"ragemp",
|
|
||||||
"rage:mp",
|
|
||||||
"rage",
|
|
||||||
"gta5"
|
|
||||||
],
|
|
||||||
"author": "Entity Seven Group",
|
|
||||||
"contributors": [
|
|
||||||
{
|
|
||||||
"name": "Danya H",
|
|
||||||
"email": "dev.rilaxik@gmail.com",
|
|
||||||
"url": "https://github.com/rilaxik/"
|
|
||||||
},
|
},
|
||||||
{
|
"dependencies": {
|
||||||
"name": "Oleksandr Honcharov",
|
"@entityseven/rage-fw-rpc": "0.2.5"
|
||||||
"email": "0976053529@ukr.net",
|
},
|
||||||
"url": "https://github.com/SashaGoncharov19/"
|
"peerDependencies": {
|
||||||
}
|
"@entityseven/rage-fw-shared-types": "0.2.0",
|
||||||
],
|
"@ragempcommunity/types-client": "^2.1.8"
|
||||||
"license": "Custom-Attribution-NoDerivs",
|
},
|
||||||
"gitHead": "ffd542c1deddb3033e16e0dae7557313ae09b05f"
|
"description": "Package used on a client-side of your Rage:MP Server",
|
||||||
|
"keywords": ["rage-fw-client", "ragefw", "rage-fw", "ragemp", "rage:mp", "rage", "gta5"],
|
||||||
|
"author": "Entity Seven Group",
|
||||||
|
"contributors": [
|
||||||
|
{
|
||||||
|
"name": "Danya H",
|
||||||
|
"email": "dev.rilaxik@gmail.com",
|
||||||
|
"url": "https://github.com/rilaxik/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Oleksandr Honcharov",
|
||||||
|
"email": "0976053529@ukr.net",
|
||||||
|
"url": "https://github.com/SashaGoncharov19/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"license": "Custom-Attribution-NoDerivs",
|
||||||
|
"gitHead": "ffd542c1deddb3033e16e0dae7557313ae09b05f"
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { rpc } from './rpc'
|
import { rpc } from './rpc'
|
||||||
import { Middleware } from './middleware'
|
import { Middleware } from './middleware'
|
||||||
import { Validation } from './validation'
|
|
||||||
import type * as T from '../types'
|
import type * as T from '../types'
|
||||||
|
|
||||||
/** Client-side interactions */
|
/** Client-side interactions */
|
||||||
@ -11,7 +10,6 @@ export class Client {
|
|||||||
* @param eventName - The name of the event to register
|
* @param eventName - The name of the event to register
|
||||||
* @param callback - The callback function to be executed when the event is triggered
|
* @param callback - The callback function to be executed when the event is triggered
|
||||||
* @param [options] - Optional settings for callback execution
|
* @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
|
* @param [options.middlewares] - Middleware functions to be checked before the callback executes
|
||||||
* @returns {Client} The current client instance, enabling method chaining
|
* @returns {Client} The current client instance, enabling method chaining
|
||||||
*
|
*
|
||||||
@ -46,7 +44,6 @@ export class Client {
|
|||||||
eventName: EventName,
|
eventName: EventName,
|
||||||
callback: T.RageFW_ClientCallback<EventName>,
|
callback: T.RageFW_ClientCallback<EventName>,
|
||||||
options?: {
|
options?: {
|
||||||
validation?: T.RageFW_ValidationOptions
|
|
||||||
middlewares?: T.RageFW_MiddlewareOptions<EventName>
|
middlewares?: T.RageFW_MiddlewareOptions<EventName>
|
||||||
},
|
},
|
||||||
): Client {
|
): Client {
|
||||||
@ -55,22 +52,9 @@ export class Client {
|
|||||||
ReturnType<typeof callback> | Promise<unknown>,
|
ReturnType<typeof callback> | Promise<unknown>,
|
||||||
EventName
|
EventName
|
||||||
>(eventName, async (...data) => {
|
>(eventName, async (...data) => {
|
||||||
if (!options?.middlewares && !options?.validation)
|
if (!options?.middlewares) return await callback(...data)
|
||||||
return await callback(...data)
|
|
||||||
|
|
||||||
const validationSuccess = Validation.process(
|
await Middleware.process(options.middlewares, callback, data)
|
||||||
data,
|
|
||||||
options?.validation,
|
|
||||||
)
|
|
||||||
if (!validationSuccess) return
|
|
||||||
|
|
||||||
const middlewaresSuccess = await Middleware.process(
|
|
||||||
data,
|
|
||||||
options?.middlewares,
|
|
||||||
)
|
|
||||||
if (!middlewaresSuccess) return
|
|
||||||
|
|
||||||
return await callback(...data)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return this
|
return this
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export * from './client'
|
export * from './client'
|
||||||
export * from './logger'
|
export * from './logger'
|
||||||
|
export * from './middleware'
|
||||||
export * from './player'
|
export * from './player'
|
||||||
export * from './rpc'
|
export * from './rpc'
|
||||||
|
@ -4,8 +4,8 @@ export class Middleware {
|
|||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
private static async execute<EventName extends T.RageFW_ClientEvent>(
|
private static async execute<EventName extends T.RageFW_ClientEvent>(
|
||||||
args: T.RageFW_ClientArgs<EventName>,
|
|
||||||
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
|
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
|
||||||
|
args: T.RageFW_ClientArgs<EventName>,
|
||||||
): Promise<T.RageFW_MiddlewareResponseInternal> {
|
): Promise<T.RageFW_MiddlewareResponseInternal> {
|
||||||
for (let i = 0; i < middlewares.length; i++) {
|
for (let i = 0; i < middlewares.length; i++) {
|
||||||
const result = await middlewares[i](...args)
|
const result = await middlewares[i](...args)
|
||||||
@ -22,25 +22,25 @@ export class Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static async process<EventName extends T.RageFW_ClientEvent>(
|
public static async process<EventName extends T.RageFW_ClientEvent>(
|
||||||
|
middlewareOptions: T.RageFW_MiddlewareOptions<EventName>,
|
||||||
|
callback: T.RageFW_ClientCallback<EventName>,
|
||||||
args: T.RageFW_ClientArgs<EventName>,
|
args: T.RageFW_ClientArgs<EventName>,
|
||||||
middlewareOptions?: T.RageFW_MiddlewareOptions<EventName>,
|
) {
|
||||||
): Promise<boolean> {
|
|
||||||
if (!middlewareOptions) return true
|
|
||||||
|
|
||||||
if (Array.isArray(middlewareOptions)) {
|
if (Array.isArray(middlewareOptions)) {
|
||||||
const middlewaresResponse = await Middleware.execute(
|
const middlewaresResponse = await Middleware.execute(
|
||||||
args,
|
|
||||||
middlewareOptions,
|
middlewareOptions,
|
||||||
|
args,
|
||||||
)
|
)
|
||||||
return middlewaresResponse.success
|
|
||||||
|
if (middlewaresResponse.success) return await callback(...args)
|
||||||
} else {
|
} else {
|
||||||
const middlewaresResponse = await Middleware.execute(
|
const middlewaresResponse = await Middleware.execute(
|
||||||
args,
|
|
||||||
middlewareOptions.executables,
|
middlewareOptions.executables,
|
||||||
|
args,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (middlewaresResponse.success) {
|
if (middlewaresResponse.success) {
|
||||||
return true
|
return await callback(...args)
|
||||||
} else {
|
} else {
|
||||||
middlewareOptions.onError(
|
middlewareOptions.onError(
|
||||||
middlewaresResponse.message ??
|
middlewaresResponse.message ??
|
||||||
@ -48,7 +48,6 @@ export class Middleware {
|
|||||||
middlewaresResponse.id +
|
middlewaresResponse.id +
|
||||||
' failed',
|
' failed',
|
||||||
)
|
)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
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,4 +2,3 @@ export * from './browser'
|
|||||||
export * from './client'
|
export * from './client'
|
||||||
export * from './middleware'
|
export * from './middleware'
|
||||||
export * from './server'
|
export * from './server'
|
||||||
export * from './validation'
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
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,6 +4,7 @@ export default defineConfig({
|
|||||||
entry: ['src/index.ts'],
|
entry: ['src/index.ts'],
|
||||||
outDir: './dist',
|
outDir: './dist',
|
||||||
format: ['cjs'],
|
format: ['cjs'],
|
||||||
|
noExternal: ['rage-rpc'],
|
||||||
experimentalDts: true,
|
experimentalDts: true,
|
||||||
splitting: false,
|
splitting: false,
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
|
14691
pnpm-lock.yaml
generated
14691
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -1,50 +1,38 @@
|
|||||||
{
|
{
|
||||||
"name": "@entityseven/rage-fw-server",
|
"name": "@entityseven/rage-fw-server",
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/src/index.d.ts",
|
"types": "dist/src/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"dist/**/*",
|
"dist/**/*",
|
||||||
"readme.md",
|
"readme.md",
|
||||||
"LICENSE"
|
"LICENSE"
|
||||||
],
|
],
|
||||||
"exports": {
|
"scripts": {
|
||||||
".": "./dist/index.js"
|
"build": "tsup"
|
||||||
},
|
|
||||||
"scripts": {
|
|
||||||
"build": "tsup"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"@entityseven/rage-fw-rpc": "0.2.5",
|
|
||||||
"zod": "^3.23.8"
|
|
||||||
},
|
|
||||||
"peerDependencies": {
|
|
||||||
"@entityseven/rage-fw-shared-types": "0.2.0",
|
|
||||||
"@ragempcommunity/types-server": "^2.1.8"
|
|
||||||
},
|
|
||||||
"description": "Package used on a server-side of your Rage:MP Server",
|
|
||||||
"keywords": [
|
|
||||||
"rage-fw-server",
|
|
||||||
"ragefw",
|
|
||||||
"rage-fw",
|
|
||||||
"ragemp",
|
|
||||||
"rage:mp",
|
|
||||||
"rage",
|
|
||||||
"gta5"
|
|
||||||
],
|
|
||||||
"author": "Entity Seven Group",
|
|
||||||
"contributors": [
|
|
||||||
{
|
|
||||||
"name": "Danya H",
|
|
||||||
"email": "dev.rilaxik@gmail.com",
|
|
||||||
"url": "https://github.com/rilaxik/"
|
|
||||||
},
|
},
|
||||||
{
|
"dependencies": {
|
||||||
"name": "Oleksandr Honcharov",
|
"@entityseven/rage-fw-rpc": "0.2.5"
|
||||||
"email": "0976053529@ukr.net",
|
},
|
||||||
"url": "https://github.com/SashaGoncharov19/"
|
"peerDependencies": {
|
||||||
}
|
"@entityseven/rage-fw-shared-types": "0.2.0",
|
||||||
],
|
"@ragempcommunity/types-server": "^2.1.8"
|
||||||
"license": "Custom-Attribution-NoDerivs",
|
},
|
||||||
"gitHead": "ffd542c1deddb3033e16e0dae7557313ae09b05f"
|
"description": "Package used on a server-side of your Rage:MP Server",
|
||||||
|
"keywords": ["rage-fw-server", "ragefw", "rage-fw", "ragemp", "rage:mp", "rage", "gta5"],
|
||||||
|
"author": "Entity Seven Group",
|
||||||
|
"contributors": [
|
||||||
|
{
|
||||||
|
"name": "Danya H",
|
||||||
|
"email": "dev.rilaxik@gmail.com",
|
||||||
|
"url": "https://github.com/rilaxik/"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Oleksandr Honcharov",
|
||||||
|
"email": "0976053529@ukr.net",
|
||||||
|
"url": "https://github.com/SashaGoncharov19/"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"license": "Custom-Attribution-NoDerivs",
|
||||||
|
"gitHead": "ffd542c1deddb3033e16e0dae7557313ae09b05f"
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
export * from './logger'
|
export * from './logger'
|
||||||
|
export * from './middleware'
|
||||||
export * from './player'
|
export * from './player'
|
||||||
export * from './rpc'
|
export * from './rpc'
|
||||||
export * from './server'
|
export * from './server'
|
||||||
|
@ -4,8 +4,8 @@ export class Middleware {
|
|||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
private static async execute<EventName extends T.RageFW_ServerEvent>(
|
private static async execute<EventName extends T.RageFW_ServerEvent>(
|
||||||
args: T.RageFW_ServerArgs<EventName>,
|
|
||||||
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
|
middlewares: T.RageFW_MiddlewareFunction<EventName>[],
|
||||||
|
args: T.RageFW_ServerArgs<EventName>,
|
||||||
): Promise<T.RageFW_MiddlewareResponseInternal> {
|
): Promise<T.RageFW_MiddlewareResponseInternal> {
|
||||||
for (let i = 0; i < middlewares.length; i++) {
|
for (let i = 0; i < middlewares.length; i++) {
|
||||||
const result = await middlewares[i](...args)
|
const result = await middlewares[i](...args)
|
||||||
@ -22,25 +22,25 @@ export class Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static async process<EventName extends T.RageFW_ServerEvent>(
|
public static async process<EventName extends T.RageFW_ServerEvent>(
|
||||||
|
middlewareOptions: T.RageFW_MiddlewareOptions<EventName>,
|
||||||
|
callback: T.RageFW_ServerCallback<EventName>,
|
||||||
args: T.RageFW_ServerArgs<EventName>,
|
args: T.RageFW_ServerArgs<EventName>,
|
||||||
middlewareOptions?: T.RageFW_MiddlewareOptions<EventName>,
|
) {
|
||||||
): Promise<boolean> {
|
|
||||||
if (!middlewareOptions) return true
|
|
||||||
|
|
||||||
if (Array.isArray(middlewareOptions)) {
|
if (Array.isArray(middlewareOptions)) {
|
||||||
const middlewaresResponse = await Middleware.execute(
|
const middlewaresResponse = await Middleware.execute(
|
||||||
args,
|
|
||||||
middlewareOptions,
|
middlewareOptions,
|
||||||
|
args,
|
||||||
)
|
)
|
||||||
return middlewaresResponse.success
|
|
||||||
|
if (middlewaresResponse.success) return await callback(...args)
|
||||||
} else {
|
} else {
|
||||||
const middlewaresResponse = await Middleware.execute(
|
const middlewaresResponse = await Middleware.execute(
|
||||||
args,
|
|
||||||
middlewareOptions.executables,
|
middlewareOptions.executables,
|
||||||
|
args,
|
||||||
)
|
)
|
||||||
|
|
||||||
if (middlewaresResponse.success) {
|
if (middlewaresResponse.success) {
|
||||||
return true
|
return await callback(...args)
|
||||||
} else {
|
} else {
|
||||||
middlewareOptions.onError(
|
middlewareOptions.onError(
|
||||||
middlewaresResponse.message ??
|
middlewaresResponse.message ??
|
||||||
@ -48,7 +48,6 @@ export class Middleware {
|
|||||||
middlewaresResponse.id +
|
middlewaresResponse.id +
|
||||||
' failed',
|
' failed',
|
||||||
)
|
)
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { rpc } from './rpc'
|
import { rpc } from './rpc'
|
||||||
import { Middleware } from './middleware'
|
import { Middleware } from './middleware'
|
||||||
import { Validation } from './validation'
|
|
||||||
import type * as T from '../types'
|
import type * as T from '../types'
|
||||||
|
|
||||||
/** Server-side interactions */
|
/** Server-side interactions */
|
||||||
@ -11,7 +10,6 @@ export class Server {
|
|||||||
* @param eventName - The name of the event to register
|
* @param eventName - The name of the event to register
|
||||||
* @param callback - The callback function to be executed when the event is triggered
|
* @param callback - The callback function to be executed when the event is triggered
|
||||||
* @param [options] - Optional settings for callback execution
|
* @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
|
* @param [options.middlewares] - Middleware functions to be checked before the callback executes
|
||||||
* @returns {Server} The current server instance, enabling method chaining
|
* @returns {Server} The current server instance, enabling method chaining
|
||||||
*
|
*
|
||||||
@ -46,7 +44,6 @@ export class Server {
|
|||||||
eventName: EventName,
|
eventName: EventName,
|
||||||
callback: T.RageFW_ServerCallback<EventName>,
|
callback: T.RageFW_ServerCallback<EventName>,
|
||||||
options?: {
|
options?: {
|
||||||
validation?: T.RageFW_ValidationOptions
|
|
||||||
middlewares?: T.RageFW_MiddlewareOptions<EventName>
|
middlewares?: T.RageFW_MiddlewareOptions<EventName>
|
||||||
},
|
},
|
||||||
): Server {
|
): Server {
|
||||||
@ -55,22 +52,9 @@ export class Server {
|
|||||||
ReturnType<typeof callback> | Promise<unknown>,
|
ReturnType<typeof callback> | Promise<unknown>,
|
||||||
EventName
|
EventName
|
||||||
>(eventName, async (...data) => {
|
>(eventName, async (...data) => {
|
||||||
if (!options?.middlewares && !options?.validation)
|
if (!options?.middlewares) return await callback(...data)
|
||||||
return await callback(...data)
|
|
||||||
|
|
||||||
const validationSuccess = Validation.process(
|
await Middleware.process(options.middlewares, callback, data)
|
||||||
data,
|
|
||||||
options?.validation,
|
|
||||||
)
|
|
||||||
if (!validationSuccess) return
|
|
||||||
|
|
||||||
const middlewaresSuccess = await Middleware.process(
|
|
||||||
data,
|
|
||||||
options?.middlewares,
|
|
||||||
)
|
|
||||||
if (!middlewaresSuccess) return
|
|
||||||
|
|
||||||
return await callback(...data)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return this
|
return this
|
||||||
@ -106,3 +90,7 @@ export class Server {
|
|||||||
// return rpc.call(eventName, args)
|
// return rpc.call(eventName, args)
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// new Server()
|
||||||
|
// .register('customServerEvent', async (a, b, c) => true)
|
||||||
|
// .unregister('customServerEvent')
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
import type * as T from '../types'
|
|
||||||
|
|
||||||
export class Validation {
|
|
||||||
constructor() {}
|
|
||||||
|
|
||||||
public static process<EventName extends T.RageFW_ServerEvent>(
|
|
||||||
args: T.RageFW_ServerArgs<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,4 +2,3 @@ export * from './browser'
|
|||||||
export * from './client'
|
export * from './client'
|
||||||
export * from './middleware'
|
export * from './middleware'
|
||||||
export * from './server'
|
export * from './server'
|
||||||
export * from './validation'
|
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
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
|
|
@ -1,12 +1,12 @@
|
|||||||
import { defineConfig } from 'tsup'
|
import { defineConfig } from 'tsup'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
entry: ['./src/index.ts'],
|
entry: ['src/index.ts'],
|
||||||
outDir: './dist',
|
outDir: './dist',
|
||||||
format: ['cjs'],
|
format: ['cjs'],
|
||||||
|
noExternal: ['rage-rpc'],
|
||||||
experimentalDts: true,
|
experimentalDts: true,
|
||||||
splitting: false,
|
splitting: false,
|
||||||
sourcemap: false,
|
sourcemap: false,
|
||||||
clean: true,
|
clean: true,
|
||||||
bundle: false,
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user