Coder Social home page Coder Social logo

azure-functions-http-api's Introduction

HTTP API Extensions for Azure Functions

Build Downloads NuGet License

Features

  • Better route precedence
  • Model validation
  • ASP.NET Core like helpers
  • Support URL generation
  • Handle static files
  • Simple reverse proxy
  • Streamlined SPA / SSG hosting

Installation

Install-Package WebJobs.Extensions.HttpApi
dotnet add package WebJobs.Extensions.HttpApi
// Inherits from `HttpFunctionBase` class
public class Function1 : HttpFunctionBase
{
    public Function1(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function1")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
        ILogger log)
    {
        return Ok($"Hello, {req.Query["name"]}");
    }
}

Examples

Model validation

public class Function1 : HttpFunctionBase
{
    public Function1(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function1")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")]
        SampleModel model,
        ILogger log)
    {
        if (!TryValidateModel(model))
        {
            return BadRequest(ModelState);
        }

        return Ok(model);
    }
}

public class SampleModel
{
    [Required]
    public string Name { get; set; }

    public string[] Array { get; set; }

    [Range(100, 10000)]
    public int Price { get; set; }
}

ASP.NET Core like helpers

public class Function2 : HttpFunctionBase
{
    public Function2(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function2")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get")]
        HttpRequest req,
        ILogger log)
    {
        Response.Headers.Add("Cache-Control", "no-cache");

        return Ok($"Now: {DateTime.Now}");
    }
}

Support URL generation

public class Function3 : HttpFunctionBase
{
    public Function3(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function3")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "route/{id}")]
        HttpRequest req,
        string id,
        ILogger log)
    {
        return CreatedAtFunction("Function3", new { id = "kazuakix" }, null);
    }
}

Handle static files

public class Function1 : HttpFunctionBase
{
    public Function1(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function1")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequest req,
        ILogger log)
    {
        return File("sample.html");
    }
}

Simple reverse proxy

public class Function1 : HttpFunctionBase
{
    public Function1(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function1")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{*path}"})] HttpRequest req,
        ILogger log)
    {
        return Proxy("https://example.com/{path}");
    }
}

Streamlined SPA / SSG hosting

public class Function1 : HttpFunctionBase
{
    public Function1(IHttpContextAccessor httpContextAccessor)
        : base(httpContextAccessor)
    {
    }

    [FunctionName("Function1")]
    public IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{*path}"})] HttpRequest req,
        ILogger log)
    {
#if USE_REMOTE
        return RemoteStaticApp("https://example.com", fallbackExclude: $"^/_nuxt/.*");
#else
        return LocalStaticApp(fallbackPath: "404.html", fallbackExclude: $"^/_nuxt/.*");
#endif
    }
}

License

This project is licensed under the MIT License

azure-functions-http-api's People

Contributors

dependabot[bot] avatar shibayan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

azure-functions-http-api's Issues

TryValidateModel is not working for nested attributes

Hi Shibayan,

When using TryValidateModel method DataAnnotations attributes (for example [Required]) on nested objects/properties are not validated.

public class SampleModel
{
    [Required]
    public string Name { get; set; }

    public string[] Array { get; set; }

    public NestedModel Nested { get; set; }
}

public class NestedModel
{
    [Required]
    public string Name { get; set; }
}

When posting the SampleModel and omitting the Name property, TryValidateModel returns true and not false as expected.

Tried with:

{
    "name": "test",
    "nested":{       
    }
}

and also with:

{
    "name": "test"
}

This is what I expected:
image

Is this something you are aware of or a bug?

Ps I found a fix for the behavior described above, and if you are interested I can provide a PR.

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.