Skip to content

Commit d04b3b5

Browse files
tyhoppTylerBarnespieh
authored
feat(gatsby-source-drupal): drupal langcode as notlangcode (#37445) (#37459)
* feat(gatsby-source-drupal): drupal langcode as notlangcode (#37445) * add ts support * make all files ts * add new enabled languages plugin option object types/docs * add local test scripts * add normalization code for langCode as def * use proper joi method name * change property name * refactor for TS * ts changes * ts fixes * use renamed code in node id * attempt to disable easlint rules for source-drupal (cherry picked from commit 3e8bb5a) * yarn.lock * fix deps in backport Co-authored-by: Tyler Barnes <[email protected]> Co-authored-by: pieh <[email protected]>
1 parent 19d3861 commit d04b3b5

File tree

11 files changed

+394
-65
lines changed

11 files changed

+394
-65
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": [["babel-preset-gatsby-package"]]
2+
"presets": [["babel-preset-gatsby-package"], "@babel/preset-typescript"]
33
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rules": {
3+
"@typescript-eslint/explicit-function-return-type": "off",
4+
"@typescript-eslint/no-use-before-define": "off",
5+
"@typescript-eslint/naming-convention": "off",
6+
"@typescript-eslint/consistent-type-definitions": "off"
7+
}
8+
}

packages/gatsby-source-drupal/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,15 @@ module.exports = {
465465
baseUrl: `https://live-contentacms.pantheonsite.io/`,
466466
languageConfig: {
467467
defaultLanguage: `en`,
468-
enabledLanguages: [`en`, `fil`],
468+
enabledLanguages: [
469+
`en`,
470+
`fil`,
471+
// add an object here if you've renamed a langcode in Drupal
472+
{
473+
langCode: `en-gb`,
474+
as: `uk`,
475+
},
476+
],
469477
translatableEntities: [`node--article`],
470478
nonTranslatableEntities: [`file--file`],
471479
},

packages/gatsby-source-drupal/package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
"url-join": "^4.0.1"
2525
},
2626
"devDependencies": {
27-
"@babel/cli": "^7.15.4",
28-
"@babel/core": "^7.15.5",
27+
"@babel/cli": "^7.20.7",
28+
"@babel/core": "^7.20.7",
29+
"@babel/preset-typescript": "^7.18.6",
2930
"babel-preset-gatsby-package": "^2.25.0",
3031
"cross-env": "^7.0.3"
3132
},
@@ -48,8 +49,10 @@
4849
"directory": "packages/gatsby-source-drupal"
4950
},
5051
"scripts": {
51-
"build": "babel src --out-dir . --ignore \"**/__tests__\"",
52+
"build": "babel src --out-dir . --ignore \"**/__tests__\" --extensions \".ts,.js\"",
5253
"prepare": "cross-env NODE_ENV=production npm run build",
53-
"watch": "babel -w src --out-dir . --ignore \"**/__tests__\""
54+
"watch": "babel -w src --out-dir . --ignore \"**/__tests__\" --extensions \".ts,.js\"",
55+
"test": "npx jest ./src/__tests__ --runInBand",
56+
"test:watch": "npx jest --watch ./src/__tests__ --runInBand"
5457
}
5558
}

packages/gatsby-source-drupal/src/__tests__/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,13 @@ describe(`gatsby-source-drupal`, () => {
551551
apiBase,
552552
languageConfig: {
553553
defaultLanguage: `en_US`,
554-
enabledLanguages: [`en_US`, `i18n-test`],
554+
enabledLanguages: [
555+
`en_US`,
556+
{
557+
langCode: `en-gb`,
558+
as: `i18n-test`,
559+
},
560+
],
555561
translatableEntities: [`node--article`],
556562
nonTranslatableEntities: [],
557563
},

packages/gatsby-source-drupal/src/gatsby-node.js renamed to packages/gatsby-source-drupal/src/gatsby-node.ts

+27-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const got = require(`got`)
22
const _ = require(`lodash`)
33
const urlJoin = require(`url-join`)
44
import HttpAgent from "agentkeepalive"
5+
56
// const http2wrapper = require(`http2-wrapper`)
67
const opentracing = require(`opentracing`)
78
const { SemanticAttributes } = require(`@opentelemetry/semantic-conventions`)
@@ -15,12 +16,13 @@ const { HttpsAgent } = HttpAgent
1516

1617
const { setOptions, getOptions } = require(`./plugin-options`)
1718

18-
const {
19+
import {
1920
nodeFromData,
2021
downloadFile,
2122
isFileNode,
2223
imageCDNState,
23-
} = require(`./normalize`)
24+
} from "./normalize"
25+
2426
const {
2527
handleReferences,
2628
handleWebhookUpdate,
@@ -567,15 +569,19 @@ ${JSON.stringify(webhookBody, null, 4)}`
567569
throw error
568570
}
569571
}
572+
570573
if (d.body.data) {
571-
dataArray.push(...d.body.data)
574+
// @ts-ignore
575+
dataArray.push(...(d.body.data || []))
572576
}
577+
573578
// Add support for includes. Includes allow entity data to be expanded
574579
// based on relationships. The expanded data is exposed as `included`
575580
// in the JSON API response.
576581
// See https://www.drupal.org/docs/8/modules/jsonapi/includes
577582
if (d.body.included) {
578-
dataArray.push(...d.body.included)
583+
// @ts-ignore
584+
dataArray.push(...(d.body.included || []))
579585
}
580586

581587
// If JSON:API extras is configured to add the resource count, we can queue
@@ -593,8 +599,8 @@ ${JSON.stringify(webhookBody, null, 4)}`
593599

594600
// Get count of API requests
595601
// We round down as we've already gotten the first page at this point.
596-
const pageSize = new URL(d.body.links.next.href).searchParams.get(
597-
`page[limit]`
602+
const pageSize = Number(
603+
new URL(d.body.links.next.href).searchParams.get(`page[limit]`)
598604
)
599605
const requestsCount = Math.floor(d.body.meta.count / pageSize)
600606

@@ -604,11 +610,14 @@ ${JSON.stringify(webhookBody, null, 4)}`
604610

605611
const newUrl = new URL(d.body.links.next.href)
606612
await Promise.all(
607-
_.range(requestsCount).map(pageOffset => {
613+
_.range(requestsCount).map((pageOffset: number) => {
608614
// We're starting 1 ahead.
609615
pageOffset += 1
610616
// Construct URL with new pageOffset.
611-
newUrl.searchParams.set(`page[offset]`, pageOffset * pageSize)
617+
newUrl.searchParams.set(
618+
`page[offset]`,
619+
String(pageOffset * pageSize)
620+
)
612621
return getNext(newUrl.toString(), currentLanguage)
613622
})
614623
)
@@ -626,6 +635,7 @@ ${JSON.stringify(webhookBody, null, 4)}`
626635
const urlPath = url.href.split(`${apiBase}/`).pop()
627636
const baseUrlWithoutTrailingSlash = baseUrl.replace(/\/$/, ``)
628637
// The default language's JSON API is at the root.
638+
629639
if (
630640
currentLanguage === getOptions().languageConfig.defaultLanguage ||
631641
baseUrlWithoutTrailingSlash.slice(-currentLanguage.length) ==
@@ -869,7 +879,15 @@ exports.pluginOptionsSchema = ({ Joi }) =>
869879
),
870880
languageConfig: Joi.object({
871881
defaultLanguage: Joi.string().required(),
872-
enabledLanguages: Joi.array().items(Joi.string()).required(),
882+
enabledLanguages: Joi.array()
883+
.items(
884+
Joi.string(),
885+
Joi.object({
886+
langCode: Joi.string().required(),
887+
as: Joi.string().required(),
888+
})
889+
)
890+
.required(),
873891
translatableEntities: Joi.array().items(Joi.string()).required(),
874892
nonTranslatableEntities: Joi.array().items(Joi.string()).required(),
875893
}),

packages/gatsby-source-drupal/src/normalize.js renamed to packages/gatsby-source-drupal/src/normalize.ts

+57-39
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,20 @@ const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
33
const path = require(`path`)
44
const probeImageSize = require(`probe-image-size`)
55

6-
const { getOptions } = require(`./plugin-options`)
7-
const getHref = link => {
6+
import { getOptions } from "./plugin-options"
7+
8+
export const getHref = link => {
89
if (typeof link === `object`) {
910
return link.href
1011
}
1112
return link
1213
}
1314

14-
exports.getHref = getHref
15-
16-
const imageCDNState = {
15+
export const imageCDNState = {
1716
foundPlaceholderStyle: false,
1817
hasLoggedNoPlaceholderStyle: false,
1918
}
2019

21-
exports.imageCDNState = imageCDNState
22-
2320
let four04WarningCount = 0
2421
let corruptFileWarningCount = 0
2522
/**
@@ -142,17 +139,20 @@ const getGatsbyImageCdnFields = async ({
142139
return {}
143140
}
144141

145-
const nodeFromData = async (
142+
export const nodeFromData = async (
146143
datum,
147144
createNodeId,
148145
entityReferenceRevisions = [],
149146
pluginOptions,
150147
fileNodesExtendedData,
151148
reporter
152149
) => {
153-
const { attributes: { id: attributeId, ...attributes } = {} } = datum
150+
const { attributes: { id: attributeId, ...attributes } = { id: null } } =
151+
datum
152+
154153
const preservedId =
155154
typeof attributeId !== `undefined` ? { _attributes_id: attributeId } : {}
155+
156156
const langcode = attributes.langcode || `und`
157157
const type = datum.type.replace(/-|__|:|\.|\s/g, `_`)
158158

@@ -164,16 +164,18 @@ const nodeFromData = async (
164164
reporter,
165165
})
166166

167+
const versionedId = createNodeIdWithVersion(
168+
datum.id,
169+
datum.type,
170+
langcode,
171+
attributes.drupal_internal__revision_id,
172+
entityReferenceRevisions
173+
)
174+
175+
const gatsbyId = createNodeId(versionedId)
176+
167177
return {
168-
id: createNodeId(
169-
createNodeIdWithVersion(
170-
datum.id,
171-
datum.type,
172-
langcode,
173-
attributes.drupal_internal__revision_id,
174-
entityReferenceRevisions
175-
)
176-
),
178+
id: gatsbyId,
177179
drupal_id: datum.id,
178180
parent: null,
179181
drupal_parent_menu_item: attributes.parent,
@@ -189,52 +191,68 @@ const nodeFromData = async (
189191
}
190192
}
191193

192-
exports.nodeFromData = nodeFromData
193-
194194
const isEntityReferenceRevision = (type, entityReferenceRevisions = []) =>
195195
entityReferenceRevisions.findIndex(
196196
revisionType => type.indexOf(revisionType) === 0
197197
) !== -1
198198

199-
const createNodeIdWithVersion = (
200-
id,
201-
type,
202-
langcode,
203-
revisionId,
199+
export const createNodeIdWithVersion = (
200+
id: string,
201+
type: string,
202+
langcode: string,
203+
revisionId: string,
204204
entityReferenceRevisions = []
205205
) => {
206+
const options = getOptions()
207+
206208
// Fallback to default language for entities that don't translate.
207-
if (getOptions()?.languageConfig?.nonTranslatableEntities.includes(type)) {
208-
langcode = getOptions().languageConfig.defaultLanguage
209+
if (
210+
options?.languageConfig?.nonTranslatableEntities?.includes(type) &&
211+
options.languageConfig.defaultLanguage
212+
) {
213+
langcode = options.languageConfig.defaultLanguage
209214
}
210215

211216
// If the source plugin hasn't enabled `translation` then always just set langcode
212217
// to "undefined".
213-
let langcodeNormalized = getOptions().languageConfig ? langcode : `und`
218+
let langcodeNormalized = options.languageConfig ? langcode : `und`
219+
220+
const renamedCode = options?.languageConfig?.renamedEnabledLanguages?.find(
221+
lang => lang.langCode === langcodeNormalized
222+
)
223+
224+
if (renamedCode) {
225+
langcodeNormalized = renamedCode.as
226+
}
214227

215228
if (
216-
getOptions().languageConfig &&
217-
!getOptions().languageConfig?.enabledLanguages.includes(langcodeNormalized)
229+
!renamedCode &&
230+
options.languageConfig &&
231+
options.languageConfig.defaultLanguage &&
232+
!options?.languageConfig?.enabledLanguages?.includes(langcodeNormalized)
218233
) {
219-
langcodeNormalized = getOptions().languageConfig.defaultLanguage
234+
langcodeNormalized = options.languageConfig.defaultLanguage
220235
}
221236

237+
const isReferenceRevision = isEntityReferenceRevision(
238+
type,
239+
entityReferenceRevisions
240+
)
241+
222242
// The relationship between an entity and another entity also depends on the revision ID if the field is of type
223243
// entity reference revision such as for paragraphs.
224-
return isEntityReferenceRevision(type, entityReferenceRevisions)
244+
const idVersion = isReferenceRevision
225245
? `${langcodeNormalized}.${id}.${revisionId || 0}`
226246
: `${langcodeNormalized}.${id}`
227-
}
228247

229-
exports.createNodeIdWithVersion = createNodeIdWithVersion
248+
return idVersion
249+
}
230250

231-
const isFileNode = node => {
251+
export const isFileNode = node => {
232252
const type = node?.internal?.type
233253
return type === `files` || type === `file__file`
234254
}
235255

236-
exports.isFileNode = isFileNode
237-
238256
const getFileUrl = (node, baseUrl) => {
239257
let fileUrl = node.url
240258

@@ -249,8 +267,8 @@ const getFileUrl = (node, baseUrl) => {
249267
return url
250268
}
251269

252-
exports.downloadFile = async (
253-
{ node, store, cache, createNode, createNodeId, getCache, reporter },
270+
export const downloadFile = async (
271+
{ node, cache, createNode, createNodeId, getCache },
254272
{ basicAuth, baseUrl }
255273
) => {
256274
// handle file downloads

packages/gatsby-source-drupal/src/plugin-options.js

-10
This file was deleted.

0 commit comments

Comments
 (0)