PageSourceSearch

https://withdom-architect.jp/libs/works/js/api.js

js withdom-architect.jp collected 2026-10-04 22:31:44 UTC 6,464 bytes, 204 lines download raw bytes

1/**
2 * WorksApi: 施工事例(Works)関連のAPIクライアント
3 * 
4 * CmsApiを使用して、works固有のエンドポイントを呼び出す。
5 */
6(function () {
7  'use strict';
8
9  if (!window.CmsApi) {
10    return;
11  }
12
13  const { fetchList, fetchDetail, buildHeaders } = window.CmsApi;
14  const CMS_NAME = 'works';
15
16  /**
17   * 施工事例一覧を取得
18   * @param {Object} params パラメータオブジェクト
19   * @param {string} [params.symfonyApiUrl] APIのベースURL(省略時はデフォルト設定から取得)
20   * @param {number} [params.page=1] ページ番号
21   * @param {number} [params.limit=8] 取得件数
22   * @param {string|null} [params.storeSlug=null] 店舗のスラグ
23   * @param {number[]} [params.conditionFilters=[]] 条件フィルターIDの配列
24   * @param {number[]} [params.designFilters=[]] デザインフィルターIDの配列
25   * @returns {Promise<{data: Array, pagination: object}|{error: string}>}
26   */
27  async function fetchWorksList(params = {}) {
28    const {
29      symfonyApiUrl = window.withdomCommonConfig?.symfonyApiUrl || '',
30      page = 1,
31      limit = 8,
32      storeSlug = null,
33      conditionFilters = [],
34      designFilters = []
35    } = params;
36
37    const queryParams = {};
38    if (storeSlug) {
39      queryParams.storeSlug = storeSlug;
40    }
41    if (conditionFilters && Array.isArray(conditionFilters) && conditionFilters.length > 0) {
42      queryParams.conditionFilters = conditionFilters;
43    }
44    if (designFilters && Array.isArray(designFilters) && designFilters.length > 0) {
45      queryParams.designFilters = designFilters;
46    }
47
48    return fetchList(CMS_NAME, {
49      symfonyApiUrl: symfonyApiUrl,
50      page: page,
51      limit: limit,
52      queryParams: queryParams
53    });
54  }
55
56  /**
57   * 施工事例詳細を取得
58   * @param {number|string} id 施工事例ID
59   * @param {Object} params パラメータオブジェクト
60   * @param {string} [params.symfonyApiUrl] APIのベースURL(省略時はデフォルト設定から取得)
61   * @returns {Promise<{data: object}|{error: string}>}
62   */
63  async function fetchWorksDetail(id, params = {}) {
64    const {
65      symfonyApiUrl = window.withdomCommonConfig?.symfonyApiUrl || ''
66    } = params;
67
68    return fetchDetail(CMS_NAME, id, {
69      symfonyApiUrl: symfonyApiUrl,
70      queryParams: {}
71    });
72  }
73
74  /**
75   * フィルター一覧を取得(条件フィルターとデザインフィルターをまとめて取得)
76   * @param {Object} params パラメータオブジェクト
77   * @param {string} [params.symfonyApiUrl] APIのベースURL(省略時はデフォルト設定から取得)
78   * @returns {Promise<{condition: Array, design: Array}|{error: string}>}
79   */
80  async function fetchWorksFilters(params = {}) {
81    const {
82      symfonyApiUrl = window.withdomCommonConfig?.symfonyApiUrl || ''
83    } = params;
84
85    if (!symfonyApiUrl) {
86      return { error: 'API URL が設定されていません。' };
87    }
88
89    const apiUrl = `${symfonyApiUrl}/api/works/filters`;
90
91    try {
92      const response = await fetch(apiUrl, {
93        method: 'GET',
94        headers: buildHeaders()
95      });
96
97      if (!response.ok) {
98        console.error('[フィルターの取得に失敗しました]', {
99          status: response.status,
100          statusText: response.statusText,
101          url: apiUrl
102        });
103        return { error: 'フィルターの取得に失敗しました。' };
104      }
105
106      const result = await response.json();
107
108      if (result.error) {
109        return { error: result.error };
110      }
111
112      return {
113        condition: result.condition || [],
114        design: result.design || []
115      };
116    } catch (error) {
117      console.error('[フィルターの取得中にエラーが発生しました]', {
118        error: error,
119        message: error.message,
120        name: error.name,
121        url: apiUrl
122      });
123      return { error: 'フィルターの取得中にエラーが発生しました。' };
124    }
125  }
126
127  /**
128   * 件数を取得
129   * @param {Object} params パラメータオブジェクト
130   * @param {string} [params.symfonyApiUrl] APIのベースURL(省略時はデフォルト設定から取得)
131   * @param {string|null} [params.storeSlug=null] 店舗のスラグ
132   * @param {Object} [params.conditionFilters={}] 条件フィルター({type: id}形式)
133   * @param {number[]} [params.designFilters=[]] デザインフィルターIDの配列
134   * @returns {Promise<{count: number}|{error: string}>}
135   */
136  async function fetchWorksCount(params = {}) {
137    const {
138      symfonyApiUrl = window.withdomCommonConfig?.symfonyApiUrl || '',
139      storeSlug = null,
140      conditionFilters = {},
141      designFilters = []
142    } = params;
143
144    if (!symfonyApiUrl) {
145      return { error: 'API URL が設定されていません。' };
146    }
147
148    const queryParams = new URLSearchParams();
149    if (storeSlug) {
150      queryParams.set('storeSlug', storeSlug);
151    }
152
153    // conditionFilter[type]=id形式で追加
154    Object.keys(conditionFilters).forEach(type => {
155      if (conditionFilters[type]) {
156        queryParams.append(`conditionFilters[${type}]`, conditionFilters[type]);
157      }
158    });
159
160    // designFilters[]形式で追加
161    designFilters.forEach(id => {
162      if (id) {
163        queryParams.append('designFilters[]', id);
164      }
165    });
166
167    const apiUrl = `${symfonyApiUrl}/api/works/count${queryParams.toString() ? '?' + queryParams.toString() : ''}`;
168
169    try {
170      const response = await fetch(apiUrl, {
171        method: 'GET',
172        headers: buildHeaders()
173      });
174
175      if (!response.ok) {
176        console.error('[件数の取得に失敗しました]', {
177          status: response.status,
178          statusText: response.statusText,
179          url: apiUrl
180        });
181        return { error: '件数の取得に失敗しました。' };
182      }
183
184      const count = await response.json();
185      return { count: typeof count === 'number' ? count : parseInt(count, 10) || 0 };
186    } catch (error) {
187      console.error('[件数の取得中にエラーが発生しました]', {
188        error: error,
189        message: error.message,
190        name: error.name,
191        url: apiUrl
192      });
193      return { error: '件数の取得中にエラーが発生しました。' };
194    }
195  }
196
197  // グローバルに公開
198  window.WorksApi = {
199    fetchWorksList,
200    fetchWorksDetail,
201    fetchWorksFilters,
202    fetchWorksCount
203  };
204})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.