Browse Source

feat(core): add undefined and empty string params control to rest service

pull/2238/head
mehmet-erim 7 years ago
parent
commit
6b78dbff86
  1. 34
      npm/ng-packs/packages/core/src/lib/services/rest.service.ts

34
npm/ng-packs/packages/core/src/lib/services/rest.service.ts

@ -23,17 +23,31 @@ export class RestService {
config = config || ({} as Rest.Config); config = config || ({} as Rest.Config);
const { observe = Rest.Observe.Body, skipHandleError } = config; const { observe = Rest.Observe.Body, skipHandleError } = config;
const url = (api || this.store.selectSnapshot(ConfigState.getApiUrl())) + request.url; const url = (api || this.store.selectSnapshot(ConfigState.getApiUrl())) + request.url;
const { method, ...options } = request; const { method, params, ...options } = request;
return this.http.request<T>(method, url, { observe, ...options } as any).pipe( return this.http
observe === Rest.Observe.Body ? take(1) : tap(), .request<T>(method, url, {
catchError(err => { observe,
if (skipHandleError) { ...(params && {
return throwError(err); params: Object.keys(params).reduce(
} (acc, key) => ({
...acc,
...(typeof params[key] !== 'undefined' && params[key] !== '' && { [key]: params[key] }),
}),
{},
),
}),
...options,
} as any)
.pipe(
observe === Rest.Observe.Body ? take(1) : tap(),
catchError(err => {
if (skipHandleError) {
return throwError(err);
}
return this.handleError(err); return this.handleError(err);
}), }),
); );
} }
} }

Loading…
Cancel
Save