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Q,GACvD,YAAsB,IAAlB,EAAO,cACH,EAAS,SACR,IAET,EAAO,YAAc,EAAS,OACvB,EACT,EAGF,MAAM,wBAAwB,WAC5B,MACA,gBACA,UACA,WAAA,CAAY,EAAU,GACpB,MAAM,EAAU,GAChB,KAAK,MAAQ,QAAQ,UAAU,OAAS,KACxC,KAAK,gBAAkB,QAAQ,UAAY,CAAC,EAC5C,KAAK,UAAY,CAAC,CACpB,CACA,GAAA,GACE,OAAO,YAAY,KACrB,CACA,QAAA,CAAS,GAEP,OADA,KAAK,MAAQ,EACN,IACT,EAGF,SAAS,YAAY,GACnB,OAAO,IAAI,QAAQ,EACrB,CAEA,SAAS,aAAa,GACpB,MAAM,EAAQ,iBAEd,OADA,gBAAgB,SAAW,EACpB,QAAQ,gBAAiB,EAClC,CAEA,SAAS,oBAAoB,GAC3B,OAAO,sBAAsB,IAC3B,EAAQ,OAAO,SAAW,EACnB,IAEX,CACA,MAAM,oBAAsB,6BAEnB,gBAAiB,QAAS,SAAU,oBAAqB,oBAAqB,YAAa","file":"/npm/@pinia-orm/axios-edge@2.0.0-29398300.a7cc431/dist/index.mjs","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.39.0.\n * Original file: /npm/@pinia-orm/axios-edge@2.0.0-29398300.a7cc431/dist/index.mjs\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","import { Repository, config, useRepo, definePiniaOrmPlugin } from 'pinia-orm';\nimport { getActivePinia } from 'pinia';\n\nclass Response {\n  /**\n   * The repository that called the request.\n   */\n  repository;\n  /**\n   * The request configuration.\n   */\n  config;\n  /**\n   * The axios response instance.\n   */\n  response;\n  /**\n   * Entities created by Pinia ORM.\n   */\n  entities = null;\n  /**\n   * Whether if response data is saved to the store or not.\n   */\n  isSaved = false;\n  /**\n   * Create a new response instance.\n   */\n  constructor(repository, config, response) {\n    this.repository = repository;\n    this.config = config;\n    this.response = response;\n  }\n  /**\n   * Save response data to the store.\n   */\n  async save() {\n    const data = this.getDataFromResponse();\n    if (!this.validateData(data)) {\n      console.warn(\n        \"[Pinia ORM Axios] The response data could not be saved to the store because it is not an object or an array. You might want to use `dataTransformer` option to handle non-array/object response before saving it to the store.\"\n      );\n      return;\n    }\n    let method = this.config.persistBy || \"save\";\n    if (!this.validatePersistAction(method)) {\n      console.warn(\n        '[Pinia ORM Axios] The \"persistBy\" option configured is not a recognized value. Response data will be persisted by the default `save` method.'\n      );\n      method = \"save\";\n    }\n    const result = await this.repository[method](data);\n    this.entities = Array.isArray(result) ? result : [result];\n    this.isSaved = true;\n  }\n  /**\n   * Delete the entity record where the `delete` option is configured.\n   */\n  async delete() {\n    if (this.config.delete === void 0) {\n      throw new Error(\n        \"[Pinia ORM Axios] Could not delete records because the `delete` option is not set.\"\n      );\n    }\n    await this.repository.query().destroy(this.config.delete);\n  }\n  /**\n   * Get the response data from the axios response object. If a `dataTransformer`\n   * option is configured, it will be applied to the response object. If the\n   * `dataKey` option is configured, it will return the data from the given\n   * property within the response body.\n   */\n  getDataFromResponse() {\n    if (this.config.dataTransformer) {\n      return this.config.dataTransformer(this.response);\n    }\n    if (this.config.dataKey) {\n      return this.response.data[this.config.dataKey];\n    }\n    return this.response.data;\n  }\n  /**\n   * Get persist options if any set in config.\n   */\n  // protected getPersistOptions (): PersistOptions | undefined {\n  //   const persistOptions = this.config.persistOptions\n  //\n  //   if (!persistOptions || typeof persistOptions !== 'object') {\n  //     return\n  //   }\n  //\n  //   return Object.keys(persistOptions)\n  //     .filter(this.validatePersistAction) // Filter to avoid polluting the payload.\n  //     .reduce((carry, key) => {\n  //       carry[key] = persistOptions[key]\n  //       return carry\n  //     }, {} as PersistOptions)\n  // }\n  /**\n   * Validate the given data to ensure the Pinia ORM persist methods accept it.\n   */\n  validateData(data) {\n    return data !== null && typeof data === \"object\";\n  }\n  /**\n   * Validate the given string as to ensure it correlates with the available\n   * Pinia ORM persist methods.\n   */\n  validatePersistAction(action) {\n    return [\"save\", \"insert\"].includes(action);\n  }\n}\n\nclass Request {\n  /**\n   * The repository class.\n   */\n  repository;\n  /**\n   * The default config.\n   */\n  config = {\n    save: true\n  };\n  /**\n   * Create a new api instance.\n   */\n  constructor(repository) {\n    this.repository = repository;\n    this.registerActions();\n  }\n  /**\n   * Get the axios client.\n   */\n  get axios() {\n    this.repository.axios = this.repository.axios ?? this.repository.config.axiosApi.axios;\n    if (!this.repository.axios) {\n      throw new Error(\n        \"[Pinia ORM Axios] The axios instance is not registered. Please register the axios instance to the repository.\"\n      );\n    }\n    return this.repository.axios;\n  }\n  /**\n   * Register actions from the repository config.\n   */\n  registerActions() {\n    const actions = { ...this.repository.config.axiosApi?.actions, ...this.repository.getModel().$config()?.axiosApi?.actions };\n    if (!actions) {\n      return;\n    }\n    for (const name in actions) {\n      const action = actions[name];\n      typeof action === \"function\" ? this.registerFunctionAction(name, action) : this.registerObjectAction(name, action);\n    }\n  }\n  /**\n   * Register the given object action.\n   */\n  registerObjectAction(name, action) {\n    this[name] = (config) => {\n      return this.request({ ...action, ...config });\n    };\n  }\n  /**\n   * Register the given function action.\n   */\n  registerFunctionAction(name, action) {\n    this[name] = action.bind(this);\n  }\n  /**\n   * Perform a get request.\n   */\n  get(url, config = {}) {\n    return this.request({ method: \"get\", url, ...config });\n  }\n  /**\n   * Perform a post request.\n   */\n  post(url, data = {}, config = {}) {\n    return this.request({ method: \"post\", url, data, ...config });\n  }\n  /**\n   * Perform a put request.\n   */\n  put(url, data = {}, config = {}) {\n    return this.request({ method: \"put\", url, data, ...config });\n  }\n  /**\n   * Perform a patch request.\n   */\n  patch(url, data = {}, config = {}) {\n    return this.request({ method: \"patch\", url, data, ...config });\n  }\n  /**\n   * Perform a delete request.\n   */\n  delete(url, config = {}) {\n    return this.request({ method: \"delete\", url, ...config });\n  }\n  /**\n   * Perform an api request.\n   */\n  async request(config) {\n    const requestConfig = this.createConfig(config);\n    const axiosResponse = await this.axios.request(requestConfig);\n    return this.createResponse(axiosResponse, requestConfig);\n  }\n  /**\n   * Create a new config by merging the global config, the repository config,\n   * and the given config.\n   */\n  createConfig(config) {\n    return {\n      ...this.config,\n      ...this.repository.globalApiConfig,\n      ...this.repository.apiConfig,\n      ...config\n    };\n  }\n  /**\n   * Create a new response instance by applying a few initialization processes.\n   * For example, it saves response data if `save` option id set to `true`.\n   */\n  async createResponse(axiosResponse, config) {\n    const response = new Response(this.repository, config, axiosResponse);\n    if (config.delete !== void 0) {\n      await response.delete();\n      return response;\n    }\n    config.save && await response.save();\n    return response;\n  }\n}\n\nclass AxiosRepository extends Repository {\n  axios;\n  globalApiConfig;\n  apiConfig;\n  constructor(database, pinia) {\n    super(database, pinia);\n    this.axios = config?.axiosApi?.axios || null;\n    this.globalApiConfig = config?.axiosApi || {};\n    this.apiConfig = {};\n  }\n  api() {\n    return useAxiosApi(this);\n  }\n  setAxios(axios) {\n    this.axios = axios;\n    return this;\n  }\n}\n\nfunction useAxiosApi(repository) {\n  return new Request(repository);\n}\n\nfunction useAxiosRepo(model) {\n  const pinia = getActivePinia();\n  AxiosRepository.useModel = model;\n  return useRepo(AxiosRepository, pinia);\n}\n\nfunction createPiniaOrmAxios(axiosConfig) {\n  return definePiniaOrmPlugin((context) => {\n    context.config.axiosApi = axiosConfig;\n    return context;\n  });\n}\nconst piniaOrmPluginAxios = createPiniaOrmAxios();\n\nexport { AxiosRepository, Request, Response, createPiniaOrmAxios, piniaOrmPluginAxios, useAxiosApi, useAxiosRepo };\n"]}