Skip to content

Commit

Permalink
feat: added getNextPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Uninen committed Jan 5, 2024
1 parent c58d5ee commit af54dc6
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.2.0 (2024-01-05)

- Feat: added `getNextPath`
- Refactor: refactored internals
- Chore: bumped deps.

## 0.1.2 (2023-07-17)

- CI: create tags and releases on publish.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
General utilities for Web development

## Features

### Browser

- `copyToClipboard(content: string): boolean`
- `getCookie(name: string): string | null`
- `storageAvailable(type: 'localStorage' | 'sessionStorage'): boolean`

### Vue

- `getNextPath(): string` - returns the value of `?next` query param or `/`

## Installation

`pnpm add @slipmatio/toolbelt`
Expand Down
25 changes: 14 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Slipmat Toolbelt DEV</title>
</head>
<body class="bg-slate-950">
<div id="app"></div>
<script type="module" src="/src/dev.ts"></script>
</body>
</html>

<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Slipmat Toolbelt DEV</title>
</head>

<body class="bg-slate-950">
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>

</html>
3 changes: 3 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<router-view></router-view>
</template>
4 changes: 0 additions & 4 deletions src/dev.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { storageAvailable, getCookie, copyToClipboard } from './browser'
export { getNextPath } from './routes'
20 changes: 20 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createApp } from 'vue'
import App from './App.vue'
import { createRouter, createWebHistory } from 'vue-router'

import BrowserPage from './pages/BrowserPage.vue'
import VuePage from './pages/VuePage.vue'

const routes = [
{ path: '/', component: BrowserPage },
{ path: '/vue/', component: VuePage },
]

const router = createRouter({
history: createWebHistory(),
routes,
})

const app = createApp(App)
app.use(router)
app.mount('#app')
2 changes: 1 addition & 1 deletion src/Dev.vue → src/pages/BrowserPage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { copyToClipboard, getCookie, storageAvailable } from './browser'
import { copyToClipboard, getCookie, storageAvailable } from '../browser'
const clipboardText = 'clipboard works'
Expand Down
7 changes: 7 additions & 0 deletions src/pages/VuePage.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts">
import { getNextPath } from '../routes'
const nextPath = getNextPath()
console.log(`nextPath: ${nextPath}`)
</script>
<template></template>
20 changes: 20 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRoute } from 'vue-router'
import { isString } from './type-helpers'

/**
* Helper for figuring our ?next= query param in a safe way.
*/
export function getNextPath(): string {
const route = useRoute()

let next = '/'
if (
route.query &&
route.query.next &&
isString(route.query.next) &&
route.query.next.startsWith('/')
) {
next = route.query.next as string
}
return next
}
5 changes: 5 additions & 0 deletions src/type-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { LocationQueryValue } from 'vue-router'

export function isString(value: string | LocationQueryValue[]): value is string {
return typeof value === 'string'
}
45 changes: 45 additions & 0 deletions tests/e2e/vue.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { test, expect } from '@playwright/test'

test('getNextPath with no querypath should return "/"', async ({ page }) => {
page.on('console', (msg) => {
const text = msg.text()
if (!text.startsWith('[vite]')) {
expect(text).toBe('nextPath: /')
}
})

await page.goto('/vue/')
})

test('getNextPath with next=foo should return "/"', async ({ page }) => {
page.on('console', (msg) => {
const text = msg.text()
if (!text.startsWith('[vite]')) {
expect(text).toBe('nextPath: /')
}
})

await page.goto('/vue/?next=foo')
})

test('getNextPath with next=http://foo.bar should return "/"', async ({ page }) => {
page.on('console', (msg) => {
const text = msg.text()
if (!text.startsWith('[vite]')) {
expect(text).toBe('nextPath: /')
}
})

await page.goto('/vue/?next=http://foo.bar')
})

test('getNextPath with next=/next-url/ should return "/next-url/"', async ({ page }) => {
page.on('console', (msg) => {
const text = msg.text()
if (!text.startsWith('[vite]')) {
expect(text).toBe('nextPath: /next-url/')
}
})

await page.goto('/vue/?next=/next-url/')
})

0 comments on commit af54dc6

Please sign in to comment.