@jesuferanmi/resource-transformer
Version:
Laravel-style API resources for NestJS apps
28 lines (27 loc) • 970 B
TypeScript
export declare abstract class BaseResource<T = any> {
protected resource: T;
protected additionalData: Record<string, any>;
constructor(resource: T);
/**
* Transform the resource into a plain object.
* Must be implemented by child classes.
*/
abstract toArray(): Record<string, any>;
/**
* Convert the resource to JSON, merging additional data.
*/
toJSON(): Record<string, any>;
/**
* Conditionally include a value if condition is true.
*/
protected when(condition: boolean, value: any, defaultValue?: any): any;
/**
* Include related resource only if relation is loaded (exists).
* Handles both single object and array of objects.
*/
protected whenLoaded<R>(relation: string, ResourceClass: new (resource: any) => BaseResource<R>): object | object[] | null;
/**
* Add extra data to merge into final output.
*/
additional(data: Record<string, any>): this;
}