Skip to content

Commit

Permalink
move good api to vue backend
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Nov 19, 2024
1 parent bdd2867 commit 8c10d26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
3 changes: 3 additions & 0 deletions api/pages.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export const getItems = (path) => {
return $fetch(`https://newapi.mixdrinks.org/api${path}`)
}
export const getGood = (path) => {
return $fetch(`/api${path}`)
}
export const getCoctails = (path, fetchWIXUP) => {
return fetchWIXUP(`https://newapi.mixdrinks.org/api/filter${path}`)
}
Expand Down
4 changes: 2 additions & 2 deletions pages/goods/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script>
import { defineComponent, unref } from 'vue'
import { useAsyncData, useNuxtApp, useRoute } from 'nuxt/app'
import { getItems, getCoctails } from '~~/api/pages'
import { getGood, getCoctails } from '~~/api/pages'
import { querySTR } from '~~/utils/querySTR'
import ItemsPage from '~~/components/items/ItemsPage.vue'
Expand All @@ -31,7 +31,7 @@ export default defineComponent({
const { data, refresh } = await useAsyncData(async () => {
const [cocktailsFull, items] = await Promise.all([
getCoctails(getPath(), $fetchWIXUP),
getItems(route.path)
getGood(route.path)
])
return { cocktailsFull, items }
})
Expand Down
52 changes: 52 additions & 0 deletions server/api/goods/[slug].get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { defineEventHandler } from 'h3'

import { db } from '~/server/utils/mongo'

import * as dotenv from 'dotenv'

dotenv.config()

const imageDomain = process.env.IMAGE_DOMAIN

const formats = ['webp', 'jpg']

const buildOgImage = (id) => {
return `${imageDomain}/goods/${id}/256/${id}.jpg`;
};

const buildGoodDetailsImage = (slug) => {
const sizes = [
{ responseSize: "414px", imageSize: "500" },
{ responseSize: "0", imageSize: "334" }
];

return formats.flatMap(format =>
sizes.map(size => ({
srcset: `${imageDomain}/v2/goods/${slug}/${size.imageSize}.${format}`,
media: `screen and (min-width: ${size.responseSize})`,
type: `image/${format}`
}))
);
}

async function getGoodBySlug(slug) {
return db
.collection('goods')
.findOne({ slug: slug })
}

export default defineEventHandler(async (req) => {
const slug = req.context.params.slug;

const good = await getGoodBySlug(slug);
return {
id: good.id,
slug: good.slug,
name: good.name,
about: good.about,
images: buildGoodDetailsImage(good.slug),
meta: {
ogImage: buildOgImage(good.id),
},
}
})

0 comments on commit 8c10d26

Please sign in to comment.