-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: connect page, apikey and v1 generation (#134)
* feat: remove unused variables * fix: update openapi client * fix: temporary update to rankings page * fix: frontend version showing unknown * fix: lint * feat: connect page, apikey and v1 generation * chore: formatting, include format-all script * chore: eslint fixes * fix: invalid client api baseurl, onboarding hook moved to connect action * chore: generate new openapi client * chore: redirect to connect on invalid api key, validate on mount /connect * fix: lint errors * chore: move client config to svelte api, store serverconfig.json * chore: linting fixes * chore: set locals in hook * chore: revert back client sided baseUrl configuration * chore: linting fixes --------- Co-authored-by: Ayush Sehrawat <[email protected]> Co-authored-by: Gaisberg <None> Co-authored-by: davidemarcoli <[email protected]>
- Loading branch information
1 parent
4522930
commit 7469bd9
Showing
15 changed files
with
469 additions
and
278 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -11,3 +11,5 @@ vite.config.ts.timestamp-* | |
.idea | ||
/.vs | ||
/package-lock.json | ||
.vscode | ||
server-config.json |
This file was deleted.
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
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
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
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,41 +1,55 @@ | ||
import type { Handle } from '@sveltejs/kit'; | ||
import { redirect, error } from '@sveltejs/kit'; | ||
import { redirect } from '@sveltejs/kit'; | ||
import { sequence } from '@sveltejs/kit/hooks'; | ||
import { env } from '$env/dynamic/private'; | ||
const BACKEND_URL = env.BACKEND_URL || 'http://127.0.0.1:8080'; | ||
import { client, DefaultService } from '$lib/client/services.gen'; | ||
import { client } from '$lib/client/services.gen'; | ||
import { getServerConfig } from '$lib/serverConfig'; | ||
|
||
const setLocals: Handle = async ({ event, resolve }) => { | ||
event.locals.BACKEND_URL = BACKEND_URL; | ||
const configureClientMiddleware: Handle = async ({ event, resolve }) => { | ||
const config = await getServerConfig(); | ||
|
||
return resolve(event); | ||
}; | ||
if (config) { | ||
event.locals.backendUrl = config.backendUrl; | ||
event.locals.apiKey = config.apiKey; | ||
client.setConfig({ | ||
baseUrl: config.backendUrl, | ||
headers: { | ||
'x-api-key': config.apiKey | ||
} | ||
}); | ||
} | ||
|
||
const onboarding: Handle = async ({ event, resolve }) => { | ||
if (!(event.url.pathname.startsWith('/onboarding') || event.url.pathname.startsWith('/api')) && event.request.method === 'GET') { | ||
const { data, error: apiError } = await DefaultService.services(); | ||
if (apiError || !data) { | ||
return error(500, 'API Error'); | ||
} | ||
const toCheck = ['symlink', 'symlinklibrary']; | ||
const allServicesTrue: boolean = toCheck.every((service) => data[service] === true); | ||
if (!allServicesTrue) { | ||
redirect(302, '/onboarding'); | ||
if ( | ||
!event.url.pathname.startsWith('/connect') && | ||
!event.url.pathname.startsWith('/api/configure-client') && | ||
event.request.method === 'GET' | ||
) { | ||
if (!event.locals.backendUrl || !event.locals.apiKey) { | ||
throw redirect(307, '/connect'); | ||
} | ||
} | ||
|
||
return resolve(event); | ||
}; | ||
|
||
client.setConfig({ | ||
baseUrl: BACKEND_URL | ||
}); | ||
const errorInterceptor: Handle = async ({ event, resolve }) => { | ||
const response = await resolve(event); | ||
|
||
client.interceptors.error.use((error: unknown) => { | ||
if (error && typeof error == 'object' && 'detail' in error && typeof error.detail == 'string') { | ||
return error.detail; | ||
} | ||
return undefined; | ||
}); | ||
client.interceptors.error.use((error: unknown) => { | ||
if ( | ||
error && | ||
typeof error === 'object' && | ||
'detail' in error && | ||
typeof error.detail === 'string' | ||
) { | ||
if (error.detail === 'Missing or invalid API key') { | ||
throw redirect(307, '/connect'); | ||
} | ||
return error.detail; | ||
} | ||
return undefined; | ||
}); | ||
|
||
return response; | ||
}; | ||
|
||
export const handle = sequence(setLocals, onboarding); | ||
export const handle = sequence(configureClientMiddleware, errorInterceptor); |
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
Oops, something went wrong.