99
1010import { RuntimeException } from '@poppinss/exception'
1111
12- import { Item } from './item.js'
13- import { Maybe } from './maybe.js'
14- import { Collection } from './collection.js'
12+ import { Maybe } from './maybe.ts'
13+ import { Item } from './resource/item.ts'
14+ import { Collection } from './resource/collection.ts'
15+ import { Paginator } from './paginator.ts'
1516
1617/**
1718 * Serves as the base for creating custom data transformers.
@@ -37,35 +38,102 @@ import { Collection } from './collection.js'
3738 */
3839export abstract class BaseTransformer < T > {
3940 /**
40- * Specify a one-to-one relationship with a given transformer.
41+ * Static method to transform data into Item or Collection instances.
42+ * Handles single objects, arrays, null values, and Maybe-wrapped values.
4143 *
42- * @param data - The data to transform, can be wrapped in Maybe for optional values
44+ * @param data - The data to transform ( can be single object, array, null, or Maybe-wrapped)
4345 *
4446 * @example
4547 * ```ts
4648 * class UserTransformer extends BaseTransformer<User> {
4749 * toObject() {
48- * return {
49- * id: this.resource.id,
50- * profile: UserTransformer.item(this.resource.profile)
51- * }
50+ * return { id: this.resource.id, name: this.resource.name }
5251 * }
5352 * }
53+ *
54+ * // Transform single user
55+ * const userItem = UserTransformer.transform(user)
56+ *
57+ * // Transform array of users
58+ * const userCollection = UserTransformer.transform([user1, user2])
59+ *
60+ * // Transform with Maybe wrapper
61+ * const maybeUser = UserTransformer.transform(new Maybe(user))
5462 * ```
5563 */
56- static item < Self extends { new ( ...args : any [ ] ) : any } > (
64+ /**
65+ * Transform data wrapped in Maybe that can be undefined
66+ *
67+ * @param data - Maybe-wrapped data that can be undefined
68+ */
69+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
70+ this : Self ,
71+ data : Maybe < ConstructorParameters < Self > [ 0 ] >
72+ ) : Item < InstanceType < Self > , 1 , 'toObject' > | undefined
73+
74+ /**
75+ * Transform a single data object
76+ *
77+ * @param data - Single data object to transform
78+ */
79+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
80+ this : Self ,
81+ data : ConstructorParameters < Self > [ 0 ]
82+ ) : Item < InstanceType < Self > , 1 , 'toObject' >
83+
84+ /**
85+ * Transform data wrapped in Maybe that can be undefined or null
86+ *
87+ * @param data - Maybe-wrapped data that can be undefined or null
88+ */
89+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
5790 this : Self ,
5891 data : Maybe < ConstructorParameters < Self > [ 0 ] | null >
59- ) : Item < InstanceType < Self > , 1 , 'toObject' , null > | undefined
92+ ) : Item < InstanceType < Self > , 1 , 'toObject' > | undefined | null
6093
61- static item < Self extends { new ( ...args : any [ ] ) : any } > (
94+ /**
95+ * Transform a single data object that can be null
96+ *
97+ * @param data - Single data object that can be null
98+ */
99+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
62100 this : Self ,
63101 data : ConstructorParameters < Self > [ 0 ] | null
64- ) : Item < InstanceType < Self > , 1 , 'toObject' , null >
102+ ) : Item < InstanceType < Self > , 1 , 'toObject' > | null
65103
66- static item < Self extends { new ( ...args : any [ ] ) : any } > (
104+ /**
105+ * Transform an array wrapped in Maybe that can be undefined
106+ *
107+ * @param data - Maybe-wrapped array that can be undefined
108+ */
109+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
67110 this : Self ,
68- data : ConstructorParameters < Self > [ 0 ] | null | Maybe < ConstructorParameters < Self > [ 0 ] | null >
111+ data : Maybe < ConstructorParameters < Self > [ 0 ] [ ] >
112+ ) : Collection < InstanceType < Self > , 1 , 'toObject' > | undefined
113+
114+ /**
115+ * Transform an array of data objects
116+ *
117+ * @param data - Array of data objects to transform
118+ */
119+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
120+ this : Self ,
121+ data : ConstructorParameters < Self > [ 0 ] [ ]
122+ ) : Collection < InstanceType < Self > , 1 , 'toObject' >
123+
124+ /**
125+ * Implementation method that handles all transform overloads
126+ *
127+ * @param data - Union of all possible data types that can be transformed
128+ */
129+ static transform < Self extends { new ( ...args : any [ ] ) : any } > (
130+ this : Self ,
131+ data :
132+ | ConstructorParameters < Self > [ 0 ]
133+ | null
134+ | Maybe < ConstructorParameters < Self > [ 0 ] | null >
135+ | ConstructorParameters < Self > [ 0 ] [ ]
136+ | Maybe < ConstructorParameters < Self > [ 0 ] [ ] >
69137 ) {
70138 /**
71139 * If optional values are allowed and the value is undefined, then
@@ -77,58 +145,50 @@ export abstract class BaseTransformer<T> {
77145 return undefined
78146 }
79147
80- return new Item < InstanceType < Self > , 1 , 'toObject' , null > (
148+ if ( Array . isArray ( unwrappedValue ) ) {
149+ return new Collection ( unwrappedValue , this , 1 , 'toObject' , new RuntimeException ( ) )
150+ }
151+
152+ if ( unwrappedValue === null ) {
153+ return null
154+ }
155+
156+ return new Item < InstanceType < Self > , 1 , 'toObject' > (
81157 unwrappedValue ,
82158 this ,
83159 1 ,
84160 'toObject' ,
85- new RuntimeException ( ) ,
86- true
161+ new RuntimeException ( )
87162 )
88163 }
89164
90165 /**
91- * Specify a many relationship with a given transformer.
166+ * Create a paginated collection from an array of data
92167 *
93- * @param data - Array of data to transform, can be wrapped in Maybe for optional values
168+ * @param data - Array of data objects to transform into a paginated collection
94169 *
95170 * @example
96171 * ```ts
97172 * class UserTransformer extends BaseTransformer<User> {
98173 * toObject() {
99- * return {
100- * id: this.resource.id,
101- * posts: PostTransformer.collection(this.resource.posts)
102- * }
174+ * return { id: this.resource.id, name: this.resource.name }
103175 * }
104176 * }
177+ *
178+ * // Create paginated collection
179+ * const paginatedUsers = UserTransformer.paginate([user1, user2, user3])
105180 * ```
106181 */
107- static collection < Self extends { new ( ...args : any [ ] ) : any } > (
108- this : Self ,
109- data : Maybe < ConstructorParameters < Self > [ 0 ] [ ] >
110- ) : Collection < InstanceType < Self > , 1 , 'toObject' > | undefined
111-
112- static collection < Self extends { new ( ...args : any [ ] ) : any } > (
182+ static paginate < Self extends { new ( ...args : any [ ] ) : any } , MetaData extends Record < string , any > > (
113183 this : Self ,
114- data : ConstructorParameters < Self > [ 0 ] [ ]
115- ) : Collection < InstanceType < Self > , 1 , 'toObject' >
116-
117- static collection < Self extends { new ( ...args : any [ ] ) : any } > (
118- this : Self ,
119- data : ConstructorParameters < Self > [ 0 ] [ ] | Maybe < ConstructorParameters < Self > [ 0 ] [ ] >
120- ) {
121- /**
122- * If optional values are allowed and the value is undefined, then
123- * we return undefined
124- */
125- const canBeOptional = data instanceof Maybe
126- const unwrappedValue = canBeOptional ? data . value : data
127- if ( canBeOptional && unwrappedValue === undefined ) {
128- return undefined
129- }
130-
131- return new Collection ( unwrappedValue , this , 1 , 'toObject' , new RuntimeException ( ) )
184+ data : ConstructorParameters < Self > [ 0 ] [ ] ,
185+ metaData : MetaData
186+ ) : Paginator < Collection < InstanceType < Self > , 1 , 'toObject' > , 'data' , MetaData > {
187+ return new Paginator (
188+ new Collection ( data , this , 1 , 'toObject' , new RuntimeException ( ) ) ,
189+ 'data' ,
190+ metaData
191+ )
132192 }
133193
134194 /**
0 commit comments