Skip to content

Commit

Permalink
fix: url IDs rules #442
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab committed Dec 16, 2024
1 parent dfb4859 commit 7dfff26
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 105 deletions.
57 changes: 33 additions & 24 deletions pages/embedded.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mapStore as useMapStore } from '~/stores/map'
//
// Composables
//
const { params, query, path } = useRoute()
const route = useRoute()
const siteStore = useSiteStore()
const mapStore = useMapStore()
const { config, settings } = storeToRefs(siteStore)
Expand All @@ -20,9 +20,9 @@ const { $trackingInit } = useNuxtApp()
// Data
//
const boundaryGeojson = ref<Polygon | MultiPolygon>()
const categoryIdsJoin = ref<string>()
const poiId = ref<string>()
const categoryIds = ref<number[]>()
const categoryIdsRegex = /^(?:(?<cartocode>cartocode:[a-zA-Z0-9]{2})|(?<reference>ref:[a-z0-9-]+:[a-zA-Z0-9]+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
//
// Hooks
Expand All @@ -31,7 +31,7 @@ onBeforeMount(() => {
$trackingInit(config.value!)
})
const { boundary } = query
const { boundary } = route.query
if (boundary && typeof boundary === 'string' && settings.value!.polygons_extra) {
const boundaryObject = settings.value!.polygons_extra[boundary]
if (boundaryObject) {
Expand All @@ -51,33 +51,42 @@ if (boundary && typeof boundary === 'string' && settings.value!.polygons_extra)
}
}
// Workaround Nuxt missing feature to simple respect trialling slash meaning
if (params.poiId) {
categoryIdsJoin.value = params.p1 as string
poiId.value = params.poiId as string
}
else if (path.endsWith('/')) {
categoryIdsJoin.value = params.p1 as string
poiId.value = undefined
}
else {
categoryIdsJoin.value = undefined
poiId.value = params.p1 as string
// Get category IDs from URL
if (route.params.p1) {
const match = route.params.p1.toString().match(categoryIdsRegex)
if (!match || (!route.path.endsWith('/') && match.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)))
throw createError({ statusCode: 400, message: `No match for category ID: ${route.params.p1}` })
categoryIds.value = match.input?.split(',').map(id => Number.parseInt(id))
}
categoryIds.value = categoryIdsJoin.value?.split(',').map(id => Number.parseInt(id))
// Get POI ID from URL
if (categoryIds.value?.length === 1 && route.name === 'index-p1' && route.path.endsWith('/')) {
poiId.value = route.params.p1?.toString()
categoryIds.value = undefined
}
const { data, error } = await useFetch<ApiPoi>(`${API_ENDPOINT}/${API_PROJECT}/${API_THEME}/poi/${poiId.value}.geojson?geometry_as=bbox&short_description=false`)
if (route.params.poiId)
poiId.value = route.params.poiId.toString()
if (categoryIds.value && poiId.value) {
if (error.value)
throw createError(error.value)
// Fetch inital POI
const { data, error, status } = await useFetch<ApiPoi>(
() => `${API_ENDPOINT}/${API_PROJECT}/${API_THEME}/poi/${poiId.value}.geojson`,
{
query: {
geometry_as: 'bbox',
short_description: false,
},
immediate: !!poiId.value,
},
)
if (!data.value)
throw createError({ statusCode: 404, message: 'Initial POI not found !' })
if (error.value)
createError(error.value)
mapStore.setSelectedFeature(data.value!)
}
if (status.value === 'success' && data.value)
mapStore.setSelectedFeature(data.value)
</script>

<template>
Expand Down
14 changes: 0 additions & 14 deletions pages/embedded/[p1].vue

This file was deleted.

17 changes: 10 additions & 7 deletions pages/embedded/[p1]/[poiId].vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
// Validators
//
definePageMeta({
validate({ params }) {
return (
typeof params.p1 === 'string'
&& typeof params.poiId === 'string'
&& /^[0-9,]+$/.test(params.p1)
&& /^[-\w:]+$/.test(params.poiId)
)
validate: ({ params }) => {
const regexForCategoryIds = /^(?:(?<cartocode>cartocode:[a-zA-Z0-9]{2})|(?<reference>ref:[a-z0-9-]+:[a-zA-Z0-9]+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
const regexForPOIIds = /^(?:cartocode:[a-zA-Z0-9]{2}|ref:[a-z0-9-]+:[a-zA-Z0-9]+|osm:[nwr]\d+|\d+)$/
const match = params.p1.toString().match(regexForCategoryIds)
if (match?.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)) {
return false
}
return regexForCategoryIds.test(params.p1.toString()) && regexForPOIIds.test(params.poiId.toString())
},
})
</script>
Expand Down
9 changes: 4 additions & 5 deletions pages/embedded/[p1]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// Validators
//
definePageMeta({
validate({ params }) {
return (
params.p1 === undefined
|| (typeof params.p1 === 'string' && /^[0-9,]+$/.test(params.p1))
)
validate: ({ params }) => {
const regexForIds = /^(?:cartocode:[a-zA-Z0-9]{2}|ref:[a-z0-9-]+:[a-zA-Z0-9]+|osm:[nwr]\d+|\d+(?:,\d+)*)$/
return regexForIds.test(params.p1.toString())
},
})
</script>
Expand Down
66 changes: 42 additions & 24 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { mapStore as useMapStore } from '~/stores/map'
//
// Composables
//
const { params, query, path } = useRoute()
const route = useRoute()
const siteStore = useSiteStore()
const mapStore = useMapStore()
const { config, settings } = storeToRefs(siteStore)
Expand All @@ -20,9 +20,9 @@ const { $trackingInit } = useNuxtApp()
// Data
//
const boundaryGeojson = ref<Polygon | MultiPolygon>()
const categoryIdsJoin = ref<string>()
const poiId = ref<string>()
const categoryIds = ref<number[]>()
const categoryIdsRegex = /^(?:(?<cartocode>cartocode:[a-zA-Z0-9]{2})|(?<reference>ref:[a-z0-9-]+:[a-zA-Z0-9]+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
//
// Hooks
Expand All @@ -31,7 +31,7 @@ onBeforeMount(() => {
$trackingInit(config.value!)
})
const { boundary } = query
const { boundary } = route.query
if (boundary && typeof boundary === 'string' && settings.value!.polygons_extra) {
const boundaryObject = settings.value!.polygons_extra[boundary]
if (boundaryObject) {
Expand All @@ -51,38 +51,56 @@ if (boundary && typeof boundary === 'string' && settings.value!.polygons_extra)
}
}
// Workaround Nuxt missing feature to simple respect trialling slash meaning
if (params.poiId) {
categoryIdsJoin.value = params.p1 as string
poiId.value = params.poiId as string
}
else if (path.endsWith('/')) {
categoryIdsJoin.value = params.p1 as string
poiId.value = undefined
// Get category IDs from URL
if (route.params.p1) {
const match = route.params.p1.toString().match(categoryIdsRegex)
if (!match || (!route.path.endsWith('/') && match.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)))
throw createError({ statusCode: 400, message: `No match for category ID: ${route.params.p1}` })
categoryIds.value = match.input?.split(',').map(id => Number.parseInt(id))
}
else {
categoryIdsJoin.value = undefined
poiId.value = params.p1 as string
// Get POI ID from URL
if (categoryIds.value?.length === 1 && route.name === 'index-p1' && route.path.endsWith('/')) {
poiId.value = route.params.p1?.toString()
categoryIds.value = undefined
}
categoryIds.value = categoryIdsJoin.value?.split(',').map(id => Number.parseInt(id))
if (route.params.poiId)
poiId.value = route.params.poiId.toString()
// Fetch inital POI
const { data, error } = await useFetch<ApiPoi>(`${API_ENDPOINT}/${API_PROJECT}/${API_THEME}/poi/${poiId.value}.geojson?geometry_as=bbox&short_description=true`)
const { data, error, status } = await useFetch<ApiPoi>(
() => `${API_ENDPOINT}/${API_PROJECT}/${API_THEME}/poi/${poiId.value}.geojson`,
{
query: {
geometry_as: 'bbox',
short_description: true,
},
immediate: !!poiId.value,
},
)
if (categoryIds.value && poiId.value) {
if (error.value)
throw createError(error.value)
if (error.value)
createError(error.value)
if (!data.value)
throw createError({ statusCode: 404, message: 'Initial POI not found !' })
mapStore.setSelectedFeature(data.value!)
}
if (status.value === 'success' && data.value)
mapStore.setSelectedFeature(data.value)
</script>

<template>
<VApp>
<VAlert
v-if="error"
:closable="true"
:style="{ zIndex: 999 }"
:text="error.message"
location="top center"
position="fixed"
type="error"
variant="elevated"
/>
<Home
:boundary-area="boundaryGeojson"
:initial-category-ids="categoryIds"
Expand Down
18 changes: 0 additions & 18 deletions pages/index/[p1].vue

This file was deleted.

17 changes: 10 additions & 7 deletions pages/index/[p1]/[poiId].vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
// Validators
//
definePageMeta({
validate({ params }) {
return (
typeof params.p1 === 'string'
&& typeof params.poiId === 'string'
&& /^[-\w:,]/.test(params.p1)
&& /^[-\w:]+$/.test(params.poiId)
)
validate: ({ params }) => {
const regexForCategoryIds = /^(?:(?<cartocode>cartocode:[a-zA-Z0-9]{2})|(?<reference>ref:[a-z0-9-]+:[a-zA-Z0-9]+)|(?<osm>osm:[nwr]\d+)|\d+(?:,\d+)*)$/
const regexForPOIIds = /^(?:cartocode:[a-zA-Z0-9]{2}|ref:[a-z0-9-]+:[a-zA-Z0-9]+|osm:[nwr]\d+|\d+)$/
const match = params.p1.toString().match(regexForCategoryIds)
if (match?.groups && (match.groups.cartocode || match.groups.reference || match.groups.osm)) {
return false
}
return regexForCategoryIds.test(params.p1.toString()) && regexForPOIIds.test(params.poiId.toString())
},
})
</script>
Expand Down
10 changes: 4 additions & 6 deletions pages/index/[p1]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
// Validators
//
definePageMeta({
validate({ params }) {
return (
params.p1 === undefined
|| (typeof params.p1 === 'string'
&& /^[-\w:,]+$/.test(params.p1))
)
validate: ({ params }) => {
const regexForIds = /^(?:cartocode:[a-zA-Z0-9]{2}|ref:[a-z0-9-]+:[a-zA-Z0-9]+|osm:[nwr]\d+|\d+(?:,\d+)*)$/
return regexForIds.test(params.p1.toString())
},
})
</script>
Expand Down

0 comments on commit 7dfff26

Please sign in to comment.