mn-angular-lib 0.0.44 → 0.0.45

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mn-angular-lib",
3
- "version": "0.0.44",
3
+ "version": "0.0.45",
4
4
  "peerDependencies": {
5
5
  "@angular/common": "^21.1.3",
6
6
  "@angular/core": "^21.1.3"
@@ -8,7 +8,7 @@ import { Observable, BehaviorSubject } from 'rxjs';
8
8
  import * as mn_angular_lib from 'mn-angular-lib';
9
9
  import * as _angular_forms from '@angular/forms';
10
10
  import { ValidationErrors, NgControl, AbstractControl, FormGroup, FormBuilder } from '@angular/forms';
11
- import { HttpClient } from '@angular/common/http';
11
+ import { HttpClient, HttpStatusCode, HttpHeaders, HttpErrorResponse, HttpResponse, HttpParams } from '@angular/common/http';
12
12
 
13
13
  declare const mnAlertVariants: tailwind_variants.TVReturnType<{
14
14
  kind: {
@@ -2853,6 +2853,297 @@ declare class MnInstanceDirective {
2853
2853
  static ɵdir: i0.ɵɵDirectiveDeclaration<MnInstanceDirective, "[mn-instance]", never, { "mnInstance": { "alias": "mn-instance"; "required": false; }; }, {}, never, never, true, never>;
2854
2854
  }
2855
2855
 
2856
+ /**
2857
+ * A structured representation of an API error.
2858
+ *
2859
+ * Captures all relevant details — status, message, validation errors,
2860
+ * and retry information — so consumers can log, display, or act on
2861
+ * failures without inspecting the raw HTTP response.
2862
+ */
2863
+ interface ApiError {
2864
+ status: HttpStatusCode | null;
2865
+ message: string;
2866
+ details?: unknown;
2867
+ backendMessage?: string;
2868
+ validationErrors?: Record<string, string[]>;
2869
+ url?: string | null;
2870
+ headers?: HttpHeaders;
2871
+ original: HttpErrorResponse | Error;
2872
+ retryable: boolean;
2873
+ timestamp: string;
2874
+ }
2875
+ /**
2876
+ * Metadata associated with an API result.
2877
+ *
2878
+ * Attached to both `SuccessResult` and `FailureResult` to provide
2879
+ * transport-level details such as the HTTP status code, response
2880
+ * headers, and the final URL after any redirects.
2881
+ */
2882
+ interface ResultMeta {
2883
+ statusCode?: number;
2884
+ headers?: HttpHeaders;
2885
+ url?: string;
2886
+ }
2887
+ /**
2888
+ * Represents a successful API result containing the response data.
2889
+ *
2890
+ * Discriminated by `ok: true`. Use `result.ok` to narrow the union
2891
+ * before accessing `data`.
2892
+ *
2893
+ * @template T The type of the response data.
2894
+ */
2895
+ interface SuccessResult<T> {
2896
+ ok: true;
2897
+ data: T;
2898
+ meta?: ResultMeta;
2899
+ }
2900
+ /**
2901
+ * Represents a failed API result containing the structured error.
2902
+ *
2903
+ * Discriminated by `ok: false`. Use `result.ok` to narrow the union
2904
+ * before accessing `error`.
2905
+ */
2906
+ interface FailureResult {
2907
+ ok: false;
2908
+ error: ApiError;
2909
+ meta?: ResultMeta;
2910
+ }
2911
+ /**
2912
+ * A discriminated union representing either a successful or failed API result.
2913
+ *
2914
+ * Discriminated by `ok`. Use `result.ok` to narrow the type
2915
+ * before accessing `data` or `error`.
2916
+ *
2917
+ * @template T The type of the response data on success.
2918
+ */
2919
+ type Result<T> = SuccessResult<T> | FailureResult;
2920
+ /**
2921
+ * A JavaScript primitive value.
2922
+ *
2923
+ * Used as the building block for query parameter values.
2924
+ */
2925
+ type Primitive = string | number | boolean | null | undefined;
2926
+ /**
2927
+ * A value that can be used as a query parameter.
2928
+ *
2929
+ * Either a single primitive or an array of primitives.
2930
+ * Array values are appended as multiple entries for the same key.
2931
+ */
2932
+ type QueryValue = Primitive | Primitive[];
2933
+ /**
2934
+ * A record of query parameter key-value pairs.
2935
+ *
2936
+ * Passed to CRUD service methods and converted to `HttpParams`
2937
+ * before the request is sent. `null` and `undefined` values
2938
+ * are silently skipped during conversion.
2939
+ */
2940
+ type QueryParams = Record<string, QueryValue>;
2941
+
2942
+ /**
2943
+ * Configuration for a CRUD service endpoint.
2944
+ *
2945
+ * Passed to the `CrudService` constructor to define which API
2946
+ * resource the service operates on.
2947
+ */
2948
+ interface CrudConfig {
2949
+ endpoint: string;
2950
+ }
2951
+ /**
2952
+ * Abstract base class for CRUD services.
2953
+ * Provides standard HTTP operations with typed `Result<T>` responses.
2954
+ *
2955
+ * @template TEntity The entity type returned by single-item operations.
2956
+ * @template TListResponse The response type for list operations (defaults to `TEntity[]`).
2957
+ * @template TCreatePayload The payload type for create operations (defaults to `Partial<TEntity>`).
2958
+ * @template TUpdatePayload The payload type for update operations (defaults to `Partial<TEntity>`).
2959
+ * @template TId The type of the entity identifier (defaults to `number`).
2960
+ * @template TGetByIdResponse The response type for getById (defaults to `TEntity`).
2961
+ * @template TCreateResponse The response type for create (defaults to `TEntity`).
2962
+ * @template TUpdateResponse The response type for update and patch (defaults to `TEntity`).
2963
+ * @template TDeleteResponse The response type for delete (defaults to `void`).
2964
+ */
2965
+ declare abstract class CrudService<TEntity, TListResponse = TEntity[], TCreatePayload = Partial<TEntity>, TUpdatePayload = Partial<TEntity>, TId extends string | number = number, TGetByIdResponse = TEntity, TCreateResponse = TEntity, TUpdateResponse = TEntity, TDeleteResponse = void> {
2966
+ protected readonly http: HttpClient;
2967
+ protected readonly baseUrl: string;
2968
+ protected readonly endpoint: string;
2969
+ protected constructor(config: CrudConfig);
2970
+ /**
2971
+ * Retrieves all entities from the configured endpoint.
2972
+ *
2973
+ * Sends a GET request to the base endpoint. Query values are
2974
+ * converted to `HttpParams` before the request is sent.
2975
+ *
2976
+ * @param query Optional query parameters appended to the request URL.
2977
+ * @returns An observable emitting a `Result` with the list response or a structured failure.
2978
+ */
2979
+ getAll(query?: QueryParams): Observable<Result<TListResponse>>;
2980
+ /**
2981
+ * Retrieves a single entity by its identifier.
2982
+ *
2983
+ * Sends a GET request to `{endpoint}/{id}`.
2984
+ *
2985
+ * @param id The unique identifier of the entity to retrieve.
2986
+ * @returns An observable emitting a `Result` with the entity or a structured failure.
2987
+ */
2988
+ getById(id: TId): Observable<Result<TGetByIdResponse>>;
2989
+ /**
2990
+ * Creates a new entity at the configured endpoint.
2991
+ *
2992
+ * Sends a POST request with the provided payload as the request body.
2993
+ *
2994
+ * @param payload The data used to create the entity.
2995
+ * @returns An observable emitting a `Result` with the created entity or a structured failure.
2996
+ */
2997
+ create(payload: TCreatePayload): Observable<Result<TCreateResponse>>;
2998
+ /**
2999
+ * Fully replaces an existing entity.
3000
+ *
3001
+ * Sends a PUT request to `{endpoint}/{id}` with the provided payload,
3002
+ * replacing the entire entity.
3003
+ *
3004
+ * @param id The unique identifier of the entity to update.
3005
+ * @param payload The complete data to replace the existing entity with.
3006
+ * @returns An observable emitting a `Result` with the updated entity or a structured failure.
3007
+ */
3008
+ update(id: TId, payload: TUpdatePayload): Observable<Result<TUpdateResponse>>;
3009
+ /**
3010
+ * Partially updates an existing entity.
3011
+ *
3012
+ * Sends a PATCH request to `{endpoint}/{id}` with the provided payload,
3013
+ * merging changes into the existing entity.
3014
+ *
3015
+ * @param id The unique identifier of the entity to patch.
3016
+ * @param payload A partial set of fields to update on the existing entity.
3017
+ * @returns An observable emitting a `Result` with the updated entity or a structured failure.
3018
+ */
3019
+ patch(id: TId, payload: Partial<TUpdatePayload>): Observable<Result<TUpdateResponse>>;
3020
+ /**
3021
+ * Deletes an entity by its identifier.
3022
+ *
3023
+ * Sends a DELETE request to `{endpoint}/{id}`.
3024
+ *
3025
+ * @param id The unique identifier of the entity to delete.
3026
+ * @returns An observable emitting a `Result` with the delete response or a structured failure.
3027
+ */
3028
+ delete(id: TId): Observable<Result<TDeleteResponse>>;
3029
+ /**
3030
+ * Retrieves all entities with the full `HttpResponse` wrapper.
3031
+ *
3032
+ * Behaves like {@link getAll} but observes the complete HTTP response,
3033
+ * giving access to headers, status code, and URL alongside the body.
3034
+ *
3035
+ * @param query Optional query parameters appended to the request URL.
3036
+ * @returns An observable emitting a `Result` with the full HTTP response or a structured failure.
3037
+ */
3038
+ getAllResponse(query?: QueryParams): Observable<Result<HttpResponse<TListResponse>>>;
3039
+ /**
3040
+ * Builds the URL for a single entity by appending the identifier to the endpoint.
3041
+ *
3042
+ * @param id The unique identifier to append.
3043
+ * @returns The full URL targeting the specific entity.
3044
+ */
3045
+ protected itemUrl(id: TId): string;
3046
+ /**
3047
+ * Wraps a value in a `SuccessResult`.
3048
+ *
3049
+ * @template T The type of the response data.
3050
+ * @param data The response data to wrap.
3051
+ * @param meta Optional metadata (status code, headers, URL) to attach.
3052
+ * @returns A `SuccessResult` containing the provided data.
3053
+ */
3054
+ protected success<T>(data: T, meta?: ResultMeta): SuccessResult<T>;
3055
+ /**
3056
+ * Wraps an error in a `FailureResult`.
3057
+ *
3058
+ * When no explicit metadata is provided, metadata is derived from
3059
+ * the `ApiError` itself (status code, headers, URL).
3060
+ *
3061
+ * @param error The structured API error.
3062
+ * @param meta Optional metadata to override the error-derived values.
3063
+ * @returns A `FailureResult` containing the error and metadata.
3064
+ */
3065
+ protected failure(error: ApiError, meta?: ResultMeta): FailureResult;
3066
+ /**
3067
+ * Maps an unknown error into a structured `ApiError`.
3068
+ *
3069
+ * Handles both `HttpErrorResponse` instances and unexpected error types.
3070
+ * Extracts backend messages, validation errors, and retry information
3071
+ * so callers receive a consistent error shape.
3072
+ *
3073
+ * @param error The raw error caught from the HTTP pipeline.
3074
+ * @returns A fully populated `ApiError` object.
3075
+ */
3076
+ protected mapHttpError(error: unknown): ApiError;
3077
+ /**
3078
+ * Extracts a human-readable message from the error response body.
3079
+ *
3080
+ * Checks common keys (`message`, `title`, `detail`, `error`) on the
3081
+ * body object and returns the first non-empty string found.
3082
+ *
3083
+ * @param body The parsed error response body.
3084
+ * @returns The extracted message, or `undefined` if none was found.
3085
+ */
3086
+ protected extractBackendMessage(body: unknown): string | undefined;
3087
+ /**
3088
+ * Extracts field-level validation errors from the error response body.
3089
+ *
3090
+ * Expects an `errors` property on the body containing a record of
3091
+ * field names to error messages (string or string array).
3092
+ *
3093
+ * @param body The parsed error response body.
3094
+ * @returns A record mapping field names to their error messages, or `undefined` if none were found.
3095
+ */
3096
+ protected extractValidationErrors(body: unknown): Record<string, string[]> | undefined;
3097
+ /**
3098
+ * Returns a default user-facing message for the given HTTP status code.
3099
+ *
3100
+ * Provides human-readable messages for common HTTP status codes.
3101
+ * Override this method to customise messages.
3102
+ *
3103
+ * @param status The HTTP status code, or `null` when unknown.
3104
+ * @returns A descriptive error message.
3105
+ */
3106
+ protected defaultMessage(status: HttpStatusCode | null): string;
3107
+ /**
3108
+ * Determines whether a request with the given status can be retried.
3109
+ *
3110
+ * Timeouts, rate-limiting responses, and server errors
3111
+ * (5xx) are considered retryable by default.
3112
+ *
3113
+ * @param status The HTTP status code, or `null` when unknown.
3114
+ * @returns `true` if the request is safe to retry.
3115
+ */
3116
+ protected isRetryable(status: HttpStatusCode | null): boolean;
3117
+ /**
3118
+ * Normalises a raw HTTP status into an `HttpStatusCode | null` value.
3119
+ *
3120
+ * Converts `undefined`, `NaN`, and `0` (network error) to `null`
3121
+ * so downstream code only needs to handle `HttpStatusCode | null`.
3122
+ *
3123
+ * @param status The raw status value from the HTTP response.
3124
+ * @returns The normalised status code, or `null` when indeterminate.
3125
+ */
3126
+ protected normalizeStatus(status: number | null | undefined): HttpStatusCode | null;
3127
+ /**
3128
+ * Converts query parameters into Angular `HttpParams`.
3129
+ *
3130
+ * `null` and `undefined` values are silently skipped.
3131
+ * Array values are appended as multiple entries for the same key.
3132
+ *
3133
+ * @param query The query parameter record to convert.
3134
+ * @returns An `HttpParams` instance, or `undefined` when no parameters are provided.
3135
+ */
3136
+ protected toHttpParams(query?: QueryParams): HttpParams | undefined;
3137
+ }
3138
+
3139
+ /**
3140
+ * Injection token for the base URL used by all CRUD service requests.
3141
+ *
3142
+ * Provide this token at the application or module level to configure
3143
+ * the root API URL that `CrudService` prepends to every endpoint.
3144
+ */
3145
+ declare const API_BASE_URL: InjectionToken<string>;
3146
+
2856
3147
  /**
2857
3148
  * A marker object used in config values to indicate that the value
2858
3149
  * should be resolved via the MnLanguageService.
@@ -2966,5 +3257,5 @@ declare class MnTranslatePipe implements PipeTransform {
2966
3257
  static ɵpipe: i0.ɵɵPipeDeclaration<MnTranslatePipe, "mnTranslate", true>;
2967
3258
  }
2968
3259
 
2969
- export { ActionStyle, BackdropMode, BaseModalBuilder, CloseMode, ColumnSortType, ConfirmationModalBuilder, ConfirmationTone, CustomModalBuilder, DEFAULT_MN_ALERT_CONFIG, FieldAppearance, FieldKind, FormLayoutMode, FormModalBuilder, KeyboardMode, MN_ALERT_CONFIG, MN_CHECKBOX_CONFIG, MN_DATETIME_CONFIG, MN_INPUT_FIELD_CONFIG, MN_INSTANCE_ID, MN_LIB_DUAL_HORIZONTAL_IMAGE, MN_MULTI_SELECT_CONFIG, MN_SECTION_PATH, MN_TEST_COMPONENT_CONFIG, MN_TEXTAREA_CONFIG, MnAlertOutletComponent, MnAlertService, MnAlertStore, MnButton, MnCheckbox, MnConfigService, MnConfirmationBodyComponent, MnCustomBodyHostComponent, MnDatetime, MnDualHorizontalImage, MnFormBodyComponent, MnInformationCard, MnInputField, MnInstanceDirective, MnLanguageService, MnModalRef, MnModalService, MnModalShellComponent, MnMultiSelect, MnSectionDirective, MnTable, MnTestComponent, MnTextarea, MnTranslatePipe, MnWizardBodyComponent, ModalBuilder, ModalCloseReason, ModalIntent, ModalKind, ModalSize, NavigationDirection, OptionState, SelectionMode, StepBuilder, StepState, SubmitMode, Test, ValidationCode, ValidationStatus, WizardFlowMode, WizardModalBuilder, dateTimeAdapter, defaultTextAdapter, isTranslatable, mnAlertVariants, mnButtonVariants, mnCheckboxVariants, mnDatetimeVariants, mnInformationCardVariants, mnInputFieldVariants, mnMultiSelectVariants, mnTextareaVariants, numberAdapter, pickAdapter, provideMnAlerts, provideMnComponentConfig, provideMnConfig, provideMnLanguage };
2970
- export type { BaseModalConfig, CancellationActionConfig, CheckboxFieldConfig, ColorFieldConfig, ColumnDefinition, ConfirmationActionConfig, ConfirmationModalConfig, CursorPaginationStrategy, CustomFieldConfig, CustomModalConfig, DateFieldConfig, DatetimeFieldConfig, FieldDataSource, FieldValidator, FieldVisibilityCondition, FileFieldConfig, FormFieldConfig, FormFieldGroup, FormModalConfig, FormRow, FormRowField, FormValidator, MnAlert, MnAlertConfig, MnAlertId, MnAlertKind, MnAlertTemplateContext, MnAlertVariants, MnButtonTypes, MnButtonVariants, MnCheckboxErrorMessageData, MnCheckboxErrorMessagesData, MnCheckboxProps, MnCheckboxUIConfig, MnCheckboxVariants, MnConfigFile, MnConfigValue, MnDatetimeErrorMessageData, MnDatetimeErrorMessagesData, MnDatetimeMode, MnDatetimeProps, MnDatetimeUIConfig, MnDatetimeVariants, MnDomAttrs, MnDualHorizontalImageConfig, MnDualHorizontalImageTypes, MnErrorMessageData, MnErrorMessagesData, MnImageType, MnInformationCardBaseData, MnInformationCardData, MnInformationCardVariants, MnInputAdapter, MnInputBaseProps, MnInputDateTimeProps, MnInputFieldProps, MnInputFieldUIConfig, MnInputProps, MnInputType, MnInputVariants, MnLanguageConfig, MnMultiSelectErrorMessageData, MnMultiSelectErrorMessagesData, MnMultiSelectOption, MnMultiSelectProps, MnMultiSelectUIConfig, MnMultiSelectVariants, MnShowInput, MnTextareaErrorMessageData, MnTextareaErrorMessagesData, MnTextareaProps, MnTextareaUIConfig, MnTextareaVariants, MnTranslatable, MnTranslationMap, MnTranslations, ModalCancelHandler, ModalCloseEvent, ModalConfig, ModalFooterAction, ModalI18nLabels, ModalInputMap, ModalPollingConfig, ModalRef, ModalResultHandler, ModalStepId, MultiSelectFieldConfig, MultiSelectTableFieldConfig, NumberFieldConfig, OffsetPaginationStrategy, PaginationStrategy, PasswordFieldConfig, RatingFieldConfig, SelectFieldConfig, SelectOption, SingleSelectTableFieldConfig, SliderFieldConfig, SortState, StepBodyConfig, StepGuard, StepValidator, TableAppearance, TableDataSource, TableRowAction, TextFieldConfig, TextareaFieldConfig, ValidationResult, WizardBeforeCompleteValidator, WizardModalConfig, WizardResult, WizardStepChangeEvent, WizardStepChangeHandler, WizardStepConfig };
3260
+ export { API_BASE_URL, ActionStyle, BackdropMode, BaseModalBuilder, CloseMode, ColumnSortType, ConfirmationModalBuilder, ConfirmationTone, CrudService, CustomModalBuilder, DEFAULT_MN_ALERT_CONFIG, FieldAppearance, FieldKind, FormLayoutMode, FormModalBuilder, KeyboardMode, MN_ALERT_CONFIG, MN_CHECKBOX_CONFIG, MN_DATETIME_CONFIG, MN_INPUT_FIELD_CONFIG, MN_INSTANCE_ID, MN_LIB_DUAL_HORIZONTAL_IMAGE, MN_MULTI_SELECT_CONFIG, MN_SECTION_PATH, MN_TEST_COMPONENT_CONFIG, MN_TEXTAREA_CONFIG, MnAlertOutletComponent, MnAlertService, MnAlertStore, MnButton, MnCheckbox, MnConfigService, MnConfirmationBodyComponent, MnCustomBodyHostComponent, MnDatetime, MnDualHorizontalImage, MnFormBodyComponent, MnInformationCard, MnInputField, MnInstanceDirective, MnLanguageService, MnModalRef, MnModalService, MnModalShellComponent, MnMultiSelect, MnSectionDirective, MnTable, MnTestComponent, MnTextarea, MnTranslatePipe, MnWizardBodyComponent, ModalBuilder, ModalCloseReason, ModalIntent, ModalKind, ModalSize, NavigationDirection, OptionState, SelectionMode, StepBuilder, StepState, SubmitMode, Test, ValidationCode, ValidationStatus, WizardFlowMode, WizardModalBuilder, dateTimeAdapter, defaultTextAdapter, isTranslatable, mnAlertVariants, mnButtonVariants, mnCheckboxVariants, mnDatetimeVariants, mnInformationCardVariants, mnInputFieldVariants, mnMultiSelectVariants, mnTextareaVariants, numberAdapter, pickAdapter, provideMnAlerts, provideMnComponentConfig, provideMnConfig, provideMnLanguage };
3261
+ export type { ApiError, BaseModalConfig, CancellationActionConfig, CheckboxFieldConfig, ColorFieldConfig, ColumnDefinition, ConfirmationActionConfig, ConfirmationModalConfig, CrudConfig, CursorPaginationStrategy, CustomFieldConfig, CustomModalConfig, DateFieldConfig, DatetimeFieldConfig, FailureResult, FieldDataSource, FieldValidator, FieldVisibilityCondition, FileFieldConfig, FormFieldConfig, FormFieldGroup, FormModalConfig, FormRow, FormRowField, FormValidator, MnAlert, MnAlertConfig, MnAlertId, MnAlertKind, MnAlertTemplateContext, MnAlertVariants, MnButtonTypes, MnButtonVariants, MnCheckboxErrorMessageData, MnCheckboxErrorMessagesData, MnCheckboxProps, MnCheckboxUIConfig, MnCheckboxVariants, MnConfigFile, MnConfigValue, MnDatetimeErrorMessageData, MnDatetimeErrorMessagesData, MnDatetimeMode, MnDatetimeProps, MnDatetimeUIConfig, MnDatetimeVariants, MnDomAttrs, MnDualHorizontalImageConfig, MnDualHorizontalImageTypes, MnErrorMessageData, MnErrorMessagesData, MnImageType, MnInformationCardBaseData, MnInformationCardData, MnInformationCardVariants, MnInputAdapter, MnInputBaseProps, MnInputDateTimeProps, MnInputFieldProps, MnInputFieldUIConfig, MnInputProps, MnInputType, MnInputVariants, MnLanguageConfig, MnMultiSelectErrorMessageData, MnMultiSelectErrorMessagesData, MnMultiSelectOption, MnMultiSelectProps, MnMultiSelectUIConfig, MnMultiSelectVariants, MnShowInput, MnTextareaErrorMessageData, MnTextareaErrorMessagesData, MnTextareaProps, MnTextareaUIConfig, MnTextareaVariants, MnTranslatable, MnTranslationMap, MnTranslations, ModalCancelHandler, ModalCloseEvent, ModalConfig, ModalFooterAction, ModalI18nLabels, ModalInputMap, ModalPollingConfig, ModalRef, ModalResultHandler, ModalStepId, MultiSelectFieldConfig, MultiSelectTableFieldConfig, NumberFieldConfig, OffsetPaginationStrategy, PaginationStrategy, PasswordFieldConfig, Primitive, QueryParams, QueryValue, RatingFieldConfig, Result, ResultMeta, SelectFieldConfig, SelectOption, SingleSelectTableFieldConfig, SliderFieldConfig, SortState, StepBodyConfig, StepGuard, StepValidator, SuccessResult, TableAppearance, TableDataSource, TableRowAction, TextFieldConfig, TextareaFieldConfig, ValidationResult, WizardBeforeCompleteValidator, WizardModalConfig, WizardResult, WizardStepChangeEvent, WizardStepChangeHandler, WizardStepConfig };