Skip to content

Commit

Permalink
refactor: IndexPage to TagFront
Browse files Browse the repository at this point in the history
  • Loading branch information
OllysCoding committed May 31, 2023
1 parent 1c7358a commit 0cb345b
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 121 deletions.
8 changes: 4 additions & 4 deletions dotcom-rendering/scripts/json-schema/check-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
getArticleSchema,
getFrontSchema,
getNewsletterPageSchema,
getIndexPageSchema,
getTagFrontSchema,
} = require('./get-schema');

const root = path.resolve(__dirname, '..', '..');
Expand All @@ -18,7 +18,7 @@ const existingFrontSchema = fs.readFileSync(
`${root}/src/model/front-schema.json`,
{ encoding: 'utf-8' },
);
const existingIndexPageSchema = fs.readFileSync(
const existingTagFrontSchema = fs.readFileSync(
`${root}/src/model/front-schema.json`,
{ encoding: 'utf-8' },
);
Expand All @@ -29,13 +29,13 @@ const existingNewsletterSchema = fs.readFileSync(

const articleSchema = getArticleSchema();
const frontSchema = getFrontSchema();
const indexPageSchema = getIndexPageSchema();
const tagFrontSchema = getTagFrontSchema();
const newsletterSchema = getNewsletterPageSchema();

if (
existingArticleSchema !== articleSchema ||
existingFrontSchema !== frontSchema ||
existingIndexPageSchema !== indexPageSchema ||
existingTagFrontSchema !== tagFrontSchema ||
existingNewsletterSchema !== newsletterSchema
) {
throw new Error('Schemas do not match ... please run "make gen-schema"');
Expand Down
8 changes: 4 additions & 4 deletions dotcom-rendering/scripts/json-schema/gen-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const {
getArticleSchema,
getFrontSchema,
getNewsletterPageSchema,
getIndexPageSchema,
getTagFrontSchema,
} = require('./get-schema');

const root = path.resolve(__dirname, '..', '..');

const articleSchema = getArticleSchema();
const frontSchema = getFrontSchema();
const indexPageSchema = getIndexPageSchema();
const tagFrontSchema = getTagFrontSchema();
const newsletterPageSchema = getNewsletterPageSchema();

fs.writeFile(
Expand All @@ -38,8 +38,8 @@ fs.writeFile(
);

fs.writeFile(
`${root}/src/model/index-page-schema.json`,
indexPageSchema,
`${root}/src/model/tag-front-schema.json`,
tagFrontSchema,
'utf8',
(err) => {
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions dotcom-rendering/scripts/json-schema/get-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const program = TJS.getProgramFromFiles(
[
path.resolve(`${root}/index.d.ts`),
path.resolve(`${root}/src/types/frontend.ts`),
path.resolve(`${root}/src/types/indexPage.ts`),
path.resolve(`${root}/src/types/tagFront.ts`),
path.resolve(`${root}/src/types/newslettersPage.ts`),
],
{
Expand All @@ -32,9 +32,9 @@ module.exports = {
4,
);
},
getIndexPageSchema: () => {
getTagFrontSchema: () => {
return JSON.stringify(
TJS.generateSchema(program, 'FEIndexPageType', settings),
TJS.generateSchema(program, 'FETagFrontType', settings),
null,
4,
);
Expand Down
57 changes: 57 additions & 0 deletions dotcom-rendering/src/model/front-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,25 @@
"tagType",
"webTitle"
]
},
"pagination": {
"type": "object",
"properties": {
"currentPage": {
"type": "number"
},
"lastPage": {
"type": "number"
},
"totalContent": {
"type": "number"
}
},
"required": [
"currentPage",
"lastPage",
"totalContent"
]
}
},
"required": [
Expand Down Expand Up @@ -1400,6 +1419,25 @@
"tagType",
"webTitle"
]
},
"pagination": {
"type": "object",
"properties": {
"currentPage": {
"type": "number"
},
"lastPage": {
"type": "number"
},
"totalContent": {
"type": "number"
}
},
"required": [
"currentPage",
"lastPage",
"totalContent"
]
}
},
"required": [
Expand Down Expand Up @@ -2080,6 +2118,25 @@
"tagType",
"webTitle"
]
},
"pagination": {
"type": "object",
"properties": {
"currentPage": {
"type": "number"
},
"lastPage": {
"type": "number"
},
"totalContent": {
"type": "number"
}
},
"required": [
"currentPage",
"lastPage",
"totalContent"
]
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@
"tagType",
"webTitle"
]
},
"pagination": {
"type": "object",
"properties": {
"currentPage": {
"type": "number"
},
"lastPage": {
"type": "number"
},
"totalContent": {
"type": "number"
}
},
"required": [
"currentPage",
"lastPage",
"totalContent"
]
}
},
"required": [
Expand Down Expand Up @@ -684,6 +703,94 @@
"nav": {
"$ref": "#/definitions/FENavType"
},
"tags": {
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"description": "This type comes from `frontend`, hence the FE prefix.",
"type": "object",
"properties": {
"properties": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"tagType": {
"type": "string"
},
"webTitle": {
"type": "string"
},
"bio": {
"type": "string"
},
"bylineImageUrl": {
"type": "string"
},
"bylineLargeImageUrl": {
"type": "string"
},
"contributorLargeImagePath": {
"type": "string"
},
"paidContentType": {
"type": "string"
},
"sectionId": {
"type": "string"
},
"sectionName": {
"type": "string"
},
"twitterHandle": {
"type": "string"
},
"url": {
"type": "string"
},
"webUrl": {
"type": "string"
}
},
"required": [
"id",
"tagType",
"webTitle"
]
},
"pagination": {
"type": "object",
"properties": {
"currentPage": {
"type": "number"
},
"lastPage": {
"type": "number"
},
"totalContent": {
"type": "number"
}
},
"required": [
"currentPage",
"lastPage",
"totalContent"
]
}
},
"required": [
"properties"
]
}
}
},
"required": [
"tags"
]
},
"editionId": {
"$ref": "#/definitions/EditionId"
},
Expand Down Expand Up @@ -1042,6 +1149,9 @@
},
"isAdFreeUser": {
"type": "boolean"
},
"forceDay": {
"type": "boolean"
}
},
"required": [
Expand All @@ -1050,11 +1160,13 @@
"contents",
"editionId",
"editionLongForm",
"forceDay",
"guardianBaseURL",
"isAdFreeUser",
"nav",
"pageFooter",
"pageId",
"tags",
"webTitle",
"webURL"
],
Expand Down
12 changes: 6 additions & 6 deletions dotcom-rendering/src/model/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import Ajv from 'ajv';
import addFormats from 'ajv-formats';
import type { FEFrontType } from '../../src/types/front';
import type { FEArticleType } from '../types/frontend';
import type { FEIndexPageType } from '../types/indexPage';
import type { FENewslettersPageType } from '../types/newslettersPage';
import type { FETagFrontType } from '../types/tagFront';
import articleSchema from './article-schema.json';
import frontSchema from './front-schema.json';
import indexPageSchema from './index-page-schema.json';
import newslettersPageSchema from './newsletter-page-schema.json';
import tagFrontSchema from './tag-front-schema.json';

const options: Options = {
verbose: false,
Expand All @@ -23,7 +23,7 @@ addFormats(ajv);

const validateArticle = ajv.compile<FEArticleType>(articleSchema);
const validateFront = ajv.compile<FEFrontType>(frontSchema);
const validateIndexPage = ajv.compile<FEIndexPageType>(indexPageSchema);
const validateTagFront = ajv.compile<FETagFrontType>(tagFrontSchema);
const validateAllEditorialNewslettersPage = ajv.compile<FENewslettersPageType>(
newslettersPageSchema,
);
Expand Down Expand Up @@ -52,15 +52,15 @@ export const validateAsFrontType = (data: unknown): FEFrontType => {
);
};

export const validateAsIndexPageType = (data: unknown): FEIndexPageType => {
if (validateIndexPage(data)) return data;
export const validateAsTagFrontType = (data: unknown): FETagFrontType => {
if (validateTagFront(data)) return data;

const url =
isObject(data) && isString(data.webURL) ? data.webURL : 'unknown url';

throw new TypeError(
`Unable to validate request body for url ${url}.\n
${JSON.stringify(validateFront.errors, null, 2)}`,
${JSON.stringify(validateTagFront.errors, null, 2)}`,
);
};

Expand Down
12 changes: 6 additions & 6 deletions dotcom-rendering/src/server/dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
handleBlocks,
handleFront,
handleFrontJson,
handleIndexPage,
handleIndexPageJson,
handleInteractive,
handleKeyEvents,
handleTagFront,
handleTagPageJson,
} from '../web/server';

/** article URLs contain a part that looks like “2022/nov/25” */
Expand Down Expand Up @@ -52,10 +52,10 @@ export const devServer = (): Handler => {
return handleFront(req, res, next);
case 'FrontJSON':
return handleFrontJson(req, res, next);
case 'IndexPage':
return handleIndexPage(req, res, next);
case 'IndexPageJSON':
return handleIndexPageJson(req, res, next);
case 'TagFront':
return handleTagFront(req, res, next);
case 'TagFrontJSON':
return handleTagPageJson(req, res, next);
case 'EmailNewsletters':
return handleAllEditorialNewslettersPage(req, res, next);
case 'AppsArticle':
Expand Down

0 comments on commit 0cb345b

Please sign in to comment.