Skip to content

Commit a936d38

Browse files
refactor: normalize route state to avoid undefined usage
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 5576faf commit a936d38

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

src/views/Validation.vue

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ defineOptions({
146146
})
147147
148148
type RouteState = {
149-
name?: string
150-
params?: Record<string, string | undefined>
151-
query?: Record<string, string | undefined>
149+
name: string | null
150+
params: Record<string, string>
151+
query: Record<string, string>
152152
}
153153
154154
type RouterState = {
@@ -199,6 +199,21 @@ function isRecord(value: unknown): value is Record<string, unknown> {
199199
return typeof value === 'object' && value !== null
200200
}
201201
202+
function toStringRecord(value: unknown): Record<string, string> {
203+
if (!isRecord(value)) {
204+
return {}
205+
}
206+
207+
const result: Record<string, string> = {}
208+
for (const [key, entry] of Object.entries(value)) {
209+
if (typeof entry === 'string') {
210+
result[key] = entry
211+
}
212+
}
213+
214+
return result
215+
}
216+
202217
function toNumber(value: unknown): number | null {
203218
return typeof value === 'number' && Number.isFinite(value) ? value : null
204219
}
@@ -365,11 +380,18 @@ const filesStore = useFilesStore()
365380
const instance = getCurrentInstance()
366381
const EXPIRATION_WARNING_DAYS = 30
367382
368-
const route = computed<RouteState>(() => (instance?.proxy?.$route as RouteState | undefined) ?? { params: {}, query: {} })
383+
const route = computed<RouteState>(() => {
384+
const rawRoute = (instance?.proxy?.$route as Partial<RouteState> | undefined) ?? {}
385+
return {
386+
name: typeof rawRoute.name === 'string' ? rawRoute.name : null,
387+
params: toStringRecord(rawRoute.params),
388+
query: toStringRecord(rawRoute.query),
389+
}
390+
})
369391
const router = computed<RouterState>(() => (instance?.proxy?.$router as RouterState | undefined) ?? { push: () => {}, replace: () => {} })
370392
371393
const logo = ref(logoGray)
372-
const uuidToValidate = ref(route.value.params?.uuid ?? '')
394+
const uuidToValidate = ref(route.value.params.uuid ?? '')
373395
const hasInfo = ref(false)
374396
const loading = ref(false)
375397
const document = ref<ValidationFileRecord | null>(null)
@@ -392,8 +414,8 @@ const signRequestUuidForProgress = computed(() => {
392414
const doc = signStore?.document || {}
393415
const fromState = loadState('libresign', 'sign_request_uuid', null)
394416
const fromDocument = getSigningRouteUuid(doc, typeof fromState === 'string' ? fromState : null)
395-
return route.value.query?.signRequestUuid
396-
|| route.value.params?.signRequestUuid
417+
return route.value.query.signRequestUuid
418+
|| route.value.params.signRequestUuid
397419
|| fromDocument
398420
|| uuidToValidate.value
399421
})
@@ -591,7 +613,7 @@ function goBack() {
591613
}
592614
hasInfo.value = false
593615
document.value = null
594-
uuidToValidate.value = route.value.params?.uuid ?? ''
616+
uuidToValidate.value = route.value.params.uuid ?? ''
595617
validationErrorMessage.value = null
596618
documentValidMessage.value = null
597619
}
@@ -832,7 +854,7 @@ function handleValidationSuccess(data: unknown) {
832854
|| routeName === 'ValidationFile'
833855
|| routeName === 'ValidationFileExternal'
834856
|| routeName === 'ValidationFileShortUrl'
835-
if (shouldUpdateRoute && route.value.params?.uuid !== normalizedDocument.uuid) {
857+
if (shouldUpdateRoute && route.value.params.uuid !== normalizedDocument.uuid) {
836858
router.value.replace({
837859
name: route.value.name,
838860
params: {

0 commit comments

Comments
 (0)