deleteman / vatican

Micro-framework designed to create REST APIs with minor effort
98 stars 6 forks source link

Support automatic version on url / add constructor option to set it #41

Closed deleteman closed 6 years ago

deleteman commented 9 years ago

when doing new Vatican we should be able to add a version to the API, so we can use it during url matching.

Also, think about adding support for different versioning schemes? So by default use only major version number, but allow to setup a function that returns the version number.

Example:

   var vat = new Vatican({version: "1.0.4"})
  //will generate routes like this: /1/xxx/xxx
  var vat = new Vatican({version: "1.0.4", versionFormat: function(v) { return "v" + v.split(".")[0] })
  //will generate routes like this: /v1/xxx/xxxx
deleteman commented 6 years ago

Versioning your API

By default, there is no versioning available. On the constructor, you can set the versioning strategy.

new Vatican({
    versioning: true
    })

Will enable default versioning behavior, which means it'll use the major version of each endpoint as part of the URL prefixed by a "v", i.e: /v1/your/endpoint

new Vatican({
    versioning: {
        strategy: "url",
        matching: (urlV, endpointV) {
            let v = urlV.substring(1);
            return +v == +endpointV.split(".")[0];
        }
    }
    })

Using the strategy property, you can set a custom version matcher for your URLs. In the above example, it'll match URLs as /1/your/api

You can also tell Vatican to use header versioning instead of modifying your URLs

new Vatican({
    versioning: {
        strategy: "header"
    }
    })

The header used will be the "Accept" header, with the following format:

accept/vnd.vatican-version.[version number]+json

Specifying your endpoint's version compatibility

@endpoint (url: /books method:post versions:[1.2, 1.3, 1.4])

Matching function

This function is optional, if you don't provide a matching function, urls will contain the full version number you define for your endpoint (i.e /1.2.3/your/endpoint) and that number will be use to match it during the request.