{"version":3,"file":"Resource.mjs","names":[],"sources":["../../../../../../http/src/Resource.ts"],"sourcesContent":["import concatRoute from \"@mongez/concat-route\";\nimport type { Http } from \"./Http\";\nimport type { HttpData, HttpMethod, HttpParams, HttpResult, RequestOptions } from \"./Http.types\";\nimport type { CancellablePromise } from \"./cancellable\";\nimport { getCurrentHttp } from \"./current-http\";\nimport type { ResourceService } from \"./Resource.types\";\n\n/**\n * RESTful resource helper built on top of Http.\n *\n * Extend this class and set `route` to get fully-typed CRUD methods:\n *\n * @example\n * class UsersResource extends Resource {\n *   route = '/users';\n * }\n *\n * const users = new UsersResource();\n * const { data, error } = await users.list({ page: 1 });\n */\nexport class Resource implements ResourceService {\n  /**\n   * Base route for this resource, e.g. \"/users\".\n   * All methods build URLs relative to this route.\n   */\n  public route = \"\";\n\n  /**\n   * Default query params merged into every `list()` call.\n   * Override per-instance or in a subclass.\n   */\n  public defaultListParams: HttpParams = {};\n\n  /**\n   * Lazy reference to the Http instance.\n   * Resolved on first use via getCurrentHttp() so that setCurrentHttp()\n   * does not have to be called before the class is instantiated.\n   */\n  private _http: Http | null = null;\n\n  protected get http(): Http {\n    if (!this._http) {\n      this._http = getCurrentHttp();\n    }\n    return this._http;\n  }\n\n  /**\n   * Override the Http instance used by this specific resource.\n   * Useful for resources that talk to a different API base URL.\n   */\n  useHttp(instance: Http): this {\n    this._http = instance;\n    return this;\n  }\n\n  // ─── CRUD ────────────────────────────────────────────────────────────────────\n\n  list<T = unknown>(\n    params?: HttpParams,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    const mergedParams = { ...this.defaultListParams, ...params };\n    return this.http.get<T>(this.route, { ...options, params: mergedParams });\n  }\n\n  get<T = unknown>(\n    id: number | string,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.get<T>(this.path(id), options);\n  }\n\n  create<T = unknown>(\n    data: HttpData,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.post<T>(this.route, data, options);\n  }\n\n  update<T = unknown>(\n    id: number | string,\n    data: HttpData,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.put<T>(this.path(id), data, options);\n  }\n\n  patch<T = unknown>(\n    id: number | string,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.patch<T>(this.path(id), options);\n  }\n\n  delete<T = unknown>(\n    id: number | string,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.delete<T>(this.path(id), options);\n  }\n\n  bulkDelete<T = unknown>(\n    data: HttpData,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.delete<T>(this.route, { ...options, data });\n  }\n\n  /**\n   * Toggle the published state of a record.\n   *\n   * Sends a PATCH to `/<route>/<id>` with `{ [publishKey]: published }`.\n   * If `published` is already an object, it is sent as-is.\n   *\n   * Uses `this.http.patch` directly — no double-route bug.\n   */\n  publish<T = unknown>(\n    id: number | string,\n    published: boolean | HttpData,\n    publishKey?: string,\n    options?: RequestOptions,\n  ): CancellablePromise<HttpResult<T>> {\n    const key = publishKey ?? this.http.getConfig().publishKey ?? \"published\";\n    const body = typeof published === \"object\" ? published : { [key]: published };\n    return this.http.patch<T>(this.path(id), { ...options, data: body });\n  }\n\n  /**\n   * Send a non-CRUD action on a specific record.\n   *\n   * @example\n   * // POST /users/42/activate\n   * await users.action(42, 'activate');\n   *\n   * // POST /orders/5/refund  { amount: 100 }\n   * await orders.action(5, 'refund', { amount: 100 });\n   *\n   * // PATCH /posts/1/publish\n   * await posts.action(1, 'publish', undefined, {}, 'PATCH');\n   */\n  action<T = unknown>(\n    id: number | string,\n    actionName: string,\n    data?: HttpData,\n    options?: RequestOptions,\n    method: HttpMethod = \"POST\",\n  ): CancellablePromise<HttpResult<T>> {\n    return this.http.request<T>(method, this.actionPath(id, actionName), data, options);\n  }\n\n  // ─── Utilities ───────────────────────────────────────────────────────────────\n\n  /**\n   * Build a path relative to the base route.\n   *\n   * @example\n   * this.path(42)          // \"/users/42\"\n   * this.path('profile')   // \"/users/profile\"\n   * this.path()            // \"/users\"\n   */\n  path(suffix: string | number = \"\"): string {\n    return concatRoute(this.route, String(suffix));\n  }\n\n  /**\n   * Build a path for a named action on a specific record.\n   *\n   * @example\n   * this.actionPath(42, 'activate')   // \"/users/42/activate\"\n   */\n  actionPath(id: string | number, actionName: string): string {\n    return concatRoute(this.path(id), actionName);\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,IAAa,WAAb,MAAiD;;eAKhC;2BAMwB,CAAC;eAOX;;CAE7B,IAAc,OAAa;EACzB,IAAI,CAAC,KAAK,OACR,KAAK,QAAQ,eAAe;EAE9B,OAAO,KAAK;CACd;;;;;CAMA,QAAQ,UAAsB;EAC5B,KAAK,QAAQ;EACb,OAAO;CACT;CAIA,KACE,QACA,SACmC;EACnC,MAAM,eAAe;GAAE,GAAG,KAAK;GAAmB,GAAG;EAAO;EAC5D,OAAO,KAAK,KAAK,IAAO,KAAK,OAAO;GAAE,GAAG;GAAS,QAAQ;EAAa,CAAC;CAC1E;CAEA,IACE,IACA,SACmC;EACnC,OAAO,KAAK,KAAK,IAAO,KAAK,KAAK,EAAE,GAAG,OAAO;CAChD;CAEA,OACE,MACA,SACmC;EACnC,OAAO,KAAK,KAAK,KAAQ,KAAK,OAAO,MAAM,OAAO;CACpD;CAEA,OACE,IACA,MACA,SACmC;EACnC,OAAO,KAAK,KAAK,IAAO,KAAK,KAAK,EAAE,GAAG,MAAM,OAAO;CACtD;CAEA,MACE,IACA,SACmC;EACnC,OAAO,KAAK,KAAK,MAAS,KAAK,KAAK,EAAE,GAAG,OAAO;CAClD;CAEA,OACE,IACA,SACmC;EACnC,OAAO,KAAK,KAAK,OAAU,KAAK,KAAK,EAAE,GAAG,OAAO;CACnD;CAEA,WACE,MACA,SACmC;EACnC,OAAO,KAAK,KAAK,OAAU,KAAK,OAAO;GAAE,GAAG;GAAS;EAAK,CAA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