Giter Club home page Giter Club logo

upfront's Introduction

Upfront

Upfront is a very small library for adding type signatures to your HTTP handlers using generics.

Useage

For requests where you don't care about the request body:

func handleGet() upfront.Handler[ResponseType, ErrorType] {
	return func(in upfront.Request) upfront.Result[ResponseType, ErrorType] {
		// `in` will contain your normall *http.Request

		// Use `upfront.OKResult` or `upfront.ErrResult` to return a status code
		// along with either the left or right type paramters
		return upfront.OKResult[ResponseType, ErrorType](
			val,
			http.StatusOK,
		)
	}
}

For requests where you want the request body, upfront will try to decode it for you and pass it along:

func handlePost() upfront.BodyHandler[Body, ResponseType, ErrorType] {
	return func(in upfront.BodyRequest[Record]) upfront.Result[Record, APIError] {
		// `in` not only has your `*http.Request`, but also has your `Body` type
		// in there as well

		// Do something with `in.Body`...

		// Maybe something has gone wrong
		return upfront.Err[Body, ResponseType, ErrorType](
			ErrorType{
				Err: err,
			},
			http.StatusInternalServerError,
		)
	}
}

Example server

There's an example server and in-memory database to see how someone might use the package. Run make example to start the server at :4444.

Contributing

This is a small library, so I don't imagine there will be too much to add to it, but you're more than welcome to open a PR or open an issue! If you're looking to get into the source code, you'll just need Go, and make sure it's at least version 1.18 to use generics. You can run make test at the root of the repo to easily run tests.

upfront's People

Contributors

jdholdren 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.