Instantiates a new NextDrupal.
const client = new NextDrupal(baseUrl)
The baseUrl of your Drupal site. Do not add the /jsonapi suffix.
Options for NextDrupal.
Optional
accessOptional
cacheOptional
fetcherOptional
init: RequestInitAdds a locale prefix to the given path.
The path.
The options for adding the locale prefix.
Optional
defaultLocale?: stringThe default locale.
Optional
locale?: stringThe locale.
The path with the locale prefix.
Builds an endpoint URL for the specified parameters.
The parameters for the endpoint.
The built endpoint URL.
Builds a URL with the given path and search parameters.
The path for the url. Example: "/example"
Optional
searchParams: EndpointSearchParamsOptional query parameters.
The constructed URL.
const drupal = new DrupalClient("https://example.com")
// https://drupal.org
drupal.buildUrl("https://drupal.org").toString()
// https://example.com/foo
drupal.buildUrl("/foo").toString()
// https://example.com/foo?bar=baz
client.buildUrl("/foo", { bar: "baz" }).toString()
Build a URL from DrupalJsonApiParams
const params = {
getQueryObject: () => ({
sort: "-created",
"fields[node--article]": "title,path",
}),
}
// https://example.com/jsonapi/node/article?sort=-created&fields%5Bnode--article%5D=title%2Cpath
drupal.buildUrl("/jsonapi/node/article", params).toString()
Constructs a path from the given segment and options.
The path segment.
The options for constructing the path.
Optional
defaultLocale?: stringThe default locale.
Optional
locale?: stringThe locale.
Optional
pathPrefix?: stringThe path prefix.
The constructed path.
Creates a new file resource for the specified media type.
The type of the resource. In most cases this is file--file
.
The body payload with data.
media--image
field_media_image
avatar.jpg
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The created file resource.
Create a file resource for a media--image entity
const file = await drupal.createFileResource("file--file", {
data: {
attributes: {
type: "media--image", // The type of the parent resource
field: "field_media_image", // The name of the field on the parent resource
filename: "filename.jpg",
file: await fs.readFile("/path/to/file.jpg"),
},
},
})
You can then use this to create a new media--image with a relationship to the file:
const media = await drupal.createResource<DrupalMedia>("media--image", {
data: {
attributes: {
name: "Name for the media",
},
relationships: {
field_media_image: {
data: {
type: "file--file",
id: file.id,
},
},
},
},
})
Creates a new resource of the specified type.
The type of the resource. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The body payload with data.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The created resource.
Create a node--page resource
const page = await drupal.createResource("node--page", {
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
})
Create a node--article with a taxonomy term
const article = await drupal.createResource("node--article", {
data: {
attributes: {
title: "Title of Article",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
relationships: {
field_category: {
data: {
type: "taxonomy_term--category",
id: "28ab9f26-927d-4e33-9510-b59a7ccdafe6",
},
},
},
},
})
Using filters
const page = await drupal.createResource(
"node--page",
{
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
},
{
params: {
"fields[node--page]": "title,path",
},
}
)
Using TypeScript with DrupalNode
import { DrupalNode } from "next-drupal"
const page = await drupal.createResource<DrupalNode>("node--page", {
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
})
Deletes an existing resource of the specified type.
The type of the resource. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The resource id. Example: a50ffee7-ba94-46c9-9705-f9f8f440db94
.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
True if the resource was deleted, false otherwise.
Fetches a resource from the given input URL or path.
The url to fetch from.
The fetch options with withAuth
.
If withAuth
is set, fetch
will fetch an Authorization
header before making the request.
The fetch response.
Retrieve an access token.
Optional
clientIdSecret: NextDrupalAuthClientIdSecretThe client ID and secret.
The access token.
Gets the authorization header value based on the provided auth configuration.
The auth configuration.
The authorization header value.
Extracts errors from the fetch response.
The fetch response.
The extracted errors.
Fetches the JSON:API index.
Optional
locale: stringThe locale for the request.
Optional
options: JsonApiWithNextFetchOptionsOptions for the request.
The JSON:API index.
Fetches a menu by its name.
The name of the menu. Example: main
or footer
.
Optional
options: JsonApiOptions & JsonApiWithCacheOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched menu.
DrupalMenuLinkContent
DrupalMenuLinkContent
with children nested to match the hierarchy from DrupalFetches a resource of the specified type by its UUID.
The resource type. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The id of the resource. Example: 15486935-24bf-4be7-b858-a5b2de78d09d
.
Optional
options: JsonApiOptions & JsonApiWithCacheOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched resource.
Get a page by uuid.
const node = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
)
Get the es translation for a page by uuid.
const node = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{
locale: "es",
defaultLocale: "en",
}
)
Get the raw JSON:API response.
const { data, meta, links } = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{
deserialize: false,
}
)
Get a node--article resource using cache.
const id = "07464e9f-9221-4a4f-b7f2-01389408e6c8"
const article = await drupal.getResource("node--article", id, {
withCache: true,
cacheKey: `node--article:${id}`,
})
Get a page resource with time-based revalidation.
const node = await drupal.getResource(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8",
{ next: { revalidate: 3600 } }
)
Get a page resource with tag-based revalidation.
const {slug} = params;
const path = drupal.translatePath(slug)
const type = path.jsonapi.resourceName
const tag = `${path.entity.type}:${path.entity.id}`
const node = await drupal.getResource(path, path.entity.uuid, {
params: params.getQueryObject(),
tags: [tag]
})
Using DrupalNode for a node entity type.
import { DrupalNode } from "next-drupal"
const node = await drupal.getResource<DrupalNode>(
"node--page",
"07464e9f-9221-4a4f-b7f2-01389408e6c8"
)
Using DrupalTaxonomyTerm for a taxonomy term entity type.
import { DrupalTaxonomyTerm } from "next-drupal"
const term = await drupal.getResource<DrupalTaxonomyTerm>(
"taxonomy_term--tags",
"7b47d7cc-9b1b-4867-a909-75dc1d61dfd3"
)
Fetches a resource of the specified type by its path.
The path of the resource. Example: /blog/slug-for-article
.
Optional
options: { isVersionable?: boolean } & (JsonApiOptions & JsonApiWithNextFetchOptions)Options for the request.
The fetched resource.
Get a page by path
const node = await drupal.getResourceByPath("/blog/slug-for-article")
Get the raw JSON:API response
const { data, meta, links } = await drupal.getResourceByPath(
"/blog/slug-for-article",
{
deserialize: false,
}
)
Using DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const node = await drupal.getResourceByPath<DrupalNode>(
"/blog/slug-for-article"
)
Fetches a collection of resources of the specified type.
The type of the resources. Example: node--article
or user--user
.
Optional
options: { deserialize?: boolean } & (JsonApiOptions & JsonApiWithNextFetchOptions)Options for the request.
The fetched collection of resources.
Get all articles
const articles = await drupal.getResourceCollection("node--article")
Using filters
const publishedArticles = await drupal.getResourceCollection("node--article", {
params: {
"filter[status]": "1",
},
})
Get the raw JSON:API response
const { data, meta, links } = await drupal.getResourceCollection("node--page", {
deserialize: false,
})
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const nodes = await drupal.getResourceCollection<DrupalNode[]>("node--article")
Fetches path segments for a collection of resources of the specified types.
The types of the resources.
Optional
options: { params?: JsonApiParams; pathPrefix?: string } & JsonApiWithAuthOption & (Options for the request.
The fetched path segments.
Fetches a search index by its name.
The name of the search index.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched search index.
Fetches a view by its name.
The name of the view and the display id. Example: articles--promoted
.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The fetched view.
Get a view named articles
and display id promoted
const view = await drupal.getView("articles--promoted")
Using sparse fieldsets to only fetch the title and body fields
const view = await drupal.getView("articles--promoted", {
params: {
fields: {
"node--article": "title,body",
},
},
})
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const view = await drupal.getView<DrupalNode>("articles--promoted")
Translates a path to a DrupalTranslatedPath object.
The resource path. Example: /blog/slug-for-article
.
Optional
options: JsonApiWithAuthOption & JsonApiWithNextFetchOptionsOptions for the request.
The translated path.
Updates an existing resource of the specified type.
The type of the resource. Example: node--article
, taxonomy_term--tags
, or block_content--basic
.
The resource id. Example: a50ffee7-ba94-46c9-9705-f9f8f440db94
.
The body payload with data.
Optional
options: JsonApiOptions & JsonApiWithNextFetchOptionsOptions for the request.
The updated resource.
Update a node--page resource
const page = await drupal.updateResource(
"node--page",
"a50ffee7-ba94-46c9-9705-f9f8f440db94",
{
data: {
attributes: {
title: "Updated Title",
},
},
}
)
Using TypeScript with DrupalNode for a node entity type
import { DrupalNode } from "next-drupal"
const page = await drupal.updateResource<DrupalNode>(
"node--page",
"a50ffee7-ba94-46c9-9705-f9f8f440db94",
{
data: {
attributes: {
title: "Updated Title",
},
},
}
)
The NextDrupal class extends the NextDrupalBase class and provides methods for interacting with a Drupal backend.