@yaagoub/decorators
v2.0.8
Published
Angular decorators
Maintainers
Readme
decorators
Official Documentation
For detailed documentation, visit yaagoub.org decorators.
Request Handlers with @yaagoub/decorators
This guide demonstrates how to handle HTTP requests in Angular using decorators from the @yaagoub/decorators library.
Setup
Use provideDecoratorsConfig in your app.config.ts to initialize global configuration:
import { provideDecoratorsConfig } from '@yaagoub/decorators';
export const appConfig: ApplicationConfig = {
providers: [
provideDecoratorsConfig(),
],
};Example
import { HttpClient, Get, returnType } from '@yaagoub/decorators';
import { Observable } from 'rxjs';
@HttpClient('/sources/api/v1')
export class ViewsService {
@Get({
path: '/views',
options: {
responseType: 'json',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token',
},
},
})
getViews(): Observable<any> {
return returnType<any>();
}
}
// Usage
const views = new ViewsService();
views.getViews().subscribe({
next: (res) => console.log(res),
error: (err) => console.error(err),
complete: () => console.log('Completed'),
});Usage
import { BasePath, Get, Res, HttpRespose } from '@yaagoub/decorators';
@HttpClient('/sources/api/v1')
@DefaultHeaders({
'x-test': 'class-header',
'x-test-writable': 'class-header-write', // this header is writable (4º priority)
'x-class-only': 'class-only-value',
})
export class ViewsService {
@Get({
path: '/testMethod',
options: {
headers: {
'x-test-1': 'decorator-header-1',
'x-test-writable': 'decorator-header-write', // this header is writable (2º priority)
'x-get-options-only': 'get-options-only-value',
},
},
})
@Headers({
'x-test-2': 'method-header-2',
'x-test-writable': 'method-header-write', // this header is writable (3º priority)
'x-method-only': 'method-only-value',
})
getViews(
@Header('x-test-3') header: string,
@Header('x-test-writable') writableHeader: string, // this header is writable (1º priority)
): Observable<any> {
return returnType();
}
}
const views = new ViewsService()
views.getViews('x-test-3-value','x-test-writable-value').subscribe({
next: (res: any) => {
console.log(res)
},
error: (err: any) => {
console.error(err)
},
complete: () => {
console.log('completed')
}
})
;
Mapper Decorator
import { Mapper } from '@yaagoub/decorators';
interface DestinationType {
property1: string;
property2: number;
propertyC: string;
}
interface SourceType {
property1: string;
property2: number;
propertyC: string;
}
export class MapperToDestination {
@Mapper<DestinationType>()
static toDestination(source: SourceType): DestinationType {
return source as any;
}
}
const example = new ExampleClass();
const result = example.exampleMethod();
console.log(result);
// Output: { propertyA: "example", propertyB: 42, propertyC: "example" }