Skip to content

Commit 334b05c

Browse files
committed
feature: Use httpClient http Method rather than request method
1 parent 604421c commit 334b05c

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

projects/ngx-http-annotations/src/lib/ngx-http-annotations.utils.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ const NO_DELAY = 0;
4949

5050
const MOCK_DEFAULT_DELAY = 500;
5151

52+
export enum RequestMethodParams {
53+
get = 'Get',
54+
post = 'Post',
55+
put = 'Put',
56+
delete = 'Delete',
57+
options = 'Options',
58+
head = 'Head',
59+
patch = 'Patch'
60+
}
61+
5262
export class HttpRestUtils {
5363

5464
public static http: HttpClient = null;
@@ -116,7 +126,7 @@ export class HttpRestUtils {
116126
target[RESOURCE_METADATA_ROOT][entityType] = metadataObj;
117127
}
118128

119-
public static requestMethod(requestMethodName: string): any {
129+
public static requestMethod(requestMethodName: RequestMethodParams): any {
120130
// @dynamic
121131
return (target: any, key: string, descriptor: any) => {
122132
const originalFunction = descriptor.value;
@@ -150,7 +160,7 @@ export class HttpRestUtils {
150160
if (HttpRestUtils.ifUseMock(callConfig)) { // If "use mock" is true, call original function, to get mock directly from function
151161
return HttpRestUtils.processIfUseMock(responseIndex, args, newArgs, originalFunction);
152162
}
153-
const request = HttpRestUtils.http.request(requestMethodName, url, params);
163+
const request = HttpRestUtils.getRequest(callConfig);
154164

155165

156166

@@ -170,6 +180,27 @@ export class HttpRestUtils {
170180
};
171181
}
172182

183+
private static getRequest(callConfig: CallConfig) {
184+
switch (callConfig.requestMethodName) {
185+
case RequestMethodParams.get:
186+
return HttpRestUtils.http.get(callConfig.url, callConfig.params);
187+
case RequestMethodParams.post:
188+
return HttpRestUtils.http.post(callConfig.url, callConfig.params);
189+
case RequestMethodParams.put:
190+
return HttpRestUtils.http.put(callConfig.url, callConfig.params);
191+
case RequestMethodParams.delete:
192+
return HttpRestUtils.http.delete(callConfig.url, callConfig.params);
193+
case RequestMethodParams.options:
194+
return HttpRestUtils.http.options(callConfig.url, callConfig.params);
195+
case RequestMethodParams.head:
196+
return HttpRestUtils.http.head(callConfig.url, callConfig.params);
197+
case RequestMethodParams.patch:
198+
return HttpRestUtils.http.patch(callConfig.url, callConfig.params);
199+
default:
200+
return HttpRestUtils.http.request(callConfig.requestMethodName, callConfig.url, callConfig.params);
201+
}
202+
}
203+
173204
private static ifUseMock(callConfig: CallConfig): boolean | Function {
174205
let useMock: boolean | Function = HttpRestUtils.appInjector.get(HTTP_ANNOTATIONS_USE_MOCKS, false);
175206
if (typeof useMock === 'function') {

projects/ngx-http-annotations/src/public_api.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
export * from './lib/ngx-http-annotations.module';
22
export * from './lib/ngx-http-annotations.utils';
3-
import {HttpRestUtils, path, body, query, headers, produces, observe, response} from "./lib/ngx-http-annotations.utils";
3+
import {
4+
body,
5+
headers,
6+
HttpRestUtils,
7+
observe,
8+
path,
9+
produces,
10+
query,
11+
RequestMethodParams,
12+
response
13+
} from './lib/ngx-http-annotations.utils';
414

515
export let Path = path;
616
export let PathParam = path;
@@ -18,10 +28,10 @@ export let Headers = headers;
1828
export let Produces = produces;
1929

2030
// Request methods
21-
export let GET = HttpRestUtils.requestMethod('Get');
22-
export let POST = HttpRestUtils.requestMethod('Post');
23-
export let PUT = HttpRestUtils.requestMethod('Put');
24-
export let DELETE = HttpRestUtils.requestMethod('Delete');
25-
export let OPTIONS = HttpRestUtils.requestMethod('Options');
26-
export let HEAD = HttpRestUtils.requestMethod('Head');
27-
export let PATCH = HttpRestUtils.requestMethod('Patch');
31+
export let GET = HttpRestUtils.requestMethod(RequestMethodParams.get);
32+
export let POST = HttpRestUtils.requestMethod(RequestMethodParams.post);
33+
export let PUT = HttpRestUtils.requestMethod(RequestMethodParams.put);
34+
export let DELETE = HttpRestUtils.requestMethod(RequestMethodParams.delete);
35+
export let OPTIONS = HttpRestUtils.requestMethod(RequestMethodParams.options);
36+
export let HEAD = HttpRestUtils.requestMethod(RequestMethodParams.head);
37+
export let PATCH = HttpRestUtils.requestMethod(RequestMethodParams.patch);

0 commit comments

Comments
 (0)