Skip to content

Commit

Permalink
Remove commonjs package
Browse files Browse the repository at this point in the history
This is a ES module that you should import into your browser bundle
and not require in NodeJS.
  • Loading branch information
tvdeyen committed Nov 15, 2024
1 parent f9a85f0 commit a746308
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 224 deletions.
File renamed without changes.
86 changes: 57 additions & 29 deletions dist/alchemy-json_api.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,132 @@
'use strict';
import structuredClone from '@ungap/structured-clone';

var structuredClone = require('@ungap/structured-clone');

function deserialize(originalResponse) {
function deserialize(originalResponse, options = {}) {
const response = structuredClone(originalResponse);
if (!options) {
options = {};
}

const included = response.included || [];

if (Array.isArray(response.data)) {
return response.data.map(data => {
return parseJsonApiSimpleResourceData(data, included, false);
});
return response.data.map((data) => {
return parseJsonApiSimpleResourceData(data, included, false)
})
} else {
return parseJsonApiSimpleResourceData(response.data, included, false);
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];
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);

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);
resource[relationName] = findJsonApiIncluded(
included,
relationRef.data.type,
relationRef.data.id);
} else {
resource[relationName] = null;
}
}
}
return resource;

return resource
}

function findJsonApiIncluded(included, type, id, options) {
let found = null;
included.forEach(item => {

included.forEach((item) => {
if (item.type === type && item.id === id) {
found = parseJsonApiSimpleResourceData(item, included, true);
}
});

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

return found
}

// Recursively filters all deprecated elements and essences from collection
function filterDeprecatedElements(elements) {
const els = [];
elements.forEach(element => {

elements.forEach((element) => {
if (element.nested_elements?.length > 0) {
element.nested_elements = filterDeprecatedElements(element.nested_elements);
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;
element.essences = element.essences.filter((essence) => {
return !essence.deprecated
});
}
if (!element.deprecated) {
els.push(element);
}
});
return els;

return els
}

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

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

exports.deserialize = deserialize;
exports.deserializePage = deserializePage;
exports.deserializePages = deserializePages;
export { deserialize, deserializePage, deserializePages };
100 changes: 0 additions & 100 deletions dist/alchemy-json_api.mjs

This file was deleted.

64 changes: 45 additions & 19 deletions dist/deserialize.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,90 @@
'use strict';
import structuredClone from '@ungap/structured-clone';

var structuredClone = require('@ungap/structured-clone');

function deserialize(originalResponse) {
function deserialize(originalResponse, options = {}) {
const response = structuredClone(originalResponse);
if (!options) {
options = {};
}

const included = response.included || [];

if (Array.isArray(response.data)) {
return response.data.map(data => {
return parseJsonApiSimpleResourceData(data, included, false);
});
return response.data.map((data) => {
return parseJsonApiSimpleResourceData(data, included, false)
})
} else {
return parseJsonApiSimpleResourceData(response.data, included, false);
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];
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);

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);
resource[relationName] = findJsonApiIncluded(
included,
relationRef.data.type,
relationRef.data.id);
} else {
resource[relationName] = null;
}
}
}
return resource;

return resource
}

function findJsonApiIncluded(included, type, id, options) {
let found = null;
included.forEach(item => {

included.forEach((item) => {
if (item.type === type && item.id === id) {
found = parseJsonApiSimpleResourceData(item, included, true);
}
});

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

return found
}

exports.deserialize = deserialize;
export { deserialize };
Loading

0 comments on commit a746308

Please sign in to comment.