@kalutskii/foundation 0.3.5 → 0.4.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 +28 -4
- package/dist/index.js +14 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -45,6 +45,8 @@ declare function failure({ status, error }: {
|
|
|
45
45
|
declare function fetchSafely<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<FetchResult<APIContractData<TResult>>>;
|
|
46
46
|
/** Resolves an APIContractResult fetcher, throwing an error if the result is an APIError. */
|
|
47
47
|
declare function fetchAndThrow<TResult extends APIContractResult<unknown>>(fetcher: () => Promise<TResult>): Promise<APIContractData<TResult>>;
|
|
48
|
+
/** Pure type guard for APIError responses, used to narrow the type of a response object. */
|
|
49
|
+
declare function isErrorResponse<TResponse>(response: TResponse): response is TResponse & APIError;
|
|
48
50
|
|
|
49
51
|
/**
|
|
50
52
|
* Global error handler for Hono framework. Catches all exceptions thrown in route handlers and middlewares.
|
|
@@ -151,6 +153,22 @@ declare const log: {
|
|
|
151
153
|
error: (message: string, service?: string, stack?: string) => void;
|
|
152
154
|
};
|
|
153
155
|
|
|
156
|
+
/**
|
|
157
|
+
* A utility type that represents a value that can be of type T or null.
|
|
158
|
+
* Commonly used for optional fields in data models or function return types.
|
|
159
|
+
*
|
|
160
|
+
* ```
|
|
161
|
+
* export type ObjectSelect = XOR<
|
|
162
|
+
* Pick<Object, 'id'>,
|
|
163
|
+
* Pick<Object, 'anotherUniqueField'>
|
|
164
|
+
* >;
|
|
165
|
+
*/
|
|
166
|
+
type XOR<T, U> = (T & {
|
|
167
|
+
[K in keyof U]?: never;
|
|
168
|
+
}) | (U & {
|
|
169
|
+
[K in keyof T]?: never;
|
|
170
|
+
});
|
|
171
|
+
|
|
154
172
|
/** Options for configuring the JWT service. */
|
|
155
173
|
type JWTServiceOptions = {
|
|
156
174
|
/** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
|
|
@@ -168,6 +186,10 @@ type Payload<T> = T extends z$1.ZodType ? z$1.infer<T> : T;
|
|
|
168
186
|
/** Utility type to extract the payload schema type from a Zod schema. */
|
|
169
187
|
type PayloadSchema<T> = T extends z$1.ZodType ? T : never;
|
|
170
188
|
|
|
189
|
+
/**
|
|
190
|
+
* A service class for handling JWT operations with optional Zod schema validation for the payload.
|
|
191
|
+
* The class is generic, allowing you to specify the type of the payload or a schema for validation.
|
|
192
|
+
*/
|
|
171
193
|
declare class ZodJWTService<TPayloadOrSchema> {
|
|
172
194
|
readonly payloadSchema: PayloadSchema<TPayloadOrSchema> | undefined;
|
|
173
195
|
protected readonly algorithm: SymmetricAlgorithm;
|
|
@@ -179,15 +201,17 @@ declare class ZodJWTService<TPayloadOrSchema> {
|
|
|
179
201
|
*/
|
|
180
202
|
sign(payload: Payload<TPayloadOrSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
|
|
181
203
|
/**
|
|
182
|
-
* Decodes a JWT token, and validates the payload against
|
|
183
|
-
*
|
|
204
|
+
* Decodes a JWT token, and if a Zod schema is provided, validates the payload against it.
|
|
205
|
+
* Returns the decoded payload if valid, or null if invalid or if the token cannot be decoded.
|
|
206
|
+
* Example usage: `const payload = await JWTServiceInstance.decode(token);`
|
|
184
207
|
*/
|
|
185
208
|
decode(token: string): Promise<Payload<TPayloadOrSchema> | null>;
|
|
186
209
|
/**
|
|
187
210
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
188
|
-
* then validates the payload against the Zod schema
|
|
211
|
+
* then validates the payload against the Zod schema if one is provided. Throws an error if verification fails or if the payload is invalid.
|
|
212
|
+
* Example usage: `const payload = await JWTServiceInstance.verifyOrThrow(token, 'my-secret');`
|
|
189
213
|
*/
|
|
190
214
|
verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
|
|
191
215
|
}
|
|
192
216
|
|
|
193
|
-
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, type Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, log, onHandlerError, respond, safeExecute, success };
|
|
217
|
+
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, type Payload, type PayloadSchema, SUCCESS_STATUS_CODES, type SerializeDates, type SuccessStatusCode, type XOR, ZodJWTService, asQueryBoolean, asQueryNumber, failure, fetchAndThrow, fetchSafely, formatTime, generateRandomString, getColoredHTTPStatus, getFormattedDate, getFormattedTime, getUTCOffset, getZonedTime, honoLoggingHandler, isErrorResponse, log, onHandlerError, respond, safeExecute, success };
|
package/dist/index.js
CHANGED
|
@@ -23,6 +23,14 @@ async function fetchAndThrow(fetcher) {
|
|
|
23
23
|
throw new Error(response.error);
|
|
24
24
|
return response.data;
|
|
25
25
|
}
|
|
26
|
+
function isErrorResponse(response) {
|
|
27
|
+
if (typeof response !== "object" || response === null)
|
|
28
|
+
return false;
|
|
29
|
+
if (!("kind" in response) || !("status" in response))
|
|
30
|
+
return false;
|
|
31
|
+
const { kind, status } = response;
|
|
32
|
+
return kind === "error" && (typeof status === "number" || typeof status === "string");
|
|
33
|
+
}
|
|
26
34
|
|
|
27
35
|
// src/hono/hono.execution.ts
|
|
28
36
|
import { HTTPException } from "hono/http-exception";
|
|
@@ -181,8 +189,9 @@ var ZodJWTService = class {
|
|
|
181
189
|
return signJWT({ ...payload, exp }, secret, this.algorithm);
|
|
182
190
|
}
|
|
183
191
|
/**
|
|
184
|
-
* Decodes a JWT token, and validates the payload against
|
|
185
|
-
*
|
|
192
|
+
* Decodes a JWT token, and if a Zod schema is provided, validates the payload against it.
|
|
193
|
+
* Returns the decoded payload if valid, or null if invalid or if the token cannot be decoded.
|
|
194
|
+
* Example usage: `const payload = await JWTServiceInstance.decode(token);`
|
|
186
195
|
*/
|
|
187
196
|
async decode(token) {
|
|
188
197
|
const { payload } = decodeJWT(token);
|
|
@@ -194,7 +203,8 @@ var ZodJWTService = class {
|
|
|
194
203
|
}
|
|
195
204
|
/**
|
|
196
205
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
197
|
-
* then validates the payload against the Zod schema
|
|
206
|
+
* then validates the payload against the Zod schema if one is provided. Throws an error if verification fails or if the payload is invalid.
|
|
207
|
+
* Example usage: `const payload = await JWTServiceInstance.verifyOrThrow(token, 'my-secret');`
|
|
198
208
|
*/
|
|
199
209
|
async verifyOrThrow(token, secret) {
|
|
200
210
|
const payload = await verifyJWT(token, secret, this.algorithm);
|
|
@@ -221,6 +231,7 @@ export {
|
|
|
221
231
|
getUTCOffset,
|
|
222
232
|
getZonedTime,
|
|
223
233
|
honoLoggingHandler,
|
|
234
|
+
isErrorResponse,
|
|
224
235
|
log,
|
|
225
236
|
onHandlerError,
|
|
226
237
|
respond,
|