Skip to content

Commit

Permalink
fix(@dpc-sdp/ripple-tide-api): passed headers to route, page and publ…
Browse files Browse the repository at this point in the history
…ication menu endpoints

fixes publication menu and children not display in previews
  • Loading branch information
jeffdowdle committed Oct 10, 2023
1 parent fad884b commit e760940
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
10 changes: 3 additions & 7 deletions packages/nuxt-ripple/server/api/tide/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
import { createHandler, TidePageApi } from '@dpc-sdp/ripple-tide-api'
import { BadRequestError } from '@dpc-sdp/ripple-tide-api/errors'
import { useNitroApp } from '#imports'
import {
isPreviewPath,
AuthCookieNames
} from '@dpc-sdp/nuxt-ripple-preview/utils'
import { AuthCookieNames } from '@dpc-sdp/nuxt-ripple-preview/utils'

export const createPageHandler = async (
event: H3Event,
Expand All @@ -32,8 +29,7 @@ export const createPageHandler = async (
const tokenCookie = getCookie(event, AuthCookieNames.ACCESS_TOKEN)
const headers = {}

// Only add the access token to the headers if the path is a preview path
if (isPreviewPath(query.path)) {
if (tokenCookie) {
headers['X-OAuth2-Authorization'] = `Bearer ${tokenCookie}`
}

Expand All @@ -45,7 +41,7 @@ export const createPageHandler = async (
)

// Need to pass on the section cache tags to the nuxt app
if (pageResponse.headers) {
if (pageResponse.headers && pageResponse.headers['section-cache-tags']) {
setResponseHeader(
event,
'section-cache-tags',
Expand Down
12 changes: 7 additions & 5 deletions packages/nuxt-ripple/server/api/tide/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export const createSiteHandler = async (
const siteResponse = await tideSiteApi.getSiteData(query.id)

// Need to pass on the section cache tags to the nuxt app
setResponseHeader(
event,
'section-cache-tags',
siteResponse.headers['section-cache-tags']
)
if (siteResponse.headers && siteResponse.headers['section-cache-tags']) {
setResponseHeader(
event,
'section-cache-tags',
siteResponse.headers['section-cache-tags']
)
}

return siteResponse.data
})
Expand Down
8 changes: 4 additions & 4 deletions packages/ripple-tide-api/src/services/tide-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ export default class TidePageApi extends TideApiBase {
}
}

async getRouteByPath(path: string, site: string = this.site) {
async getRouteByPath(path: string, site: string = this.site, headers = {}) {
this.path = path

const routeUrl = `/route?site=${site}&path=${path}`

return this.get(routeUrl)
return this.get(routeUrl, { headers })
.then((response) => response?.data?.data?.attributes)
.catch((error) => {
throw new NotFoundError(
Expand Down Expand Up @@ -117,7 +117,7 @@ export default class TidePageApi extends TideApiBase {
return this.getPageFromPreviewLink(path, site, params, headers)
}

const route = await this.getRouteByPath(path, site)
const route = await this.getRouteByPath(path, site, headers)
if (route && !route.error) {
if (route.hasOwnProperty('redirect_type')) {
return {
Expand All @@ -135,7 +135,7 @@ export default class TidePageApi extends TideApiBase {
if (includes !== '') {
fullParams['include'] = includes
}
return this.getPageByRouteData(route, { params: fullParams })
return this.getPageByRouteData(route, { params: fullParams, headers })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
} from '@dpc-sdp/ripple-tide-api/types'
import type { indexNode, apiNode } from '../../../types'
import { useRuntimeConfig } from '#imports'
import { AuthCookieNames } from '@dpc-sdp/nuxt-ripple-preview/utils'

/**
* @description Recursively transform API response to match component props
Expand Down Expand Up @@ -51,12 +52,13 @@ class TidePublicationIndexApi extends TideApiBase {
this.logLabel = 'TidePublicationIndex'
}

async getPublicationMenu(id: string) {
async getPublicationMenu(id: string, headers = {}) {
try {
const { data: response } = await this.get(
`/node/publication/${id}/hierarchy`,
{
params: { site: this.siteId }
params: { site: this.siteId },
headers
}
)
const resource = jsonapiParse.parse(response).data.meta.hierarchy
Expand All @@ -83,7 +85,14 @@ export const createPublicationIndexHandler = async (
throw new BadRequestError('Publication ID is required')
}

return await publicationIndexApi.getPublicationMenu(query.id)
const tokenCookie = getCookie(event, AuthCookieNames.ACCESS_TOKEN)
const headers = {}

if (tokenCookie) {
headers['X-OAuth2-Authorization'] = `Bearer ${tokenCookie}`
}

return await publicationIndexApi.getPublicationMenu(query.id, headers)
})
}

Expand Down

0 comments on commit e760940

Please sign in to comment.