-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
20253b7
commit 3ef2e8f
Showing
22 changed files
with
4,258 additions
and
4,315 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,80 @@ | ||
import { user } from '$lib/stores/user'; | ||
import { get } from 'svelte/store'; | ||
import { Err, Ok, type Result } from './utilities'; | ||
import { location } from './stores'; | ||
|
||
export namespace api { | ||
export function get<TResponse>(endpoint: string, params?: RequestInit) { | ||
return sendRequest<TResponse>(endpoint, 'GET', null, params); | ||
} | ||
export function get<TResponse>(endpoint: string, params?: RequestInit) { | ||
return sendRequest<TResponse>(endpoint, 'GET', null, params); | ||
} | ||
|
||
export function post<TResponse>(endpoint: string, data?: unknown) { | ||
return sendRequest<TResponse>(endpoint, 'POST', data); | ||
} | ||
export function post<TResponse>(endpoint: string, data?: unknown) { | ||
return sendRequest<TResponse>(endpoint, 'POST', data); | ||
} | ||
|
||
export function put<TResponse>(endpoint: string, data?: unknown) { | ||
return sendRequest<TResponse>(endpoint, 'PUT', data); | ||
} | ||
export function put<TResponse>(endpoint: string, data?: unknown) { | ||
return sendRequest<TResponse>(endpoint, 'PUT', data); | ||
} | ||
|
||
export function remove<TResponse>(endpoint: string) { | ||
return sendRequest<TResponse>(endpoint, 'DELETE'); | ||
} | ||
export function remove<TResponse>(endpoint: string) { | ||
return sendRequest<TResponse>(endpoint, 'DELETE'); | ||
} | ||
} | ||
|
||
async function sendRequest<TResponse>( | ||
endpoint: string, | ||
method: string, | ||
data?: unknown, | ||
params?: RequestInit | ||
endpoint: string, | ||
method: string, | ||
data?: unknown, | ||
params?: RequestInit | ||
): Promise<Result<TResponse, Error>> { | ||
endpoint = resolveUrl(endpoint); | ||
const user_token = get(user).bearer_token; | ||
const body = data !== null && typeof data !== 'undefined' ? JSON.stringify(data) : undefined; | ||
endpoint = resolveUrl(endpoint); | ||
const body = data !== null && typeof data !== 'undefined' ? JSON.stringify(data) : undefined; | ||
|
||
const request = { | ||
...params, | ||
method, | ||
body, | ||
headers: { | ||
...params?.headers, | ||
Authorization: user_token ? 'Bearer ' + user_token : 'Basic', | ||
'Content-Type': 'application/json' | ||
} | ||
}; | ||
const request = { | ||
...params, | ||
method, | ||
body, | ||
headers: { | ||
...params?.headers, | ||
Authorization: 'Basic', | ||
'Content-Type': 'application/json' | ||
} | ||
}; | ||
|
||
let response; | ||
let response; | ||
|
||
try { | ||
response = await fetch(endpoint, request); | ||
} catch (error) { | ||
return Err.new(new Error(), 'An error has occurred'); | ||
} | ||
try { | ||
response = await fetch(endpoint, request); | ||
} catch (error) { | ||
return Err.new(new Error(), 'An error has occurred'); | ||
} | ||
|
||
const isResponseOk = response.status >= 200 && response.status < 400; | ||
if (!isResponseOk) { | ||
if (response.status === 401) { | ||
return Err.new(new ApiError(response), 'User was not authorized'); | ||
} | ||
return Err.new(new ApiError(response), 'An error has occurred'); | ||
} | ||
const isResponseOk = response.status >= 200 && response.status < 400; | ||
if (!isResponseOk) { | ||
if (response.status === 401) { | ||
return Err.new(new ApiError(response), 'User was not authorized'); | ||
} | ||
return Err.new(new ApiError(response), 'An error has occurred'); | ||
} | ||
|
||
const contentType = response.headers.get('Content-Type') ?? response.headers.get('Content-Type'); | ||
if (contentType && contentType.includes('application/json')) { | ||
const data = await response.json(); | ||
return Ok.new(data as TResponse); | ||
} else { | ||
// Handle empty object as response | ||
return Ok.new(null as TResponse); | ||
} | ||
const contentType = | ||
response.headers.get('Content-Type') ?? response.headers.get('Content-Type'); | ||
if (contentType && contentType.includes('application/json')) { | ||
const data = await response.json(); | ||
return Ok.new(data as TResponse); | ||
} else { | ||
// Handle empty object as response | ||
return Ok.new(null as TResponse); | ||
} | ||
} | ||
|
||
function resolveUrl(url: string): string { | ||
if (url.startsWith('http') || !get(location)) return url; | ||
const protocol = window.location.protocol; | ||
return `${protocol}//${get(location)}${url.startsWith('/') ? '' : '/'}${url}`; | ||
if (url.startsWith('http') || !get(location)) return url; | ||
const protocol = window.location.protocol; | ||
return `${protocol}//${get(location)}${url.startsWith('/') ? '' : '/'}${url}`; | ||
} | ||
|
||
export class ApiError extends Error { | ||
constructor(public readonly response: Response) { | ||
super(`${response.status}`); | ||
} | ||
constructor(public readonly response: Response) { | ||
super(`${response.status}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
<script lang="ts"> | ||
import { onDestroy } from 'svelte'; | ||
import { user, location } from '$lib/stores'; | ||
import { onDestroy } from 'svelte'; | ||
import { location } from '$lib/stores'; | ||
let source = `${$location}/api/camera/stream?access_token=${$user.bearer_token}`; | ||
let source = `${$location}/api/camera/stream`; | ||
onDestroy(() => (source = '#')); | ||
onDestroy(() => (source = '#')); | ||
</script> | ||
|
||
<div class="w-full h-full"> | ||
<img | ||
src={source} | ||
class="absolute object-cover blur-3xl w-full h-full -z-10" | ||
alt="Live stream is down" | ||
/> | ||
<img src={source} class="object-contain w-full h-full" alt="Live stream is down" /> | ||
<img | ||
src={source} | ||
class="absolute object-cover blur-3xl w-full h-full -z-10" | ||
alt="Live stream is down" | ||
/> | ||
<img src={source} class="object-contain w-full h-full" alt="Live stream is down" /> | ||
</div> |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.