jcassee / angular-hypermedia

Hypermedia REST API client for AngularJS applications
MIT License
38 stars 5 forks source link

halresource not resulting from a $post of a halresource #41

Open gregmacbean opened 7 years ago

gregmacbean commented 7 years ago

I am using angular-hypermedia with HalResources, I have set this as the default for my HalContext.

When I do a post using the following:

provider.$linkRel('create-client', halResource).$post({ data: 'data' }).then(function(response){ 
    console.log(typeof response.data)
})

The typeof the response.data is a Resource, and not a HalResource as I expected.

Two questions:

  1. Should angular-hypermedia be updated to return a HalResource from a post when this is the expected factory.

  2. I am struggling to create a HalResource from the Resource, there seems to be some unexpected side affects, I keep getting httpPost on context undefined when using:

new halResource(createdClient._links['create-invoice'].href, myContext).$linkRel // (or $post)

The console error is :

angular.js:13645 TypeError: this.$context.httpPost is not a function
at HalResource.value (http://localhost/app/providers/jspm_packages/npm/angular-hypermedia@1.0.0/dist/hypermedia.js:934:30)

The code at location being,

$post: {value: function (data, headers, callback) {
    return this.$context.httpPost(this, data, headers, callback);
}}

Where to from here?

jcassee commented 7 years ago

Are you sure the type of response.data is even a Resource? I would expect it to be plain JSON. Because the response type from a POST is pretty much undefined.

Also, when you set the resource type (as you set it to HalResource in your example), this does not "propagate". I mean that in this example, resource1 is a HalResource but resource2 is a Resource.

var resource2 = new ResourceContext().get('http://example.com/resource1').$linkRel('relation1', HalResource).$linkRel('relation2');

If you want every resource in a context to be a HalResource (except when you override it), create the context resource like this:

var context = new ContextResource(HalResource);

To anwer you questions:

  1. No, the result from a POST is not necessarily a resource, it is often a result of some kind of processing. We might want to treat a response with status code 201 and a Location header differently, but that is currently not implemented. Pull requests welcome! :smile:
  2. You are not expected to instantiate resources yourself, but to get them from a context: new ResourceContext(HalResource).get(someUrl).

Does this help?

jcassee commented 7 years ago

@cegoya Is this issue still relevant?