@kalutskii/foundation 0.3.4 → 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 +36 -10
- package/dist/index.js +9 -5
- 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'. */
|
|
@@ -163,27 +179,37 @@ type JWTSignOptions = {
|
|
|
163
179
|
/** The number of seconds until the JWT expires. */
|
|
164
180
|
expiresInSeconds?: number;
|
|
165
181
|
};
|
|
182
|
+
/** Utility types for inferring payload types from Zod schemas. */
|
|
183
|
+
type Payload<T> = T extends z$1.ZodType ? z$1.infer<T> : T;
|
|
184
|
+
/** Utility type to extract the payload schema type from a Zod schema. */
|
|
185
|
+
type PayloadSchema<T> = T extends z$1.ZodType ? T : never;
|
|
166
186
|
|
|
167
|
-
|
|
168
|
-
|
|
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
|
+
*/
|
|
191
|
+
declare class ZodJWTService<TPayloadOrSchema> {
|
|
192
|
+
readonly payloadSchema: PayloadSchema<TPayloadOrSchema> | undefined;
|
|
169
193
|
protected readonly algorithm: SymmetricAlgorithm;
|
|
170
194
|
protected readonly defaultExpirationSeconds: number;
|
|
171
|
-
constructor(payloadSchema?:
|
|
195
|
+
constructor(payloadSchema?: PayloadSchema<TPayloadOrSchema>, options?: JWTServiceOptions);
|
|
172
196
|
/**
|
|
173
197
|
* Signs a JWT token with the provided payload and secret, using the configured algorithm and expiration settings.
|
|
174
198
|
* Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
|
|
175
199
|
*/
|
|
176
|
-
sign(payload:
|
|
200
|
+
sign(payload: Payload<TPayloadOrSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
|
|
177
201
|
/**
|
|
178
|
-
* Decodes a JWT token, and validates the payload against
|
|
179
|
-
*
|
|
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);`
|
|
180
205
|
*/
|
|
181
|
-
decode(token: string): Promise<
|
|
206
|
+
decode(token: string): Promise<Payload<TPayloadOrSchema> | null>;
|
|
182
207
|
/**
|
|
183
208
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
184
|
-
* 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');`
|
|
185
211
|
*/
|
|
186
|
-
verifyOrThrow(token: string, secret: string): Promise<
|
|
212
|
+
verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
|
|
187
213
|
}
|
|
188
214
|
|
|
189
|
-
export { type APIContractData, type APIContractError, type APIContractResult, type APIError, type APISuccess, EXCEPTION_STATUS_CODES, type ExceptionStatusCode, type FetchResult, type JWTServiceOptions, type JWTSignOptions, 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,24 +181,28 @@ 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);
|
|
189
|
-
if (!this.payloadSchema)
|
|
190
|
+
if (!this.payloadSchema) {
|
|
190
191
|
return payload;
|
|
192
|
+
}
|
|
191
193
|
const { success: success2, data } = await this.payloadSchema.safeParseAsync(payload);
|
|
192
194
|
return success2 ? data : null;
|
|
193
195
|
}
|
|
194
196
|
/**
|
|
195
197
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
196
|
-
* 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');`
|
|
197
200
|
*/
|
|
198
201
|
async verifyOrThrow(token, secret) {
|
|
199
202
|
const payload = await verifyJWT(token, secret, this.algorithm);
|
|
200
|
-
if (!this.payloadSchema)
|
|
203
|
+
if (!this.payloadSchema) {
|
|
201
204
|
return payload;
|
|
205
|
+
}
|
|
202
206
|
return this.payloadSchema.parseAsync(payload);
|
|
203
207
|
}
|
|
204
208
|
};
|