GithubHelp home page GithubHelp logo

5l1v3r1 / nancy.hal Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nancyhal/nancy.hal

0.0 1.0 0.0 664 KB

Adds support for the Hal Media Type (and Hypermedia) to Nancy

License: MIT License

Ruby 6.60% C# 93.31% Shell 0.09%

nancy.hal's Introduction

Nancy.Hal NuGet Badge

Adds lightweight support for the Hal+JSON media type to Nancy

For Nancy 2.0.0-alpha support there is a pre-release package available.

What is Hal?

Specification

What Nancy.Hal does

  • Allows Nancy web services to return hal+json formatted responses
  • Allows your web services to return plain old JSON/XML representations of your POCOs
  • Does not require your models to inherit from any base classes or implement any interfaces
  • Uses a fluent declarative syntax to configure the links used to decorate your hypermedia resources
  • Works with whatever JSON Serializer you are using with Nancy

What Nancy.Hal does not do

  • Handle hal+xml responses
  • Deserialize Hal representations back into POCOs (HAL is a serialization format, but says nothing about how to update documents)

Get started

  1. Install the Nancy.Hal package
Install-Package Nancy.Hal
  1. Create a HalConfiguration instance.
var config = new HalConfiguration();

//simple example - creates a "self" link templated with the user's id
config.For<UserSummary>()
    .Links(model => new Link("self", "/users/{id}").CreateLink(model));

//complex example - creates paging links populated with query string search terms
config.For<PagedList<UserSummary>>()
      .Embeds("users", x => x.Data)
      .Links(
          (model, ctx) =>
          LinkTemplates.Users.GetUsersPaged.CreateLink("self", ctx.Request.Query, new { blah = "123" }))
      .Links(
          (model, ctx) =>
          LinkTemplates.Users.GetUsersPaged.CreateLink("next", ctx.Request.Query, new { page = model.PageNumber + 1 }),
          model => model.PageNumber < model.TotalPages)
      .Links(
          (model, ctx) =>
          LinkTemplates.Users.GetUsersPaged.CreateLink("prev", ctx.Request.Query, new { page = model.PageNumber - 1 }),
          model => model.PageNumber > 0);


//per request configuration
public ExampleModule()
{
    this.Get["/"] = _ => 
    {
        this.Context
            .LocalHalConfigFor<Users>()
            .Links("relation", "/link");

        return 200;
    };
}
  1. Register it in your application container.
//TinyIOC
container.Register(typeof(IProvideHalTypeConfiguration), config);

//NInject
kernel.Bind<IProvideHalTypeConfiguration>().ToConstant(config);
  1. That's it! Don't forget to set your Accept header to application/hal+json

Acknowledgements

This library could not exist without the work and ideas of others:

nancy.hal's People

Contributors

danbarua avatar dependabot-preview[bot] avatar drewfreyling avatar jchannon avatar mattvane avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.