@shadow-library/fastify 0.0.9 → 0.0.10
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/README.md
CHANGED
|
@@ -695,6 +695,64 @@ export class DataController {
|
|
|
695
695
|
}
|
|
696
696
|
```
|
|
697
697
|
|
|
698
|
+
### Extending Context Service
|
|
699
|
+
|
|
700
|
+
The `ContextService` can be extended with custom methods to add application-specific functionality while maintaining type safety and method chaining capabilities.
|
|
701
|
+
|
|
702
|
+
```typescript
|
|
703
|
+
import { contextService } from '@shadow-library/fastify';
|
|
704
|
+
|
|
705
|
+
declare module '@shadow-library/fastify' {
|
|
706
|
+
export interface ContextExtension {
|
|
707
|
+
setUserRole(role: string): ContextService;
|
|
708
|
+
getUserRole(): string;
|
|
709
|
+
setCurrentUserId(userId: string): ContextService;
|
|
710
|
+
getCurrentUserId(): string;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
// Extend the context service with custom methods
|
|
715
|
+
contextService.extend({
|
|
716
|
+
setUserRole(role: string) {
|
|
717
|
+
return this.set('user-role', role);
|
|
718
|
+
},
|
|
719
|
+
getUserRole() {
|
|
720
|
+
return this.get<string>('user-role', false);
|
|
721
|
+
},
|
|
722
|
+
setCurrentUserId(userId: string) {
|
|
723
|
+
return this.set('current-user-id', userId);
|
|
724
|
+
},
|
|
725
|
+
getCurrentUserId() {
|
|
726
|
+
return this.get<string>('current-user-id', false);
|
|
727
|
+
},
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
// Use in controllers with method chaining
|
|
731
|
+
@HttpController('/api')
|
|
732
|
+
export class UserController {
|
|
733
|
+
constructor(private readonly contextService: ContextService) {}
|
|
734
|
+
|
|
735
|
+
@Post('/login')
|
|
736
|
+
async login(@Body() loginDto: LoginDto) {
|
|
737
|
+
const user = await this.authService.validateUser(loginDto);
|
|
738
|
+
|
|
739
|
+
// Chain extended methods
|
|
740
|
+
this.contextService.setCurrentUserId(user.id).setUserRole(user.role);
|
|
741
|
+
|
|
742
|
+
return { message: 'Login successful' };
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
@Get('/profile')
|
|
746
|
+
getProfile() {
|
|
747
|
+
return {
|
|
748
|
+
userId: this.contextService.getCurrentUserId(),
|
|
749
|
+
role: this.contextService.getUserRole(),
|
|
750
|
+
requestId: this.contextService.getRID(),
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
```
|
|
755
|
+
|
|
698
756
|
## Examples
|
|
699
757
|
|
|
700
758
|
Check out the [examples](./examples) directory for complete working examples:
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { onRequestHookHandler } from 'fastify';
|
|
2
2
|
import { HttpRequest, HttpResponse } from '../interfaces/index.js';
|
|
3
3
|
type Key = string | symbol;
|
|
4
|
-
export
|
|
4
|
+
export interface ContextExtension {
|
|
5
|
+
}
|
|
6
|
+
export declare class ContextService implements ContextExtension {
|
|
5
7
|
static readonly name = "ContextService";
|
|
6
8
|
private readonly storage;
|
|
7
9
|
init(): onRequestHookHandler;
|
|
@@ -21,5 +23,6 @@ export declare class ContextService {
|
|
|
21
23
|
getResponse(throwOnMissing: false): HttpResponse | null;
|
|
22
24
|
getRID(): string;
|
|
23
25
|
getRID(throwOnMissing: false): string | null;
|
|
26
|
+
extend<T extends ContextExtension = ContextExtension>(extension: T & ThisType<this & T>): this;
|
|
24
27
|
}
|
|
25
28
|
export {};
|
|
@@ -92,6 +92,9 @@ let ContextService = class ContextService {
|
|
|
92
92
|
getRID(throwOnMissing = true) {
|
|
93
93
|
return this.get(RID, throwOnMissing);
|
|
94
94
|
}
|
|
95
|
+
extend(extension) {
|
|
96
|
+
return Object.assign(this, extension);
|
|
97
|
+
}
|
|
95
98
|
};
|
|
96
99
|
exports.ContextService = ContextService;
|
|
97
100
|
exports.ContextService = ContextService = __decorate([
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { onRequestHookHandler } from 'fastify';
|
|
2
2
|
import { HttpRequest, HttpResponse } from '../interfaces/index.js';
|
|
3
3
|
type Key = string | symbol;
|
|
4
|
-
export
|
|
4
|
+
export interface ContextExtension {
|
|
5
|
+
}
|
|
6
|
+
export declare class ContextService implements ContextExtension {
|
|
5
7
|
static readonly name = "ContextService";
|
|
6
8
|
private readonly storage;
|
|
7
9
|
init(): onRequestHookHandler;
|
|
@@ -21,5 +23,6 @@ export declare class ContextService {
|
|
|
21
23
|
getResponse(throwOnMissing: false): HttpResponse | null;
|
|
22
24
|
getRID(): string;
|
|
23
25
|
getRID(throwOnMissing: false): string | null;
|
|
26
|
+
extend<T extends ContextExtension = ContextExtension>(extension: T & ThisType<this & T>): this;
|
|
24
27
|
}
|
|
25
28
|
export {};
|
package/package.json
CHANGED