|
| 1 | +/** |
| 2 | + * Client |
| 3 | + * |
| 4 | + * An API client for SuluHeadlessBundle. |
| 5 | + */ |
| 6 | +export default class Client { |
| 7 | + baseUrl; |
| 8 | + fetchClient; |
| 9 | + fetchOptions; |
| 10 | + onError; |
| 11 | + onResponse; |
| 12 | + removeEmbedded; |
| 13 | + |
| 14 | + /** |
| 15 | + * Creates a client. |
| 16 | + * |
| 17 | + * @param {Object} options - The options for the client. |
| 18 | + * @param {string} [options.baseUrl=window.location.origin] - The base URL for the API. |
| 19 | + * @param {function} [options.fetchClient=fetch.bind(window)] - The fetch client to use. |
| 20 | + * @param {Object} [options.fetchOptions={}] - The fetch client options. |
| 21 | + * @param {function} [options.onError=() => {}] - The function to call on error. |
| 22 | + * @param {function} [options.onResponse=(r) => r] - The function to call on response. |
| 23 | + * @param {boolean} [options.removeEmbedded=false] - Whether to remove the _embedded layer from the response if present. |
| 24 | + */ |
| 25 | + constructor({ |
| 26 | + baseUrl = window.location.origin, |
| 27 | + fetchClient = fetch.bind(window), |
| 28 | + fetchOptions = {}, |
| 29 | + onError = () => {}, |
| 30 | + onResponse = (r) => r, |
| 31 | + removeEmbedded = false, |
| 32 | + }) { |
| 33 | + this.baseUrl = baseUrl; |
| 34 | + this.fetchClient = fetchClient; |
| 35 | + this.fetchOptions = fetchOptions; |
| 36 | + this.onError = onError; |
| 37 | + this.onResponse = onResponse; |
| 38 | + this.removeEmbedded = removeEmbedded; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Builds a URL with the given path and optional query parameters. |
| 43 | + * |
| 44 | + * @param {string} path - The path for the URL. |
| 45 | + * @param {Object} [params={}] - The query parameters for the URL. |
| 46 | + * @returns {URL} The built URL. |
| 47 | + */ |
| 48 | + buildUrl(path, params = {}) { |
| 49 | + const url = new URL(path, this.baseUrl); |
| 50 | + url.search = new URLSearchParams(params); |
| 51 | + |
| 52 | + return url; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Sends a request to the API with the given path and query parameters. |
| 57 | + * |
| 58 | + * @param {string} path - The path for the request. |
| 59 | + * @param {Object} [params={}] - The query parameters for the request. |
| 60 | + * @returns {Promise<Object>} A Promise that resolves to the request's JSON. |
| 61 | + */ |
| 62 | + async request(path, params = {}) { |
| 63 | + let response = null; |
| 64 | + |
| 65 | + try { |
| 66 | + response = await this.fetchClient( |
| 67 | + this.buildUrl(path, params).toString(), |
| 68 | + this.fetchOptions |
| 69 | + ); |
| 70 | + } catch (error) { |
| 71 | + this.onError(error); |
| 72 | + throw error; |
| 73 | + } |
| 74 | + |
| 75 | + return await this.handleRequestResponse(response); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Handles the response of a request. |
| 80 | + * |
| 81 | + * @param {Response} response - The response object. |
| 82 | + * @returns {Promise<Object>} A Promise that resolves to the request's JSON. |
| 83 | + */ |
| 84 | + async handleRequestResponse(response) { |
| 85 | + let json = response.json ? await response.json() : response.data; |
| 86 | + |
| 87 | + if (this.removeEmbedded && json?._embedded) { |
| 88 | + json = json._embedded; |
| 89 | + } |
| 90 | + |
| 91 | + return this.onResponse(json); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Retrieves page data from the given path. |
| 96 | + * |
| 97 | + * @param {string} path - The path of the page to retrieve. |
| 98 | + * @returns {Promise<Object>} A Promise that resolves to the page data. |
| 99 | + */ |
| 100 | + getPageByPath(path) { |
| 101 | + return this.request(`${path}.json`); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Retrieves navigation data with the given key and optional query parameters. |
| 106 | + * |
| 107 | + * @param {string} key - The key of the navigation to retrieve. |
| 108 | + * @param {Object} [params={}] - The query parameters for the request. |
| 109 | + * @returns {Promise<Object>} A Promise that resolves to the navigation data. |
| 110 | + */ |
| 111 | + getNavigationByKey(key, params) { |
| 112 | + return this.request(`/api/navigations/${key}`, params); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Retrieves snippet data with the given area name and optional query parameters. |
| 117 | + * |
| 118 | + * @param {string} area - The name of the snippet area to retrieve. |
| 119 | + * @param {Object} [params={}] - The query parameters for the request. |
| 120 | + * @returns {Promise<Object>} A Promise that resolves to the snippet area data. |
| 121 | + */ |
| 122 | + getSnippetByArea(area, params) { |
| 123 | + return this.request(`/api/snippet-areas/${area}`, params); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Performs a search with the given query. |
| 128 | + * |
| 129 | + * @param {string} query - The search query. |
| 130 | + * @returns {Promise<Object>} A Promise that resolves to the search results. |
| 131 | + */ |
| 132 | + search(query) { |
| 133 | + return this.request('/api/search', { |
| 134 | + q: query, |
| 135 | + }); |
| 136 | + } |
| 137 | +} |
0 commit comments