Skip to content

Commit

Permalink
feat: connect page, apikey and v1 generation (#134)
Browse files Browse the repository at this point in the history
* 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
3 people authored Oct 21, 2024
1 parent 4522930 commit 7469bd9
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 278 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ vite.config.ts.timestamp-*
.idea
/.vs
/package-lock.json
.vscode
server-config.json
11 changes: 0 additions & 11 deletions .vscode/launch.json

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test": "vitest",
"lint": "prettier --check . && eslint .",
"format": "prettier --write .",
"format": "prettier --write",
"format-all": "prettier --write .",
"generate-client": "openapi-ts generate"
},
"devDependencies": {
Expand Down
7 changes: 2 additions & 5 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
declare global {
namespace App {
// interface Error {}
interface Locals {
BACKEND_URL: string;
backendUrl: string;
apiKey: string;
}
// interface PageData {}
// interface PageState {}
// interface Platform {}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks.client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { client } from '$lib/client/services.gen';

client.setConfig({
baseUrl: '/api'
baseUrl: ''
});

client.interceptors.error.use((error: unknown) => {
Expand Down
70 changes: 42 additions & 28 deletions src/hooks.server.ts
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);
7 changes: 6 additions & 1 deletion src/lib/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ export const AppModelSchema = {
version: {
type: 'string',
title: 'Version',
default: '0.16.0'
default: '0.15.3'
},
api_key: {
type: 'string',
title: 'Api Key',
default: ''
},
debug: {
type: 'boolean',
Expand Down
Loading

0 comments on commit 7469bd9

Please sign in to comment.