openapix

package module
v0.0.0-...-613a47d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-openapix

go-openapix provides some useful convience functions to to swaggest, so that you can build up your APIs in a type-checked and auto-documented way!

Some highlights:

  • MustCreateOpenapiEndpoint - uses generics to enforce that you are returning the correct (documented) type in code. End-user does not need to use generics themselves, making it simpler to use. It also generates a endpoint name, based on the title you provide - no accidental copy/paste doubling-up endpoints with the same name.
  • MustCheckNonNullArrays - document [] instead of null when the array is empty.

Usage:


func main() {
	apiSchema := &openapi.Collector{}
	r := chirouter.NewWrapper(chi.NewRouter())

	r.Use(nethttp.OpenAPIMiddleware(apiSchema))

	... // your setup here

	// add an endpoint
	openapix.Post(r, "/customers", GetAllCustomers(dbConnection, customersStore))

	// check array types are marked as non-null; i.e. no items will return "[]" instead of "null"
   	openapix.MustCheckNonNullArrays(apiSchema.Reflector().Spec.Components.Schemas.MapOfSchemaOrRefValues)

	openapix.MustNotHaveDuplicateOperationIDOrUnknownSecurity(apiSchema.Reflector().Spec)

	// start your web server
}

type GetAllCustomersRequest struct{}

type GetAllCustomersResponse struct {
	Customers []Customer `json:"customers" nullable:"false"`
}

func GetAllCustomers(dbConnection *sql.DB, customersStore *CustomersStore) *nethttp.Handler {
	return openapix.MustCreateOpenapiEndpoint(
		"Get All Customers",
		&openapix.HandlerOptions{Tags: []string{"Customers"}},
		func(ctx context.Context, input *GetAllCustomersRequest, output *GetAllCustomersResponse) error {
			customers, err := customersStore.GetAll(dbConnection)
			if err != nil {
				return status.Wrap(err, status.Internal)
			}

			output.Customers = customers

			return nil
		},
	)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(r router, path string, handler *nethttp.Handler)

func Delete

func Delete(r router, path string, handler *nethttp.Handler)

func Get

func Get(r router, path string, handler *nethttp.Handler)
func Head(r router, path string, handler *nethttp.Handler)

func MustCheckNonNullArrays

func MustCheckNonNullArrays(definitionMap map[string]openapi3.SchemaOrRef)

MustCheckNonNullArrays enforces non-null arrays across the whole of your schema. Use if desired on your particular schema; this isn't suitable for all schemas.

func MustCreateOpenapiEndpoint

func MustCreateOpenapiEndpoint[Req any, Resp any](title string, opts *HandlerOptions, handler OpenapiHandlerFunc[Req, Resp]) *nethttp.Handler

MustCreateOpenapiEndpoint creates an openapi endpoint. It panics on error.

func MustNotHaveDuplicateOperationIDOrUnknownSecurity

func MustNotHaveDuplicateOperationIDOrUnknownSecurity(spec *openapi3.Spec)

func Options

func Options(r router, path string, handler *nethttp.Handler)

func Patch

func Patch(r router, path string, handler *nethttp.Handler)

func Post

func Post(r router, path string, handler *nethttp.Handler)

func Put

func Put(r router, path string, handler *nethttp.Handler)

func Trace

func Trace(r router, path string, handler *nethttp.Handler)

Types

type HandlerOptions

type HandlerOptions struct {
	Tags     []string
	Security *HandlerSecurity
}

type HandlerSecurity

type HandlerSecurity struct {
	SecurityName string
	Scopes       []string
}

type OpenapiHandlerFunc

type OpenapiHandlerFunc[Req any, Resp any] func(ctx context.Context, input *Req, output *Resp) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址