Skip to content

Commit

Permalink
feat: add getEntityByPath
Browse files Browse the repository at this point in the history
  • Loading branch information
shadcn committed May 17, 2021
1 parent 64662df commit 072ead7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
15 changes: 13 additions & 2 deletions examples/example-blog/pages/api/preview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next"
import { getEntityByPath } from "next-drupal"

export default function (request: NextApiRequest, response: NextApiResponse) {
export default async function (
request: NextApiRequest,
response: NextApiResponse
) {
const { slug, resourceVersion, secret } = request.query

if (secret !== process.env.DRUPAL_PREVIEW_SECRET) {
Expand All @@ -11,9 +15,16 @@ export default function (request: NextApiRequest, response: NextApiResponse) {
return response.status(401).json({ message: "Invalid slug." })
}

const node = await getEntityByPath(slug as string)

if (!node) {
return response.status(404).json({ message: "Invalid slug" })
}

response.setPreviewData({
resourceVersion,
})

response.redirect(slug as string)
response.writeHead(307, { Location: node.path.alias })
response.end()
}
15 changes: 13 additions & 2 deletions examples/example-marketing/pages/api/preview.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next"
import { getEntityByPath } from "next-drupal"

export default function (request: NextApiRequest, response: NextApiResponse) {
export default async function (
request: NextApiRequest,
response: NextApiResponse
) {
const { slug, resourceVersion, secret } = request.query

if (secret !== process.env.DRUPAL_PREVIEW_SECRET) {
Expand All @@ -11,9 +15,16 @@ export default function (request: NextApiRequest, response: NextApiResponse) {
return response.status(401).json({ message: "Invalid slug." })
}

const node = await getEntityByPath(slug as string)

if (!node) {
return response.status(404).json({ message: "Invalid slug" })
}

response.setPreviewData({
resourceVersion,
})

response.redirect(slug as string)
response.writeHead(307, { Location: node.path.alias })
response.end()
}
12 changes: 10 additions & 2 deletions packages/next-drupal/src/get-entity-from-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,17 @@ export async function getEntityFromContext(
return options?.deserialize ? deserialize(entity) : entity
}

export async function getEntityByPath(path: string) {
const entity = await resolveEntityUsingPath(path)

if (!entity) return null

return deserialize(entity)
}

async function resolveEntityUsingPath(
path: string,
options?: { resourceVersion?: string; params?: Record<string, unknown> }
options: { resourceVersion?: string; params?: Record<string, unknown> } = {}
) {
try {
const url = new URL(
Expand Down Expand Up @@ -95,6 +103,6 @@ async function resolveEntityUsingPath(

return JSON.parse(json["resolvedResource#uri{0}"]?.body)
} catch (error) {
//
// console.error(error)
}
}
15 changes: 13 additions & 2 deletions starters/basic-starter/pages/api/preview.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next"
import { getEntityByPath } from "next-drupal"

export default function (request: NextApiRequest, response: NextApiResponse) {
export default async function (
request: NextApiRequest,
response: NextApiResponse
) {
const { slug, resourceVersion, secret } = request.query

if (secret !== process.env.DRUPAL_PREVIEW_SECRET) {
Expand All @@ -11,9 +15,16 @@ export default function (request: NextApiRequest, response: NextApiResponse) {
return response.status(401).json({ message: "Invalid slug." })
}

const node = await getEntityByPath(slug as string)

if (!node) {
return response.status(404).json({ message: "Invalid slug" })
}

response.setPreviewData({
resourceVersion,
})

response.redirect(slug as string)
response.writeHead(307, { Location: node.path.alias })
response.end()
}

1 comment on commit 072ead7

@vercel
Copy link

@vercel vercel bot commented on 072ead7 May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.