UNPKG

@yaagoub/decorators

Version:

Angular decorators

163 lines (134 loc) 4.63 kB
# decorators ## Official Documentation For detailed documentation, visit [yaagoub.org decorators](https://yaagoub.org/docs/decorators/http/introduction). # 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: ```ts import { provideDecoratorsConfig } from '@yaagoub/decorators'; export const appConfig: ApplicationConfig = { providers: [ provideDecoratorsConfig(), ], }; ``` ## Example ```ts 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 ```typescript 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 ```typescript 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" } ``` ## Decoradores HTTP #### for setup please visit [SETUP](https://yaagoub.org/docs/decorators/http/introduction) ### 501. `@Get` – Defines a GET request handler for a specific route. ### 502. `@Post` – Defines a POST request handler for a specific route. ### 503. `@Put` – Defines a PUT request handler for a specific route. ### 504. `@Delete` – Defines a DELETE request handler for a specific route. ### 505. `@Patch` – Defines a PATCH request handler for a specific route. ### 506. `@Head` – Defines a HEAD request handler for a specific route. ### 507. `@Options` – Defines an OPTIONS request handler for a specific route. ### 508. `@HttpClient` - Defines a base endpoint ### 509. `@Path` - Defines a param ### 510. `@body` - Defines a body of the request ### 514. `@Mapper` - Maps properties from one type to anotherrepresentation ### .................................................................... #### for setup please visit [SETUP](https://yaagoub.org/docs/decorators/logging/introduction) ### 516. `@LogDebug` - Logs debug information to the server or to the console ### 517. `@LogError` - Logs error information to the server or to the console ### 518. `@LogInfo` - Logs informational messages to the server or to the console ### 519. `@LogWarn` - Logs warning messages to the server or to the console ### 520. `@Log` - Logs class general information to the server or to the console ### ....................................................................