@brandboostinggmbh/painless
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -7,2 +7,3 @@ var __defProp = Object.defineProperty; | ||
}; | ||
var _a, _b; | ||
function pascalCaseToKebabCase(str) { | ||
@@ -188,7 +189,161 @@ return str.replace(/([A-z])([A-Z])/g, "$1-$2").toLowerCase(); | ||
} | ||
class GlobalEvents { | ||
static emit(type, data) { | ||
document.dispatchEvent(new CustomEvent(type, { detail: data })); | ||
this.evt.emit(type, data); | ||
} | ||
static on(type, func) { | ||
this.evt.on(type, func); | ||
} | ||
} | ||
__publicField(GlobalEvents, "evt", new EventManager()); | ||
class Cart { | ||
constructor() { | ||
__publicField(this, "baseUrl", `${(_b = (_a = window == null ? void 0 : window.Shopify) == null ? void 0 : _a.routes) == null ? void 0 : _b.root}`); | ||
__publicField(this, "evt", new EventManager()); | ||
} | ||
on(type, func) { | ||
this.evt.on(type, func); | ||
} | ||
emit(type, data) { | ||
this.evt.emit(type, data); | ||
GlobalEvents.emit(`cart:${type}`, data); | ||
} | ||
async fetch() { | ||
const res = await fetch(`${this.baseUrl}cart.js`, { credentials: "same-origin" }); | ||
return res.json(); | ||
} | ||
async updateLineItem(keyOrId, quantity) { | ||
var _a2; | ||
const response = await this.fetch(); | ||
const lineItems = response.items; | ||
const item = lineItems == null ? void 0 : lineItems.find((item2) => item2.id == keyOrId || item2.key == keyOrId); | ||
const entries = [[keyOrId, quantity]]; | ||
if (quantity == 0 && item != null && ((_a2 = item.properties) == null ? void 0 : _a2._unique) != null) { | ||
const findAddons = lineItems == null ? void 0 : lineItems.filter((i) => { | ||
var _a3, _b2; | ||
return ((_a3 = i.properties) == null ? void 0 : _a3._unique) == ((_b2 = item.properties) == null ? void 0 : _b2._unique) && i.id != item.id; | ||
}); | ||
entries.push(...findAddons.map((i) => [i.key, quantity])); | ||
} | ||
await fetch( | ||
`${this.baseUrl}cart/update.js`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ | ||
updates: Object.fromEntries(entries) | ||
}), | ||
credentials: "same-origin", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Requested-With": "XMLHttpRequest" | ||
} | ||
} | ||
); | ||
this.evt.emit("updated"); | ||
} | ||
async removeLineItem(key) { | ||
await this.updateLineItem(key, 0); | ||
} | ||
async updateCartAttributes(attributes) { | ||
await fetch( | ||
`${this.baseUrl}cart/update.js`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ | ||
attributes | ||
}), | ||
credentials: "same-origin", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Requested-With": "XMLHttpRequest" | ||
} | ||
} | ||
); | ||
this.evt.emit("updated"); | ||
} | ||
async addItem(...items) { | ||
let normalizedItems = items.map( | ||
(item) => { | ||
let id; | ||
let quantity = 1; | ||
let properties; | ||
if (typeof item === "number") { | ||
id = item; | ||
} else if (typeof item === "string") { | ||
id = parseInt(item); | ||
} else { | ||
id = item.id; | ||
quantity = item.quantity ?? 1; | ||
properties = item.properties; | ||
} | ||
return { | ||
id, | ||
quantity, | ||
properties | ||
}; | ||
} | ||
); | ||
await fetch( | ||
`${this.baseUrl}cart/add.js`, | ||
{ | ||
method: "POST", | ||
body: JSON.stringify({ "items": normalizedItems }), | ||
credentials: "same-origin", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"X-Requested-With": "XMLHttpRequest" | ||
} | ||
} | ||
); | ||
this.evt.emit("updated"); | ||
} | ||
} | ||
class SectionRenderer { | ||
static async renderSection(sectionId) { | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("sections", sectionId); | ||
const response = await fetch(url); | ||
const data = await response.json(); | ||
return data[sectionId]; | ||
} | ||
} | ||
class Theme { | ||
} | ||
__publicField(Theme, "Cart", new Cart()); | ||
const wait = (ms) => new Promise((res) => setTimeout(() => res(), ms)); | ||
class Debounce { | ||
static debounce(callback, wait2) { | ||
let timeoutId = null; | ||
return (...args) => { | ||
window.clearTimeout(timeoutId); | ||
timeoutId = window.setTimeout(() => { | ||
callback(...args); | ||
}, wait2); | ||
}; | ||
} | ||
} | ||
const defineSchema = (schema) => { | ||
return schema; | ||
}; | ||
const defineTemplate = (schema) => { | ||
return schema; | ||
}; | ||
const defineThemeSettingsSchema = (schema) => { | ||
return schema; | ||
}; | ||
export { | ||
Cart, | ||
Debounce, | ||
EventManager, | ||
GlobalEvents, | ||
PainlessElement, | ||
SectionElement, | ||
SectionRenderer, | ||
SnippetElement, | ||
Theme, | ||
defineElement, | ||
defineSchema, | ||
defineTemplate, | ||
defineThemeSettingsSchema, | ||
globalOn, | ||
@@ -203,3 +358,4 @@ liquidValue, | ||
setting, | ||
state | ||
state, | ||
wait | ||
}; |
@@ -13,1 +13,12 @@ export { defineElement, pascalCaseToKebabCase } from './decorators/defineElement'; | ||
export { SnippetElement } from './elements/SnippetElement'; | ||
export { Cart } from './shared/Cart'; | ||
export { EventManager } from './shared/EventManager'; | ||
export { GlobalEvents } from './shared/GlobalEvents'; | ||
export { SectionRenderer } from './shared/SectionRenderer'; | ||
export { Theme } from './shared/Theme'; | ||
export * from './shared/types'; | ||
export { wait } from './shared/wait'; | ||
export { Debounce } from './util/debounce'; | ||
export { defineSchema } from './util/defineSchema'; | ||
export { defineTemplate } from './util/defineTemplate'; | ||
export { defineThemeSettingsSchema } from './util/defineThemeSettingsSchema'; |
{ | ||
"name": "@brandboostinggmbh/painless", | ||
"private": false, | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40372
29
1181
10