@kalutskii/foundation 0.3.5 → 0.3.6
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 +26 -4
- package/dist/index.js +5 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -151,6 +151,22 @@ declare const log: {
|
|
|
151
151
|
error: (message: string, service?: string, stack?: string) => void;
|
|
152
152
|
};
|
|
153
153
|
|
|
154
|
+
/**
|
|
155
|
+
* A utility type that represents a value that can be of type T or null.
|
|
156
|
+
* Commonly used for optional fields in data models or function return types.
|
|
157
|
+
*
|
|
158
|
+
* ```
|
|
159
|
+
* export type ObjectSelect = XOR<
|
|
160
|
+
* Pick<Object, 'id'>,
|
|
161
|
+
* Pick<Object, 'anotherUniqueField'>
|
|
162
|
+
* >;
|
|
163
|
+
*/
|
|
164
|
+
type XOR<T, U> = (T & {
|
|
165
|
+
[K in keyof U]?: never;
|
|
166
|
+
}) | (U & {
|
|
167
|
+
[K in keyof T]?: never;
|
|
168
|
+
});
|
|
169
|
+
|
|
154
170
|
/** Options for configuring the JWT service. */
|
|
155
171
|
type JWTServiceOptions = {
|
|
156
172
|
/** The algorithm to use for signing and verifying JWTs. Defaults to 'HS256'. */
|
|
@@ -168,6 +184,10 @@ type Payload<T> = T extends z$1.ZodType ? z$1.infer<T> : T;
|
|
|
168
184
|
/** Utility type to extract the payload schema type from a Zod schema. */
|
|
169
185
|
type PayloadSchema<T> = T extends z$1.ZodType ? T : never;
|
|
170
186
|
|
|
187
|
+
/**
|
|
188
|
+
* A service class for handling JWT operations with optional Zod schema validation for the payload.
|
|
189
|
+
* The class is generic, allowing you to specify the type of the payload or a schema for validation.
|
|
190
|
+
*/
|
|
171
191
|
declare class ZodJWTService<TPayloadOrSchema> {
|
|
172
192
|
readonly payloadSchema: PayloadSchema<TPayloadOrSchema> | undefined;
|
|
173
193
|
protected readonly algorithm: SymmetricAlgorithm;
|
|
@@ -179,15 +199,17 @@ declare class ZodJWTService<TPayloadOrSchema> {
|
|
|
179
199
|
*/
|
|
180
200
|
sign(payload: Payload<TPayloadOrSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
|
|
181
201
|
/**
|
|
182
|
-
* Decodes a JWT token, and validates the payload against
|
|
183
|
-
*
|
|
202
|
+
* Decodes a JWT token, and if a Zod schema is provided, validates the payload against it.
|
|
203
|
+
* Returns the decoded payload if valid, or null if invalid or if the token cannot be decoded.
|
|
204
|
+
* Example usage: `const payload = await JWTServiceInstance.decode(token);`
|
|
184
205
|
*/
|
|
185
206
|
decode(token: string): Promise<Payload<TPayloadOrSchema> | null>;
|
|
186
207
|
/**
|
|
187
208
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
188
|
-
* then validates the payload against the Zod schema
|
|
209
|
+
* then validates the payload against the Zod schema if one is provided. Throws an error if verification fails or if the payload is invalid.
|
|
210
|
+
* Example usage: `const payload = await JWTServiceInstance.verifyOrThrow(token, 'my-secret');`
|
|
189
211
|
*/
|
|
190
212
|
verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
|
|
191
213
|
}
|
|
192
214
|
|
|
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 };
|
|
215
|
+
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, log, onHandlerError, respond, safeExecute, success };
|
package/dist/index.js
CHANGED
|
@@ -181,8 +181,9 @@ var ZodJWTService = class {
|
|
|
181
181
|
return signJWT({ ...payload, exp }, secret, this.algorithm);
|
|
182
182
|
}
|
|
183
183
|
/**
|
|
184
|
-
* Decodes a JWT token, and validates the payload against
|
|
185
|
-
*
|
|
184
|
+
* Decodes a JWT token, and if a Zod schema is provided, validates the payload against it.
|
|
185
|
+
* Returns the decoded payload if valid, or null if invalid or if the token cannot be decoded.
|
|
186
|
+
* Example usage: `const payload = await JWTServiceInstance.decode(token);`
|
|
186
187
|
*/
|
|
187
188
|
async decode(token) {
|
|
188
189
|
const { payload } = decodeJWT(token);
|
|
@@ -194,7 +195,8 @@ var ZodJWTService = class {
|
|
|
194
195
|
}
|
|
195
196
|
/**
|
|
196
197
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
197
|
-
* then validates the payload against the Zod schema
|
|
198
|
+
* then validates the payload against the Zod schema if one is provided. Throws an error if verification fails or if the payload is invalid.
|
|
199
|
+
* Example usage: `const payload = await JWTServiceInstance.verifyOrThrow(token, 'my-secret');`
|
|
198
200
|
*/
|
|
199
201
|
async verifyOrThrow(token, secret) {
|
|
200
202
|
const payload = await verifyJWT(token, secret, this.algorithm);
|