All Projects → cmeeren → FSharp.JsonApi

cmeeren / FSharp.JsonApi

Licence: MIT license
Use F# to create and consume flexible, strongly typed web APIs following the JSON:API specification

Programming Languages

F#
602 projects
HTML
75241 projects

Projects that are alternatives of or similar to FSharp.JsonApi

Json Api
Implementation of JSON API in PHP 7
Stars: ✭ 171 (+755%)
Mutual labels:  json-api, jsonapi
Hotels server
酒店预订系统后台管理系统
Stars: ✭ 249 (+1145%)
Mutual labels:  json-api, api-rest
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (+855%)
Mutual labels:  json-api, jsonapi
Jsonapi.rb
Lightweight, simple and maintained JSON:API support for your next Ruby HTTP API.
Stars: ✭ 116 (+480%)
Mutual labels:  json-api, jsonapi
jsonapi-serializable
Conveniently build and efficiently render JSON API resources.
Stars: ✭ 43 (+115%)
Mutual labels:  json-api, jsonapi
Jsona
Data formatter that creates simplified objects from JSON or stored reduxObject, creates JSON from the same simplified objects (in according with JSON API specification)
Stars: ✭ 144 (+620%)
Mutual labels:  json-api, jsonapi
Jsonapi Rails
Rails gem for fast jsonapi-compliant APIs.
Stars: ✭ 242 (+1110%)
Mutual labels:  json-api, jsonapi
Jsonapi Client
JSON API (jsonapi.org) client for Python
Stars: ✭ 63 (+215%)
Mutual labels:  json-api, jsonapi
php-serializer
Serialize PHP variables, including objects, in any format. Support to unserialize it too.
Stars: ✭ 47 (+135%)
Mutual labels:  json-api, jsonapi
rest-api
Laravel restfull api boilerplate
Stars: ✭ 57 (+185%)
Mutual labels:  json-api, api-rest
Symfony Jsonapi
JSON API Transformer Bundle for Symfony 2 and Symfony 3
Stars: ✭ 114 (+470%)
Mutual labels:  json-api, jsonapi
fire
An idiomatic micro-framework for building Ember.js compatible APIs with Go.
Stars: ✭ 56 (+180%)
Mutual labels:  json-api, jsonapi
Laravel Api Boilerplate
A Boilerplate Project For Laravel API's (NOT MAINTAINED)
Stars: ✭ 113 (+465%)
Mutual labels:  json-api, api-rest
Coloquent
Javascript/Typescript library mapping objects and their interrelations to JSON API, with a clean, fluent ActiveRecord-like (e.g. similar to Laravel's Eloquent) syntax for creating, retrieving, updating and deleting model objects.
Stars: ✭ 149 (+645%)
Mutual labels:  json-api, jsonapi
Loopback Component Jsonapi
JSONAPI support for loopback.
Stars: ✭ 104 (+420%)
Mutual labels:  json-api, jsonapi
Jsonapi Rb
Efficiently produce and consume JSON API documents.
Stars: ✭ 219 (+995%)
Mutual labels:  json-api, jsonapi
Jsonapi parameters
Rails-way to consume JSON:API input
Stars: ✭ 50 (+150%)
Mutual labels:  json-api, jsonapi
Json Api Dart
JSON:API client for Dart/Flutter
Stars: ✭ 53 (+165%)
Mutual labels:  json-api, jsonapi
MyAPI
A template to create awesome APIs easily ⚡️
Stars: ✭ 117 (+485%)
Mutual labels:  web-api, api-server
dart-express
Express-like HTTP framework written in Dart
Stars: ✭ 34 (+70%)
Mutual labels:  api-server, api-rest

FSharp.JsonApi


No longer actively maintained, please check out Felicity


FSharp.JsonApi is a library (not framework) that allows you to use F# to easily create and consume flexible, strongly typed web APIs following the JSON:API specification. There’s even an almost-production-ready API implementation sample to get you started on the right foot!

Core features:

  • Full support for sparse fieldsets and included resources
  • Support for loading included resources asynchronously on-demand, in parallel
  • Uses FSharp.JsonSkippable for strong typing of whether JSON properties are included or excluded
  • Plays very nicely with the robust error handling of FsToolkit.ErrorHandling, whether monadic or applicative (the latter works perfectly to return multiple JSON:API errors at once)
  • Lots of utilities to allow you to easily wire up your perfect domain snowflakes (single-case DU wrappers with smart constructors, Result-returning record field setters, etc.) with the raw resource attributes and relationships present in a JSON:API request
  • And much more

The focus is on server implementations, but it may also be useful when implementing clients (please get in touch!).

Production readiness

We use this library for at least three mission-critical production APIs. I have developed and tweaked it internally for around one and a half years before finally polishing it and publishing to NuGet. I’m not claiming it’s perfect, or even bug-free, but it’s battle-tested, and I have a vested interest in keeping this library working properly.

Installation

FSharp.JsonApi consists of three NuGet packages:

  • FSharp.JsonApi contains all the core stuff: JSON:API document models for serialization/deserialization, resource builders, parsing and validation of query parameters and documents, helpers for calling domain code based on a JSON:API request, etc. If you don’t use ASP.NET Core, you can easily use this library to build your own abstractions.
  • FSharp.JsonApi.AspNetCore contains lots of useful helpers and additional overloads for parsing and validating requests using ASP.NET Core’s HttpContext.
  • FSharp.JsonApi.Giraffe contains a few simple HttpHandlers that may be useful if using Giraffe.

Install all packages that are relevant for you. You can get away with installing only the highest-level package – e.g. FSharp.JsonApi.Giraffe – and have the rest installed automatically as transitive dependencies, but depending on your package manager, you might not be able to easily update the lower-level (transitive) packages (Paket is better at this than NuGet).

Contributing

Contributions and ideas are welcome! Please see Contributing.md for details.

Quick start

I highly recommend you check out the sample API in this repo, which is a simple but complete and almost-production-ready example API implementation. Open the main solution in VS, start at the topmost file, and read through the project in compilation order. There are lots of comments along the way to explain what’s going on.

As a very short introduction, I hope the steps below are useful, but bear in mind that they only skim the surface.

1. Define resources

Define each resource’s attributes and relationships using records, enums, and any relevant .NET attributes. The names will be used as-is; use double backticks for special names. Use option for nullable attributes. Use ToOne and ToMany for relationships. All attributes and relationships must be wrapped in Skippable (see FSharp.JsonSkippable).

type ArticleType =
  | personal = 0
  | commercial = 1

[<CLIMutable>]
type ArticleAttrs = {
  title: string Skippable
  articleType: ArticleType Skippable
  [<ReadOnly>] updated: DateTimeOffset option Skippable
}

[<CLIMutable>]
type ArticleRels = {
  [<NotNull; AllowedTypes("person")>]
  author: ToOne Skippable
  [<ReadOnly; AllowedTypes("comment")>]
  comments: ToMany Skippable
}

2. Define the resource discriminator and the JsonApiContext

The resource discriminator is a DU where each case is a Resource<'attrs, 'rels>. It’s used to provide FSharp.JsonApi with all relevant information about the resources, and allows for a nice and friendly syntax when parsing resources.

type ResourceDiscriminator =
  | [<ResourceName("article")>]
    Article of Resource<ArticleAttrs, ArticleRels>

The JsonApiContext is what actually contains all information about the resources, obtained from the resource discriminator. It is used to serialize, deserialize, create, parse, and validate documents. Define it once and use it everywhere.

let jsonApiCtx = JsonApiContext.create<ResourceDiscriminator>

3. Define the resource builders

The ResourceBuildContext is your friend when building attributes, relationships, links, and meta. It contains all information about sparse fieldsets, the current resource being built, its place in the include path, etc. and provides many helpers to get attributes, relationships, and related resources. Most ResourceBuildContext methods have many overloads for convenience; only some are used below.

module Article =

  let private getIdentifier (a: Article) =
    ResourceIdentifier.create TypeNames.article a.Id

  let private getAttributes (ctx: ResourceBuildContext) (a: Article) =
    {
      title = ctx.GetAttribute("title", a.Title)
      articleType = ctx.GetAttribute("articleType", a.Type, ArticleType.toApi)
      updated = ctx.GetAttribute("updated", a.Updated)
    }

  let private getRelationships baseUrl (ctx: ResourceBuildContext) (a: Article) =
    async {

      // Use Async.StartChild to fetch the included resources in parallel

      let! authorComp =
        ctx.IncludeToOne("author", a.Id, Db.Person.authorForArticle, Person.getBuilder baseUrl)
        |> Async.StartChild

      let! commentsComp =
        ctx.IncludeToMany("comments", a.Id, Db.Comment.allForArticle, Comment.getBuilder baseUrl)
        |> Async.StartChild

      // Actually wait for them to be fetched

      let! authorRelationship, authorBuilder = authorComp
      let! commentsRelationship, commentsBuilders = commentsComp

      // Return the relationships and the builders that will be
      // used to build the related resources

      let relationships = {
        author = authorRelationship
        comments = commentsRelationship
      }

      let builders = [
        yield! authorBuilder |> Option.toList
        yield! commentsBuilders
      ]

      return relationships, builders
    }

  // Here is the function to actually get the resource builder.
  // It must be cheap (do not eagerly evaluate attributes or resources)
  // and the two final parameters must be the ResourceBuildContext
  // and the domain object to be built.

  let getBuilder baseUrl (ctx: ResourceBuildContext) a =
    ResourceBuilder
      .Create(Article, getIdentifier a)
      .WithAttributes(fun () -> getAttributes ctx a)
      .WithRelationships(getRelationships baseUrl ctx a)
      // You can add links and meta, too; check out the sample API

Profit!

Build documents

You can now build JSON:API documents like so:

jsonApiCtx.BuildDocument(article, Article.getBuilder, ctx)

where ctx is the ASP.NET Core HttpContext. Your resource document will be built and all sparse fieldsets, included resources etc. will be handled automatically for you.

Receive documents

For example:

let result =
  jsonApiCtx
    .WithNoIdForPost()
    .Parse(Article, ctx)

This returns Async<Result<Resource<ArticleAttrs, ArticleRels>, RequestDocumentError list>>. See the sample API for more information on simple and robust error handling, and more deserialization/parsing/validation options.

Parse query parameters

Example of applicative error handling (using operators from FsToolkit.ErrorHandling):

ArticleSearchArgs.create
<!> Query.GetSingle("filter[title]", ctx)
<*> Query.GetBoundInt("page[offset]", 0, ctx, min=0)
<*> Query.GetBoundInt("page[limit]", 10, ctx, min=1)

Call domain logic

result {
  let (a: ArticleAttrs) = Resource.attributesOrDefault result
  let! article =
    Ok article
    |> set.Optional(Article.setTitle, Attribute.Get(a.title))
    |> set.Required(Article.setBody, Attribute.Require(a.body))
}

Return useful errors to API clients

Many functions in FSharp.JsonApi return Result<_, SomeError list> where SomeError is one of a few discriminated union types having cases that contain all the information you need to provide useful errors to the client in whatever manner you desire.

You can, for example, define your own DU for all errors returned through your API, and map FSharp.JsonApi’s errors to this type. You can then have a function that takes your error DU and returns a JSON:API error object. This allows you to easily take an FSharp.JsonApi error (or a list of errors) and produce a JSON:API error response. See ErrorHandling.fs as well as the error-related functions in the first part of HttpHandlers.fs in the sample API. (I basically copy-paste all of that whenever I create a new JSON:API.)

Never worry about null in requests

Nulls are only allowed for option-wrapped properties. Any other null in the response body will make JsonApiContext.Parse return errors you can use in an error response as described above.

You can also easily require that option-wrapped attributes be non-null for a specific request, giving you a syntactically concise way to get either the inner value of an Option or an error you can return to the API client. It can, for example, be easily chained along with other setters as shown above.

And more

Check out the sample API y’all 😉

Documentation

It would be nice to have complete API documentation, but I have no immediate plans to work on that.

In the meantime, I highly recommend you check out the sample API in this repo, which is a simple but complete and almost-production-ready example API implementation. Open the main solution in VS, start at the topmost file, and read through the project in compilation order. There are lots of comments along the way to explain what’s going on.

Release notes

2.1.1

  • Improved Attribute overload resolution

2.1.0

  • Added Attribute overloads for async parsers, plus a few more
  • Added Setter overloads accepting async arguments

2.0.3

  • Fixed overload resolution for Async<'a> overloads of ResourceBuildContext methods GetAttribute and GetExplicitAttribute

2.0.1

  • Breaking: Removed SimpleResource and related methods/extensions on JsonApiContext. Use Resource.attributesOrDefault and Resource.relationshipsOrDefault instead to get a (possibly default) attribute/relationship instance from a resource.
  • Breaking: Added RequestDocumentError.UnknownMainResourceType
  • Breaking: JsonApiContext.GetResource and the JsonApiContext.Parse overload returning a resource discriminator now return errors if the resource type is unknown. The signature of the former method is changed; the latter is a behaviour change only.
  • Breaking: Renamed Query to QueryParser, added static methods to create an instance with a query parameter map or HttpContext, and changed most methods to instance members that do not depend on a query parameter map or HttpContext
  • Breaking: JsonApiContext.create now throws on invalid attribute and relationship names. Use the new AllowIllegalNameAttribute to disable the check for specific attributes/relationships.
  • Added Setter type with helper methods to chain and lift normal "immutable setter" functions to accept parsed, possibly optional arguments, combining any errors
  • Added JsonApiContext methods RequireResource, WithAllReadOnly, ToDiscriminator, FromDiscriminator, and SerializeAndGetBytes
  • Added JsonApiContext overloads for ParseRequired, WithReadOnly, WithWriteOnly, and WithNotNull
  • Added async overloads for ResourceBuildContext methods GetAttribute and GetExplicitAttribute
  • Added an HttpContext.WriteJsonApiAsync extension overload accepting a byte array
  • Added new Giraffe HTTP handlers jsonApiETag, jsonApiETagWith, and jsonApiBytes
  • Fixed Uri.addQuery and Uri.setQuery not behaving correctly for multiple identical query keys and for query keys that only differ by case
  • Fixed validation when links collection is null (JSON: "links": null)
  • Fixed validation when a relationship is null (JSON: "relationships": {"myRel": null})
  • Serializes Uris to canonical format using Uri.ToString() instead of Uri.OriginalString which is normally used by Newtonsoft.Json. See dotnet/corefx#41679 and JamesNK/Newtonsoft.Json/2190.
  • Made order of included resources deterministic (needed to get stable hashes of response for ETag)

FSharp.JsonApi 1.4.1

  • Fixed Attribute.GetNonNull returning errors for skipped values

FSharp.JsonApi.AspNetCore 1.1.0

  • Add validate optional parameter to all JsonApiContext.Parse extensions
  • Add ParseRequired and ParseSimpleRequired extensions members for JsonApiContext

FSharp.JsonApi 1.4.0

  • Add ParseRequired and ParseSimpleRequired to JsonApiContext

FSharp.JsonApi 1.3.0

  • Add more Attribute overloads

FSharp.JsonApi 1.1.0

  • Add more Relationship overloads

FSharp.JsonApi 1.0.0, FSharp.JsonApi.AspNetCore 1.0.0, FSharp.JsonApi.Giraffe 1.0.0

  • Initial release
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].