Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Harden convertToHttpParams against TypeError #140

Open
K-e-l-v-i-n-12 opened this issue Oct 29, 2024 · 0 comments
Open

Harden convertToHttpParams against TypeError #140

K-e-l-v-i-n-12 opened this issue Oct 29, 2024 · 0 comments

Comments

@K-e-l-v-i-n-12
Copy link

I have noticed that the convertToHttpParams method is susceptible to undefined parameters. This can be verified using the following example:

public getCars(filter?: CarFilter): Observable<ResourceCollection<CarItem>> {
  const params = {
    ...filter
    // additional params...
  };
  return this.resourceHateoasService.getCollection(CarItem, { params });
}

This method is now called by a business service:

getCarsByPerson(manufacturer: string | undefined, personId: string): Observable<CarItem[]> {
  const filter = {
    manufacturer,
    personId
  } as CarFilter;
  return this.carResourceService.getCars(filter).pipe(
    catchError(() => { return of(new ResourceCollection<CarItem>()) }),
    map(page => page.resources)
  );
}

If a call is now made without a manufacturer, the following error is thrown:

ERROR TypeError: value is undefined
    convertToHttpParams lagoshny-ngx-hateoas-client.mjs:517
    convertToHttpOptions lagoshny-ngx-hateoas-client.mjs:540
    get lagoshny-ngx-hateoas-client.mjs:1623
    getResourceCollection lagoshny-ngx-hateoas-client.mjs:1653
    getCollection lagoshny-ngx-hateoas-client.mjs:2406
    getCars car-resource.service.ts:19

As a workaround, the filter can be created as follows:

const filter = {
  ...(manufacturer !== undefined && { manufacturer }),
  personId
} as CarFilter;

I would like ngx-hateoas-client to be able to handle undefined parameters in the future. I look forward to your feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant