Introduction
This is an internal package, containing the bulding blocks of the Content and Admin packages. Unless you want to build your own implementation you probably don't need this package.
Documentation here may be outdated, this package being internal, it is changing a lot through versions to versions... still under construction 🏗️
@ts-ghost/core-api
contains the core building blocks for the @ts-ghost/content-api
package. It contains the Type-safe logic of Query Builder and Fetchers. Unless you are building a new package for @ts-ghost
you should not need to use this package directly.
Install
Requirements
This client is only compatible with Ghost versions 5.x for now.
-
Ghost 5^
-
Node.js 16+
- We rely on global
fetch
being available, so you can bring your own polyfill and if you run Node 16, you'll need to run with the--experimental-fetch
flag enabled.
- We rely on global
APIComposer
The APIComposer is a class that helps you build the target API with the available methods for a resource based on a combinations of ZodSchema. This APIComposer exposes 5 methods:
read
to fetch a single record andbrowse
to fetch multiple records.add
to create a record.edit
to update a record.delete
to delete a record.
All these methods like read
and browse
gives you back the appropriate Fetcher
instance that will handle the actual request to the API with the correct parameters.
APIComposer
will handle type-safety of the query parameters, will return the appropriate fetcher and will pass along the correct output type based on the ZodSchema
you instantiate it with. For the query methods like browse
and read
, this output schema will be modified if required when you select specific fields, includes etc.
Instantiation
- first argument is the name of the resource you want to target.
- second argument is an object containing the different schemas to validate the inputs and outputs of the APIComposer:
identitySchema
can be anyZodType
and can also be an emptyz.object({})
if you don't need theread
method.include
is aZodObject
that will validate theinclude
parameters of the API call. It is specific to the Ghost API resource targeted. The format is always{ 'name_of_the_field': true }
createSchema
(Optional) is a Zod Schema that will validate the input of theadd
method of the APIComposer.add
will take exactly the schema to parse
createOptionsSchema
(Optional) is a Zod Schema that will validate options that are going to be passed as query parameters to thePOST
url.updateSchema
(Optional) is a Zod Schema that will validate the input of theedit
method of the APIComposer.edit
will fallback to aZodPartial
(all fields are optional) version of thecreateSchema
ifupdateSchema
is not provided.
- last argument is the
HTTPClient
instance that will be used to do the actualfetch
and handle the JWT if using the admin endpoint.
Building Queries
After instantiation you can use the APIComposer
to build your queries with 2 available methods.
The browse
and read
methods accept a config object with 2 properties: input
and an output
. These params mimic the way Ghost API Content is built but with the power of Zod and TypeScript they are type-safe here.
- browse parameters are
page
,limit
,order
,filter
. And read parameters areid
orslug
.
Method options
.browse
options
Input are totally optionals on the browse
method but they let you filter and order your search.
This is an example containing all the available keys in the input
object
These browse params are then parsed through a Zod
Schema that will validate all the fields.
page:number
The current page requestedlimit:number
Between 0 and 15 (limitation of the Ghost API)filter:string
Contains the filter with Ghost APIfilter
syntax.order:string
Contains the name of the field and the orderASC
orDESC
.
For the order
and filter
if you use fields that are not present on the schema (for example name
on a Post
) then the APIComposer will throw an Error with message containing the unknown field.
.read
options
Read is meant to be used to fetch 1 object only by id
or slug
.
You can submit both id
and slug
, but the fetcher will then chose the id
in priority if present to make the final URL query to the Ghost API.
Query Fetchers
If the parsing went okay, the read
and browse
methods from the APIComposer
will return the associated Fetcher
.
BrowseFetcher
for thebrowse
methodReadFetcher
for theread
methodBasicFetcher
is a special case when you don't need a APIComposer at all and want to fetch directly.
Fetchers are instatiated automatically after using read
or browse
but these Fetchers can also be instantiated in isolation, in a similar way as the APIComposer with a config
containing the same schemas. But also a set of params
necessary to build the URL to the Ghost API.
The option output
schema will be modified along the way after the params like fields
, formats
, include
are added to the query. At instantiation it will most likely be the same as the original schema.
These fetchers have a fetch
method that will return a discriminated union of 2 types:
Read Fetcher
After using .read
query, you will get a ReadFetcher
with an async fetch
method giving you a discriminated union of 2 types:
Browse Fetcher
After using .read
query, you will get a BrowseFetcher
with 2 methods:
async fetch
async paginate
Browse .fetch()
That result is a discriminated union of 2 types:
Browse .paginate()
Here you can use the next
property to get the next page fetcher if it is defined.
Modifiying Fetchers output by selecting fields, formats, include
Output can be modified on the BrowseFetcher
and the ReadFetcher
through available methods:
.fields
.formats
.include
.fields()
The fields
methods lets you change the output of the result to have only your selected fields, it works by giving the property key and the value true
to the field you want to keep. 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
The include
method lets you include some additionnal data that the Ghost API doesn't give you by default. This include
key is specific to each resource and is defined in the Schema
of the resource. You will have to let TypeScript guide you to know what you can include.
The output type will be modified to make the fields you include non-optionals.
formats
The formats
method lets you include some additionnal formats that the Ghost API doesn't give you by default. This is used on the Post
and Page
resource to retrieve the content in plaintext, html, or mobiledoc format. The available keys are html | mobiledoc | plaintext
and the value is a boolean to indicate if you want to include it or not.
The output type will be modified to make the fields html
and plaintext
non-optionals.
Chaining methods
You can chain the methods to select the fields, formats, and include you want.
fetch
options
You can pass an optional options
object to the fetch
and paginate
method. The options
object is the standard RequestInit
object from the fetch
API.
This may be useful if you use NextJS augmented fetch
!
Mutations
These mutations are async methods, they will return a Promise
that will resolve to the parsed result.
Create record
- The first argument is the
input
object that will be parsed and typed with thecreateSchema
schema. - The second argument is the
options
object that will be parsed and typed with thecreateOptionsSchema
schema.
The result will be parsed and typed with the output
schema and represent the newly created record.
Edit record
Edit requires the id
of the record to edit.
The result will be parsed and typed with the output
schema and represent the updated record.
- The first argument is the
id
of the record to edit. - The second argument is the
input
object that will be parsed and typed with thecreateSchema
schema wrapped with Partial. So all fields are optional.
Delete record
Delete requires the id
of the record to delete.
- The first argument is the
id
of the record to delete.
The response will not contain any data since Ghost API just return a 204 empty response. You will have to check the discriminator success
to know if the deletion was successful or not.
Roadmap
- Handling POST, PUT and DELETE requests.
- Writing examples documentation for mutations.
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- If you have suggestions for adding or removing projects, feel free to open an issue to discuss it, or directly create a pull request after you edit the README.md file with necessary changes.
- Please make sure you check your spelling and grammar.
- Create individual PR for each suggestion.
- Please also read through the Code Of Conduct before posting your first idea as well.
License
Distributed under the MIT License. See LICENSE for more information.
Authors
- PhilDL - Creator