From a746308ce5bb23054036870f86e3c9e65ddb584e Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Fri, 15 Nov 2024 12:55:39 +0100 Subject: [PATCH] Remove commonjs package This is a ES module that you should import into your browser bundle and not require in NodeJS. --- babel.config.js => babel.config.cjs | 0 dist/alchemy-json_api.js | 86 ++++++++++++++++-------- dist/alchemy-json_api.mjs | 100 ---------------------------- dist/deserialize.js | 64 ++++++++++++------ dist/deserialize.mjs | 62 ----------------- package.json | 2 +- rollup.config.mjs | 13 ---- 7 files changed, 103 insertions(+), 224 deletions(-) rename babel.config.js => babel.config.cjs (100%) delete mode 100644 dist/alchemy-json_api.mjs delete mode 100644 dist/deserialize.mjs diff --git a/babel.config.js b/babel.config.cjs similarity index 100% rename from babel.config.js rename to babel.config.cjs diff --git a/dist/alchemy-json_api.js b/dist/alchemy-json_api.js index f5dc001..2aa91ff 100644 --- a/dist/alchemy-json_api.js +++ b/dist/alchemy-json_api.js @@ -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 }; diff --git a/dist/alchemy-json_api.mjs b/dist/alchemy-json_api.mjs deleted file mode 100644 index c498ffc..0000000 --- a/dist/alchemy-json_api.mjs +++ /dev/null @@ -1,100 +0,0 @@ -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 }; diff --git a/dist/deserialize.js b/dist/deserialize.js index 50d6199..64c23ab 100644 --- a/dist/deserialize.js +++ b/dist/deserialize.js @@ -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 }; diff --git a/dist/deserialize.mjs b/dist/deserialize.mjs deleted file mode 100644 index 9290883..0000000 --- a/dist/deserialize.mjs +++ /dev/null @@ -1,62 +0,0 @@ -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; -} - -export { deserialize }; diff --git a/package.json b/package.json index ae98c71..c204f2e 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "Thomas von Deyen" ], "main": "dist/alchemy-json_api.js", - "module": "dist/alchemy-json_api.mjs", + "type": "module", "files": [ "dist" ], diff --git a/rollup.config.mjs b/rollup.config.mjs index 37f838b..728d6d7 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -1,6 +1,3 @@ -import babel from "@rollup/plugin-babel" - -const plugins = [babel({ babelHelpers: "bundled" })] const external = ["@ungap/structured-clone"] export default [ @@ -9,14 +6,9 @@ export default [ output: [ { file: "dist/alchemy-json_api.js", - format: "cjs" - }, - { - file: "dist/alchemy-json_api.mjs", format: "esm" } ], - plugins, external }, { @@ -24,14 +16,9 @@ export default [ output: [ { file: "dist/deserialize.js", - format: "cjs" - }, - { - file: "dist/deserialize.mjs", format: "esm" } ], - plugins, external } ]