summaryrefslogtreecommitdiff
path: root/src/immich.js
blob: d938fea0b14fdca267fe97a0fe6c6ee9bd142dbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class ImmichConnector {
	constructor(url, apiKey) {
		this.url = url
		this.apiKey = apiKey
	}

	fetchAlbums() {
		return this.fetch("/albums")
	}

	#fetch(endpoint) {
		return fetch(this.url + "/api" + endpoint, {
			headers: { "x-api-key": this.apiKey }
		})
	}

	fetch(endpoint) {
		return this.#fetch(endpoint)
			.then(response => {
				if (!response.ok)
					throw new Error(`HTTP error! Status: ${response.status}`)
				return response.json()
			})
			.then(data => {
				return data
			})
			.catch(error => {
				console.error("Fetch error:", error)
			})
	}

	fetchImageSrc(key, size) {
		const url = `/assets/${key}/thumbnail` + (this.size ? `?size=${this.size}` : "")
		return this.#fetch(url)
			.then(response => {
				if (!response.ok)
					throw new Error(`HTTP error! Status: ${response.status}`)
				return response.blob()
			})
			.then(blob => {
				return URL.createObjectURL(blob)
			})
	}
}

const immichConnector = new ImmichConnector("http://192.168.1.13", "m5nqOoBc4uhAba21gZdCP3z8D3JT4GPxDXL2psd52EA")
document.immichConnector = immichConnector // FIXME TEMP
export default immichConnector