@DISingleton()@BaseUrl("/api/v2020/app/")exportdefaultclassTaskServiceextendsBaseService{@Get("user")publicgetUser():Promise<IUser>;@Get("tasks")publicgetTasks(@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<ITask[]>@Get("tasks/{id}/attachments")publicgetAttachments(@Path("id")id:number):Promise<ITaskAttachment[]>;@Put("tasks")publiccreateTask(@Bodytask:ITask):Promise<ITask>;@Post("tasks/{id}/attachments")publicuploadAttachment(@Path("id")id:number,@Bodyatt:IAttachment,cancelToken:CancelToken):Promise<void>;}
As easy as it looks, its very easy to configure REST service.
Caching
@Get("tasks",{jsCacheSeconds:900})publicgetTasks(@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<ITask[]>
Caches response in JavaScript for 900 seconds.
Caching based on result
@Get("tasks",{jsCacheSeconds:(r)=>r.length?900:0})publicgetTasks(@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<ITask[]>
Cache response only if returned array has any items.
Fixed Headers
@Get("tasks",{headers:{"x-cache":"none","accept":"application/json"}})publicgetTasks(@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<ITask[]>
Header in Parameters
@Get("tasks")publicgetTasks(@Header("x-auth")auth:string,@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<ITask[]>
Json Parsing Options
@Get("tasks",{jsonOptions:{namingStrategy:"underscore",indent:2,dateConverter:{regex:dateFormatRegex,valueConverter:{fromSource:(v:string)=>Date,fromTarget:(date:Date)=>string}}}})publicgetTasks(@Header("x-auth")auth:string,@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<ITask[]>
Returning Headers !!
@Get("tasks",{returnHeaders:true})publicgetTasks(@Header("x-auth")auth:string,@Query("search")search:string,// default value should be specified in// decorator and not in argument declaration@Query("status","open")status?:string):Promise<IApiResponse<ITask[]>>
The only difference is, result type is always IApiResponse<T>, which contains headers and value.
Mocking
Mocking services makes unit testing and design time development very easy.