@vritti/api-sdk 0.1.5 → 0.1.7
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.cjs +668 -253
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +82 -28
- package/dist/index.d.ts +82 -28
- package/dist/index.js +530 -119
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -511,6 +511,52 @@ declare class VrittiAuthGuard implements CanActivate {
|
|
|
511
511
|
* @throws UnauthorizedException if token binding validation fails
|
|
512
512
|
*/
|
|
513
513
|
private validateRefreshTokenBinding;
|
|
514
|
+
/**
|
|
515
|
+
* Validate CSRF token for state-changing requests
|
|
516
|
+
* Uses Fastify's csrf-protection plugin for token validation
|
|
517
|
+
*
|
|
518
|
+
* @param request - Fastify request object
|
|
519
|
+
* @param reply - Fastify reply object
|
|
520
|
+
* @throws ForbiddenException if CSRF validation fails
|
|
521
|
+
*/
|
|
522
|
+
private validateCsrf;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* SSE Authentication Guard - For Server-Sent Events endpoints
|
|
527
|
+
*
|
|
528
|
+
* This guard is specifically designed for SSE endpoints where:
|
|
529
|
+
* 1. Browser's EventSource API cannot send custom headers
|
|
530
|
+
* 2. Token must be passed via query parameter
|
|
531
|
+
* 3. CORS headers must be set before any response (including errors)
|
|
532
|
+
*
|
|
533
|
+
* Validation Flow:
|
|
534
|
+
* 1. Set CORS headers FIRST (ensures error responses include CORS)
|
|
535
|
+
* 2. Extract token from query param (?token=<jwt>)
|
|
536
|
+
* 3. Validate token is type='onboarding'
|
|
537
|
+
* 4. Attach user data to request.user
|
|
538
|
+
*
|
|
539
|
+
* Usage:
|
|
540
|
+
* ```typescript
|
|
541
|
+
* @Sse('events')
|
|
542
|
+
* @Public() // Bypass global VrittiAuthGuard
|
|
543
|
+
* @UseGuards(SseAuthGuard)
|
|
544
|
+
* async subscribeToEvents(@UserId() userId: string) { ... }
|
|
545
|
+
* ```
|
|
546
|
+
*
|
|
547
|
+
* Note: Must be used with @Public() to bypass the global VrittiAuthGuard
|
|
548
|
+
* since EventSource cannot send Authorization headers.
|
|
549
|
+
*/
|
|
550
|
+
declare class SseAuthGuard implements CanActivate {
|
|
551
|
+
private readonly jwtService;
|
|
552
|
+
private readonly logger;
|
|
553
|
+
constructor(jwtService: JwtService);
|
|
554
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
555
|
+
/**
|
|
556
|
+
* Set CORS headers for SSE responses
|
|
557
|
+
* Must be called before any potential exceptions
|
|
558
|
+
*/
|
|
559
|
+
private setCorsHeaders;
|
|
514
560
|
}
|
|
515
561
|
|
|
516
562
|
/**
|
|
@@ -582,6 +628,12 @@ interface CookieConfig {
|
|
|
582
628
|
* @default 'strict'
|
|
583
629
|
*/
|
|
584
630
|
refreshCookieSameSite: 'strict' | 'lax' | 'none';
|
|
631
|
+
/**
|
|
632
|
+
* Cookie domain (e.g., 'localhost' for dev, '.vritti.cloud' for prod)
|
|
633
|
+
* Required for cross-subdomain auth (e.g., cloud.localhost accessing localhost API)
|
|
634
|
+
* @default undefined (uses request domain)
|
|
635
|
+
*/
|
|
636
|
+
refreshCookieDomain?: string;
|
|
585
637
|
}
|
|
586
638
|
/**
|
|
587
639
|
* JWT token configuration options
|
|
@@ -674,13 +726,7 @@ declare function resetConfig(): void;
|
|
|
674
726
|
/**
|
|
675
727
|
* Get refresh cookie options (convenience method)
|
|
676
728
|
*/
|
|
677
|
-
declare function getRefreshCookieOptions():
|
|
678
|
-
httpOnly: boolean;
|
|
679
|
-
secure: boolean;
|
|
680
|
-
sameSite: "strict" | "lax" | "none";
|
|
681
|
-
path: string;
|
|
682
|
-
maxAge: number;
|
|
683
|
-
};
|
|
729
|
+
declare function getRefreshCookieOptions(): Record<string, unknown>;
|
|
684
730
|
/**
|
|
685
731
|
* Get JWT expiry settings (convenience method)
|
|
686
732
|
*/
|
|
@@ -2075,30 +2121,25 @@ declare class HttpExceptionFilter implements ExceptionFilter {
|
|
|
2075
2121
|
catch(exception: unknown, host: ArgumentsHost): void;
|
|
2076
2122
|
}
|
|
2077
2123
|
|
|
2124
|
+
declare const SKIP_CSRF_KEY = "skipCsrf";
|
|
2078
2125
|
/**
|
|
2079
|
-
* CSRF
|
|
2126
|
+
* Decorator to skip CSRF validation for specific routes or controllers.
|
|
2127
|
+
* Use this for webhook endpoints that receive requests from external services
|
|
2128
|
+
* (e.g., WhatsApp, Twilio) which cannot include CSRF tokens.
|
|
2080
2129
|
*
|
|
2081
|
-
*
|
|
2082
|
-
*
|
|
2083
|
-
*
|
|
2084
|
-
*
|
|
2085
|
-
*
|
|
2086
|
-
* 2. Skip endpoints marked with @Public()
|
|
2087
|
-
* 3. Validate CSRF token for all other requests
|
|
2088
|
-
*
|
|
2089
|
-
* Token Sources (in priority order by @fastify/csrf-protection):
|
|
2090
|
-
* 1. req.headers['csrf-token']
|
|
2091
|
-
* 2. req.headers['xsrf-token']
|
|
2092
|
-
* 3. req.headers['x-csrf-token']
|
|
2093
|
-
* 4. req.headers['x-xsrf-token']
|
|
2094
|
-
* 5. req.body._csrf
|
|
2130
|
+
* @example
|
|
2131
|
+
* // Skip CSRF for entire controller
|
|
2132
|
+
* @Controller('webhooks')
|
|
2133
|
+
* @SkipCsrf()
|
|
2134
|
+
* export class WebhookController { ... }
|
|
2095
2135
|
*
|
|
2096
|
-
*
|
|
2136
|
+
* @example
|
|
2137
|
+
* // Skip CSRF for specific route
|
|
2138
|
+
* @Post()
|
|
2139
|
+
* @SkipCsrf()
|
|
2140
|
+
* async handleWebhook() { ... }
|
|
2097
2141
|
*/
|
|
2098
|
-
declare
|
|
2099
|
-
private readonly logger;
|
|
2100
|
-
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
2101
|
-
}
|
|
2142
|
+
declare const SkipCsrf: () => _nestjs_common.CustomDecorator<string>;
|
|
2102
2143
|
|
|
2103
2144
|
/**
|
|
2104
2145
|
* HTTP Module
|
|
@@ -2114,6 +2155,19 @@ declare class CsrfGuard implements CanActivate {
|
|
|
2114
2155
|
declare class HttpModule {
|
|
2115
2156
|
}
|
|
2116
2157
|
|
|
2158
|
+
/**
|
|
2159
|
+
* Extract ISO country code from E.164 phone number
|
|
2160
|
+
* @param phone Phone number in E.164 format (e.g., +919876543210)
|
|
2161
|
+
* @returns ISO 3166-1 alpha-2 country code (e.g., "IN") or undefined
|
|
2162
|
+
*/
|
|
2163
|
+
declare function extractCountryFromPhone(phone: string): string | undefined;
|
|
2164
|
+
/**
|
|
2165
|
+
* Normalize phone number to E.164 format with + prefix
|
|
2166
|
+
* @param phone Phone number (with or without + prefix)
|
|
2167
|
+
* @returns Phone number in E.164 format
|
|
2168
|
+
*/
|
|
2169
|
+
declare function normalizePhoneNumber(phone: string): string;
|
|
2170
|
+
|
|
2117
2171
|
/**
|
|
2118
2172
|
* Supported log levels for the logging system.
|
|
2119
2173
|
*/
|
|
@@ -2515,4 +2569,4 @@ declare function generateCorrelationId(): string;
|
|
|
2515
2569
|
*/
|
|
2516
2570
|
declare function addCorrelationIdToResponse(reply: FastifyReply, correlationId: string, headerName?: string): void;
|
|
2517
2571
|
|
|
2518
|
-
export { type ApiErrorResponse, type ApiSdkConfig, AuthConfigModule, BadGatewayException, BadRequestException, BaseFieldException, ConflictException, type CookieConfig, type CorrelationContext, CorrelationIdMiddleware,
|
|
2572
|
+
export { type ApiErrorResponse, type ApiSdkConfig, AuthConfigModule, BadGatewayException, BadRequestException, BaseFieldException, ConflictException, type CookieConfig, type CorrelationContext, CorrelationIdMiddleware, DEFAULT_CORRELATION_HEADER, DatabaseModule, type DatabaseModuleOptions, type FieldError, ForbiddenException, GoneException, type GuardConfig, HttpExceptionFilter, HttpLoggerInterceptor, type HttpLoggerOptions, HttpModule, InternalServerErrorException, type JwtConfig, LOGGER_MODULE_OPTIONS, type LogFormat, type LogLevel, type LogMetadata, LoggerModule, type LoggerModuleAsyncOptions, type LoggerModuleOptions, type LoggerOptionsFactory, LoggerService, MethodNotAllowedException, NotAcceptableException, NotFoundException, NotImplementedException, Onboarding, PayloadTooLargeException, PrimaryBaseRepository, PrimaryDatabaseService, type PrimaryDbConfig, type ProblemDetails, Public, type RegisteredSchema, RequestTimeoutException, SKIP_CSRF_KEY, ServiceUnavailableException, SkipCsrf, SseAuthGuard, Tenant, TenantBaseRepository, TenantContextService, TenantDatabaseService, type TenantInfo, TooManyRequestsException, type TypedDrizzleClient, UnauthorizedException, UnprocessableEntityException, UnsupportedMediaTypeException, UserId, ValidationException, VrittiAuthGuard, addCorrelationIdToResponse, configureApiSdk, correlationStorage, defineConfig, extractCountryFromPhone, generateCorrelationId, getConfig, getCorrelationContext, getHttpStatusTitle, getJwtExpiry, getRefreshCookieOptions, hashToken, normalizePhoneNumber, resetConfig, runWithCorrelationContext, updateCorrelationContext, verifyTokenHash };
|
package/dist/index.d.ts
CHANGED
|
@@ -511,6 +511,52 @@ declare class VrittiAuthGuard implements CanActivate {
|
|
|
511
511
|
* @throws UnauthorizedException if token binding validation fails
|
|
512
512
|
*/
|
|
513
513
|
private validateRefreshTokenBinding;
|
|
514
|
+
/**
|
|
515
|
+
* Validate CSRF token for state-changing requests
|
|
516
|
+
* Uses Fastify's csrf-protection plugin for token validation
|
|
517
|
+
*
|
|
518
|
+
* @param request - Fastify request object
|
|
519
|
+
* @param reply - Fastify reply object
|
|
520
|
+
* @throws ForbiddenException if CSRF validation fails
|
|
521
|
+
*/
|
|
522
|
+
private validateCsrf;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* SSE Authentication Guard - For Server-Sent Events endpoints
|
|
527
|
+
*
|
|
528
|
+
* This guard is specifically designed for SSE endpoints where:
|
|
529
|
+
* 1. Browser's EventSource API cannot send custom headers
|
|
530
|
+
* 2. Token must be passed via query parameter
|
|
531
|
+
* 3. CORS headers must be set before any response (including errors)
|
|
532
|
+
*
|
|
533
|
+
* Validation Flow:
|
|
534
|
+
* 1. Set CORS headers FIRST (ensures error responses include CORS)
|
|
535
|
+
* 2. Extract token from query param (?token=<jwt>)
|
|
536
|
+
* 3. Validate token is type='onboarding'
|
|
537
|
+
* 4. Attach user data to request.user
|
|
538
|
+
*
|
|
539
|
+
* Usage:
|
|
540
|
+
* ```typescript
|
|
541
|
+
* @Sse('events')
|
|
542
|
+
* @Public() // Bypass global VrittiAuthGuard
|
|
543
|
+
* @UseGuards(SseAuthGuard)
|
|
544
|
+
* async subscribeToEvents(@UserId() userId: string) { ... }
|
|
545
|
+
* ```
|
|
546
|
+
*
|
|
547
|
+
* Note: Must be used with @Public() to bypass the global VrittiAuthGuard
|
|
548
|
+
* since EventSource cannot send Authorization headers.
|
|
549
|
+
*/
|
|
550
|
+
declare class SseAuthGuard implements CanActivate {
|
|
551
|
+
private readonly jwtService;
|
|
552
|
+
private readonly logger;
|
|
553
|
+
constructor(jwtService: JwtService);
|
|
554
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
555
|
+
/**
|
|
556
|
+
* Set CORS headers for SSE responses
|
|
557
|
+
* Must be called before any potential exceptions
|
|
558
|
+
*/
|
|
559
|
+
private setCorsHeaders;
|
|
514
560
|
}
|
|
515
561
|
|
|
516
562
|
/**
|
|
@@ -582,6 +628,12 @@ interface CookieConfig {
|
|
|
582
628
|
* @default 'strict'
|
|
583
629
|
*/
|
|
584
630
|
refreshCookieSameSite: 'strict' | 'lax' | 'none';
|
|
631
|
+
/**
|
|
632
|
+
* Cookie domain (e.g., 'localhost' for dev, '.vritti.cloud' for prod)
|
|
633
|
+
* Required for cross-subdomain auth (e.g., cloud.localhost accessing localhost API)
|
|
634
|
+
* @default undefined (uses request domain)
|
|
635
|
+
*/
|
|
636
|
+
refreshCookieDomain?: string;
|
|
585
637
|
}
|
|
586
638
|
/**
|
|
587
639
|
* JWT token configuration options
|
|
@@ -674,13 +726,7 @@ declare function resetConfig(): void;
|
|
|
674
726
|
/**
|
|
675
727
|
* Get refresh cookie options (convenience method)
|
|
676
728
|
*/
|
|
677
|
-
declare function getRefreshCookieOptions():
|
|
678
|
-
httpOnly: boolean;
|
|
679
|
-
secure: boolean;
|
|
680
|
-
sameSite: "strict" | "lax" | "none";
|
|
681
|
-
path: string;
|
|
682
|
-
maxAge: number;
|
|
683
|
-
};
|
|
729
|
+
declare function getRefreshCookieOptions(): Record<string, unknown>;
|
|
684
730
|
/**
|
|
685
731
|
* Get JWT expiry settings (convenience method)
|
|
686
732
|
*/
|
|
@@ -2075,30 +2121,25 @@ declare class HttpExceptionFilter implements ExceptionFilter {
|
|
|
2075
2121
|
catch(exception: unknown, host: ArgumentsHost): void;
|
|
2076
2122
|
}
|
|
2077
2123
|
|
|
2124
|
+
declare const SKIP_CSRF_KEY = "skipCsrf";
|
|
2078
2125
|
/**
|
|
2079
|
-
* CSRF
|
|
2126
|
+
* Decorator to skip CSRF validation for specific routes or controllers.
|
|
2127
|
+
* Use this for webhook endpoints that receive requests from external services
|
|
2128
|
+
* (e.g., WhatsApp, Twilio) which cannot include CSRF tokens.
|
|
2080
2129
|
*
|
|
2081
|
-
*
|
|
2082
|
-
*
|
|
2083
|
-
*
|
|
2084
|
-
*
|
|
2085
|
-
*
|
|
2086
|
-
* 2. Skip endpoints marked with @Public()
|
|
2087
|
-
* 3. Validate CSRF token for all other requests
|
|
2088
|
-
*
|
|
2089
|
-
* Token Sources (in priority order by @fastify/csrf-protection):
|
|
2090
|
-
* 1. req.headers['csrf-token']
|
|
2091
|
-
* 2. req.headers['xsrf-token']
|
|
2092
|
-
* 3. req.headers['x-csrf-token']
|
|
2093
|
-
* 4. req.headers['x-xsrf-token']
|
|
2094
|
-
* 5. req.body._csrf
|
|
2130
|
+
* @example
|
|
2131
|
+
* // Skip CSRF for entire controller
|
|
2132
|
+
* @Controller('webhooks')
|
|
2133
|
+
* @SkipCsrf()
|
|
2134
|
+
* export class WebhookController { ... }
|
|
2095
2135
|
*
|
|
2096
|
-
*
|
|
2136
|
+
* @example
|
|
2137
|
+
* // Skip CSRF for specific route
|
|
2138
|
+
* @Post()
|
|
2139
|
+
* @SkipCsrf()
|
|
2140
|
+
* async handleWebhook() { ... }
|
|
2097
2141
|
*/
|
|
2098
|
-
declare
|
|
2099
|
-
private readonly logger;
|
|
2100
|
-
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
2101
|
-
}
|
|
2142
|
+
declare const SkipCsrf: () => _nestjs_common.CustomDecorator<string>;
|
|
2102
2143
|
|
|
2103
2144
|
/**
|
|
2104
2145
|
* HTTP Module
|
|
@@ -2114,6 +2155,19 @@ declare class CsrfGuard implements CanActivate {
|
|
|
2114
2155
|
declare class HttpModule {
|
|
2115
2156
|
}
|
|
2116
2157
|
|
|
2158
|
+
/**
|
|
2159
|
+
* Extract ISO country code from E.164 phone number
|
|
2160
|
+
* @param phone Phone number in E.164 format (e.g., +919876543210)
|
|
2161
|
+
* @returns ISO 3166-1 alpha-2 country code (e.g., "IN") or undefined
|
|
2162
|
+
*/
|
|
2163
|
+
declare function extractCountryFromPhone(phone: string): string | undefined;
|
|
2164
|
+
/**
|
|
2165
|
+
* Normalize phone number to E.164 format with + prefix
|
|
2166
|
+
* @param phone Phone number (with or without + prefix)
|
|
2167
|
+
* @returns Phone number in E.164 format
|
|
2168
|
+
*/
|
|
2169
|
+
declare function normalizePhoneNumber(phone: string): string;
|
|
2170
|
+
|
|
2117
2171
|
/**
|
|
2118
2172
|
* Supported log levels for the logging system.
|
|
2119
2173
|
*/
|
|
@@ -2515,4 +2569,4 @@ declare function generateCorrelationId(): string;
|
|
|
2515
2569
|
*/
|
|
2516
2570
|
declare function addCorrelationIdToResponse(reply: FastifyReply, correlationId: string, headerName?: string): void;
|
|
2517
2571
|
|
|
2518
|
-
export { type ApiErrorResponse, type ApiSdkConfig, AuthConfigModule, BadGatewayException, BadRequestException, BaseFieldException, ConflictException, type CookieConfig, type CorrelationContext, CorrelationIdMiddleware,
|
|
2572
|
+
export { type ApiErrorResponse, type ApiSdkConfig, AuthConfigModule, BadGatewayException, BadRequestException, BaseFieldException, ConflictException, type CookieConfig, type CorrelationContext, CorrelationIdMiddleware, DEFAULT_CORRELATION_HEADER, DatabaseModule, type DatabaseModuleOptions, type FieldError, ForbiddenException, GoneException, type GuardConfig, HttpExceptionFilter, HttpLoggerInterceptor, type HttpLoggerOptions, HttpModule, InternalServerErrorException, type JwtConfig, LOGGER_MODULE_OPTIONS, type LogFormat, type LogLevel, type LogMetadata, LoggerModule, type LoggerModuleAsyncOptions, type LoggerModuleOptions, type LoggerOptionsFactory, LoggerService, MethodNotAllowedException, NotAcceptableException, NotFoundException, NotImplementedException, Onboarding, PayloadTooLargeException, PrimaryBaseRepository, PrimaryDatabaseService, type PrimaryDbConfig, type ProblemDetails, Public, type RegisteredSchema, RequestTimeoutException, SKIP_CSRF_KEY, ServiceUnavailableException, SkipCsrf, SseAuthGuard, Tenant, TenantBaseRepository, TenantContextService, TenantDatabaseService, type TenantInfo, TooManyRequestsException, type TypedDrizzleClient, UnauthorizedException, UnprocessableEntityException, UnsupportedMediaTypeException, UserId, ValidationException, VrittiAuthGuard, addCorrelationIdToResponse, configureApiSdk, correlationStorage, defineConfig, extractCountryFromPhone, generateCorrelationId, getConfig, getCorrelationContext, getHttpStatusTitle, getJwtExpiry, getRefreshCookieOptions, hashToken, normalizePhoneNumber, resetConfig, runWithCorrelationContext, updateCorrelationContext, verifyTokenHash };
|