resora 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +176 -0
- package/dist/index.cjs +371 -0
- package/dist/index.d.cts +328 -0
- package/dist/index.d.mts +328 -0
- package/dist/index.mjs +368 -0
- package/package.json +77 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { H3Event } from "h3";
|
|
2
|
+
import { Response } from "express";
|
|
3
|
+
|
|
4
|
+
//#region src/ApiResource.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* ApiResource function to return the Resource instance
|
|
7
|
+
*
|
|
8
|
+
* @param instance Resource instance
|
|
9
|
+
* @returns Resource instance
|
|
10
|
+
*/
|
|
11
|
+
declare function ApiResource<R extends Resource>(instance: Resource<R>): Resource<R>;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/types.d.ts
|
|
14
|
+
interface MetaData {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}
|
|
17
|
+
interface ResourceData {
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}
|
|
20
|
+
interface Resource$1 extends ResourceData {
|
|
21
|
+
cursor?: Cursor | undefined;
|
|
22
|
+
pagination?: Pagination | undefined;
|
|
23
|
+
}
|
|
24
|
+
interface NonCollectible {
|
|
25
|
+
data: ResourceData;
|
|
26
|
+
}
|
|
27
|
+
interface Collectible {
|
|
28
|
+
data: ResourceData[];
|
|
29
|
+
cursor?: Cursor | undefined;
|
|
30
|
+
pagination?: Pagination | undefined;
|
|
31
|
+
}
|
|
32
|
+
interface ResponseData<R extends ResourceData = any> extends Resource$1 {
|
|
33
|
+
data: R;
|
|
34
|
+
meta?: MetaData | undefined;
|
|
35
|
+
}
|
|
36
|
+
type PaginatedMetaData<R extends Collectible = Collectible> = MetaData & Omit<R, 'data'>;
|
|
37
|
+
interface ResponseDataCollection<R extends Collectible | undefined = undefined> extends ResourceData {
|
|
38
|
+
data: R extends Collectible ? R['data'] : R;
|
|
39
|
+
meta?: R extends Collectible ? PaginatedMetaData<R> | undefined : undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @description A type that represents the body of a response for a collection of resources.
|
|
43
|
+
* It can be either an array of ResourceData objects or a Collectible object, which contains an
|
|
44
|
+
* array of ResourceData objects. The type also includes the meta property, which is optional
|
|
45
|
+
* and can contain any additional metadata related to the response.
|
|
46
|
+
* @example
|
|
47
|
+
* const collectionResponse: CollectionBody = {
|
|
48
|
+
* data: [
|
|
49
|
+
* { id: 1, name: 'Resource 1' },
|
|
50
|
+
* { id: 2, name: 'Resource 2' }
|
|
51
|
+
* ],
|
|
52
|
+
* meta: {
|
|
53
|
+
* pagination: {
|
|
54
|
+
* currentPage: 1,
|
|
55
|
+
* total: 2
|
|
56
|
+
* }
|
|
57
|
+
* }
|
|
58
|
+
* };
|
|
59
|
+
*/
|
|
60
|
+
type CollectionBody<R extends ResourceData[] | Collectible = ResourceData[]> = ResponseDataCollection<R extends Collectible ? R : {
|
|
61
|
+
data: R;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* @description A type that represents the body of a response for a single resource.
|
|
65
|
+
* It can be either a ResourceData object or a NonCollectible object, which contains a
|
|
66
|
+
* ResourceData object. The type also includes the meta property, which is optional and can
|
|
67
|
+
* contain any additional metadata related to the response.
|
|
68
|
+
* @example
|
|
69
|
+
* const resourceResponse: ResourceBody = {
|
|
70
|
+
* data: {
|
|
71
|
+
* id: 1,
|
|
72
|
+
* name: 'Resource Name',
|
|
73
|
+
* description: 'Resource Description'
|
|
74
|
+
* },
|
|
75
|
+
* meta: {
|
|
76
|
+
* timestamp: '2024-06-01T12:00:00Z'
|
|
77
|
+
* }
|
|
78
|
+
* };
|
|
79
|
+
*/
|
|
80
|
+
type ResourceBody<R extends ResourceData | NonCollectible = ResourceData> = ResponseData<R extends NonCollectible ? R : {
|
|
81
|
+
data: R;
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* @description A type that represents the pagination information for a collection of resources. It includes properties such as currentPage, from, to, perPage, total, firstPage, lastPage, prevPage, and nextPage. All properties are optional and can be undefined if not applicable.
|
|
85
|
+
* @example
|
|
86
|
+
* const paginationInfo: Pagination = {
|
|
87
|
+
* currentPage: 1,
|
|
88
|
+
* from: 1,
|
|
89
|
+
* to: 10,
|
|
90
|
+
* perPage: 10,
|
|
91
|
+
* total: 100,
|
|
92
|
+
* firstPage: 1,
|
|
93
|
+
* lastPage: 10,
|
|
94
|
+
* prevPage: null,
|
|
95
|
+
* nextPage: 2
|
|
96
|
+
* };
|
|
97
|
+
*/
|
|
98
|
+
interface Pagination {
|
|
99
|
+
currentPage?: number | undefined;
|
|
100
|
+
from?: number | undefined;
|
|
101
|
+
to?: number | undefined;
|
|
102
|
+
perPage?: number | undefined;
|
|
103
|
+
total?: number | undefined;
|
|
104
|
+
firstPage?: number | undefined;
|
|
105
|
+
lastPage?: number | undefined;
|
|
106
|
+
prevPage?: number | undefined;
|
|
107
|
+
nextPage?: number | undefined;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @description A type that represents the cursor information for pagination. It includes properties such as before and after, which are optional and can be undefined if not applicable. The before property represents the cursor for the previous page, while the after property represents the cursor for the next page.
|
|
111
|
+
* @example
|
|
112
|
+
* const cursorInfo: Cursor = {
|
|
113
|
+
* previous: 'cursor-for-previous-page',
|
|
114
|
+
* next: 'cursor-for-next-page'
|
|
115
|
+
* };
|
|
116
|
+
*/
|
|
117
|
+
interface Cursor {
|
|
118
|
+
previous?: string | undefined;
|
|
119
|
+
next?: string | undefined;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/ServerResponse.d.ts
|
|
123
|
+
declare class ServerResponse<R extends NonCollectible | Collectible | ResourceData[] | ResourceData = ResourceData> {
|
|
124
|
+
#private;
|
|
125
|
+
private response;
|
|
126
|
+
private body;
|
|
127
|
+
private _status;
|
|
128
|
+
headers: Record<string, string>;
|
|
129
|
+
constructor(response: H3Event['res'], body: R);
|
|
130
|
+
constructor(response: Response, body: R);
|
|
131
|
+
/**
|
|
132
|
+
* Set the HTTP status code for the response
|
|
133
|
+
*
|
|
134
|
+
* @param status
|
|
135
|
+
* @returns The current ServerResponse instance
|
|
136
|
+
*/
|
|
137
|
+
setStatusCode(status: number): this;
|
|
138
|
+
/**
|
|
139
|
+
* Get the current HTTP status code for the response
|
|
140
|
+
*
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
status(): number;
|
|
144
|
+
/**
|
|
145
|
+
* Get the current HTTP status text for the response
|
|
146
|
+
*
|
|
147
|
+
* @returns
|
|
148
|
+
*/
|
|
149
|
+
statusText(): string | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* Set a cookie in the response header
|
|
152
|
+
*
|
|
153
|
+
* @param name The name of the cookie
|
|
154
|
+
* @param value The value of the cookie
|
|
155
|
+
* @param options Optional cookie attributes (e.g., path, domain, maxAge)
|
|
156
|
+
* @returns The current ServerResponse instance
|
|
157
|
+
*/
|
|
158
|
+
setCookie(name: string, value: string, options?: Record<string, any>): this;
|
|
159
|
+
/**
|
|
160
|
+
* Convert the resource to a JSON response body
|
|
161
|
+
*
|
|
162
|
+
* @param headers Optional headers to add to the response
|
|
163
|
+
* @returns The current ServerResponse instance
|
|
164
|
+
*/
|
|
165
|
+
setHeaders(headers: Record<string, string>): this;
|
|
166
|
+
/**
|
|
167
|
+
* Add a single header to the response
|
|
168
|
+
*
|
|
169
|
+
* @param key The name of the header
|
|
170
|
+
* @param value The value of the header
|
|
171
|
+
* @returns The current ServerResponse instance
|
|
172
|
+
*/
|
|
173
|
+
header(key: string, value: string): this;
|
|
174
|
+
/**
|
|
175
|
+
* Promise-like then method to allow chaining with async/await or .then() syntax
|
|
176
|
+
*
|
|
177
|
+
* @param onfulfilled Callback to handle the fulfilled state of the promise, receiving the response body
|
|
178
|
+
* @param onrejected Callback to handle the rejected state of the promise, receiving the error reason
|
|
179
|
+
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
180
|
+
*/
|
|
181
|
+
then<TResult1 = R, TResult2 = never>(onfulfilled?: ((value: R) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
182
|
+
/**
|
|
183
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
184
|
+
*
|
|
185
|
+
* @param onrejected
|
|
186
|
+
* @returns
|
|
187
|
+
*/
|
|
188
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<R | TResult>;
|
|
189
|
+
/**
|
|
190
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
191
|
+
*
|
|
192
|
+
* @param onfinally
|
|
193
|
+
* @returns
|
|
194
|
+
*/
|
|
195
|
+
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
196
|
+
}
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/ResourceCollection.d.ts
|
|
199
|
+
/**
|
|
200
|
+
* ResourceCollection class to handle API resource transformation and response building for collections
|
|
201
|
+
*/
|
|
202
|
+
declare class ResourceCollection<R extends ResourceData[] | Collectible = ResourceData[], T extends ResourceData = any> {
|
|
203
|
+
private res?;
|
|
204
|
+
[key: string]: any;
|
|
205
|
+
body: CollectionBody<R>;
|
|
206
|
+
resource: R;
|
|
207
|
+
collects?: typeof Resource<T>;
|
|
208
|
+
private called;
|
|
209
|
+
constructor(rsc: R);
|
|
210
|
+
constructor(rsc: R, res: Response);
|
|
211
|
+
/**
|
|
212
|
+
* Get the original resource data
|
|
213
|
+
*/
|
|
214
|
+
data(): (R extends Collectible ? R["data"][number] : R extends ResourceData[] ? R[number] : never)[];
|
|
215
|
+
/**
|
|
216
|
+
* Convert resource to JSON response format
|
|
217
|
+
*
|
|
218
|
+
* @returns
|
|
219
|
+
*/
|
|
220
|
+
json(): this;
|
|
221
|
+
/**
|
|
222
|
+
* Flatten resource to return original data
|
|
223
|
+
*
|
|
224
|
+
* @returns
|
|
225
|
+
*/
|
|
226
|
+
toArray(): (R extends Collectible ? R['data'][number] : R extends ResourceData[] ? R[number] : never)[];
|
|
227
|
+
/**
|
|
228
|
+
* Add additional properties to the response body
|
|
229
|
+
*
|
|
230
|
+
* @param extra Additional properties to merge into the response body
|
|
231
|
+
* @returns
|
|
232
|
+
*/
|
|
233
|
+
additional<X extends Record<string, any>>(extra: X): this;
|
|
234
|
+
response(): ServerResponse<CollectionBody<R>>;
|
|
235
|
+
response(res: H3Event['res']): ServerResponse<CollectionBody<R>>;
|
|
236
|
+
setCollects(collects: typeof Resource<T>): this;
|
|
237
|
+
/**
|
|
238
|
+
* Promise-like then method to allow chaining with async/await or .then() syntax
|
|
239
|
+
*
|
|
240
|
+
* @param onfulfilled Callback to handle the fulfilled state of the promise, receiving the response body
|
|
241
|
+
* @param onrejected Callback to handle the rejected state of the promise, receiving the error reason
|
|
242
|
+
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
243
|
+
*/
|
|
244
|
+
then<TResult1 = CollectionBody<R>, TResult2 = never>(onfulfilled?: ((value: CollectionBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
245
|
+
/**
|
|
246
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
247
|
+
*
|
|
248
|
+
* @param onrejected
|
|
249
|
+
* @returns
|
|
250
|
+
*/
|
|
251
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<CollectionBody<R> | TResult>;
|
|
252
|
+
/**
|
|
253
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
254
|
+
*
|
|
255
|
+
* @param onfinally
|
|
256
|
+
* @returns
|
|
257
|
+
*/
|
|
258
|
+
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
259
|
+
}
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/Resource.d.ts
|
|
262
|
+
/**
|
|
263
|
+
* Resource class to handle API resource transformation and response building
|
|
264
|
+
*/
|
|
265
|
+
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
266
|
+
private res?;
|
|
267
|
+
[key: string]: any;
|
|
268
|
+
body: ResourceBody<R>;
|
|
269
|
+
resource: R;
|
|
270
|
+
private called;
|
|
271
|
+
constructor(rsc: R, res?: Response | undefined);
|
|
272
|
+
/**
|
|
273
|
+
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
274
|
+
*
|
|
275
|
+
* @param data
|
|
276
|
+
* @returns
|
|
277
|
+
*/
|
|
278
|
+
static collection<C extends ResourceData[] | Collectible = ResourceData[], T extends ResourceData = any>(data: C): ResourceCollection<C, T>;
|
|
279
|
+
/**
|
|
280
|
+
* Get the original resource data
|
|
281
|
+
*/
|
|
282
|
+
data(): R extends NonCollectible ? R["data"] : R;
|
|
283
|
+
/**
|
|
284
|
+
* Convert resource to JSON response format
|
|
285
|
+
*
|
|
286
|
+
* @returns
|
|
287
|
+
*/
|
|
288
|
+
json(): this;
|
|
289
|
+
/**
|
|
290
|
+
* Flatten resource to array format (for collections) or return original data for single resources
|
|
291
|
+
*
|
|
292
|
+
* @returns
|
|
293
|
+
*/
|
|
294
|
+
toArray(): R extends NonCollectible ? R['data'] : R;
|
|
295
|
+
/**
|
|
296
|
+
* Add additional properties to the response body
|
|
297
|
+
*
|
|
298
|
+
* @param extra Additional properties to merge into the response body
|
|
299
|
+
* @returns
|
|
300
|
+
*/
|
|
301
|
+
additional<X extends Record<string, any>>(extra: X): this;
|
|
302
|
+
response(): ServerResponse<ResourceBody<R>>;
|
|
303
|
+
response(res: H3Event['res']): ServerResponse<ResourceBody<R>>;
|
|
304
|
+
/**
|
|
305
|
+
* Promise-like then method to allow chaining with async/await or .then() syntax
|
|
306
|
+
*
|
|
307
|
+
* @param onfulfilled Callback to handle the fulfilled state of the promise, receiving the response body
|
|
308
|
+
* @param onrejected Callback to handle the rejected state of the promise, receiving the error reason
|
|
309
|
+
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
310
|
+
*/
|
|
311
|
+
then<TResult1 = ResourceBody<R>, TResult2 = never>(onfulfilled?: ((value: ResourceBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
312
|
+
/**
|
|
313
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
314
|
+
*
|
|
315
|
+
* @param onrejected
|
|
316
|
+
* @returns
|
|
317
|
+
*/
|
|
318
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<ResourceBody<R> | TResult>;
|
|
319
|
+
/**
|
|
320
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
321
|
+
*
|
|
322
|
+
* @param onfinally
|
|
323
|
+
* @returns
|
|
324
|
+
*/
|
|
325
|
+
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
export { ApiResource, Resource };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { H3Event } from "h3";
|
|
2
|
+
import { Response } from "express";
|
|
3
|
+
|
|
4
|
+
//#region src/ApiResource.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* ApiResource function to return the Resource instance
|
|
7
|
+
*
|
|
8
|
+
* @param instance Resource instance
|
|
9
|
+
* @returns Resource instance
|
|
10
|
+
*/
|
|
11
|
+
declare function ApiResource<R extends Resource>(instance: Resource<R>): Resource<R>;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/types.d.ts
|
|
14
|
+
interface MetaData {
|
|
15
|
+
[key: string]: any;
|
|
16
|
+
}
|
|
17
|
+
interface ResourceData {
|
|
18
|
+
[key: string]: any;
|
|
19
|
+
}
|
|
20
|
+
interface Resource$1 extends ResourceData {
|
|
21
|
+
cursor?: Cursor | undefined;
|
|
22
|
+
pagination?: Pagination | undefined;
|
|
23
|
+
}
|
|
24
|
+
interface NonCollectible {
|
|
25
|
+
data: ResourceData;
|
|
26
|
+
}
|
|
27
|
+
interface Collectible {
|
|
28
|
+
data: ResourceData[];
|
|
29
|
+
cursor?: Cursor | undefined;
|
|
30
|
+
pagination?: Pagination | undefined;
|
|
31
|
+
}
|
|
32
|
+
interface ResponseData<R extends ResourceData = any> extends Resource$1 {
|
|
33
|
+
data: R;
|
|
34
|
+
meta?: MetaData | undefined;
|
|
35
|
+
}
|
|
36
|
+
type PaginatedMetaData<R extends Collectible = Collectible> = MetaData & Omit<R, 'data'>;
|
|
37
|
+
interface ResponseDataCollection<R extends Collectible | undefined = undefined> extends ResourceData {
|
|
38
|
+
data: R extends Collectible ? R['data'] : R;
|
|
39
|
+
meta?: R extends Collectible ? PaginatedMetaData<R> | undefined : undefined;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @description A type that represents the body of a response for a collection of resources.
|
|
43
|
+
* It can be either an array of ResourceData objects or a Collectible object, which contains an
|
|
44
|
+
* array of ResourceData objects. The type also includes the meta property, which is optional
|
|
45
|
+
* and can contain any additional metadata related to the response.
|
|
46
|
+
* @example
|
|
47
|
+
* const collectionResponse: CollectionBody = {
|
|
48
|
+
* data: [
|
|
49
|
+
* { id: 1, name: 'Resource 1' },
|
|
50
|
+
* { id: 2, name: 'Resource 2' }
|
|
51
|
+
* ],
|
|
52
|
+
* meta: {
|
|
53
|
+
* pagination: {
|
|
54
|
+
* currentPage: 1,
|
|
55
|
+
* total: 2
|
|
56
|
+
* }
|
|
57
|
+
* }
|
|
58
|
+
* };
|
|
59
|
+
*/
|
|
60
|
+
type CollectionBody<R extends ResourceData[] | Collectible = ResourceData[]> = ResponseDataCollection<R extends Collectible ? R : {
|
|
61
|
+
data: R;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* @description A type that represents the body of a response for a single resource.
|
|
65
|
+
* It can be either a ResourceData object or a NonCollectible object, which contains a
|
|
66
|
+
* ResourceData object. The type also includes the meta property, which is optional and can
|
|
67
|
+
* contain any additional metadata related to the response.
|
|
68
|
+
* @example
|
|
69
|
+
* const resourceResponse: ResourceBody = {
|
|
70
|
+
* data: {
|
|
71
|
+
* id: 1,
|
|
72
|
+
* name: 'Resource Name',
|
|
73
|
+
* description: 'Resource Description'
|
|
74
|
+
* },
|
|
75
|
+
* meta: {
|
|
76
|
+
* timestamp: '2024-06-01T12:00:00Z'
|
|
77
|
+
* }
|
|
78
|
+
* };
|
|
79
|
+
*/
|
|
80
|
+
type ResourceBody<R extends ResourceData | NonCollectible = ResourceData> = ResponseData<R extends NonCollectible ? R : {
|
|
81
|
+
data: R;
|
|
82
|
+
}>;
|
|
83
|
+
/**
|
|
84
|
+
* @description A type that represents the pagination information for a collection of resources. It includes properties such as currentPage, from, to, perPage, total, firstPage, lastPage, prevPage, and nextPage. All properties are optional and can be undefined if not applicable.
|
|
85
|
+
* @example
|
|
86
|
+
* const paginationInfo: Pagination = {
|
|
87
|
+
* currentPage: 1,
|
|
88
|
+
* from: 1,
|
|
89
|
+
* to: 10,
|
|
90
|
+
* perPage: 10,
|
|
91
|
+
* total: 100,
|
|
92
|
+
* firstPage: 1,
|
|
93
|
+
* lastPage: 10,
|
|
94
|
+
* prevPage: null,
|
|
95
|
+
* nextPage: 2
|
|
96
|
+
* };
|
|
97
|
+
*/
|
|
98
|
+
interface Pagination {
|
|
99
|
+
currentPage?: number | undefined;
|
|
100
|
+
from?: number | undefined;
|
|
101
|
+
to?: number | undefined;
|
|
102
|
+
perPage?: number | undefined;
|
|
103
|
+
total?: number | undefined;
|
|
104
|
+
firstPage?: number | undefined;
|
|
105
|
+
lastPage?: number | undefined;
|
|
106
|
+
prevPage?: number | undefined;
|
|
107
|
+
nextPage?: number | undefined;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @description A type that represents the cursor information for pagination. It includes properties such as before and after, which are optional and can be undefined if not applicable. The before property represents the cursor for the previous page, while the after property represents the cursor for the next page.
|
|
111
|
+
* @example
|
|
112
|
+
* const cursorInfo: Cursor = {
|
|
113
|
+
* previous: 'cursor-for-previous-page',
|
|
114
|
+
* next: 'cursor-for-next-page'
|
|
115
|
+
* };
|
|
116
|
+
*/
|
|
117
|
+
interface Cursor {
|
|
118
|
+
previous?: string | undefined;
|
|
119
|
+
next?: string | undefined;
|
|
120
|
+
}
|
|
121
|
+
//#endregion
|
|
122
|
+
//#region src/ServerResponse.d.ts
|
|
123
|
+
declare class ServerResponse<R extends NonCollectible | Collectible | ResourceData[] | ResourceData = ResourceData> {
|
|
124
|
+
#private;
|
|
125
|
+
private response;
|
|
126
|
+
private body;
|
|
127
|
+
private _status;
|
|
128
|
+
headers: Record<string, string>;
|
|
129
|
+
constructor(response: H3Event['res'], body: R);
|
|
130
|
+
constructor(response: Response, body: R);
|
|
131
|
+
/**
|
|
132
|
+
* Set the HTTP status code for the response
|
|
133
|
+
*
|
|
134
|
+
* @param status
|
|
135
|
+
* @returns The current ServerResponse instance
|
|
136
|
+
*/
|
|
137
|
+
setStatusCode(status: number): this;
|
|
138
|
+
/**
|
|
139
|
+
* Get the current HTTP status code for the response
|
|
140
|
+
*
|
|
141
|
+
* @returns
|
|
142
|
+
*/
|
|
143
|
+
status(): number;
|
|
144
|
+
/**
|
|
145
|
+
* Get the current HTTP status text for the response
|
|
146
|
+
*
|
|
147
|
+
* @returns
|
|
148
|
+
*/
|
|
149
|
+
statusText(): string | undefined;
|
|
150
|
+
/**
|
|
151
|
+
* Set a cookie in the response header
|
|
152
|
+
*
|
|
153
|
+
* @param name The name of the cookie
|
|
154
|
+
* @param value The value of the cookie
|
|
155
|
+
* @param options Optional cookie attributes (e.g., path, domain, maxAge)
|
|
156
|
+
* @returns The current ServerResponse instance
|
|
157
|
+
*/
|
|
158
|
+
setCookie(name: string, value: string, options?: Record<string, any>): this;
|
|
159
|
+
/**
|
|
160
|
+
* Convert the resource to a JSON response body
|
|
161
|
+
*
|
|
162
|
+
* @param headers Optional headers to add to the response
|
|
163
|
+
* @returns The current ServerResponse instance
|
|
164
|
+
*/
|
|
165
|
+
setHeaders(headers: Record<string, string>): this;
|
|
166
|
+
/**
|
|
167
|
+
* Add a single header to the response
|
|
168
|
+
*
|
|
169
|
+
* @param key The name of the header
|
|
170
|
+
* @param value The value of the header
|
|
171
|
+
* @returns The current ServerResponse instance
|
|
172
|
+
*/
|
|
173
|
+
header(key: string, value: string): this;
|
|
174
|
+
/**
|
|
175
|
+
* Promise-like then method to allow chaining with async/await or .then() syntax
|
|
176
|
+
*
|
|
177
|
+
* @param onfulfilled Callback to handle the fulfilled state of the promise, receiving the response body
|
|
178
|
+
* @param onrejected Callback to handle the rejected state of the promise, receiving the error reason
|
|
179
|
+
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
180
|
+
*/
|
|
181
|
+
then<TResult1 = R, TResult2 = never>(onfulfilled?: ((value: R) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
182
|
+
/**
|
|
183
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
184
|
+
*
|
|
185
|
+
* @param onrejected
|
|
186
|
+
* @returns
|
|
187
|
+
*/
|
|
188
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<R | TResult>;
|
|
189
|
+
/**
|
|
190
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
191
|
+
*
|
|
192
|
+
* @param onfinally
|
|
193
|
+
* @returns
|
|
194
|
+
*/
|
|
195
|
+
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
196
|
+
}
|
|
197
|
+
//#endregion
|
|
198
|
+
//#region src/ResourceCollection.d.ts
|
|
199
|
+
/**
|
|
200
|
+
* ResourceCollection class to handle API resource transformation and response building for collections
|
|
201
|
+
*/
|
|
202
|
+
declare class ResourceCollection<R extends ResourceData[] | Collectible = ResourceData[], T extends ResourceData = any> {
|
|
203
|
+
private res?;
|
|
204
|
+
[key: string]: any;
|
|
205
|
+
body: CollectionBody<R>;
|
|
206
|
+
resource: R;
|
|
207
|
+
collects?: typeof Resource<T>;
|
|
208
|
+
private called;
|
|
209
|
+
constructor(rsc: R);
|
|
210
|
+
constructor(rsc: R, res: Response);
|
|
211
|
+
/**
|
|
212
|
+
* Get the original resource data
|
|
213
|
+
*/
|
|
214
|
+
data(): (R extends Collectible ? R["data"][number] : R extends ResourceData[] ? R[number] : never)[];
|
|
215
|
+
/**
|
|
216
|
+
* Convert resource to JSON response format
|
|
217
|
+
*
|
|
218
|
+
* @returns
|
|
219
|
+
*/
|
|
220
|
+
json(): this;
|
|
221
|
+
/**
|
|
222
|
+
* Flatten resource to return original data
|
|
223
|
+
*
|
|
224
|
+
* @returns
|
|
225
|
+
*/
|
|
226
|
+
toArray(): (R extends Collectible ? R['data'][number] : R extends ResourceData[] ? R[number] : never)[];
|
|
227
|
+
/**
|
|
228
|
+
* Add additional properties to the response body
|
|
229
|
+
*
|
|
230
|
+
* @param extra Additional properties to merge into the response body
|
|
231
|
+
* @returns
|
|
232
|
+
*/
|
|
233
|
+
additional<X extends Record<string, any>>(extra: X): this;
|
|
234
|
+
response(): ServerResponse<CollectionBody<R>>;
|
|
235
|
+
response(res: H3Event['res']): ServerResponse<CollectionBody<R>>;
|
|
236
|
+
setCollects(collects: typeof Resource<T>): this;
|
|
237
|
+
/**
|
|
238
|
+
* Promise-like then method to allow chaining with async/await or .then() syntax
|
|
239
|
+
*
|
|
240
|
+
* @param onfulfilled Callback to handle the fulfilled state of the promise, receiving the response body
|
|
241
|
+
* @param onrejected Callback to handle the rejected state of the promise, receiving the error reason
|
|
242
|
+
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
243
|
+
*/
|
|
244
|
+
then<TResult1 = CollectionBody<R>, TResult2 = never>(onfulfilled?: ((value: CollectionBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
245
|
+
/**
|
|
246
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
247
|
+
*
|
|
248
|
+
* @param onrejected
|
|
249
|
+
* @returns
|
|
250
|
+
*/
|
|
251
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<CollectionBody<R> | TResult>;
|
|
252
|
+
/**
|
|
253
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
254
|
+
*
|
|
255
|
+
* @param onfinally
|
|
256
|
+
* @returns
|
|
257
|
+
*/
|
|
258
|
+
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
259
|
+
}
|
|
260
|
+
//#endregion
|
|
261
|
+
//#region src/Resource.d.ts
|
|
262
|
+
/**
|
|
263
|
+
* Resource class to handle API resource transformation and response building
|
|
264
|
+
*/
|
|
265
|
+
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
266
|
+
private res?;
|
|
267
|
+
[key: string]: any;
|
|
268
|
+
body: ResourceBody<R>;
|
|
269
|
+
resource: R;
|
|
270
|
+
private called;
|
|
271
|
+
constructor(rsc: R, res?: Response | undefined);
|
|
272
|
+
/**
|
|
273
|
+
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
274
|
+
*
|
|
275
|
+
* @param data
|
|
276
|
+
* @returns
|
|
277
|
+
*/
|
|
278
|
+
static collection<C extends ResourceData[] | Collectible = ResourceData[], T extends ResourceData = any>(data: C): ResourceCollection<C, T>;
|
|
279
|
+
/**
|
|
280
|
+
* Get the original resource data
|
|
281
|
+
*/
|
|
282
|
+
data(): R extends NonCollectible ? R["data"] : R;
|
|
283
|
+
/**
|
|
284
|
+
* Convert resource to JSON response format
|
|
285
|
+
*
|
|
286
|
+
* @returns
|
|
287
|
+
*/
|
|
288
|
+
json(): this;
|
|
289
|
+
/**
|
|
290
|
+
* Flatten resource to array format (for collections) or return original data for single resources
|
|
291
|
+
*
|
|
292
|
+
* @returns
|
|
293
|
+
*/
|
|
294
|
+
toArray(): R extends NonCollectible ? R['data'] : R;
|
|
295
|
+
/**
|
|
296
|
+
* Add additional properties to the response body
|
|
297
|
+
*
|
|
298
|
+
* @param extra Additional properties to merge into the response body
|
|
299
|
+
* @returns
|
|
300
|
+
*/
|
|
301
|
+
additional<X extends Record<string, any>>(extra: X): this;
|
|
302
|
+
response(): ServerResponse<ResourceBody<R>>;
|
|
303
|
+
response(res: H3Event['res']): ServerResponse<ResourceBody<R>>;
|
|
304
|
+
/**
|
|
305
|
+
* Promise-like then method to allow chaining with async/await or .then() syntax
|
|
306
|
+
*
|
|
307
|
+
* @param onfulfilled Callback to handle the fulfilled state of the promise, receiving the response body
|
|
308
|
+
* @param onrejected Callback to handle the rejected state of the promise, receiving the error reason
|
|
309
|
+
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
310
|
+
*/
|
|
311
|
+
then<TResult1 = ResourceBody<R>, TResult2 = never>(onfulfilled?: ((value: ResourceBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
312
|
+
/**
|
|
313
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
314
|
+
*
|
|
315
|
+
* @param onrejected
|
|
316
|
+
* @returns
|
|
317
|
+
*/
|
|
318
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<ResourceBody<R> | TResult>;
|
|
319
|
+
/**
|
|
320
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
321
|
+
*
|
|
322
|
+
* @param onfinally
|
|
323
|
+
* @returns
|
|
324
|
+
*/
|
|
325
|
+
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
export { ApiResource, Resource };
|