Skip to content

fix: debugging contentful routes #7048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 77 additions & 43 deletions src/server/routes/contentful.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,48 @@ routes.options('*', cors());
routes.use(
'/:spaceName/:environment/assets/:id/:version/:name',
(req, res) => {
const {
environment,
id,
name,
spaceName,
version,
} = req.params;
const spaceId = getSpaceId(spaceName);
if (!ALLOWED_DOMAINS.includes(ASSETS_DOMAIN)) {
throw new Error('Invalid domain detected!');
try {
const {
environment,
id,
name,
spaceName,
version,
} = req.params;
const spaceId = getSpaceId(spaceName);
if (!ALLOWED_DOMAINS.includes(ASSETS_DOMAIN)) {
console.log('Invalid domain detected!');
throw new Error('Invalid domain detected!');
}
const url = new URL(`https://${ASSETS_DOMAIN}/spaces/${spaceId}/environments/${environment}/${id}/${version}/${name}`);
res.redirect(url.href);
} catch (e) {
console.log('error in getting asset', e);
}
const url = new URL(`https://${ASSETS_DOMAIN}/spaces/${spaceId}/environments/${environment}/${id}/${version}/${name}`);
res.redirect(url.href);
},
);

/* Gets image file. */
routes.use(
'/:spaceName/:environment/images/:id/:version/:name',
(req, res) => {
const {
environment,
id,
name,
spaceName,
version,
} = req.params;
if (!ALLOWED_DOMAINS.includes(IMAGES_DOMAIN)) {
throw new Error('Invalid domain detected!');
try {
const {
environment,
id,
name,
spaceName,
version,
} = req.params;
if (!ALLOWED_DOMAINS.includes(IMAGES_DOMAIN)) {
throw new Error('Invalid domain detected!');
}
const spaceId = getSpaceId(spaceName);
const url = new URL(`https://${IMAGES_DOMAIN}/spaces/${spaceId}/environments/${environment}/${id}/${version}/${name}`);
res.redirect(url.href);
} catch (e) {
console.log('error in getting image', e);
}
const spaceId = getSpaceId(spaceName);
const url = new URL(`https://${IMAGES_DOMAIN}/spaces/${spaceId}/environments/${environment}/${id}/${version}/${name}`);
res.redirect(url.href);
},
);

Expand Down Expand Up @@ -102,45 +111,70 @@ routes.use('/:spaceName/:environment/preview/entries', (req, res, next) => {
routes.use(
'/:spaceName/:environment/published/assets/:id',
(req, res, next) => {
const { environment, id, spaceName } = req.params;
getService(spaceName, environment, false)
.getAsset(id)
.then(res.send.bind(res), next);
try {
const { environment, id, spaceName } = req.params;
getService(spaceName, environment, false)
.getAsset(id)
.then(res.send.bind(res), next);
} catch (e) {
console.log('error in getting published asset', e);
next(e);
}
},
);

/* Queries published assets of a given space name & environment. */
routes.use(':spaceName/:environment/published/assets', (req, res, next) => {
const { environment, spaceName } = req.params;
getService(spaceName, environment, false)
.queryAssets(req.query)
.then(res.send.bind(res), next);
try {
const { environment, spaceName } = req.params;
getService(spaceName, environment, false)
.queryAssets(req.query)
.then(res.send.bind(res), next);
} catch (e) {
console.log('error in getting published assets', e);
next(e);
}
});

/* Gets the specified published entry of a given space name & environment. */
routes.use(
'/:spaceName/:environment/published/entries/:id',
(req, res, next) => {
const { environment, id, spaceName } = req.params;
getService(spaceName, environment, false)
.getEntry(id)
.then(res.send.bind(res), next);
try {
const { environment, id, spaceName } = req.params;
getService(spaceName, environment, false)
.getEntry(id)
.then(res.send.bind(res), next);
} catch (e) {
console.log('error in getting published entry', e);
next(e);
}
},
);

/* Queries published entries of a given space name and environment. */
routes.use('/:spaceName/:environment/published/entries', (req, res, next) => {
const { environment, spaceName } = req.params;
console.log('hits cdn published entries');
getService(spaceName, environment, true)
.queryEntries(req.query)
.then(res.send.bind(res), next);
try {
console.log('hits cdn published entries');
const { environment, spaceName } = req.params;
getService(spaceName, environment, false)
.queryEntries(req.query)
.then(res.send.bind(res), next);
} catch (e) {
console.log('error in getting published entries', e);
next(e);
}
});

/* Update votes on article. */
routes.use('/:spaceName/:environment/votes', (req, res, next) => authenticator(authenticatorOptions)(req, res, next), (req, res, next) => {
articleVote(req.body)
.then(res.send.bind(res), next);
try {
articleVote(req.body)
.then(res.send.bind(res), next);
} catch (e) {
console.log('error in voting', e);
next(e);
}
});

export default routes;
Loading