@kalutskii/foundation 0.7.25 → 2.0.5

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
@@ -22,7 +22,7 @@ declare const onHandlerError: ErrorHandler;
22
22
  //#endregion
23
23
  //#region src/http/http.constants.d.ts
24
24
  declare const SUCCESS_STATUS_CODES: readonly [200, 201, 202, 307];
25
- declare const EXCEPTION_STATUS_CODES: readonly [400, 401, 403, 404, 405, 409, 500];
25
+ declare const EXCEPTION_STATUS_CODES: readonly [400, 401, 403, 404, 405, 409, 413, 500];
26
26
  //#endregion
27
27
  //#region src/http/http.types.d.ts
28
28
  type SuccessStatusCode = (typeof SUCCESS_STATUS_CODES)[number];
@@ -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 {
@@ -420,9 +420,26 @@ const EXCEPTION_STATUS_CODES = [
420
420
  404,
421
421
  405,
422
422
  409,
423
+ 413,
423
424
  500
424
425
  ];
425
426
  //#endregion
427
+ //#region src/http/http.errors.ts
428
+ /**
429
+ * Represents an error envelope rejected by an API request resolver.
430
+ * The original status and typed code remain available for client handling.
431
+ */
432
+ var APIRequestError = class extends Error {
433
+ code;
434
+ status;
435
+ constructor(status, code) {
436
+ super(code);
437
+ this.name = "APIRequestError";
438
+ this.code = code;
439
+ this.status = status;
440
+ }
441
+ };
442
+ //#endregion
426
443
  //#region src/http/http.resolvers.ts
427
444
  /**
428
445
  * Converts an API contract envelope into a mutually exclusive safe result.
@@ -440,12 +457,12 @@ async function fetchSafely(fetcher) {
440
457
  };
441
458
  }
442
459
  /**
443
- * Returns successful API data and throws for an `APIError` envelope.
444
- * Rejected fetchers propagate their original error without replacement.
460
+ * Returns successful API data and throws `APIRequestError` for a failed envelope.
461
+ * Transport and programming rejections propagate without replacement.
445
462
  */
446
463
  async function fetchAndThrow(fetcher) {
447
464
  const response = await fetcher();
448
- if (response.kind === "error") throw new Error(response.error);
465
+ if (response.kind === "error") throw new APIRequestError(response.status, response.error);
449
466
  return response.data;
450
467
  }
451
468
  //#endregion
@@ -1037,4 +1054,4 @@ function stringifyPayloadValues(value) {
1037
1054
  return String(value);
1038
1055
  }
1039
1056
  //#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 };
1057
+ 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.5",
4
4
  "description": "Typescript collection of most common utilities, schemas and functions among private projects.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/bun": "^1.3.14",
34
- "oxfmt": "^0.63.0",
34
+ "oxfmt": "^0.65.0",
35
35
  "oxlint": "^1.78.0",
36
36
  "oxlint-tsgolint": "^7.0.2001",
37
37
  "tsdown": "^0.22.14",