Skip to content

Directus CMS golang generics API client

License

Notifications You must be signed in to change notification settings

zdebra/directusapi

Repository files navigation

Directus API client

This is generics golang client for Directus v8 CMS. Never write the same API client again. Just define your collection model and use strongly typed methods.

PkgGoDev Go Report Card


Example usage

// define Read and Write model
type FruitR struct {
    ID          int             `json:"id"`
    Name        string          `json:"name"`
    Weight      float64         `json:"weight"`
}

type FruitW struct {
    Name        string          `json:"name"`
    Weight      float64         `json:"weight"`
}

// initialize generic API client
api := API[FruitR, FruitW, int]{
    Scheme:         "http",
    Host:           "localhost:8080",
    Namespace:      "_",
    CollectionName: "fruits",
    HTTPClient:     http.DefaultClient,
    BearerToken:    "1a2bd3db-8026-4494-ad36-9873ee46c0af"
}

// use typed methods
// - insert
watermelon, err := api.Insert(ctx, FruitW{
	Name:         "watermelon",
	Weight:       20.3,
})
// watermelon's type is FruitR

// - retrieve collection of items
fruits, err := api.Items(ctx, None())
// fruits's type is []FruitR

// update (set) item
passionfruit, err := api.Set(ctx, watermelonID, FruitW{
	Name:         "passionfruit",
	Weight:       3.3,
})
// passionfruit's type is FruitR

Go to the documentation to see all available methods.

Features

  • strongly-typed API methods based on directus reference
  • different models for reads and writes
  • collection querying support: filtering, sorting, limit, offset, fulltext search
  • custom directusapi.Time to support Directus API time format
  • custom directusapi.Optional to support optional fields

What is Directus?

Directus is open sourced Content Management System, it has UI and exposed API for dynamicly created collections.

Setup

go get github.com/zdebra/directusapi

Limitations

  • directus v9 is not supported at this moment; this library was developed for directus v8
  • pointers are not allowed in your Read and Write models, directusapi.Optional should be used for optional fields
  • directusapi.Time has to be used instead of time.Time

Next steps

  • update/create partials to be removed as it could be replaced with directusapi.Optional
  • add godoc examples

License

You can check out the full license here

This project is licensed under the terms of the MIT license.

Buy me a coffee

Whether you use this project, have learned something from it, or just like it, please consider supporting it by buying me a coffee, so I can dedicate more time on open-source projects like this :)

Buy Me A Coffee