resora 0.2.7 → 0.2.9
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 +2 -2
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +336 -100
- package/dist/index.d.mts +336 -100
- package/dist/index.mjs +4 -4
- package/package.json +2 -2
- package/stubs/resource.collection.stub +1 -1
- package/stubs/resource.stub +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -238,6 +238,27 @@ interface Config {
|
|
|
238
238
|
*/
|
|
239
239
|
extraCommands?: typeof Command<any>[];
|
|
240
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* @description A type that represents the configuration options that can be set at the resource level, allowing for customization of case style, response structure, and additional data included in paginated responses on a per-resource basis.
|
|
243
|
+
*/
|
|
244
|
+
interface ResourceLevelConfig {
|
|
245
|
+
/**
|
|
246
|
+
* @description The preferred case style for resource keys in the response.
|
|
247
|
+
* Can be set to a preset string ('camel', 'snake', 'pascal', 'kebab') or a custom transformer function.
|
|
248
|
+
* @default 'camel'
|
|
249
|
+
*/
|
|
250
|
+
preferredCase?: CaseStyle | undefined;
|
|
251
|
+
/**
|
|
252
|
+
* @description An object specifying the structure of the response body for resources. It includes a rootKey property that defines the key under which the resource data will be nested in the response. The rootKey is a string that represents the name of this key in the response body.
|
|
253
|
+
*/
|
|
254
|
+
responseStructure?: ResponseStructureConfig | undefined;
|
|
255
|
+
/**
|
|
256
|
+
* @description An array or object specifying which additional data to include in paginated responses.
|
|
257
|
+
* Can include 'meta', 'links', and/or 'cursor'. If an object is provided, the keys can be customized to specify the property names for each type of extra data in the response.
|
|
258
|
+
* @default ['meta', 'links']
|
|
259
|
+
*/
|
|
260
|
+
paginatedExtras?: Config['paginatedExtras'] | undefined;
|
|
261
|
+
}
|
|
241
262
|
interface ResoraConfig extends Partial<Omit<Config, 'stubs' | 'cursorMeta' | 'paginatedMeta' | 'paginatedLinks' | 'responseStructure'>> {
|
|
242
263
|
/**
|
|
243
264
|
* @description The file names of the various stub templates used for generating resources and configuration.
|
|
@@ -337,6 +358,13 @@ declare class MakeResource extends Command<CliApp> {
|
|
|
337
358
|
}
|
|
338
359
|
//#endregion
|
|
339
360
|
//#region src/ServerResponse.d.ts
|
|
361
|
+
/**
|
|
362
|
+
* ServerResponse class to handle HTTP response construction and sending, compatible
|
|
363
|
+
* with both Express and H3 response objects.
|
|
364
|
+
*
|
|
365
|
+
* @author Legacy (3m1n3nc3)
|
|
366
|
+
* @since 0.1.0
|
|
367
|
+
*/
|
|
340
368
|
declare class ServerResponse<R extends NonCollectible | Collectible | ResourceData[] | ResourceData = any> {
|
|
341
369
|
#private;
|
|
342
370
|
private response;
|
|
@@ -412,32 +440,196 @@ declare class ServerResponse<R extends NonCollectible | Collectible | ResourceDa
|
|
|
412
440
|
finally(onfinally?: (() => void) | null): Promise<void>;
|
|
413
441
|
}
|
|
414
442
|
//#endregion
|
|
443
|
+
//#region src/BaseSerializer.d.ts
|
|
444
|
+
interface SerializerConstructor {
|
|
445
|
+
preferredCase?: CaseStyle;
|
|
446
|
+
responseStructure?: ResponseStructureConfig;
|
|
447
|
+
config?: () => ResourceLevelConfig;
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* @description BaseSerializer is an abstract class that provides common functionality for
|
|
451
|
+
* serializing resources. It handles configuration, metadata management, and response
|
|
452
|
+
* structure resolution. Concrete serializers should extend this class and implement
|
|
453
|
+
* the abstract methods to define how resources are transformed and how metadata is
|
|
454
|
+
* applied to the response body.
|
|
455
|
+
*
|
|
456
|
+
* @author Legacy (3m1n3nc3)
|
|
457
|
+
* @since 0.2.8
|
|
458
|
+
*/
|
|
459
|
+
declare abstract class BaseSerializer<TResource = any> {
|
|
460
|
+
static preferredCase?: CaseStyle;
|
|
461
|
+
static responseStructure?: ResponseStructureConfig;
|
|
462
|
+
static config?: () => ResourceLevelConfig;
|
|
463
|
+
protected instanceConfig?: ResourceLevelConfig;
|
|
464
|
+
protected additionalMeta?: MetaData;
|
|
465
|
+
protected called: {
|
|
466
|
+
json?: boolean;
|
|
467
|
+
data?: boolean;
|
|
468
|
+
toObject?: boolean;
|
|
469
|
+
toArray?: boolean;
|
|
470
|
+
additional?: boolean;
|
|
471
|
+
with?: boolean;
|
|
472
|
+
withResponse?: boolean;
|
|
473
|
+
status?: boolean;
|
|
474
|
+
then?: boolean;
|
|
475
|
+
toResponse?: boolean;
|
|
476
|
+
};
|
|
477
|
+
/**
|
|
478
|
+
* Helper method to conditionally resolve a value based on a condition.
|
|
479
|
+
*
|
|
480
|
+
* @param condition
|
|
481
|
+
* @param value
|
|
482
|
+
* @returns
|
|
483
|
+
*/
|
|
484
|
+
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
485
|
+
/**
|
|
486
|
+
* Helper method to conditionally resolve a value only if it's not null or undefined.
|
|
487
|
+
*
|
|
488
|
+
* @param value
|
|
489
|
+
* @returns
|
|
490
|
+
*/
|
|
491
|
+
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
492
|
+
/**
|
|
493
|
+
* Helper method to conditionally merge values into the response based on a condition.
|
|
494
|
+
*
|
|
495
|
+
* @param condition
|
|
496
|
+
* @param value
|
|
497
|
+
* @returns
|
|
498
|
+
*/
|
|
499
|
+
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
500
|
+
/**
|
|
501
|
+
* Resolve the current root key for the response structure.
|
|
502
|
+
*/
|
|
503
|
+
protected abstract resolveCurrentRootKey(): string;
|
|
504
|
+
protected abstract applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
505
|
+
protected abstract getResourceForMeta(): TResource;
|
|
506
|
+
/**
|
|
507
|
+
* Add additional metadata to the response. If called without arguments.
|
|
508
|
+
*
|
|
509
|
+
* @param meta
|
|
510
|
+
* @returns
|
|
511
|
+
*/
|
|
512
|
+
with(meta?: any): any;
|
|
513
|
+
/**
|
|
514
|
+
* Add additional metadata to the response, ensuring it is merged with any existing metadata.
|
|
515
|
+
*
|
|
516
|
+
* @param meta
|
|
517
|
+
* @returns
|
|
518
|
+
*/
|
|
519
|
+
withMeta<M extends MetaData>(meta: M | ((resource: TResource) => M)): this;
|
|
520
|
+
/**
|
|
521
|
+
* Resolve the merged metadata for the current response, combining any metadata
|
|
522
|
+
* defined in hooks with additional metadata added via with() or withMeta().
|
|
523
|
+
*
|
|
524
|
+
* @param withMethod
|
|
525
|
+
* @returns
|
|
526
|
+
*/
|
|
527
|
+
protected resolveMergedMeta(withMethod: (meta?: any) => any): Record<string, any> | undefined;
|
|
528
|
+
/**
|
|
529
|
+
* Run the response generation process, ensuring that the response is properly structured.
|
|
530
|
+
*
|
|
531
|
+
* @param input
|
|
532
|
+
* @returns
|
|
533
|
+
*/
|
|
534
|
+
protected runResponse<TBody, TRawResponse, TServerResponse>(input: {
|
|
535
|
+
ensureJson: () => void;
|
|
536
|
+
rawResponse: TRawResponse;
|
|
537
|
+
body: () => TBody;
|
|
538
|
+
createServerResponse: (raw: TRawResponse, body: TBody) => TServerResponse;
|
|
539
|
+
callWithResponse: (response: TServerResponse, raw: TRawResponse) => void;
|
|
540
|
+
}): TServerResponse;
|
|
541
|
+
/**
|
|
542
|
+
* Run the thenable process for the resource, allowing for async handling and
|
|
543
|
+
* response generation.
|
|
544
|
+
*
|
|
545
|
+
* @param input
|
|
546
|
+
* @returns
|
|
547
|
+
*/
|
|
548
|
+
protected runThen<TBody, TRawResponse, TServerResponse, TResult1, TResult2>(input: {
|
|
549
|
+
ensureJson: () => void;
|
|
550
|
+
body: () => TBody;
|
|
551
|
+
rawResponse?: TRawResponse;
|
|
552
|
+
createServerResponse: (raw: TRawResponse, body: TBody) => TServerResponse;
|
|
553
|
+
callWithResponse: (response?: TServerResponse, raw?: TRawResponse) => void;
|
|
554
|
+
sendRawResponse?: (raw: TRawResponse, body: TBody) => void;
|
|
555
|
+
onfulfilled?: ((value: TBody) => TResult1 | PromiseLike<TResult1>) | null;
|
|
556
|
+
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null;
|
|
557
|
+
}): Promise<TResult1 | TResult2>;
|
|
558
|
+
/**
|
|
559
|
+
* Get or set the resource-level configuration for this serializer instance.
|
|
560
|
+
*/
|
|
561
|
+
config(): ResourceLevelConfig;
|
|
562
|
+
/**
|
|
563
|
+
* Set the resource-level configuration for this serializer instance.
|
|
564
|
+
*
|
|
565
|
+
* @param config The configuration object to set for this serializer instance.
|
|
566
|
+
*/
|
|
567
|
+
config(config: ResourceLevelConfig): this;
|
|
568
|
+
/**
|
|
569
|
+
* Resolve the effective serializer configuration for this instance, combining
|
|
570
|
+
* class-level and instance-level configurations.
|
|
571
|
+
*
|
|
572
|
+
* @param localConstructor
|
|
573
|
+
* @param _fallbackConfig
|
|
574
|
+
* @returns
|
|
575
|
+
*/
|
|
576
|
+
protected resolveSerializerConfig(localConstructor: SerializerConstructor, _fallbackConfig?: ResourceLevelConfig): {
|
|
577
|
+
preferredCase: CaseStyle | undefined;
|
|
578
|
+
responseStructure: {
|
|
579
|
+
wrap?: boolean | undefined;
|
|
580
|
+
rootKey?: string | undefined;
|
|
581
|
+
factory?: ResponseFactory | undefined;
|
|
582
|
+
};
|
|
583
|
+
};
|
|
584
|
+
/**
|
|
585
|
+
* Resolve the preferred case style for this serializer instance, considering
|
|
586
|
+
* instance-level configuration, class-level configuration, and global defaults.
|
|
587
|
+
*
|
|
588
|
+
* @param localConstructor The constructor of the serializer class.
|
|
589
|
+
* @param fallbackConfig The fallback configuration to use if no other configuration is found.
|
|
590
|
+
* @returns The resolved case style for this serializer instance.
|
|
591
|
+
*/
|
|
592
|
+
protected resolveSerializerCaseStyle(localConstructor: SerializerConstructor, fallbackConfig?: ResourceLevelConfig): CaseStyle | undefined;
|
|
593
|
+
/**
|
|
594
|
+
* Resolve the response structure configuration for this serializer instance, considering
|
|
595
|
+
*
|
|
596
|
+
* @param localConstructor
|
|
597
|
+
* @param fallbackConfig
|
|
598
|
+
* @returns
|
|
599
|
+
*/
|
|
600
|
+
protected resolveSerializerResponseStructure(localConstructor: SerializerConstructor, fallbackConfig?: ResourceLevelConfig): {
|
|
601
|
+
wrap: boolean;
|
|
602
|
+
rootKey: string;
|
|
603
|
+
factory: ResponseFactory | undefined;
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
//#endregion
|
|
415
607
|
//#region src/ResourceCollection.d.ts
|
|
416
608
|
/**
|
|
417
|
-
* ResourceCollection class to handle API resource transformation and response building
|
|
609
|
+
* ResourceCollection class to handle API resource transformation and response building
|
|
610
|
+
* for collections
|
|
611
|
+
*
|
|
612
|
+
* @author Legacy (3m1n3nc3)
|
|
613
|
+
* @since 0.1.0
|
|
614
|
+
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
418
615
|
*/
|
|
419
|
-
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> {
|
|
616
|
+
declare class ResourceCollection<R extends ResourceData[] | Collectible | CollectionLike | PaginatorLike = ResourceData[] | Collectible | CollectionLike | PaginatorLike, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
420
617
|
private res?;
|
|
421
618
|
[key: string]: any;
|
|
422
619
|
private body;
|
|
423
620
|
resource: R;
|
|
424
621
|
collects?: typeof Resource<T>;
|
|
425
|
-
private additionalMeta?;
|
|
426
622
|
protected withResponseContext?: {
|
|
427
623
|
response: ServerResponse<CollectionBody<R>>;
|
|
428
624
|
raw: Response | H3Event['res'];
|
|
429
625
|
};
|
|
430
|
-
private isPaginatedCollectible;
|
|
431
|
-
/**
|
|
432
|
-
* Preferred case style for this collection's output keys.
|
|
433
|
-
* Set on a subclass to override the global default.
|
|
434
|
-
*/
|
|
435
|
-
static preferredCase?: CaseStyle;
|
|
436
626
|
/**
|
|
437
|
-
*
|
|
627
|
+
* Type guard to determine if the provided value is a Collectible with pagination information.
|
|
628
|
+
*
|
|
629
|
+
* @param value The value to check.
|
|
630
|
+
* @returns True if the value is a Collectible with pagination information, false otherwise.
|
|
438
631
|
*/
|
|
439
|
-
|
|
440
|
-
private called;
|
|
632
|
+
isPaginatedCollectible(value: unknown): value is Collectible;
|
|
441
633
|
constructor(rsc: R);
|
|
442
634
|
constructor(rsc: R, res: Response);
|
|
443
635
|
/**
|
|
@@ -452,44 +644,54 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
452
644
|
* Replace the current serialized output body.
|
|
453
645
|
*/
|
|
454
646
|
protected setBody(body: CollectionBody<R>): this;
|
|
647
|
+
private resolveCollectsConfig;
|
|
648
|
+
private resolveResponseStructure;
|
|
455
649
|
/**
|
|
456
|
-
*
|
|
650
|
+
* Resolve the current root key for the response structure, based on configuration and defaults.
|
|
651
|
+
*
|
|
652
|
+
* @returns
|
|
457
653
|
*/
|
|
458
|
-
|
|
654
|
+
protected resolveCurrentRootKey(): string;
|
|
459
655
|
/**
|
|
460
|
-
*
|
|
656
|
+
* Apply metadata properties to the response body, ensuring they are merged with
|
|
657
|
+
* any existing properties and respecting the configured root key.
|
|
658
|
+
*
|
|
659
|
+
* @param meta
|
|
660
|
+
* @param rootKey
|
|
461
661
|
*/
|
|
462
|
-
|
|
662
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
463
663
|
/**
|
|
464
|
-
*
|
|
664
|
+
* Get the resource data to be used for generating metadata, allowing for
|
|
665
|
+
* customization in subclasses.
|
|
666
|
+
*
|
|
667
|
+
* @returns
|
|
465
668
|
*/
|
|
466
|
-
|
|
467
|
-
private resolveResponseStructure;
|
|
468
|
-
private getPayloadKey;
|
|
669
|
+
protected getResourceForMeta(): R;
|
|
469
670
|
/**
|
|
470
|
-
*
|
|
671
|
+
* Get the appropriate key for the response payload based on the current response
|
|
672
|
+
* structure configuration.
|
|
471
673
|
*
|
|
472
|
-
* @returns
|
|
674
|
+
* @returns The key to use for the response payload, or undefined if no key is needed.
|
|
473
675
|
*/
|
|
474
|
-
|
|
676
|
+
private getPayloadKey;
|
|
475
677
|
/**
|
|
476
|
-
*
|
|
678
|
+
* Convert resource to JSON response format
|
|
477
679
|
*
|
|
478
|
-
* @param meta Metadata object or metadata factory
|
|
479
680
|
* @returns
|
|
480
681
|
*/
|
|
481
|
-
|
|
682
|
+
json(): this;
|
|
482
683
|
/**
|
|
483
|
-
*
|
|
684
|
+
* Convert resource to object format and return original data.
|
|
484
685
|
*
|
|
485
|
-
* @param meta Metadata object or metadata factory
|
|
486
686
|
* @returns
|
|
487
687
|
*/
|
|
488
|
-
|
|
688
|
+
toObject(): (R extends Collectible ? R['data'][number] : R extends PaginatorLike<infer TPaginatorData> ? TPaginatorData : R extends CollectionLike<infer TCollectionData> ? TCollectionData : R extends ResourceData[] ? R[number] : never)[];
|
|
489
689
|
/**
|
|
490
|
-
*
|
|
690
|
+
* Convert resource to object format and return original data.
|
|
491
691
|
*
|
|
492
|
-
* @
|
|
692
|
+
* @deprecated Use toObject() instead.
|
|
693
|
+
* @alias toArray
|
|
694
|
+
* @since 0.2.9
|
|
493
695
|
*/
|
|
494
696
|
toArray(): (R extends Collectible ? R['data'][number] : R extends PaginatorLike<infer TPaginatorData> ? TPaginatorData : R extends CollectionLike<infer TCollectionData> ? TCollectionData : R extends ResourceData[] ? R[number] : never)[];
|
|
495
697
|
/**
|
|
@@ -529,33 +731,27 @@ declare class ResourceCollection<R extends ResourceData[] | Collectible | Collec
|
|
|
529
731
|
* @param onfinally
|
|
530
732
|
* @returns
|
|
531
733
|
*/
|
|
532
|
-
finally(onfinally?: (() => void) | null): Promise<
|
|
734
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
533
735
|
}
|
|
534
736
|
//#endregion
|
|
535
737
|
//#region src/Resource.d.ts
|
|
536
738
|
/**
|
|
537
739
|
* Resource class to handle API resource transformation and response building
|
|
740
|
+
*
|
|
741
|
+
*
|
|
742
|
+
* @author Legacy (3m1n3nc3)
|
|
743
|
+
* @since 0.1.0
|
|
744
|
+
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
538
745
|
*/
|
|
539
|
-
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
746
|
+
declare class Resource<R extends ResourceData | NonCollectible = ResourceData> extends BaseSerializer<R> {
|
|
540
747
|
private res?;
|
|
541
748
|
[key: string]: any;
|
|
542
749
|
private body;
|
|
543
750
|
resource: R;
|
|
544
|
-
private additionalMeta?;
|
|
545
751
|
protected withResponseContext?: {
|
|
546
752
|
response: ServerResponse<ResourceBody<R>>;
|
|
547
753
|
raw: Response | H3Event['res'];
|
|
548
754
|
};
|
|
549
|
-
/**
|
|
550
|
-
* Preferred case style for this resource's output keys.
|
|
551
|
-
* Set on a subclass to override the global default.
|
|
552
|
-
*/
|
|
553
|
-
static preferredCase?: CaseStyle;
|
|
554
|
-
/**
|
|
555
|
-
* Response structure override for this resource class.
|
|
556
|
-
*/
|
|
557
|
-
static responseStructure?: ResponseStructureConfig;
|
|
558
|
-
private called;
|
|
559
755
|
constructor(rsc: R, res?: Response | undefined);
|
|
560
756
|
/**
|
|
561
757
|
* Create a ResourceCollection from an array of resource data or a Collectible instance
|
|
@@ -576,44 +772,34 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
576
772
|
* Replace the current serialized output body.
|
|
577
773
|
*/
|
|
578
774
|
protected setBody(body: ResourceBody<R>): this;
|
|
579
|
-
/**
|
|
580
|
-
* Conditionally include a value in serialized output.
|
|
581
|
-
*/
|
|
582
|
-
when<T>(condition: any, value: T | (() => T)): T | undefined;
|
|
583
|
-
/**
|
|
584
|
-
* Include a value only when it is not null/undefined.
|
|
585
|
-
*/
|
|
586
|
-
whenNotNull<T>(value: T | null | undefined): T | undefined;
|
|
587
|
-
/**
|
|
588
|
-
* Conditionally merge object attributes into serialized output.
|
|
589
|
-
*/
|
|
590
|
-
mergeWhen<T extends Record<string, any>>(condition: any, value: T | (() => T)): Partial<T>;
|
|
591
775
|
private resolveResponseStructure;
|
|
592
|
-
private getPayloadKey;
|
|
593
776
|
/**
|
|
594
|
-
*
|
|
777
|
+
* Resolve the current root key for the response body based on configuration and defaults.
|
|
595
778
|
*
|
|
596
779
|
* @returns
|
|
597
780
|
*/
|
|
598
|
-
|
|
781
|
+
protected resolveCurrentRootKey(): string;
|
|
782
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
783
|
+
protected getResourceForMeta(): R;
|
|
784
|
+
private getPayloadKey;
|
|
599
785
|
/**
|
|
600
|
-
*
|
|
786
|
+
* Convert resource to JSON response format
|
|
601
787
|
*
|
|
602
|
-
* @param meta Metadata object or metadata factory
|
|
603
788
|
* @returns
|
|
604
789
|
*/
|
|
605
|
-
|
|
790
|
+
json(): this;
|
|
606
791
|
/**
|
|
607
|
-
*
|
|
792
|
+
* Convert resource to object format (for collections) or return original data for single resources.
|
|
608
793
|
*
|
|
609
|
-
* @param meta Metadata object or metadata factory
|
|
610
794
|
* @returns
|
|
611
795
|
*/
|
|
612
|
-
|
|
796
|
+
toObject(): R extends NonCollectible ? R['data'] : R;
|
|
613
797
|
/**
|
|
614
|
-
*
|
|
798
|
+
* Convert resource to object format and return original data.
|
|
615
799
|
*
|
|
616
|
-
* @
|
|
800
|
+
* @deprecated Use toObject() instead.
|
|
801
|
+
* @alias toArray
|
|
802
|
+
* @since 0.2.9
|
|
617
803
|
*/
|
|
618
804
|
toArray(): R extends NonCollectible ? R['data'] : R;
|
|
619
805
|
/**
|
|
@@ -623,7 +809,14 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
623
809
|
* @returns
|
|
624
810
|
*/
|
|
625
811
|
additional<X extends Record<string, any>>(extra: X): this;
|
|
812
|
+
/**
|
|
813
|
+
* Build a response object, optionally accepting a raw response to mutate in withResponse.
|
|
814
|
+
*/
|
|
626
815
|
response(): ServerResponse<ResourceBody<R>>;
|
|
816
|
+
/**
|
|
817
|
+
* Build a response object, optionally accepting a raw response to mutate in withResponse.
|
|
818
|
+
* @param res Optional raw response object (e.g. Express Response or H3Event res)
|
|
819
|
+
*/
|
|
627
820
|
response(res: H3Event['res']): ServerResponse<ResourceBody<R>>;
|
|
628
821
|
/**
|
|
629
822
|
* Customize the outgoing transport response right before dispatch.
|
|
@@ -652,34 +845,27 @@ declare class Resource<R extends ResourceData | NonCollectible = ResourceData> {
|
|
|
652
845
|
* @param onfinally
|
|
653
846
|
* @returns
|
|
654
847
|
*/
|
|
655
|
-
finally(onfinally?: (() => void) | null): Promise<
|
|
848
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
656
849
|
}
|
|
657
850
|
//#endregion
|
|
658
851
|
//#region src/GenericResource.d.ts
|
|
659
852
|
/**
|
|
660
853
|
* GenericResource class to handle API resource transformation and response building
|
|
854
|
+
*
|
|
855
|
+
* @author Legacy (3m1n3nc3)
|
|
856
|
+
* @since 0.1.0
|
|
857
|
+
* @see BaseSerializer for shared serialization logic and configuration handling
|
|
661
858
|
*/
|
|
662
|
-
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> {
|
|
859
|
+
declare class GenericResource<R extends NonCollectible | Collectible | CollectionLike | PaginatorLike | ResourceData = ResourceData, T extends ResourceData = any> extends BaseSerializer<R> {
|
|
663
860
|
private res?;
|
|
664
861
|
[key: string]: any;
|
|
665
862
|
private body;
|
|
666
863
|
resource: R;
|
|
667
864
|
collects?: typeof Resource<T>;
|
|
668
|
-
private additionalMeta?;
|
|
669
865
|
protected withResponseContext?: {
|
|
670
866
|
response: ServerResponse<GenericBody<R>>;
|
|
671
867
|
raw: Response | H3Event['res'];
|
|
672
868
|
};
|
|
673
|
-
/**
|
|
674
|
-
* Preferred case style for this resource's output keys.
|
|
675
|
-
* Set on a subclass to override the global default.
|
|
676
|
-
*/
|
|
677
|
-
static preferredCase?: CaseStyle;
|
|
678
|
-
/**
|
|
679
|
-
* Response structure override for this generic resource class.
|
|
680
|
-
*/
|
|
681
|
-
static responseStructure?: ResponseStructureConfig;
|
|
682
|
-
private called;
|
|
683
869
|
constructor(rsc: R, res?: Response | undefined);
|
|
684
870
|
/**
|
|
685
871
|
* Get the original resource data
|
|
@@ -693,19 +879,27 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
693
879
|
* Replace the current serialized output body.
|
|
694
880
|
*/
|
|
695
881
|
protected setBody(body: GenericBody<R>): this;
|
|
882
|
+
private resolveCollectsConfig;
|
|
883
|
+
private resolveResponseStructure;
|
|
696
884
|
/**
|
|
697
|
-
*
|
|
885
|
+
* Resolve the current root key for the response structure, based on configuration and defaults.
|
|
886
|
+
*
|
|
887
|
+
* @returns
|
|
698
888
|
*/
|
|
699
|
-
|
|
889
|
+
protected resolveCurrentRootKey(): string;
|
|
700
890
|
/**
|
|
701
|
-
*
|
|
891
|
+
* Apply metadata properties to the response body, ensuring they are merged with.
|
|
892
|
+
*
|
|
893
|
+
* @param meta
|
|
894
|
+
* @param rootKey
|
|
702
895
|
*/
|
|
703
|
-
|
|
896
|
+
protected applyMetaToBody(meta: MetaData, rootKey: string): void;
|
|
704
897
|
/**
|
|
705
|
-
*
|
|
898
|
+
* Get the resource data to be used for generating metadata.
|
|
899
|
+
*
|
|
900
|
+
* @returns
|
|
706
901
|
*/
|
|
707
|
-
|
|
708
|
-
private resolveResponseStructure;
|
|
902
|
+
protected getResourceForMeta(): R;
|
|
709
903
|
private getPayloadKey;
|
|
710
904
|
/**
|
|
711
905
|
* Convert resource to JSON response format
|
|
@@ -714,23 +908,17 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
714
908
|
*/
|
|
715
909
|
json(): this;
|
|
716
910
|
/**
|
|
717
|
-
*
|
|
718
|
-
*
|
|
719
|
-
* @param meta Metadata object or metadata factory
|
|
720
|
-
* @returns
|
|
721
|
-
*/
|
|
722
|
-
with(meta?: any): any;
|
|
723
|
-
/**
|
|
724
|
-
* Typed fluent metadata helper.
|
|
911
|
+
* Convert resource to object format (for collections).
|
|
725
912
|
*
|
|
726
|
-
* @param meta Metadata object or metadata factory
|
|
727
913
|
* @returns
|
|
728
914
|
*/
|
|
729
|
-
|
|
915
|
+
toObject(): any;
|
|
730
916
|
/**
|
|
731
|
-
* Convert resource to
|
|
917
|
+
* Convert resource to object format and return original data.
|
|
732
918
|
*
|
|
733
|
-
* @
|
|
919
|
+
* @deprecated Use toObject() instead.
|
|
920
|
+
* @alias toArray
|
|
921
|
+
* @since 0.2.9
|
|
734
922
|
*/
|
|
735
923
|
toArray(): any;
|
|
736
924
|
/**
|
|
@@ -740,7 +928,15 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
740
928
|
* @returns
|
|
741
929
|
*/
|
|
742
930
|
additional<X extends Record<string, any>>(extra: X): this;
|
|
931
|
+
/**
|
|
932
|
+
* Build a response object, optionally accepting a raw response to write to directly.
|
|
933
|
+
*/
|
|
743
934
|
response(): ServerResponse<GenericBody<R>>;
|
|
935
|
+
/**
|
|
936
|
+
* Build a response object, writing to the provided raw response if possible.
|
|
937
|
+
*
|
|
938
|
+
* @param res
|
|
939
|
+
*/
|
|
744
940
|
response(res: H3Event['res']): ServerResponse<GenericBody<R>>;
|
|
745
941
|
/**
|
|
746
942
|
* Customize the outgoing transport response right before dispatch.
|
|
@@ -756,6 +952,20 @@ declare class GenericResource<R extends NonCollectible | Collectible | Collectio
|
|
|
756
952
|
* @returns A promise that resolves to the result of the onfulfilled or onrejected callback
|
|
757
953
|
*/
|
|
758
954
|
then<TResult1 = GenericBody<R>, TResult2 = never>(onfulfilled?: ((value: GenericBody<R>) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
955
|
+
/**
|
|
956
|
+
* Promise-like catch method to handle rejected state of the promise
|
|
957
|
+
*
|
|
958
|
+
* @param onrejected
|
|
959
|
+
* @returns
|
|
960
|
+
*/
|
|
961
|
+
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<GenericBody<R> | TResult>;
|
|
962
|
+
/**
|
|
963
|
+
* Promise-like finally method to handle cleanup after promise is settled
|
|
964
|
+
*
|
|
965
|
+
* @param onfinally
|
|
966
|
+
* @returns
|
|
967
|
+
*/
|
|
968
|
+
finally(onfinally?: (() => void) | null): Promise<unknown>;
|
|
759
969
|
}
|
|
760
970
|
//#endregion
|
|
761
971
|
//#region src/utilities/case.d.ts
|
|
@@ -847,6 +1057,11 @@ declare const resolveMergeWhen: <T extends Record<string, any>>(condition: any,
|
|
|
847
1057
|
declare const sanitizeConditionalAttributes: (value: any) => any;
|
|
848
1058
|
//#endregion
|
|
849
1059
|
//#region src/utilities/config.d.ts
|
|
1060
|
+
/**
|
|
1061
|
+
* Get the default configuration for the application
|
|
1062
|
+
*
|
|
1063
|
+
* @returns
|
|
1064
|
+
*/
|
|
850
1065
|
declare const getDefaultConfig: () => Config;
|
|
851
1066
|
/**
|
|
852
1067
|
* Defines the configuration for the application by merging the provided configuration with the default configuration. This function takes a partial configuration object as input and returns a complete configuration object that includes all required properties, using default values for any properties that are not specified in the input.
|
|
@@ -866,8 +1081,29 @@ type ArkormLikeModel = {
|
|
|
866
1081
|
type ArkormLikeCollection = {
|
|
867
1082
|
all: () => unknown;
|
|
868
1083
|
};
|
|
1084
|
+
/**
|
|
1085
|
+
* Type guard to check if a value is an Arkorm-like model, which is defined as an object
|
|
1086
|
+
* that has a toObject method and optionally getRawAttributes, getAttribute, and
|
|
1087
|
+
* setAttribute methods.
|
|
1088
|
+
*
|
|
1089
|
+
* @param value The value to check
|
|
1090
|
+
* @returns True if the value is an Arkorm-like model, false otherwise
|
|
1091
|
+
*/
|
|
869
1092
|
declare const isArkormLikeModel: (value: unknown) => value is ArkormLikeModel;
|
|
1093
|
+
/**
|
|
1094
|
+
* Type guard to check if a value is an Arkorm-like collection, which is defined as an object
|
|
1095
|
+
*
|
|
1096
|
+
* @param value
|
|
1097
|
+
* @returns
|
|
1098
|
+
*/
|
|
870
1099
|
declare const isArkormLikeCollection: (value: unknown) => value is ArkormLikeCollection;
|
|
1100
|
+
/**
|
|
1101
|
+
* Normalize a value for serialization by recursively converting Arkorm-like models and
|
|
1102
|
+
* collections to plain objects, while preserving the structure of arrays and plain objects.
|
|
1103
|
+
*
|
|
1104
|
+
* @param value The value to normalize
|
|
1105
|
+
* @returns The normalized value, ready for serialization
|
|
1106
|
+
*/
|
|
871
1107
|
declare const normalizeSerializableData: (value: unknown) => unknown;
|
|
872
1108
|
//#endregion
|
|
873
1109
|
//#region src/utilities/metadata.d.ts
|
|
@@ -1126,4 +1362,4 @@ declare const setGlobalCursorMeta: (meta: Config["cursorMeta"]) => void;
|
|
|
1126
1362
|
*/
|
|
1127
1363
|
declare const getGlobalCursorMeta: () => Config["cursorMeta"];
|
|
1128
1364
|
//#endregion
|
|
1129
|
-
export { ApiResource, CONDITIONAL_ATTRIBUTE_MISSING, CaseStyle, CliApp, Collectible, CollectionBody, CollectionLike, Config, Cursor, GenericBody, GenericResource, InitCommand, MakeResource, MetaData, NonCollectible, PaginatedMetaData, Pagination, PaginatorLike, ResoraConfig, Resource, ResourceBody, ResourceCollection, ResourceData, ResourceDef, ResponseData, ResponseDataCollection, ResponseFactory, ResponseFactoryContext, ResponseKind, ResponseStructureConfig, ServerResponse, appendRootProperties, applyRuntimeConfig, buildPaginationExtras, buildResponseEnvelope, defineConfig, getCaseTransformer, getDefaultConfig, getGlobalBaseUrl, getGlobalCase, getGlobalCursorMeta, getGlobalPageName, getGlobalPaginatedExtras, getGlobalPaginatedLinks, getGlobalPaginatedMeta, getGlobalResponseFactory, getGlobalResponseRootKey, getGlobalResponseStructure, getGlobalResponseWrap, getPaginationExtraKeys, isArkormLikeCollection, isArkormLikeModel, isPlainObject, loadRuntimeConfig, mergeMetadata, normalizeSerializableData, resetRuntimeConfigForTests, resolveMergeWhen, resolveWhen, resolveWhenNotNull, resolveWithHookMetadata, sanitizeConditionalAttributes, setGlobalBaseUrl, setGlobalCase, setGlobalCursorMeta, setGlobalPageName, setGlobalPaginatedExtras, setGlobalPaginatedLinks, setGlobalPaginatedMeta, setGlobalResponseFactory, setGlobalResponseRootKey, setGlobalResponseStructure, setGlobalResponseWrap, splitWords, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, transformKeys };
|
|
1365
|
+
export { ApiResource, CONDITIONAL_ATTRIBUTE_MISSING, CaseStyle, CliApp, Collectible, CollectionBody, CollectionLike, Config, Cursor, GenericBody, GenericResource, InitCommand, MakeResource, MetaData, NonCollectible, PaginatedMetaData, Pagination, PaginatorLike, ResoraConfig, Resource, ResourceBody, ResourceCollection, ResourceData, ResourceDef, ResourceLevelConfig, ResponseData, ResponseDataCollection, ResponseFactory, ResponseFactoryContext, ResponseKind, ResponseStructureConfig, ServerResponse, appendRootProperties, applyRuntimeConfig, buildPaginationExtras, buildResponseEnvelope, defineConfig, getCaseTransformer, getDefaultConfig, getGlobalBaseUrl, getGlobalCase, getGlobalCursorMeta, getGlobalPageName, getGlobalPaginatedExtras, getGlobalPaginatedLinks, getGlobalPaginatedMeta, getGlobalResponseFactory, getGlobalResponseRootKey, getGlobalResponseStructure, getGlobalResponseWrap, getPaginationExtraKeys, isArkormLikeCollection, isArkormLikeModel, isPlainObject, loadRuntimeConfig, mergeMetadata, normalizeSerializableData, resetRuntimeConfigForTests, resolveMergeWhen, resolveWhen, resolveWhenNotNull, resolveWithHookMetadata, sanitizeConditionalAttributes, setGlobalBaseUrl, setGlobalCase, setGlobalCursorMeta, setGlobalPageName, setGlobalPaginatedExtras, setGlobalPaginatedLinks, setGlobalPaginatedMeta, setGlobalResponseFactory, setGlobalResponseRootKey, setGlobalResponseStructure, setGlobalResponseWrap, splitWords, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, transformKeys };
|