'use strict'; var METHOD = /* @__PURE__ */ ((METHOD2) => { METHOD2["GET"] = "get"; METHOD2["POST"] = "post"; METHOD2["DELETE"] = "delete"; METHOD2["PATCH"] = "patch"; METHOD2["PUT"] = "put"; return METHOD2; })(METHOD || {}); const post = createAction(METHOD.POST); const del = createAction(METHOD.DELETE); const get = createAction(METHOD.GET); const patch = createAction(METHOD.PATCH); const put = createAction(METHOD.PUT); function createAction(type) { return (path, resolve) => ({ path, type, resolve }); } function createAbortController() { if (typeof AbortController !== "undefined") { return new AbortController(); } return { abort: () => null, signal: null }; } const createPath = ({ entity, params, baseUrl, path: inputPath }) => { let path = `/${entity || ""}${inputPath}`; const existsParams = params && Object.keys(params).length !== 0; if (existsParams) { const matches = path.match(/:\w+/gi) || []; matches.forEach((matchItem) => { const name = matchItem.substring(1); path = path.replace(matchItem, params[name]); }); } if (baseUrl) { path = `${baseUrl}${path}`; } return path; }; const createPath$1 = createPath; const isFormData = (params) => typeof params.__proto__.append !== "undefined"; function fetcher(props) { const { type, signal, path, params } = props; const parseMethod = (type || "get").toUpperCase(); return fetch(path, { method: parseMethod, headers: { "Content-Type": "application/json" }, signal, ...parseMethod !== "GET" && { body: isFormData(params) ? params : JSON.stringify(params || "{}") } }).then((response) => response.json()); } function resolveAdapter(adapter) { const resolver = async (props) => { const { path, params, type, signal } = props; const getParams = params ?? {}; return adapter[type || "get"](path, { ...getParams, signal }); }; return resolver; } function createServiceEntity(config) { const finalActions = {}; const abort = {}; function configure(customConfig) { if (customConfig.baseUrl) { config.baseUrl = customConfig.baseUrl; } if (customConfig.adapter) { config.adapter = customConfig.adapter; } if (config.entity === null || config.entity === void 0) { config.entity = customConfig.entity; } } function setupActions() { const { actions, entity, adapter, baseUrl } = config; const request = resolveAdapter(adapter); for (const actionName in actions) { const action = actions[actionName]; const isActionFullPath = typeof action === "string"; const controller = createAbortController(); abort[actionName] = () => controller.abort(); finalActions[actionName] = async (params = void 0) => { if (typeof action === "function") { const filterActions = { ...finalActions }; delete filterActions[actionName]; return action({ actions: { ...filterActions, abort }, params }); } const path = createPath$1({ path: isActionFullPath ? action : action.path, entity, params, baseUrl }); return new Promise(async (resolve, reject) => { try { const buildAdapterCallback = adapter ? request : fetcher; const response = await buildAdapterCallback({ type: isActionFullPath ? "get" : action.type, path, params, ...controller.signal && { signal: controller.signal } }); if (!isActionFullPath && action.resolve) { const resolver = action.resolve(response); resolve(resolver); } resolve(response); } catch (error) { reject(new Error(error)); } }); }; } return finalActions; } if (config.baseUrl || config.adapter) { setupActions(); } return { ...finalActions, abort, setupActions, configure }; } function configureServiceEntity(config) { const { entities, baseUrl, adapter } = config; const services = {}; for (const name in entities) { const currentEntity = entities[name]; currentEntity.configure({ baseUrl, adapter, entity: name }); const actions = currentEntity.setupActions(); services[name] = { ...currentEntity, ...actions }; } return services; } exports.configureServiceEntity = configureServiceEntity; exports.createServiceEntity = createServiceEntity; exports.del = del; exports.get = get; exports.patch = patch; exports.post = post; exports.put = put;