Skip to content

Commit 2c24a1d

Browse files
committed
DRUP-810: Load and parse specs on client side
The field formatter now puts the file URL and file extension into drupalSettings instead of actual file. The smartdos_integration js file is now responsible for pulling file from server and parsing into javascript to save into session storage for SmartDocs Angular app to retrieve.
1 parent 1605f05 commit 2c24a1d

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

apigee_api_catalog.libraries.yml

+10
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ apigee_api_catalog.smartdocs_integration:
2424
dependencies:
2525
- core/jquery
2626
- core/drupal
27+
28+
# Library to parse OpenAPI YAML files to pass to SmartDocs Angular app.
29+
apigee_api_catalog.js_yaml:
30+
version: 3.13.1
31+
license:
32+
name: MIT
33+
url: https://github.com/nodeca/js-yaml/blob/master/LICENSE
34+
gpl-compatible: true
35+
js:
36+
https://cdn.jsdelivr.net/npm/[email protected]/dist/js-yaml.min.js: { type: external, minified: true }

js/smartdocs_integration.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@
1515
if (drupalSettings.smartdocsFieldFormatter.hasOwnProperty(fieldName)) {
1616
const field = drupalSettings.smartdocsFieldFormatter[fieldName];
1717
for (let fieldDelta = 0; fieldDelta < field.openApiFiles.length; fieldDelta++) {
18-
specMap[field.entityId] = field.openApiFiles[fieldDelta];
18+
// Each openApiFile var has the spec url and file extension.
19+
const fileUrl = field.openApiFiles[fieldDelta].fileUrl;
20+
const fileExtention = field.openApiFiles[fieldDelta].fileExtension;
21+
// Get the OpenAPI spec file from the server.
22+
$.get(fileUrl, function(data, status) {
23+
if (fileExtention === 'json') {
24+
// JSON files do not need any conversion.
25+
specMap[field.entityId] = data;
26+
}
27+
else {
28+
// YAML files need to be parsed into javascript object.
29+
specMap[field.entityId] = jsyaml.load(data);
30+
}
31+
// Store the spec into session storage.
32+
sessionStorage.setItem('specs', JSON.stringify(specMap));
33+
});
1934
}
2035
}
2136
}
22-
sessionStorage.setItem('specs', JSON.stringify(specMap));
2337
}
2438
};
2539

src/Plugin/Field/FieldFormatter/SmartDocsFormatter.php

+5-29
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,17 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
122122

123123
/** @var \Drupal\file\Entity\File $file */
124124
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $file) {
125-
$file_extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
126-
$file_data = file_get_contents($file->getFileUri());
127-
128-
// Decode the file to pass to the javascript file.
129-
if ($file_extension == 'json') {
130-
$spec = Json::decode($file_data);
131-
132-
if (!$spec) {
133-
$this->loggerFactory->error('Error parsing JSON file ' . $file->getFilename());
134-
$this->messenger()->addError($this->t('Error parsing JSON file %filename.', [
135-
'%filename' => $file->getFilename(),
136-
]));
137-
continue;
138-
}
139-
}
140-
else {
141-
try {
142-
$spec = Yaml::decode($file_data);
143-
}
144-
catch (InvalidDataTypeException $e) {
145-
$this->loggerFactory->error('Error parsing Yaml file ' . $file->getFilename() . ': ' . $e->getMessage());
146-
$this->messenger()->addError($this->t('Error parsing Yaml file %filename: %error', [
147-
'%filename' => $file->getFilename(),
148-
'%error' => $e->getMessage(),
149-
]));
150-
continue;
151-
}
152-
}
153-
$openapi_files[] = $spec;
125+
$openapi_files[] = [
126+
'fileUrl' => $file->url(),
127+
'fileExtension' => pathinfo($file->getFilename(), PATHINFO_EXTENSION),
128+
];
154129
}
155130

156131
$elements['#attached'] = [
157132
'library' => [
158133
'apigee_api_catalog/apigee_api_catalog.smartdocs',
159134
'apigee_api_catalog/apigee_api_catalog.smartdocs_integration',
135+
'apigee_api_catalog/apigee_api_catalog.js_yaml',
160136
],
161137
];
162138

0 commit comments

Comments
 (0)