Output modifiers
Output modifiers are available for the read
and browse
methods. They let you change the output of the result to have only your selected fields, include some additionnal data that the Ghost API doesn't give you by default or get the content in different formats.
The result type from the subsequent fetch
will be modified to match your selection giving you full type-safety.
Select specific fields with .fields()
The fields
method lets you change the output of the result to have only your selected fields, it works by giving an object with the field name and the value true
. Under the hood it will use the zod.pick
method to pick only the fields you want.
The output schema will be modified to only have the fields you selected and TypeScript will pick up on that to warn you if you access non-existing fields.
Include relations with .include()
The include
method lets you include some additionnal data that the Ghost API doesn't give you by default. The include
params is specific to each resource and is defined in the "include" Schema
of the resource. You will have to let TypeScript guide you to know what you can include.
Available include keys by resource:
- Posts & Pages:
authors
,tags
- Authors:
count.posts
- Tags:
count.posts
- Tiers:
monthly_price
,yearly_price
,benefits
The output type will be modified to make the fields you include non-optionals.
Output page/post content into different .formats()
In the Admin API, the formats
method lets you add alternative content formats on the output of Post
or Page
resource to get the content in plaintext
or html
. Available options are plaintext | html | mobiledoc
.
In the Admin API the html
is not the default format given back when calling read
or browse
on the
Post
or Page
resource. You have to use the formats
method to get it.
The output type will be modified to make the formatted fields you include non-optionals.
Combining output modifiers
You can combine output modifiers to get the result you want. For example if you want to get the html
and plaintext
content of a Post
:
The order of calling is important!
fields
will modify the output schema to only have the fields you selected.formats
will make required the fields you selected (from the already filtered fields!).
So, if you call formats({"html": true})
but you didn't include it in the fields({...})
before, TypeScript will yell at you.