Search code examples
angularrestangular

Can two custom methods be added to ngx-restangular resource?


I'm configuring an ngx-restangular resource with more than one method, but only the first method is being recognized as a function. With the following configuration, the feature function is recognized, but not the nearest function.

restangular.withConfig(
  (configurer) => {

    /* Add Auth header to each request. */
    configurer.addFullRequestInterceptor(
      (element, operation, path, url, headers, params) => {
        return {
          headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
        };
      }
    );

    /* For a given locationId, provide a feature set (geometry) for the location. */
    configurer.addElementTransformer('location', false,
      (resource) => {
        resource.addRestangularMethod(
          'feature',
          'get',
          'map'
        );
        return resource;
      }
    );

    /* Resource providing Nearest Location instances suitable for edit. */
    configurer.addElementTransformer('location', true,
      (resource) => {
        resource.addRestangularMethod(
          'nearest',
          'get',
          'nearest'
        );
        return resource;
      }
    );

  }
);
return restangular.service('location');

Is it possible to add more than one Restangular Method?


Solution

  • It is possible. I got this working by invoking withConfig for each method to be added:

    restangular.withConfig(
      (configurer) => {
    
        /* Add Auth header to each request. */
        configurer.addFullRequestInterceptor(
          (element, operation, path, url, headers, params) => {
            return {
              headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
            };
          }
        );
    
        /* For a given locationId, provide a feature set (geometry) for the location. */
        configurer.addElementTransformer('location', false,
          (resource) => {
            resource.addRestangularMethod(
              'feature',
              'get',
              'map'
            );
            return resource;
          }
        );
    
      }
    );
    
    restangular.withConfig(
      (configurer) => {
    
        /* Add Auth header to each request. */
        configurer.addFullRequestInterceptor(
          (element, operation, path, url, headers, params) => {
            return {
              headers: Object.assign({}, headers, {Authorization: `Bearer ${creds.getBearerToken()}`})
            };
          }
        );
    
        /* Resource providing Nearest Location instances suitable for edit. */
        configurer.addElementTransformer('location', true,
          (resource) => {
            resource.addRestangularMethod(
              'nearest',
              'get',
              'nearest'
            );
            return resource;
          }
        );
    
      }
    );
    return restangular.service('location');
    

    Adding the auth header for each is also necessary when added as part of the withConfig.