api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.4k stars 859 forks source link

Json HAL: URI template implementation #2120

Open tounefr opened 6 years ago

tounefr commented 6 years ago

First, thank you for this amazing project that saved me a lot of time to create my API!

For my VueJS application, i would like to implement an auto discoverable API with HATEOAS + JSON HAL (imposed for an educational project).

In my client side, i use Traverson to request my API but without URI template i can't fetch a document with an ID parameter.

https://github.com/traverson/traverson/blob/master/user-guide.markdown#uri-templates

traverson
.from('https://demo.api-platform.com/')
.follow('book', 'article')
.jsonHal()
.withTemplateParameters({ id: 1 })
.getResource(function(error, doc) {
  ...
});

https://demo.api-platform.com/books.jsonhal

{
    "_links": {
        "self": {
            "href": "\/books.jsonhal?page=1"
        },
        "first": {
            "href": "\/books.jsonhal?page=1"
        },
        "last": {
            "href": "\/books.jsonhal?page=4"
        },
        "next": {
            "href": "\/books.jsonhal?page=2"
        },
        "item": [{
            "href": "\/books\/1"
        }, {
            "href": "\/books\/2"
        }]
    },
    ...
}

With URI template it would be something like:

{
    "_links": {
        "self": {
            "href": "\/books.jsonhal?page=1"
        },
        "first": {
            "href": "\/books.jsonhal?page=1"
        },
        "last": {
            "href": "\/books.jsonhal?page=4"
        },
        "next": {
            "href": "\/books.jsonhal?page=2"
        },
        "article": "\/articles\/{id}",
        "item": [{
            "href": "\/books\/1"
        }, {
            "href": "\/books\/2"
        }]
    },
    ...
}

At the moment, i've not found a way to request the API without providing the URI to fetch a document (collection it's okey). Are URI templates planned to be implemented ?

Thanks

FireLizard commented 4 years ago

Maybe one could try to extend ApiPlatform\Core\Hal\Serializer\EntrypointNormalizer (service id api_platform.hal.normalizer.entrypoint) and add HAL capabilities of curies ;)