Skip to content

Commit 25f4730

Browse files
Benoit NgoNgob
Benoit Ngo
authored andcommitted
Updating AppFetch, useAppFetch
Fixing various eslint errors
1 parent 16dd460 commit 25f4730

File tree

25 files changed

+1378
-443
lines changed

25 files changed

+1378
-443
lines changed

apps/back/src/Controller/HealthCheckController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class HealthCheckController
1313
#[Route('/healthcheck', name: 'app_health_check')]
1414
public function index(): JsonResponse
1515
{
16-
return new JsonResponse(['success' => true]);
16+
return new JsonResponse(['success' => 'ok']);
1717
}
1818

1919
#[Route('/healthcheck/logged', name: 'app_health_check_logged')]
2020
#[IsGranted('IS_AUTHENTICATED_FULLY')]
2121
public function logged(): JsonResponse
2222
{
23-
return new JsonResponse(['success' => true]);
23+
return new JsonResponse(['success' => 'ok']);
2424
}
2525
}

apps/back/src/Entity/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function jsonSerialize(): mixed
100100
{
101101
return [
102102
'email' => $this->getEmail(),
103+
'username' => $this->getUsername(),
103104
];
104105
}
105106
}

apps/front/.eslintrc.cjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ require("@rushstack/eslint-patch/modern-module-resolution")
22
module.exports = {
33
root: true,
44
extends: [
5-
"plugin:vue/vue3-recommended",
6-
"@nuxtjs/eslint-config-typescript",
7-
"@vue/eslint-config-airbnb-with-typescript"
5+
"@nuxt/eslint-config",
6+
// "plugin:vue/vue3-recommended"
87
],
98
rules: {
10-
'semi': ["error", "always"],
11-
'comma-dangle': "off", // covered by eslint-config-airbnb-with-typescript
12-
'space-before-function-paren': "off" // covered by eslint-config-airbnb-with-typescript
9+
'semi': ["error", "always"]
1310
}
1411
};

apps/front/nuxt.config.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
export default defineNuxtConfig({
33
srcDir: 'src/',
44
modules: [
5-
'@pinia/nuxt',
5+
'@pinia/nuxt'
66
],
77
runtimeConfig: {
8-
API_URL: process.env.API_URL || '',
8+
API_URL: process.env.API_URL || ''
99
},
1010
app: {
1111

@@ -17,21 +17,21 @@ export default defineNuxtConfig({
1717
meta: [
1818
// <meta name="description" content="My amazing site">
1919
// { name: 'description', content: 'My amazing site.' }
20-
],
21-
},
20+
]
21+
}
2222
},
2323
css: [
24-
'@/assets/styles/main.scss',
24+
'@/assets/styles/main.scss'
2525
],
2626
vite: {
2727
css: {
2828
preprocessorOptions: {
2929
scss: {
30-
additionalData: '@import "@/assets/styles/_functions.scss";@import "@/assets/styles/_variables.scss";@import "@/assets/styles/_mixins.scss";',
31-
},
32-
},
33-
},
34-
},
30+
additionalData: '@import "@/assets/styles/_functions.scss";@import "@/assets/styles/_variables.scss";@import "@/assets/styles/_mixins.scss";'
31+
}
32+
}
33+
}
34+
}
3535
// ssr: false
3636

3737
});

apps/front/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"generate": "nuxt generate",
77
"preview": "nuxt preview",
88
"postinstall": "nuxt prepare",
9-
"lint": "eslint nuxt.config.ts --fix ; eslint ./src/"
9+
"lint": "eslint nuxt.config.ts --fix ; nuxi typecheck ; eslint ./src/"
1010
},
1111
"devDependencies": {
1212
"@nuxt/eslint-config": "^0.1.1",
@@ -15,15 +15,15 @@
1515
"@types/http-proxy": "^1.17.11",
1616
"@types/lodash": "^4.14.195",
1717
"@typescript-eslint/eslint-plugin": "^5.59.7",
18-
"@vue/eslint-config-prettier": "^7.1.0",
1918
"eslint": "^8.41.0",
20-
"eslint-plugin-vue": "^9.14.1",
2119
"pino-pretty": "^10.0.0",
2220
"typescript": "^5.0.4",
2321
"vue-tsc": "^1.6.5"
2422
},
2523
"dependencies": {
24+
"@nuxtjs/eslint-config-typescript": "^12.0.0",
2625
"bootstrap": "^5.2.3",
26+
"defu": "^6.1.2",
2727
"h3": "^1.6.6",
2828
"nitropack": "^2.4.1",
2929
"nuxt": "3.5.1",

apps/front/src/components/layout/menu/AppMenu.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<div
33
class="d-flex flex-sm-column flex-row flex-nowrap bg-light align-items-center sticky-top"
44
>
5-
<NuxtLink to="/" class="d-block p-3 link-dark text-decoration-none">
5+
<NuxtLink
6+
to="/"
7+
class="d-block p-3 link-dark text-decoration-none"
8+
>
69
<icons1-square-fill />
710
</NuxtLink>
811
<ul

apps/front/src/components/layout/menu/MenuItem.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<li class="nav-item">
3-
<NuxtLink v-bind="$attrs" class="nav-link py-3 px-2">
3+
<NuxtLink
4+
v-bind="$attrs"
5+
class="nav-link py-3 px-2"
6+
>
47
<slot />
58
</NuxtLink>
69
</li>

apps/front/src/composables/api/auth/useLogin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { Me } from './useMe';
22

3-
export default function useLogin(): (email: string, password: string | undefined) => Promise<Me> {
3+
export default function useLogin (): (email: string, password: string | undefined) => Promise<Me> {
44
const { $appFetch } = useNuxtApp();
55
return async (email: string, password: string | undefined) => {
66
const response = await $appFetch<Me>('/api/1.0/auth/sso/saml2/login', {
77
method: 'post',
88
body: {
99
username: email,
10-
password,
11-
},
10+
password
11+
}
1212
});
1313
if (!response) {
1414
throw createError('/api/1.0/auth/sso/saml2/login has an empty body, the profile (me) should be returned');

apps/front/src/composables/api/auth/useMe.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface Me {
22
username: string;
33
}
44

5-
export default function useMe(): () => Promise<Me> {
5+
export default function useMe (): () => Promise<Me> {
66
const { $appFetch } = useNuxtApp();
77
return async () => {
88
const res = await $appFetch<Me>('/api/1.0/auth/me');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { FetchError } from 'ofetch';
2+
import { AsyncData } from '#app';
3+
import useAppFetch from '~/composables/useAppFetch';
4+
5+
interface LoggedHealthCheckResponse {
6+
success: string
7+
}
8+
export function useHealthCheckFetch (): AsyncData<LoggedHealthCheckResponse | null, FetchError | null> {
9+
return useAppFetch<LoggedHealthCheckResponse>(
10+
() => '/api/1.0/healthcheck/logged'
11+
);
12+
}
Lines changed: 9 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,15 @@
1-
import {
2-
AsyncDataOptions,
3-
FetchResult,
4-
UseFetchOptions,
5-
useNuxtApp,
6-
} from '#app';
7-
import { NitroFetchRequest } from 'nitropack';
8-
import { KeyOfRes } from 'nuxt/dist/app/composables/asyncData';
9-
import { FetchError } from 'ofetch';
10-
import { hash } from 'ohash';
11-
import { Ref } from 'vue';
1+
import type { UseFetchOptions } from 'nuxt/app';
2+
import { defu } from 'defu';
3+
import { $Fetch, NitroFetchRequest } from 'nitropack';
124

13-
// This might break the "pick" strategy from nuxt
14-
15-
type AppReturn<T> = Promise<{
16-
data: T;
17-
error: Error | unknown | null;
18-
pending: boolean;
19-
refresh: () => void;
20-
}>;
21-
22-
// from https://github.com/nuxt/framework/blob/main/packages/nuxt/src/app/composables/fetch.ts
23-
// So we disable our linting to keep similar code
24-
// We add the ReturnType in the generic so we can type hint API return
25-
// + we use our own wrapper appFetch
26-
/* eslint-disable */
27-
export default function useAppFetch<
28-
ReturnType = any,
29-
ResT = void,
30-
ErrorT = FetchError,
31-
ReqT extends NitroFetchRequest = NitroFetchRequest,
32-
_ResT = ResT extends void ? FetchResult<ReqT> : ResT,
33-
Transform extends (
34-
res: _ResT) => any = (res: _ResT) => _ResT,
35-
PickKeys extends KeyOfRes<Transform> = KeyOfRes<Transform>,
36-
>(
37-
request: Ref<ReqT> | ReqT | (() => ReqT),
38-
arg1?: string | UseFetchOptions<_ResT, Transform, PickKeys>,
39-
arg2?: string,
40-
): AppReturn<ReturnType> {
5+
export default function useAppFetch<T> (url: string | Request | Ref<string | Request> | (() => string | Request), options: UseFetchOptions<T> = {}) {
416
const { $appFetch } = useNuxtApp();
42-
const [opts = {}, autoKey] = typeof arg1 === 'string' ? [{}, arg1] : [arg1, arg2];
43-
const _key = opts.key;
44-
hash([
45-
autoKey,
46-
unref(opts.baseURL),
47-
typeof request === 'string' ? request : '',
48-
unref(opts.params),
49-
]);
50-
if (!_key || typeof _key !== 'string') {
51-
throw new TypeError(`[nuxt] [useFetch] key must be a string: ${_key}`);
52-
}
53-
if (!request) {
54-
throw new Error('[nuxt] [useFetch] request is missing.');
55-
}
56-
const key = _key === autoKey ? `$f${_key}` : _key;
57-
58-
const _request = computed(() => {
59-
let r = request;
60-
if (typeof r === 'function') {
61-
r = r();
62-
}
63-
return unref(r);
64-
});
65-
66-
const {
67-
server,
68-
lazy,
69-
default: defaultFn,
70-
transform,
71-
pick,
72-
watch,
73-
immediate,
74-
...fetchOptions
75-
} = opts;
76-
77-
const _fetchOptions = reactive({
78-
...fetchOptions,
79-
cache: typeof opts.cache === 'boolean' ? undefined : opts.cache,
80-
});
81-
82-
const _asyncDataOptions: AsyncDataOptions<_ResT, Transform, PickKeys> = {
83-
server,
84-
lazy,
85-
default: defaultFn,
86-
transform,
87-
pick,
88-
immediate,
89-
watch: [_fetchOptions, _request, ...(watch || [])],
7+
const defaults: UseFetchOptions<T> = {
8+
$fetch: $appFetch as $Fetch<unknown, NitroFetchRequest>
909
};
9110

92-
let controller: AbortController;
93-
94-
const asyncData = useAsyncData<_ResT, ErrorT, Transform, PickKeys>(
95-
key,
96-
() => {
97-
controller?.abort?.();
98-
controller = typeof AbortController !== 'undefined'
99-
? new AbortController()
100-
: ({} as AbortController);
101-
return $appFetch(_request.value, {
102-
signal: controller.signal,
103-
..._fetchOptions,
104-
} as any) as Promise<_ResT>;
105-
},
106-
_asyncDataOptions,
107-
);
11+
// for nice deep defaults, please use unjs/defu
12+
const params = defu(options, defaults);
10813

109-
// This force the type
110-
return asyncData as unknown as AppReturn<ReturnType>;
14+
return useFetch(url, params);
11115
}

apps/front/src/layouts/anonymous.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
<div>
33
<slot />
44
</div>
5-
</template>
5+
</template>;

apps/front/src/layouts/default.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
</div>
1515
</div>
1616
</div>
17-
</template>
17+
</template>;

apps/front/src/pages/auth/login.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
<label for="email">
66
email
77
</label>
8-
<input v-model="email" name="email" type="text">
9-
<button type="submit" @click="submitAuthenticateUser">
8+
<input
9+
v-model="email"
10+
name="email"
11+
type="text"
12+
>
13+
<button
14+
type="submit"
15+
@click="submitAuthenticateUser"
16+
>
1017
Log-in
1118
</button>
1219
</div>
@@ -17,7 +24,7 @@ import { ref } from 'vue';
1724
import { useAuthUser } from '~/store/auth';
1825
1926
definePageMeta({
20-
layout: 'anonymous',
27+
layout: 'anonymous'
2128
});
2229
2330
const authStore = useAuthUser();

apps/front/src/pages/demo/page1.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<template>
22
<div>
33
Page 1
4-
<img src="~/assets/images/panda.webp" alt="Discover Pandas" />
4+
<img
5+
src="~/assets/images/panda.webp"
6+
alt="Discover Pandas"
7+
>
58
<div class="panda-background">
69
This is to discover background scss
710
</div>

apps/front/src/pages/demo/page2.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
<template>
2-
<div>Page 2</div>
2+
<div>
3+
<div>Page 2</div>
4+
<div v-if="data">
5+
{{ data.success }}
6+
</div>
7+
</div>
38
</template>
9+
<script lang="ts" setup>
10+
import { useHealthCheckFetch } from '~/composables/api/healthCheck/useHealthCheckFetch';
11+
12+
const {
13+
data
14+
} = await useHealthCheckFetch();
15+
</script>

apps/front/src/pages/demo/page3.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
22
<div>Page 3</div>
3-
</template>
3+
</template>;

apps/front/src/pages/demo/page4.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
22
<div>Page 4</div>
3-
</template>
3+
</template>;

apps/front/src/pages/demo/page5.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
22
<div>Page 5</div>
3-
</template>
3+
</template>;

apps/front/src/pages/demo/page6.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<template>
22
<div>Page 6</div>
3-
</template>
3+
</template>;

0 commit comments

Comments
 (0)