@kalutskii/foundation 0.3.4 → 0.3.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 +11 -7
- package/dist/index.js +4 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -163,27 +163,31 @@ type JWTSignOptions = {
|
|
|
163
163
|
/** The number of seconds until the JWT expires. */
|
|
164
164
|
expiresInSeconds?: number;
|
|
165
165
|
};
|
|
166
|
+
/** Utility types for inferring payload types from Zod schemas. */
|
|
167
|
+
type Payload<T> = T extends z$1.ZodType ? z$1.infer<T> : T;
|
|
168
|
+
/** Utility type to extract the payload schema type from a Zod schema. */
|
|
169
|
+
type PayloadSchema<T> = T extends z$1.ZodType ? T : never;
|
|
166
170
|
|
|
167
|
-
declare class ZodJWTService<
|
|
168
|
-
readonly payloadSchema:
|
|
171
|
+
declare class ZodJWTService<TPayloadOrSchema> {
|
|
172
|
+
readonly payloadSchema: PayloadSchema<TPayloadOrSchema> | undefined;
|
|
169
173
|
protected readonly algorithm: SymmetricAlgorithm;
|
|
170
174
|
protected readonly defaultExpirationSeconds: number;
|
|
171
|
-
constructor(payloadSchema?:
|
|
175
|
+
constructor(payloadSchema?: PayloadSchema<TPayloadOrSchema>, options?: JWTServiceOptions);
|
|
172
176
|
/**
|
|
173
177
|
* Signs a JWT token with the provided payload and secret, using the configured algorithm and expiration settings.
|
|
174
178
|
* Example usage: `const accessToken = await JWTServiceInstance.sign({ userId: 123 }, 'my-secret', { expiresInSeconds: 300 });`
|
|
175
179
|
*/
|
|
176
|
-
sign(payload:
|
|
180
|
+
sign(payload: Payload<TPayloadOrSchema>, secret: string, options?: JWTSignOptions): Promise<string>;
|
|
177
181
|
/**
|
|
178
182
|
* Decodes a JWT token, and validates the payload against the Zod schema.
|
|
179
183
|
* Note: This method does NOT verify the token's signature or check for expiration.
|
|
180
184
|
*/
|
|
181
|
-
decode(token: string): Promise<
|
|
185
|
+
decode(token: string): Promise<Payload<TPayloadOrSchema> | null>;
|
|
182
186
|
/**
|
|
183
187
|
* Verifies and decodes a JWT token using the provided secret and configured algorithm,
|
|
184
188
|
* then validates the payload against the Zod schema, also throws on expired tokens.
|
|
185
189
|
*/
|
|
186
|
-
verifyOrThrow(token: string, secret: string): Promise<
|
|
190
|
+
verifyOrThrow(token: string, secret: string): Promise<Payload<TPayloadOrSchema>>;
|
|
187
191
|
}
|
|
188
192
|
|
|
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 };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -186,8 +186,9 @@ var ZodJWTService = class {
|
|
|
186
186
|
*/
|
|
187
187
|
async decode(token) {
|
|
188
188
|
const { payload } = decodeJWT(token);
|
|
189
|
-
if (!this.payloadSchema)
|
|
189
|
+
if (!this.payloadSchema) {
|
|
190
190
|
return payload;
|
|
191
|
+
}
|
|
191
192
|
const { success: success2, data } = await this.payloadSchema.safeParseAsync(payload);
|
|
192
193
|
return success2 ? data : null;
|
|
193
194
|
}
|
|
@@ -197,8 +198,9 @@ var ZodJWTService = class {
|
|
|
197
198
|
*/
|
|
198
199
|
async verifyOrThrow(token, secret) {
|
|
199
200
|
const payload = await verifyJWT(token, secret, this.algorithm);
|
|
200
|
-
if (!this.payloadSchema)
|
|
201
|
+
if (!this.payloadSchema) {
|
|
201
202
|
return payload;
|
|
203
|
+
}
|
|
202
204
|
return this.payloadSchema.parseAsync(payload);
|
|
203
205
|
}
|
|
204
206
|
};
|