-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.json
316 lines (315 loc) · 10.7 KB
/
snippets.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
{
"Angular CRUD Service Setup": {
"prefix": "deloitte-angular-service-CRUD-setup",
"description": "Angular 8 Template for CRUD operations.",
"body": [
"",
"import { Injectable } from '@angular/core';",
"import { HttpClient, HttpHeaders } from '@angular/common/http';",
"import { Observable, of } from 'rxjs';",
"import { catchError, map, tap, switchMap, debounceTime, distinctUntilChanged } from 'rxjs/operators';",
"",
"const httpOptions = {",
" headers: new HttpHeaders({ 'Content-Type': 'application/json' })",
"};",
"",
"@Injectable({",
" providedIn: 'root'",
"})",
"export class YourServiceName {",
"",
"",
"constructor(private http: HttpClient) { }",
"",
"// written for Deloitte Consulting LLC and Compatible with Angular 7/8 and rxjs: 6.3.3 ",
"",
"",
"${1:apiName}: string = 'http://www.server.com/api/';",
"",
"controllername : string = '${2:ControllerName}';",
"",
"CreateURL : string = this.${1} + this.controllername + '/CreateActionName';",
"DeleteURL : string = this.${1} + this.controllername + '/DeleteActionName';",
"UpdateURL : string = this.${1} + this.controllername + '/UpdateActionName';",
"GetAllRecordsURL : string = this.${1} + this.controllername + '/getAllRecordsActionName';",
"GetRecordsByIdURL : string = this.${1} + this.controllername + '/getRecordsActionName';",
"",
"",
" //-------------- Get All ${3}s ------------",
"",
"",
"public getAll${3:Action}s(): Observable<any> {",
"return this.http.get<any>(this.GetAllRecordsURL).pipe(",
"catchError((error: any) => {",
"console.error(error);",
"return of();",
"}),",
");",
"}",
"",
"",
" //-------------- Create A ${3} ------------",
"",
"",
"public add${3}(recordData): Observable<any> {",
"return this.http.post(this.CreateURL,recordData).pipe(",
"catchError((error: any) => {",
"console.error(error);",
"return of();",
"}),",
");",
"}",
"",
"",
" //-------------- Get ${3} by ID ------------",
"",
"",
"public get${3}ById(recordid: any): Observable<any> {",
"return this.http.get<any>(`${this.GetRecordsByIdURL}/${recordId}`).pipe(",
"catchError((error: any) => {",
"console.error(error);",
"return of();",
"}),",
");",
"}",
"",
"",
"//-------------- Update a ${3} ------------",
"",
"",
"public update${3}(recordUpdate): Observable<any> {",
"return this.http.put(this.UpdateURL, recordUpdate, httpOptions).pipe(",
"catchError((error: any) => {",
"console.error(error);",
"return of();",
"}),",
");",
"}",
"",
" //--------------Delete A ${3} ------------",
"",
"public delete${3}(recordid: any): Observable<any> {",
"return this.http.delete(this.DeleteURL+'/recordid').pipe(",
"catchError((error: any) => {",
"console.error(error);",
"return of();",
"}),",
");",
"}",
"",
"",
"}",
""
]
},
"Angular Service Subscribe": {
"prefix": "deloitte-angular-service-Subscribe",
"description": "Angular 8 Template for Subscribing to Service.",
"body": [
"// written for : Deloitte Consulting LLC",
"",
"uncomment below model ",
"// model:any",
"",
"getAll${3:Action}s() {",
"",
"this.service.getRecords()",
".subscribe(res => {",
"this.data = res;",
"console.log(this.data);",
"}, err => {",
"console.log(err);",
" // this.isLoadingResults = false;",
"});",
"}",
"",
"",
"get${3}Byid(id) {",
"this.service.getRecordById(id)",
".subscribe(data => {",
"this.model = data;",
"console.log(this.product);",
" // this.isLoadingResults = false;",
"});",
"}",
"",
"",
"delete${3}(id) {",
"this.service.deleteRecord(id)",
".subscribe(res => {",
" // this.router.navigate(['/listRecords']);",
"}, (err) => {",
"console.log(err);",
" // this.isLoadingResults = false;",
"}",
");",
"}",
"",
"create${3}() {",
"this.service.addRecord(this.model)",
".subscribe(",
"data => {",
"// this.alertService.success('created record successful', true);",
"// this.router.navigate(['/login']);",
"},",
"error => { ",
"// this.alertService.error(error); ",
"// this.loading = false; ",
"});",
"}",
"update${3}() {",
"this.Service.updateRecord(this.model)",
".subscribe(",
"data => {",
"// this.alertService.success('updated successful', true);",
"// this.router.navigate(['/listReords']);",
"},",
"error => { ",
"// this.alertService.error(error); ",
"// this.loading = false; ",
"});",
"}"
]
},
"Angular Component": {
"prefix": "del-angular-component",
"description": "Creates standar Angular2+ component",
"body": [
"import { Component, OnInit } from '@angular/core';",
"",
"@Component({",
"\tselector: '${1:selector-name}',",
"\ttemplateUrl: '${2:name}.component.html'",
"})",
"",
"export class ${3:Name}Component implements OnInit {",
"\tconstructor() { }",
"",
"\tngOnInit() { }$0",
"}"
]
},
"Angular Module": {
"prefix": "del-angular-module",
"description": "Creates standard Angular2+ module template",
"body": [
"import { NgModule } from '@angular/core';",
" ",
"@NgModule({",
" declarations: [${1:Name}Component],",
" entryComponents: [${1:Name}Component],",
"})",
"export class ${1:Name}Module { }"
]
},
"Angular Http Interceptor": {
"prefix": "del-http-interceptor",
"description": "Intercept an outgoing HttpRequest and optionally transform it or the response.",
"types": "typescript",
"body": [
"import { Injectable } from '@angular/core';",
"import {",
"\tHttpEvent, HttpInterceptor, HttpHandler, HttpRequest",
"} from '@angular/common/http';",
"",
"@Injectable()",
"export class ${1:Name}Interceptor implements HttpInterceptor {",
"\tintercept(req: HttpRequest<any>, next: HttpHandler) {",
"\t\treturn next.handle(req);",
"\t}",
"}"
]
},
"Angular HttpInterceptor for Logging": {
"prefix": "del-http-interceptor-logging",
"description": "Angular HttpInterceptor for Logging HttpClient",
"body": [
"import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse } from '@angular/common/http';",
"import { Observable,forkJoin } from 'rxjs';",
"import { tap, map } from 'rxjs/operators';",
"import { Injectable } from '@angular/core';",
"",
"@Injectable({providedIn: ${1:'root'}})",
"export class LogInterceptor implements HttpInterceptor {",
"\tconstructor() {}",
"",
"\tintercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {",
"\t\tconst started = Date.now();",
"\t\treturn next.handle(req).pipe(",
"\t\t\ttap(event => {",
"\t\t\t\tif (event instanceof HttpResponse) {",
"\t\t\t\t\tconst elapsed = Date.now() - started;",
"\t\t\t\t\tconsole.log(`Request for \\${req.urlWithParams} took \\${elapsed} ms.`);",
"\t\t\t\t}",
"\t\t\t})",
"\t\t);",
"\t}",
"}"
]
},
"Angular Error Interceptor":{
"prefix":"del-http-error-intercceptor",
"description":"Angular Http Error Interceptor",
"body":[
"import { Injectable } from '@angular/core';",
"import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';",
"import { Observable, throwError } from 'rxjs';",
"import { catchError } from 'rxjs/operators';",
"import { AuthenticationService } from '../_services';",
"",
"@Injectable()",
"export class ErrorInterceptor implements HttpInterceptor {",
"\tconstructor(",
"// private authenticationService: AuthenticationService",
") { }",
"",
"\tintercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {",
"\t\t\t\t\t\treturn next.handle(request).pipe(catchError(err => {",
" \t\t\t\t\t\t\t\tif ([401, 403].indexOf(err.status) !== -1) {",
" \t\t\t\t\t\t\t\t\t\t\t// auto logout if 401 Unauthorized or 403 Forbidden response returned from api",
" \t\t\t\t\t\t\t\t\t\t\t\t\tthis.authenticationService.logout();",
" \t\t\t\t\t\t\t\t\t\t\t\t\t\tlocation.reload(true);",
"\t\t\t\t\t\t\t\t\t\t\t\t\t}",
"",
"\t\t\t\t\t\t\t\t\tconst error = err.error.message || err.statusText;",
"\t\t\t\t\t\t\t\t\treturn throwError(error);",
"\t\t\t\t }))",
"\t\t\t }",
"\t} "
]
},
"Angular JWT interceptor":{
"prefix":"del-http-jwt-interceptor",
"description": "Angular JWT interceptor",
"body":[
"import { Injectable } from '@angular/core';",
"import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';",
"import { Observable } from 'rxjs';",
"import { environment } from '../../environment';",
"import { AuthenticationService } from '../_services';",
"",
"@Injectable()",
"export class JwtInterceptor implements HttpInterceptor {",
"\t\t\tconstructor(",
"\\private authenticationService: AuthenticationService",
"){ }",
"",
"\t\t\tintercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {",
" // add auth header with jwt if user is logged in and request is to api url",
"\t\t\t\tconst currentUser = this.authenticationService.currentUserValue;",
"\t\t\t\tconst isLoggedIn = currentUser && currentUser.token;",
"\t\t\t\tconst isApiUrl = request.url.startsWith(environment.apiUrl);",
"\t\t\t\tif (isLoggedIn && isApiUrl) {",
"\t\t\t\t\t\trequest = request.clone({",
"\t\t\t\t\t\t\t\tsetHeaders: {",
"\t\t\t\t\t\t\t\t\t\tAuthorization: `Bearer ${currentUser.token}`",
"\t\t\t\t\t\t\t\t}",
"\t\t\t\t\t\t\t});",
"\t\t\t\t\t\t}",
"",
"\t\t\t\t\treturn next.handle(request);",
"\t\t\t\t}",
"}"
]
}
}