Skip to content

Commit

Permalink
nuxt plugin refactor, update deps, fix telescope
Browse files Browse the repository at this point in the history
  • Loading branch information
k2so-dev committed Dec 22, 2023
1 parent c5dc150 commit 255fa1d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 48 deletions.
5 changes: 4 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ protected function schedule(Schedule $schedule): void
{
$schedule->command('auth:clear-resets')->daily();
$schedule->command('sanctum:prune-expired --hours=24')->daily();
$schedule->command('telescope:prune --hours=24')->hourly();
$schedule->command('temporary:clear')->hourly();

if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
$schedule->command('telescope:prune --hours=24')->daily();
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function convert(string $source, string $target, int $width = null
} else if ($extension === 'jpeg') {
$image->toJpeg($quality)->save($target);
} else if ($extension === 'png') {
$image->toPng($quality)->save($target);
$image->toPng()->save($target);
}
}
}
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
public function register(): void
{
// Register Telescope only in local environment
if ($this->app->environment('local')) {
if ($this->app->environment('local') && class_exists(\Laravel\Telescope\TelescopeServiceProvider::class)) {
$this->app->register(\Laravel\Telescope\TelescopeServiceProvider::class);
$this->app->register(TelescopeServiceProvider::class);
}
Expand Down
102 changes: 59 additions & 43 deletions nuxt/plugins/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,81 @@ export default defineNuxtPlugin({
nuxtApp.provide('storage', (path: string): string => {
if (!path) return ''

if (path.startsWith('http')) return path

return config.public.storageBase + path
return path.startsWith('http://') || path.startsWith('https://') ?
path
: config.public.storageBase + path
})

globalThis.$fetch = ofetch.create(<FetchOptions>{
retry: false,
credentials: 'include',
baseURL: config.public.apiBase + config.public.apiPrefix,
headers: {
Accept: 'application/json'
},
function buildHeaders(headers = <HeadersInit>{}): HeadersInit {
return {
...headers,
...{
'Accept': 'application/json',
},
...(
process.server ? {
'referer': useRequestURL().toString(),
...useRequestHeaders(['x-forwarded-for', 'user-agent', 'referer']),
} : {}
),
...(
auth.logged ? {
'Authorization': `Bearer ${auth.token}`
} : {}
)
};
}

async onRequest({ request, options }) {
if (request.toString().includes('/_nuxt/builds/meta/')) {
options.baseURL = ''
return
}
function buildBaseURL(baseURL: string): string {
if (baseURL) return baseURL;

options.headers = (options.headers || {}) as { [key: string]: string }
return process.server ?
config.apiLocal + config.public.apiPrefix
: config.public.apiBase + config.public.apiPrefix;
}

if (process.server) {
options.headers = {
referer: useRequestURL().toString(),
...useRequestHeaders(['x-forwarded-for', 'user-agent', 'referer']),
}
function buildSecureMethod(options: FetchOptions): void {
if (process.server) return;

if (options.baseURL === config.public.apiBase + config.public.apiPrefix) {
options.baseURL = config.apiLocal + config.public.apiPrefix
}
}
const method = options.method?.toLowerCase() ?? 'get'

if (auth.logged) {
options.headers['Authorization'] = 'Bearer ' + auth.token
}
if (options.body instanceof FormData && method === 'put') {
options.method = 'POST';
options.body.append('_method', 'PUT');
}
}

if (!process.client) return
function isRequestWithAuth(path: string): boolean {
return !path.startsWith('/_nuxt')
&& !path.startsWith('http://')
&& !path.startsWith('https://');
}

const method = options.method?.toLowerCase() ?? 'get'
globalThis.$fetch = ofetch.create(<FetchOptions>{
retry: false,

if (!['post', 'put', 'delete', 'patch'].includes(method)) return
onRequest({ request, options }) {
if (!isRequestWithAuth(request.toString())) return

if (options.body instanceof FormData && method === 'put') {
options.method = 'POST';
options.body.append('_method', 'PUT');
}
options.credentials = 'include';

options.baseURL = buildBaseURL(options.baseURL ?? '');
options.headers = buildHeaders(options.headers);

buildSecureMethod(options);
},

onRequestError({ error }) {
if (process.client) {
useToast().add({
icon: 'i-heroicons-exclamation-circle-solid',
color: 'red',
title: error.message ?? 'Something went wrong',
})
}
if (process.server) return;

useToast().add({
icon: 'i-heroicons-exclamation-circle-solid',
color: 'red',
title: error.message ?? 'Something went wrong',
})
},

async onResponseError({ response }) {
onResponseError({ response }) {
if (response.status === 401) {
if (auth.logged) {
auth.token = ''
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"api": "php artisan octane:start --watch --port=8000 --host=127.0.0.1"
},
"devDependencies": {
"@iconify-json/heroicons": "^1.1.16",
"@iconify-json/heroicons": "^1.1.19",
"@iconify/vue": "^4.1.1",
"@nuxt/devtools": "^1.0.6",
"@nuxt/image": "^1.1.0",
Expand All @@ -24,7 +24,7 @@
"dayjs-nuxt": "^2.1.9",
"nuxt": "^3.8.2",
"nuxt-security": "^1.0.0",
"vue": "^3.3.11",
"vue": "^3.3.13",
"vue-router": "^4.2.5"
}
}

0 comments on commit 255fa1d

Please sign in to comment.