Skip to content
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

Build .mjs (ES Modules) as well #87

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ concurrency:
jobs:
Standard:
runs-on: ubuntu-latest
env:
ALCHEMY_BRANCH: 7.3-stable
steps:
- name: Checkout code
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/alchemy/json_api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def log_error(exception)
logger = Rails.logger
return unless logger

message = +"\n#{exception.class} (#{exception.message}):\n"
message = "\n#{exception.class} (#{exception.message}):\n"
message << exception.annotated_source_code.to_s if exception.respond_to?(:annotated_source_code)
message << " " << exception.backtrace.join("\n ")
logger.fatal("#{message}\n\n")
Expand Down
78 changes: 30 additions & 48 deletions dist/alchemy-json_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,115 +9,97 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
var structuredClone__default = /*#__PURE__*/_interopDefaultLegacy(structuredClone);

function deserialize(originalResponse) {
var response = structuredClone__default["default"](originalResponse);

var included = response.included || [];

const response = structuredClone__default["default"](originalResponse);
const included = response.included || [];
if (Array.isArray(response.data)) {
return response.data.map(function (data) {
return response.data.map(data => {
return parseJsonApiSimpleResourceData(data, included, false);
});
} else {
return parseJsonApiSimpleResourceData(response.data, included, false);
}
}

function parseJsonApiSimpleResourceData(data, included, useCache, options) {
if (!included.cached) {
included.cached = {};
}

if (!(data.type in included.cached)) {
included.cached[data.type] = {};
}

if (useCache && data.id in included.cached[data.type]) {
return included.cached[data.type][data.id];
}

var attributes = data.attributes || {};
var resource = attributes;
const attributes = data.attributes || {};
const resource = attributes;
resource.id = data.id;
included.cached[data.type][data.id] = resource;

if (data.relationships) {
for (var _i = 0, _Object$keys = Object.keys(data.relationships); _i < _Object$keys.length; _i++) {
var relationName = _Object$keys[_i];
var relationRef = data.relationships[relationName];

for (const relationName of Object.keys(data.relationships)) {
const relationRef = data.relationships[relationName];
if (Array.isArray(relationRef.data)) {
(function () {
var items = [];
relationRef.data.forEach(function (relationData) {
var item = findJsonApiIncluded(included, relationData.type, relationData.id);
items.push(item);
});
resource[relationName] = items;
})();
const items = [];
relationRef.data.forEach(relationData => {
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
items.push(item);
});
resource[relationName] = items;
} else if (relationRef && relationRef.data) {
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
} else {
resource[relationName] = null;
}
}
}

return resource;
}

function findJsonApiIncluded(included, type, id, options) {
var found = null;
included.forEach(function (item) {
let found = null;
included.forEach(item => {
if (item.type === type && item.id === id) {
found = parseJsonApiSimpleResourceData(item, included, true);
}
});

if (!found) {
found = {
id: id
id
};
}

return found;
}

// Recursively filters all deprecated elements and essences from collection
function filterDeprecatedElements(elements) {
var els = [];
elements.forEach(function (element) {
var _element$nested_eleme, _element$nestedElemen, _element$essences;

if (((_element$nested_eleme = element.nested_elements) === null || _element$nested_eleme === void 0 ? void 0 : _element$nested_eleme.length) > 0) {
const els = [];
elements.forEach(element => {
if (element.nested_elements?.length > 0) {
element.nested_elements = filterDeprecatedElements(element.nested_elements);
}

if (((_element$nestedElemen = element.nestedElements) === null || _element$nestedElemen === void 0 ? void 0 : _element$nestedElemen.length) > 0) {
if (element.nestedElements?.length > 0) {
element.nestedElements = filterDeprecatedElements(element.nestedElements);
}

if (((_element$essences = element.essences) === null || _element$essences === void 0 ? void 0 : _element$essences.length) > 0) {
element.essences = element.essences.filter(function (essence) {
if (element.essences?.length > 0) {
element.essences = element.essences.filter(essence => {
return !essence.deprecated;
});
}

if (!element.deprecated) {
els.push(element);
}
});
return els;
} // Returns deserialized page without deprecated content

}

// Returns deserialized page without deprecated content
function deserializePage(pageData) {
var page = deserialize(pageData);
const page = deserialize(pageData);
page.elements = filterDeprecatedElements(page.elements);
return page;
} // Returns deserialized pages without deprecated content
}

// Returns deserialized pages without deprecated content
function deserializePages(pagesData) {
var pages = deserialize(pagesData);
pages.forEach(function (page) {
const pages = deserialize(pagesData);
pages.forEach(page => {
page.elements = filterDeprecatedElements(page.elements);
});
return pages;
Expand Down
100 changes: 100 additions & 0 deletions dist/alchemy-json_api.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import structuredClone from '@ungap/structured-clone';

function deserialize(originalResponse) {
const response = structuredClone(originalResponse);
const included = response.included || [];
if (Array.isArray(response.data)) {
return response.data.map(data => {
return parseJsonApiSimpleResourceData(data, included, false);
});
} else {
return parseJsonApiSimpleResourceData(response.data, included, false);
}
}
function parseJsonApiSimpleResourceData(data, included, useCache, options) {
if (!included.cached) {
included.cached = {};
}
if (!(data.type in included.cached)) {
included.cached[data.type] = {};
}
if (useCache && data.id in included.cached[data.type]) {
return included.cached[data.type][data.id];
}
const attributes = data.attributes || {};
const resource = attributes;
resource.id = data.id;
included.cached[data.type][data.id] = resource;
if (data.relationships) {
for (const relationName of Object.keys(data.relationships)) {
const relationRef = data.relationships[relationName];
if (Array.isArray(relationRef.data)) {
const items = [];
relationRef.data.forEach(relationData => {
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
items.push(item);
});
resource[relationName] = items;
} else if (relationRef && relationRef.data) {
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
} else {
resource[relationName] = null;
}
}
}
return resource;
}
function findJsonApiIncluded(included, type, id, options) {
let found = null;
included.forEach(item => {
if (item.type === type && item.id === id) {
found = parseJsonApiSimpleResourceData(item, included, true);
}
});
if (!found) {
found = {
id
};
}
return found;
}

// Recursively filters all deprecated elements and essences from collection
function filterDeprecatedElements(elements) {
const els = [];
elements.forEach(element => {
if (element.nested_elements?.length > 0) {
element.nested_elements = filterDeprecatedElements(element.nested_elements);
}
if (element.nestedElements?.length > 0) {
element.nestedElements = filterDeprecatedElements(element.nestedElements);
}
if (element.essences?.length > 0) {
element.essences = element.essences.filter(essence => {
return !essence.deprecated;
});
}
if (!element.deprecated) {
els.push(element);
}
});
return els;
}

// Returns deserialized page without deprecated content
function deserializePage(pageData) {
const page = deserialize(pageData);
page.elements = filterDeprecatedElements(page.elements);
return page;
}

// Returns deserialized pages without deprecated content
function deserializePages(pagesData) {
const pages = deserialize(pagesData);
pages.forEach(page => {
page.elements = filterDeprecatedElements(page.elements);
});
return pages;
}

export { deserialize, deserializePage, deserializePages };
47 changes: 16 additions & 31 deletions dist/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,61 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
var structuredClone__default = /*#__PURE__*/_interopDefaultLegacy(structuredClone);

function deserialize(originalResponse) {
var response = structuredClone__default["default"](originalResponse);

var included = response.included || [];

const response = structuredClone__default["default"](originalResponse);
const included = response.included || [];
if (Array.isArray(response.data)) {
return response.data.map(function (data) {
return response.data.map(data => {
return parseJsonApiSimpleResourceData(data, included, false);
});
} else {
return parseJsonApiSimpleResourceData(response.data, included, false);
}
}

function parseJsonApiSimpleResourceData(data, included, useCache, options) {
if (!included.cached) {
included.cached = {};
}

if (!(data.type in included.cached)) {
included.cached[data.type] = {};
}

if (useCache && data.id in included.cached[data.type]) {
return included.cached[data.type][data.id];
}

var attributes = data.attributes || {};
var resource = attributes;
const attributes = data.attributes || {};
const resource = attributes;
resource.id = data.id;
included.cached[data.type][data.id] = resource;

if (data.relationships) {
for (var _i = 0, _Object$keys = Object.keys(data.relationships); _i < _Object$keys.length; _i++) {
var relationName = _Object$keys[_i];
var relationRef = data.relationships[relationName];

for (const relationName of Object.keys(data.relationships)) {
const relationRef = data.relationships[relationName];
if (Array.isArray(relationRef.data)) {
(function () {
var items = [];
relationRef.data.forEach(function (relationData) {
var item = findJsonApiIncluded(included, relationData.type, relationData.id);
items.push(item);
});
resource[relationName] = items;
})();
const items = [];
relationRef.data.forEach(relationData => {
const item = findJsonApiIncluded(included, relationData.type, relationData.id);
items.push(item);
});
resource[relationName] = items;
} else if (relationRef && relationRef.data) {
resource[relationName] = findJsonApiIncluded(included, relationRef.data.type, relationRef.data.id);
} else {
resource[relationName] = null;
}
}
}

return resource;
}

function findJsonApiIncluded(included, type, id, options) {
var found = null;
included.forEach(function (item) {
let found = null;
included.forEach(item => {
if (item.type === type && item.id === id) {
found = parseJsonApiSimpleResourceData(item, included, true);
}
});

if (!found) {
found = {
id: id
id
};
}

return found;
}

Expand Down
Loading
Loading