@kalutskii/foundation 0.7.25 → 2.0.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/dist/index.d.ts CHANGED
@@ -32,19 +32,21 @@ type APISuccess<TData = void> = {
32
32
  status: SuccessStatusCode;
33
33
  data: TData;
34
34
  };
35
- type APIError = {
35
+ type APIError<TErrorCode extends string = string> = {
36
36
  kind: 'error';
37
37
  status: ExceptionStatusCode;
38
- error: string;
38
+ error: TErrorCode;
39
39
  };
40
- type APIContractResult<TData = void> = APISuccess<TData> | APIError;
40
+ type APIContractResult<TData = void, TErrorCode extends string = string> = APISuccess<TData> | APIError<TErrorCode>;
41
41
  type APIContractData<TResult extends APIContractResult<unknown>> = TResult extends APISuccess<infer TData> ? TData : never;
42
42
  type APIContractError<TResult extends APIContractResult<unknown>> = Extract<TResult, APIError>;
43
- type FetchResult<TData> = {
43
+ type APIContractErrorCode<TResult extends APIContractResult<unknown>> = APIContractError<TResult>['error'];
44
+ type ErrorCodeOf<TErrorFactories extends Record<string, unknown>> = keyof TErrorFactories & string;
45
+ type FetchResult<TData, TErrorCode extends string = string> = {
44
46
  error: null;
45
47
  data: TData;
46
48
  } | {
47
- error: string;
49
+ error: TErrorCode;
48
50
  data: null;
49
51
  };
50
52
  //#endregion
@@ -186,6 +188,17 @@ declare function encodeHMACSignature(signature: Uint8Array, encoding: HMACEncodi
186
188
  */
187
189
  declare function decodeHMACSignature(signature: string, encoding: HMACEncoding): Uint8Array<ArrayBuffer>;
188
190
  //#endregion
191
+ //#region src/http/http.errors.d.ts
192
+ /**
193
+ * Represents an error envelope rejected by an API request resolver.
194
+ * The original status and typed code remain available for client handling.
195
+ */
196
+ declare class APIRequestError<TErrorCode extends string = string> extends Error {
197
+ readonly code: TErrorCode;
198
+ readonly status: ExceptionStatusCode;
199
+ constructor(status: ExceptionStatusCode, code: TErrorCode);
200
+ }
201
+ //#endregion
189
202
  //#region src/http/http.factory.d.ts
190
203
  /**
191
204
  * Creates a successful API envelope without cloning its data.
@@ -197,22 +210,22 @@ declare function success<T = unknown>({ status, data }: {
197
210
  }): APISuccess<T>;
198
211
  /**
199
212
  * Creates a failed API envelope with one supported exception status.
200
- * The supplied message remains unchanged for downstream presentation.
213
+ * The supplied error code remains unchanged for downstream resolution.
201
214
  */
202
- declare function failure({ status, error }: {
215
+ declare function failure<const TErrorCode extends string>({ status, error }: {
203
216
  status: ExceptionStatusCode;
204
- error: string;
205
- }): APIError;
217
+ error: TErrorCode;
218
+ }): APIError<TErrorCode>;
206
219
  //#endregion
207
220
  //#region src/http/http.resolvers.d.ts
208
221
  /**
209
222
  * Converts an API contract envelope into a mutually exclusive safe result.
210
223
  * Only `APIError` values are normalized; rejected fetchers still reject.
211
224
  */
212
- declare function fetchSafely<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<FetchResult<APIContractData<TResult>>>;
225
+ declare function fetchSafely<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<FetchResult<APIContractData<TResult>, APIContractErrorCode<TResult>>>;
213
226
  /**
214
- * Returns successful API data and throws for an `APIError` envelope.
215
- * Rejected fetchers propagate their original error without replacement.
227
+ * Returns successful API data and throws `APIRequestError` for a failed envelope.
228
+ * Transport and programming rejections propagate without replacement.
216
229
  */
217
230
  declare function fetchAndThrow<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<APIContractData<TResult>>;
218
231
  //#endregion
@@ -1069,4 +1082,4 @@ declare function stringifyPayloadValues<TValue>(value: TValue): StringifiedPaylo
1069
1082
  */
1070
1083
  declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
1071
1084
  //#endregion
1072
- export { APIContractData, APIContractError, APIContractResult, APIError, APISuccess, AtLeastOne, DEFAULT_HMAC_ALGORITHM, DEFAULT_HMAC_ENCODING, DEFAULT_UPLOAD_MAX_FILE_SIZE, EXCEPTION_STATUS_CODES, ExceptionStatusCode, FetchResult, FileFormat, FileFormatConfig, HMACAlgorithm, HMACEncoding, HMACInput, HMACService, HMACServiceOptions, HonoFileRespondOptions, HonoRespondOptions, JWTServiceOptions, JWTSignOptions, LogLevel, MeasuredExecution, Payload, PayloadSchema, REDACTED_LOG_VALUE, ReplaceDotsWithUnderscores, ReplaceHyphensWithUnderscores, SUCCESS_STATUS_CODES, Simplify, StringEnumRecord, StringifiedPayload, SuccessStatusCode, UploadFilesValidationOptions, UploadFilesValidationResult, UploadPreset, UploadValidationError, ZodBulkExcludeSelection, ZodBulkIncludeSelection, ZodBulkSelection, ZodJWTService, ZodPaginationOptions, ZodSearchSchemaOptions, ZodUploadFileSchemaOptions, asQuery, createStringEnumRecord, createUploadAccept, createZodSearchWhereSchema, decodeBase64, decodeHMACSignature, defineUploadPreset, documentUploadPreset, encodeBase64, encodeHMACSignature, failure, fetchAndThrow, fetchSafely, fileFormat, fileFormatsArray, fileFormatsConfig, fileFormatsRecord, fileRespond, formatTime, generateRandomString, getColoredHTTPStatus, getFileExtension, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, hmacAlgorithm, hmacAlgorithmsArray, hmacAlgorithmsRecord, hmacEncoding, hmacEncodingsArray, hmacEncodingsRecord, httpStatusColors, imageTransparentUploadPreset, imageUploadPreset, isFileExtensionSupported, isFileFormatSupported, isFileMimeTypeSupported, isPlainObject, log, logLevel, logLevelColors, logLevelsArray, logLevelsRecord, loggingMiddleware, matchesMimeType, measureExecutionTime, normalizeFileExtension, onHandlerError, parseQueryValue, redactSensitiveJSON, redactSensitiveSearchParams, respond, safeExecute, sensitiveLogKeyParts, sqlWhere, stringifyPayloadValues, success, toHMACBytes, uploadValidationError, uploadValidationErrorsArray, uploadValidationErrorsRecord, validateUploadFile, validateUploadFiles, zodAtLeastOne, zodBulkSelectionSchema, zodPaginationSchema, zodPaginationShape, zodSearchQuerySchema, zodSearchSchema, zodUploadFileSchema };
1085
+ export { APIContractData, APIContractError, APIContractErrorCode, APIContractResult, APIError, APIRequestError, APISuccess, AtLeastOne, DEFAULT_HMAC_ALGORITHM, DEFAULT_HMAC_ENCODING, DEFAULT_UPLOAD_MAX_FILE_SIZE, EXCEPTION_STATUS_CODES, ErrorCodeOf, ExceptionStatusCode, FetchResult, FileFormat, FileFormatConfig, HMACAlgorithm, HMACEncoding, HMACInput, HMACService, HMACServiceOptions, HonoFileRespondOptions, HonoRespondOptions, JWTServiceOptions, JWTSignOptions, LogLevel, MeasuredExecution, Payload, PayloadSchema, REDACTED_LOG_VALUE, ReplaceDotsWithUnderscores, ReplaceHyphensWithUnderscores, SUCCESS_STATUS_CODES, Simplify, StringEnumRecord, StringifiedPayload, SuccessStatusCode, UploadFilesValidationOptions, UploadFilesValidationResult, UploadPreset, UploadValidationError, ZodBulkExcludeSelection, ZodBulkIncludeSelection, ZodBulkSelection, ZodJWTService, ZodPaginationOptions, ZodSearchSchemaOptions, ZodUploadFileSchemaOptions, asQuery, createStringEnumRecord, createUploadAccept, createZodSearchWhereSchema, decodeBase64, decodeHMACSignature, defineUploadPreset, documentUploadPreset, encodeBase64, encodeHMACSignature, failure, fetchAndThrow, fetchSafely, fileFormat, fileFormatsArray, fileFormatsConfig, fileFormatsRecord, fileRespond, formatTime, generateRandomString, getColoredHTTPStatus, getFileExtension, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, hmacAlgorithm, hmacAlgorithmsArray, hmacAlgorithmsRecord, hmacEncoding, hmacEncodingsArray, hmacEncodingsRecord, httpStatusColors, imageTransparentUploadPreset, imageUploadPreset, isFileExtensionSupported, isFileFormatSupported, isFileMimeTypeSupported, isPlainObject, log, logLevel, logLevelColors, logLevelsArray, logLevelsRecord, loggingMiddleware, matchesMimeType, measureExecutionTime, normalizeFileExtension, onHandlerError, parseQueryValue, redactSensitiveJSON, redactSensitiveSearchParams, respond, safeExecute, sensitiveLogKeyParts, sqlWhere, stringifyPayloadValues, success, toHMACBytes, uploadValidationError, uploadValidationErrorsArray, uploadValidationErrorsRecord, validateUploadFile, validateUploadFiles, zodAtLeastOne, zodBulkSelectionSchema, zodPaginationSchema, zodPaginationShape, zodSearchQuerySchema, zodSearchSchema, zodUploadFileSchema };
package/dist/index.js CHANGED
@@ -36,7 +36,7 @@ function success({ status, data }) {
36
36
  }
37
37
  /**
38
38
  * Creates a failed API envelope with one supported exception status.
39
- * The supplied message remains unchanged for downstream presentation.
39
+ * The supplied error code remains unchanged for downstream resolution.
40
40
  */
41
41
  function failure({ status, error }) {
42
42
  return {
@@ -423,6 +423,22 @@ const EXCEPTION_STATUS_CODES = [
423
423
  500
424
424
  ];
425
425
  //#endregion
426
+ //#region src/http/http.errors.ts
427
+ /**
428
+ * Represents an error envelope rejected by an API request resolver.
429
+ * The original status and typed code remain available for client handling.
430
+ */
431
+ var APIRequestError = class extends Error {
432
+ code;
433
+ status;
434
+ constructor(status, code) {
435
+ super(code);
436
+ this.name = "APIRequestError";
437
+ this.code = code;
438
+ this.status = status;
439
+ }
440
+ };
441
+ //#endregion
426
442
  //#region src/http/http.resolvers.ts
427
443
  /**
428
444
  * Converts an API contract envelope into a mutually exclusive safe result.
@@ -440,12 +456,12 @@ async function fetchSafely(fetcher) {
440
456
  };
441
457
  }
442
458
  /**
443
- * Returns successful API data and throws for an `APIError` envelope.
444
- * Rejected fetchers propagate their original error without replacement.
459
+ * Returns successful API data and throws `APIRequestError` for a failed envelope.
460
+ * Transport and programming rejections propagate without replacement.
445
461
  */
446
462
  async function fetchAndThrow(fetcher) {
447
463
  const response = await fetcher();
448
- if (response.kind === "error") throw new Error(response.error);
464
+ if (response.kind === "error") throw new APIRequestError(response.status, response.error);
449
465
  return response.data;
450
466
  }
451
467
  //#endregion
@@ -1037,4 +1053,4 @@ function stringifyPayloadValues(value) {
1037
1053
  return String(value);
1038
1054
  }
1039
1055
  //#endregion
1040
- export { DEFAULT_HMAC_ALGORITHM, DEFAULT_HMAC_ENCODING, DEFAULT_UPLOAD_MAX_FILE_SIZE, EXCEPTION_STATUS_CODES, HMACService, REDACTED_LOG_VALUE, SUCCESS_STATUS_CODES, ZodJWTService, asQuery, createStringEnumRecord, createUploadAccept, createZodSearchWhereSchema, decodeBase64, decodeHMACSignature, defineUploadPreset, documentUploadPreset, encodeBase64, encodeHMACSignature, failure, fetchAndThrow, fetchSafely, fileFormat, fileFormatsArray, fileFormatsConfig, fileFormatsRecord, fileRespond, formatTime, generateRandomString, getColoredHTTPStatus, getFileExtension, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, hmacAlgorithm, hmacAlgorithmsArray, hmacAlgorithmsRecord, hmacEncoding, hmacEncodingsArray, hmacEncodingsRecord, httpStatusColors, imageTransparentUploadPreset, imageUploadPreset, isFileExtensionSupported, isFileFormatSupported, isFileMimeTypeSupported, isPlainObject, log, logLevel, logLevelColors, logLevelsArray, logLevelsRecord, loggingMiddleware, matchesMimeType, measureExecutionTime, normalizeFileExtension, onHandlerError, parseQueryValue, redactSensitiveJSON, redactSensitiveSearchParams, respond, safeExecute, sensitiveLogKeyParts, sqlWhere, stringifyPayloadValues, success, toHMACBytes, uploadValidationError, uploadValidationErrorsArray, uploadValidationErrorsRecord, validateUploadFile, validateUploadFiles, zodAtLeastOne, zodBulkSelectionSchema, zodPaginationSchema, zodPaginationShape, zodSearchQuerySchema, zodSearchSchema, zodUploadFileSchema };
1056
+ export { APIRequestError, DEFAULT_HMAC_ALGORITHM, DEFAULT_HMAC_ENCODING, DEFAULT_UPLOAD_MAX_FILE_SIZE, EXCEPTION_STATUS_CODES, HMACService, REDACTED_LOG_VALUE, SUCCESS_STATUS_CODES, ZodJWTService, asQuery, createStringEnumRecord, createUploadAccept, createZodSearchWhereSchema, decodeBase64, decodeHMACSignature, defineUploadPreset, documentUploadPreset, encodeBase64, encodeHMACSignature, failure, fetchAndThrow, fetchSafely, fileFormat, fileFormatsArray, fileFormatsConfig, fileFormatsRecord, fileRespond, formatTime, generateRandomString, getColoredHTTPStatus, getFileExtension, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, hmacAlgorithm, hmacAlgorithmsArray, hmacAlgorithmsRecord, hmacEncoding, hmacEncodingsArray, hmacEncodingsRecord, httpStatusColors, imageTransparentUploadPreset, imageUploadPreset, isFileExtensionSupported, isFileFormatSupported, isFileMimeTypeSupported, isPlainObject, log, logLevel, logLevelColors, logLevelsArray, logLevelsRecord, loggingMiddleware, matchesMimeType, measureExecutionTime, normalizeFileExtension, onHandlerError, parseQueryValue, redactSensitiveJSON, redactSensitiveSearchParams, respond, safeExecute, sensitiveLogKeyParts, sqlWhere, stringifyPayloadValues, success, toHMACBytes, uploadValidationError, uploadValidationErrorsArray, uploadValidationErrorsRecord, validateUploadFile, validateUploadFiles, zodAtLeastOne, zodBulkSelectionSchema, zodPaginationSchema, zodPaginationShape, zodSearchQuerySchema, zodSearchSchema, zodUploadFileSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kalutskii/foundation",
3
- "version": "0.7.25",
3
+ "version": "2.0.0",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "license": "MIT",
6
6
  "repository": {