UNPKG

@frontierjs/web

Version:

Web modules for FrontierJS

295 lines (292 loc) 8.25 kB
// ../../node_modules/svelte/internal/index.mjs function noop() { } function run(fn) { return fn(); } function run_all(fns) { fns.forEach(run); } function is_function(thing) { return typeof thing === "function"; } function safe_not_equal(a, b) { return a != a ? b == b : a !== b || (a && typeof a === "object" || typeof a === "function"); } function is_empty(obj) { return Object.keys(obj).length === 0; } var render_callbacks = []; function flush_render_callbacks(fns) { const filtered = []; const targets = []; render_callbacks.forEach((c) => fns.indexOf(c) === -1 ? filtered.push(c) : targets.push(c)); targets.forEach((c) => c()); render_callbacks = filtered; } var globals = typeof window !== "undefined" ? window : typeof globalThis !== "undefined" ? globalThis : global; var _boolean_attributes = [ "allowfullscreen", "allowpaymentrequest", "async", "autofocus", "autoplay", "checked", "controls", "default", "defer", "disabled", "formnovalidate", "hidden", "inert", "ismap", "itemscope", "loop", "multiple", "muted", "nomodule", "novalidate", "open", "playsinline", "readonly", "required", "reversed", "selected" ]; var boolean_attributes = /* @__PURE__ */ new Set([..._boolean_attributes]); function destroy_component(component, detaching) { const $$ = component.$$; if ($$.fragment !== null) { flush_render_callbacks($$.after_update); run_all($$.on_destroy); $$.fragment && $$.fragment.d(detaching); $$.on_destroy = $$.fragment = null; $$.ctx = []; } } var SvelteElement; if (typeof HTMLElement === "function") { SvelteElement = class extends HTMLElement { constructor() { super(); this.attachShadow({ mode: "open" }); } connectedCallback() { const { on_mount } = this.$$; this.$$.on_disconnect = on_mount.map(run).filter(is_function); for (const key in this.$$.slotted) { this.appendChild(this.$$.slotted[key]); } } attributeChangedCallback(attr, _oldValue, newValue) { this[attr] = newValue; } disconnectedCallback() { run_all(this.$$.on_disconnect); } $destroy() { destroy_component(this, 1); this.$destroy = noop; } $on(type, callback) { if (!is_function(callback)) { return noop; } const callbacks = this.$$.callbacks[type] || (this.$$.callbacks[type] = []); callbacks.push(callback); return () => { const index = callbacks.indexOf(callback); if (index !== -1) callbacks.splice(index, 1); }; } $set($$props) { if (this.$$set && !is_empty($$props)) { this.$$.skip_bound = true; this.$$set($$props); this.$$.skip_bound = false; } } }; } // ../../node_modules/svelte/store/index.mjs var subscriber_queue = []; function writable(value, start = noop) { let stop; const subscribers = /* @__PURE__ */ new Set(); function set(new_value) { if (safe_not_equal(value, new_value)) { value = new_value; if (stop) { const run_queue = !subscriber_queue.length; for (const subscriber of subscribers) { subscriber[1](); subscriber_queue.push(subscriber, value); } if (run_queue) { for (let i = 0; i < subscriber_queue.length; i += 2) { subscriber_queue[i][0](subscriber_queue[i + 1]); } subscriber_queue.length = 0; } } } } function update(fn) { set(fn(value)); } function subscribe2(run2, invalidate = noop) { const subscriber = [run2, invalidate]; subscribers.add(subscriber); if (subscribers.size === 1) { stop = start(set) || noop; } run2(value); return () => { subscribers.delete(subscriber); if (subscribers.size === 0 && stop) { stop(); stop = null; } }; } return { set, update, subscribe: subscribe2 }; } // src/resource.js function Resource(spec = {}) { const { api, models } = spec; function createMake(schema) { const defaults = { string: "", number: 0, integer: 0, boolean: false, array: void 0, dateTime: void 0, undefined: void 0, object: {} }; const schemaProps = Object.entries(schema).reduce((acc, [key, { type, format, default: defaultValue }]) => { type = [].concat(type)[0]; if (type === "string") { if (key === "externalId") type = "undefined"; if (format === "date-time") type = "dateTime"; if (defaultValue === '"{}"') type = "object"; if (defaultValue === '"[]"') type = "array"; } acc[key] = defaults[type]; return acc; }, {}); function make(props, options = {}) { const { exclusionList = [] } = options; const instance = { ...schemaProps, ...props }; const defaultExclusions = ["id", "uuid"]; defaultExclusions.concat(exclusionList).forEach((prop) => { delete instance[prop]; }); return instance; } return make; } function createService(serviceName, { model, modelSchema }) { const context = { model, schema: modelSchema, modelSchema }; return { get(id, params) { return api.get(serviceName, id, params, context); }, find(params) { return api.find(serviceName, params, context); }, upsert(data, params) { return api.upsert(serviceName, data, params, context); }, remove(params) { return api.remove(serviceName, params, context); }, restore(data, params) { data.deletedAt = null; return api.upsert(serviceName, data, params, context); }, on(event, callback) { return api.on(serviceName, event, callback, context); } }; } function createStore(service, { optionsQuery, initial = [] }) { const store = writable(initial); store.get = async function(id, params, options) { const response = await service.get(id, params, options); store.update(() => response); return response; }; store.getOptions = async function(params, options) { params = params || optionsQuery; const response = await service.find(params, options); return response; }; store.find = async function(params, options) { const response = await service.find(params, options); if (Array.isArray(initial)) { store.update(() => response.data); } else if (response.data) { store.update(() => response.data[0]); } else { store.update(() => response); } return response; }; store.remove = async function(params, options) { const response = await service.remove(params, options); store.update(($resources) => { return Array.isArray(initial) ? Array.remove($resources, params) : response; }); return response; }; store.upsert = async function(params, options) { const response = await service.upsert(params, options); if (Array.isArray(initial)) { store.update(($resources) => { return Array.upsert($resources, response); }); } else { } return response; }; store.restore = async function(params, options) { return await service.restore(params, options); }; store.on = async function(event, callback, options) { return await service.on(event, callback, options); }; return store; } function createResource({ model, service: serviceName, optionsQuery, storeValue, schema }) { const { definitions } = models; let modelSchema = schema; if (!modelSchema) { modelSchema = definitions[model] ? definitions[model].properties : null; } const make = modelSchema ? createMake(modelSchema) : (spec2) => ({ ...spec2 }); const service = createService(serviceName, { model, modelSchema }); const store = createStore(service, { optionsQuery, initial: storeValue }); return { schema: modelSchema, modelSchema, service, store, make }; } return { createService, createStore, createResource }; } export { Resource };