Skip to content

Commit

Permalink
chore: remove nuxt proxy module and replace with server routes (#3350)
Browse files Browse the repository at this point in the history
chore: remove nuxt-proxy module

Replace nuxt-proxy module with nitro server routes 

- remove plugin and imports 
- setup server proxy for gql and file requests
- refactor fetch calls removing explicit baseUrl from call
  • Loading branch information
connoratrug authored Feb 7, 2024
1 parent fc4505c commit 0f57ee5
Show file tree
Hide file tree
Showing 31 changed files with 75 additions and 117 deletions.
6 changes: 6 additions & 0 deletions apps/nuxt3-ssr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ pnpm install --shamefully-hoist

Start the development server on http://localhost:3000

set non default (api)proxy target with
```NUXT_PUBLIC_API_BASE```

```bash
npm run dev
```

## Production

set api-proxy target with
```NUXT_PUBLIC_API_BASE```

Build the application for production:

```bash
Expand Down
21 changes: 8 additions & 13 deletions apps/nuxt3-ssr/components/CohortDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@ const props = defineProps<{
id: string;
}>();
const config = useRuntimeConfig();
const route = useRoute();
const query = moduleToString(cohortGql);
let cohort: ICohort = ref();
const { data: cohortData } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query,
variables: {
id: props.id,
},
const { data: cohortData } = await useFetch(`/${route.params.schema}/graphql`, {
method: "POST",
body: {
query,
variables: {
id: props.id,
},
}
).catch((e) => console.log(e));
},
}).catch((e) => console.log(e));
watch(
cohortData,
Expand Down
3 changes: 1 addition & 2 deletions apps/nuxt3-ssr/components/CollectionEventDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ const route = useRoute();
const query = moduleToString(collectionEventGql);
const { data, error } = await useFetch<any, IMgError>(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
key: `collection-event-${route.params.cohort}-${collectionEventName}`,
baseURL: config.public.apiBase,
method: "POST",
body: {
query,
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt3-ssr/components/DatasetDisplay.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import datasetGql from "~~/gql/datasetDetails";
const config = useRuntimeConfig();
const route = useRoute();
const { id } = defineProps<{
Expand All @@ -9,8 +8,7 @@ const { id } = defineProps<{
const query = moduleToString(datasetGql);
const { data } = await useFetch(`/${route.params.schema}/catalogue/graphql`, {
baseURL: config.public.apiBase,
const { data } = await useFetch(`/${route.params.schema}/graphql`, {
method: "POST",
body: {
query: query,
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt3-ssr/components/RepeatedVariableDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ const props = defineProps<{
variableKey: KeyObject;
}>();
const config = useRuntimeConfig();
const route = useRoute();
const { data, pending, error } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query: query,
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt3-ssr/components/SubCohortDisplay.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<script setup lang="ts">
import subcohortGql from "~~/gql/subcohort";
import ContentBlockModal from "./content/ContentBlockModal.vue";
const config = useRuntimeConfig();
const route = useRoute();
const { id } = defineProps<{
Expand All @@ -12,9 +11,8 @@ const query = moduleToString(subcohortGql);
let subcohort: Ref = ref();
const { data: subcohortData } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query: query,
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt3-ssr/components/VariableDisplay.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ const props = defineProps<{
variableKey: KeyObject;
}>();
const config = useRuntimeConfig();
const route = useRoute();
const { data, pending, error } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query: query,
Expand Down
3 changes: 1 addition & 2 deletions apps/nuxt3-ssr/components/landing/Central.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ const route = useRoute();
const config = useRuntimeConfig();
const { data, pending, error, refresh } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query: `{
Expand Down
3 changes: 1 addition & 2 deletions apps/nuxt3-ssr/components/landing/CohortsOnly.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ const route = useRoute();
const config = useRuntimeConfig();
const { data, pending, error, refresh } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query: `{
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt3-ssr/composables/fetchGql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ export const fetchGql = (
}

const route = useRoute();
const config = useRuntimeConfig();
const schema = schemaId ? schemaId : route.params.schema;
return $fetch(`/${schema}/catalogue/graphql`, {
return $fetch(`/${schema}/graphql`, {
method: "POST",
baseURL: config.public.apiBase,
body,
});
};
3 changes: 0 additions & 3 deletions apps/nuxt3-ssr/composables/fetchMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { type ISchemaMetaData } from "meta-data-utils";
const query = moduleToString(metadataGql);

export default async (schemaId: string): Promise<ISchemaMetaData> => {
const config = useRuntimeConfig();

// Use sessionStorage to cache data
const cached = useSessionStorage<ISchemaMetaData>(schemaId, null, {
serializer: StorageSerializers.object,
Expand All @@ -16,7 +14,6 @@ export default async (schemaId: string): Promise<ISchemaMetaData> => {
if (!cached.value) {
const resp = await $fetch(`/${schemaId}/graphql`, {
method: "POST",
baseURL: config.public.apiBase,
body: {
query,
},
Expand Down
4 changes: 1 addition & 3 deletions apps/nuxt3-ssr/composables/fetchSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ export const fetchSetting = (settingKey: string) => {
};

const route = useRoute();
const config = useRuntimeConfig();
return $fetch(`/${route.params.schema}/catalogue/graphql`, {
return $fetch(`/${route.params.schema}/graphql`, {
method: "POST",
baseURL: config.public.apiBase,
body,
});
};
2 changes: 0 additions & 2 deletions apps/nuxt3-ssr/composables/sendContactForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import type { IContactFormData } from "~/interfaces/types";

export const sendContactForm = (formData: IContactFormData) => {
const route = useRoute();
const config = useRuntimeConfig();
return $fetch(`/${route.params.schema}/api/message/`, {
method: "POST",
baseURL: config.public.apiBase,
body: formData,
});
};
5 changes: 1 addition & 4 deletions apps/nuxt3-ssr/composables/useGqlFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export function useGqlFetch<T, E>(
query: string | Ref<string> | DocumentNode,
options: UseGqlFetchOptions<T> = {}
) {
const config = useRuntimeConfig();

const queryString = isRef(query)
? query.value
: typeof query !== "string"
Expand All @@ -30,9 +28,8 @@ export function useGqlFetch<T, E>(
body.variables = variablesValue;
}
const schema = options.schemaId ?? useRoute().params.schema;
const url = `/${schema}/catalogue/graphql`;
const url = `/${schema}/graphql`;
const defaults: UseFetchOptions<T> = {
baseURL: config.public.apiBase,
method: "POST",
key: `gql-${url}-${queryString}`,
body,
Expand Down
6 changes: 1 addition & 5 deletions apps/nuxt3-ssr/composables/useHeaderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ export async function useHeaderData() {
return { catalogue: null, variableCount: 0 };
}

const config = useRuntimeConfig();
const apiPath = `/${route.params.schema}/catalogue/graphql`;
const baseURL = config.public.apiBase;
const apiPath = `/${route.params.schema}/graphql`;
const { data, error } = await useAsyncData<any, IMgError>(
`catalogue-${route.params.catalogue}`,
async () => {
const modelsResp = await $fetch<{
data: { Networks: { models: { id: string }[] }[] };
}>(apiPath, {
baseURL,
method,
body: {
query: modelQuery,
Expand Down Expand Up @@ -68,7 +65,6 @@ export async function useHeaderData() {

return $fetch(apiPath, {
key: `header-${route.params.catalogue}`,
baseURL,
method,
body: {
query: headerQuery,
Expand Down
31 changes: 5 additions & 26 deletions apps/nuxt3-ssr/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
// https://v3.nuxtjs.org/api/configuration/nuxt.config
import { defineNuxtConfig, type NuxtConfig } from "nuxt/config";
import { defineNuxtConfig } from "nuxt/config";

const devProxy = {
options: {
target:
process.env.PROXY_TARGET || "https://data-catalogue.molgeniscloud.org/", // 'http://localhost:8080/',
pathFilter: ["**/*/graphql", "**/api/file/**", "**/api/message/**"],
changeOrigin: true,
secure: false,
logLevel: "debug",
},
};

const config: NuxtConfig = {
modules: ["nuxt-proxy", "@nuxt/image"],
export default defineNuxtConfig({
modules: ["@nuxt/image"],
devtools: { enabled: true },
runtimeConfig: {
// Keys within public, will be also exposed to the client-side
public: {
apiBase: "http://localhost:3000/", //'https://emx2.molgeniscloud.org/',
emx2Theme: "",
emx2Logo: "",
siteTitle: "MOLGENIS",
analyticsKey: "",
cohortOnly: false,
apiBase: process.env.NUXT_PUBLIC_API_BASE || "https://data-catalogue.molgeniscloud.org/",
},
},
nitro: {
compressPublicAssets: { brotli: true },
},
imports: {
transform: {
// exclude
exclude: [/\bmeta-data-utils\b/],
},
},
};

if (process.env.NODE_ENV === "development") {
config.proxy = devProxy;
}

export default defineNuxtConfig(config);
});
11 changes: 5 additions & 6 deletions apps/nuxt3-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"parse-icons": "node ../node_modules/svgo/bin/svgo -f ./assets/icons -o ./assets/minified-icons --config ./svgo.config.js && node ./scripts/create_vue_components_from_icons.js"
},
"devDependencies": {
"@nuxt/image": "1.1.0",
"@nuxt/test-utils": "3.9.0",
"@nuxt/vite-builder": "3.9.0",
"@nuxt/image": "1.3.0",
"@nuxt/test-utils": "3.10.0",
"@nuxt/vite-builder": "3.10.0",
"@tailwindcss/forms": "^0.5.3",
"@tailwindcss/typography": "^0.5.8",
"@types/node": "16.18.74",
Expand All @@ -29,8 +29,7 @@
"graphql-tag": "2.12.6",
"jsdom": "22.1.0",
"meta-data-utils": "*",
"nuxt": "^3.10.0",
"nuxt-proxy": "0.4.1",
"nuxt": "3.10.0",
"postcss": "8.4.33",
"postcss-custom-properties": "13.3.4",
"prettier": "2.8.8",
Expand All @@ -41,7 +40,7 @@
},
"dependencies": {
"@vueuse/core": "10.5.0",
"floating-vue": "^2.0.0-beta.20",
"floating-vue": "5.2.2",
"meta-data-utils": "*",
"vite": "4.5.2",
"vue": "3.4.15"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ let sections: any;
let tocItems: { label: string; id: string }[];
const { data, pending, error, refresh } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: { query, variables: { filter: filter } },
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ const { data, pending, error, refresh } = await useFetch(
`/${route.params.schema}/api/graphql`,
{
key: `${tableId}-list-${offset.value}`,
baseURL: config.public.apiBase,
method: "POST",
body: {
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ const route = useRoute();
const query = moduleToString(collectionEventGql);
const { data, error } = await useFetch<any, IMgError>(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ interface IResponse {
};
}
const { data, error } = await useFetch<IResponse, IMgError>(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: { query, variables },
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ const query = moduleToString(subcohortGql);
let subcohort: Ref = ref();
const { data: subcohortData } = await useFetch(
`/${route.params.schema}/catalogue/graphql`,
`/${route.params.schema}/graphql`,
{
baseURL: config.public.apiBase,
method: "POST",
body: {
query,
Expand Down
Loading

0 comments on commit 0f57ee5

Please sign in to comment.