mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge branch 'main' into 1018-bug---content-type-fields-edit-form-fields-name-do-not-reflect-db-names
This commit is contained in:
@@ -1,19 +1,16 @@
|
||||
/*
|
||||
* Copyright © 2024 Hexastack. All rights reserved.
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
*
|
||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { SettingCreateDto } from '@/setting/dto/setting.dto';
|
||||
import { ExtensionSetting } from '@/setting/schemas/types';
|
||||
import { HyphenToUnderscore } from '@/utils/types/extension';
|
||||
|
||||
export type ChannelName = `${string}-channel`;
|
||||
|
||||
export type ChannelSetting<N extends string = string> = Omit<
|
||||
SettingCreateDto,
|
||||
'group' | 'weight'
|
||||
> & {
|
||||
export type ChannelSetting<N extends string = string> = ExtensionSetting<{
|
||||
group: HyphenToUnderscore<N>;
|
||||
};
|
||||
}>;
|
||||
|
||||
@@ -139,7 +139,7 @@ export class SubscriberRepository extends BaseRepository<
|
||||
* @returns The found subscriber entity with populated fields.
|
||||
*/
|
||||
async findOneByForeignIdAndPopulate(id: string): Promise<SubscriberFull> {
|
||||
const query = this.findByForeignIdQuery(id).populate(this.populate);
|
||||
const query = this.findByForeignIdQuery(id).populate(this.populatePaths);
|
||||
const [result] = await this.execute(query, SubscriberFull);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { SettingCreateDto } from '@/setting/dto/setting.dto';
|
||||
import { ExtensionSetting } from '@/setting/schemas/types';
|
||||
import { HyphenToUnderscore } from '@/utils/types/extension';
|
||||
|
||||
import BaseHelper from './lib/base-helper';
|
||||
@@ -116,9 +116,7 @@ export type HelperRegistry<H extends BaseHelper = BaseHelper> = Map<
|
||||
Map<string, H>
|
||||
>;
|
||||
|
||||
export type HelperSetting<N extends HelperName = HelperName> = Omit<
|
||||
SettingCreateDto,
|
||||
'group' | 'weight'
|
||||
> & {
|
||||
group: HyphenToUnderscore<N>;
|
||||
};
|
||||
export type HelperSetting<N extends HelperName = HelperName> =
|
||||
ExtensionSetting<{
|
||||
group: HyphenToUnderscore<N>;
|
||||
}>;
|
||||
|
||||
@@ -10,7 +10,7 @@ import { ChannelEvent } from '@/channel/lib/EventWrapper';
|
||||
import { BlockCreateDto } from '@/chat/dto/block.dto';
|
||||
import { Block } from '@/chat/schemas/block.schema';
|
||||
import { Conversation } from '@/chat/schemas/conversation.schema';
|
||||
import { SettingCreateDto } from '@/setting/dto/setting.dto';
|
||||
import { ExtensionSetting } from '@/setting/schemas/types';
|
||||
|
||||
export type PluginName = `${string}-plugin`;
|
||||
|
||||
@@ -23,7 +23,7 @@ export interface CustomBlocks {}
|
||||
|
||||
type BlockAttrs = Partial<BlockCreateDto> & { name: string };
|
||||
|
||||
export type PluginSetting = Omit<SettingCreateDto, 'weight'>;
|
||||
export type PluginSetting = ExtensionSetting;
|
||||
|
||||
export type PluginBlockTemplate = Omit<
|
||||
BlockAttrs,
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import { BaseSchema } from '@/utils/generics/base-schema';
|
||||
|
||||
import { Setting } from './setting.schema';
|
||||
|
||||
export enum SettingType {
|
||||
@@ -128,3 +130,17 @@ export type AnySetting =
|
||||
| MultipleAttachmentSetting;
|
||||
|
||||
export type SettingDict = { [group: string]: Setting[] };
|
||||
|
||||
export type ExtensionSetting<
|
||||
E extends object = object,
|
||||
U extends AnySetting = AnySetting,
|
||||
K extends keyof BaseSchema = keyof BaseSchema,
|
||||
> = U extends any
|
||||
? {
|
||||
[P in keyof U as P extends K
|
||||
? never
|
||||
: U[P] extends never
|
||||
? never
|
||||
: P]: U[P];
|
||||
} & E
|
||||
: never;
|
||||
|
||||
@@ -94,14 +94,14 @@ export abstract class BaseRepository<
|
||||
constructor(
|
||||
readonly model: Model<T>,
|
||||
private readonly cls: new () => T,
|
||||
protected readonly populate: P[] = [],
|
||||
protected readonly populatePaths: P[] = [],
|
||||
protected readonly clsPopulate?: new () => TFull,
|
||||
) {
|
||||
this.registerLifeCycleHooks();
|
||||
}
|
||||
|
||||
canPopulate(populate: string[]): boolean {
|
||||
return populate.some((p) => this.populate.includes(p as P));
|
||||
return populate.some((p) => this.populatePaths.includes(p as P));
|
||||
}
|
||||
|
||||
getEventName(suffix: EHook) {
|
||||
@@ -303,7 +303,7 @@ export abstract class BaseRepository<
|
||||
): Promise<TFull | null> {
|
||||
this.ensureCanPopulate();
|
||||
const query = this.findOneQuery(criteria, projection).populate(
|
||||
this.populate,
|
||||
this.populatePaths,
|
||||
);
|
||||
return await this.executeOne(query, this.clsPopulate!);
|
||||
}
|
||||
@@ -375,7 +375,7 @@ export abstract class BaseRepository<
|
||||
}
|
||||
|
||||
private ensureCanPopulate(): void {
|
||||
if (!this.populate || !this.clsPopulate) {
|
||||
if (!this.populatePaths || !this.clsPopulate) {
|
||||
throw new Error('Cannot populate query');
|
||||
}
|
||||
}
|
||||
@@ -403,13 +403,13 @@ export abstract class BaseRepository<
|
||||
this.ensureCanPopulate();
|
||||
if (Array.isArray(pageQuery)) {
|
||||
const query = this.findQuery(filters, pageQuery, projection).populate(
|
||||
this.populate,
|
||||
this.populatePaths,
|
||||
);
|
||||
return await this.execute(query, this.clsPopulate!);
|
||||
}
|
||||
|
||||
const query = this.findQuery(filters, pageQuery, projection).populate(
|
||||
this.populate,
|
||||
this.populatePaths,
|
||||
);
|
||||
return await this.execute(query, this.clsPopulate!);
|
||||
}
|
||||
@@ -426,7 +426,7 @@ export abstract class BaseRepository<
|
||||
|
||||
async findAllAndPopulate(sort?: QuerySortDto<T>): Promise<TFull[]> {
|
||||
this.ensureCanPopulate();
|
||||
const query = this.findAllQuery(sort).populate(this.populate);
|
||||
const query = this.findAllQuery(sort).populate(this.populatePaths);
|
||||
return await this.execute(query, this.clsPopulate!);
|
||||
}
|
||||
|
||||
@@ -463,7 +463,7 @@ export abstract class BaseRepository<
|
||||
): Promise<TFull[]> {
|
||||
this.ensureCanPopulate();
|
||||
const query = this.findPageQuery(filters, pageQuery).populate(
|
||||
this.populate,
|
||||
this.populatePaths,
|
||||
);
|
||||
return await this.execute(query, this.clsPopulate!);
|
||||
}
|
||||
|
||||
59
api/src/utils/pipes/zod.pipe.ts
Normal file
59
api/src/utils/pipes/zod.pipe.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
*
|
||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
|
||||
*/
|
||||
|
||||
import {
|
||||
ArgumentMetadata,
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
PipeTransform,
|
||||
} from '@nestjs/common';
|
||||
import { ZodError, ZodTypeAny } from 'zod';
|
||||
|
||||
/**
|
||||
* Validates a single query-parameter with a given Zod schema.
|
||||
*
|
||||
* @example
|
||||
* // Controller usage
|
||||
* @Get()
|
||||
* listUsers(
|
||||
* @Query(new ZodQueryParamPipe(z.coerce.number().int().min(1))) query: any,
|
||||
* ) {
|
||||
* // query.page is guaranteed to be a positive integer number
|
||||
* }
|
||||
*/
|
||||
@Injectable()
|
||||
export class ZodQueryParamPipe implements PipeTransform {
|
||||
constructor(
|
||||
private readonly schema: ZodTypeAny,
|
||||
private readonly accessor?: (query: any) => any,
|
||||
) {}
|
||||
|
||||
async transform(query: any, metadata: ArgumentMetadata) {
|
||||
const payload = this.accessor ? this.accessor(query) : query;
|
||||
// We care only about query params
|
||||
if (typeof payload === 'undefined' || metadata.type !== 'query') {
|
||||
return payload;
|
||||
}
|
||||
|
||||
const parsed = this.schema.safeParse(payload);
|
||||
|
||||
if (!parsed.success) {
|
||||
// Optionally format the error for client readability
|
||||
const error = parsed.error as ZodError;
|
||||
throw new BadRequestException({
|
||||
statusCode: 400,
|
||||
error: 'Bad Request',
|
||||
message: `Validation failed for query param`,
|
||||
details: error.flatten(),
|
||||
});
|
||||
}
|
||||
|
||||
// Return a new query object with the parsed value injected
|
||||
return parsed.data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user