@teamnovu/kit-shopware-composables 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,72 @@
1
+ import type { Schemas } from '#store-types'
2
+ import type { MaybeRef } from 'vue'
3
+ import { computed, reactive, toRef, unref, watch } from 'vue'
4
+
5
+ interface PaginationOptions {
6
+ page?: MaybeRef<number>
7
+ total?: MaybeRef<number>
8
+ limit?: MaybeRef<number>
9
+ }
10
+
11
+ export function usePagination(opts?: PaginationOptions) {
12
+ const { total, limit, page } = opts ?? {}
13
+
14
+ const state = reactive({
15
+ page: page ?? 1,
16
+ total: total ?? 0,
17
+ limit: limit,
18
+ })
19
+
20
+ const pageCount = computed(() => {
21
+ if (state.limit === undefined) return 1
22
+ return Math.max(1, Math.ceil(state.total / state.limit))
23
+ })
24
+
25
+ const privatePage = computed<number, number>({
26
+ get() {
27
+ return state.page
28
+ },
29
+ set(value) {
30
+ // Clamp the value between 1 and totalPages
31
+ state.page = Math.max(1, Math.min(unref(pageCount), value))
32
+ },
33
+ })
34
+
35
+ watch(
36
+ [() => unref(pageCount), () => state],
37
+ ([newPageCount, newState]) => {
38
+ // Reset the page to the last available page when the total pages change
39
+ state.page = Math.min(newPageCount, state.page)
40
+ state.total = newState.total ?? 0
41
+ state.limit = newState.limit
42
+ },
43
+ )
44
+
45
+ const isLastPage = computed(() => state.page === unref(pageCount))
46
+ const isFirstPage = computed(() => state.page === 1)
47
+
48
+ const queryOptions = computed(() => ({
49
+ p: state.page,
50
+ limit: state.limit,
51
+ }))
52
+
53
+ const usePaginationSync = (data: MaybeRef<Schemas['EntitySearchResult']>) => {
54
+ watch(() => unref(data), (newData) => {
55
+ state.total = newData?.total ?? 0
56
+ state.limit = newData?.limit ?? 0
57
+ })
58
+ }
59
+
60
+ return {
61
+ page: unref(privatePage),
62
+ total: toRef(state, 'total'),
63
+ limit: toRef(state, 'limit'),
64
+ pageCount,
65
+ isLastPage,
66
+ isFirstPage,
67
+ usePaginationSync,
68
+
69
+ // This can be used to pass the pagination options directly to the query options
70
+ queryOptions,
71
+ }
72
+ }
@@ -0,0 +1,8 @@
1
+ export const relativizeSeoUrl = (seoUrl: string) => {
2
+ const url = seoUrl.replace(/\/?$/, '')
3
+ return url.startsWith('/') ? url.slice(1) : url
4
+ }
5
+
6
+ export const absolutizeSeoUrl = (seoUrl: string) => {
7
+ return `/${relativizeSeoUrl(seoUrl)}`
8
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "types": [
6
+ "vue",
7
+ "vite/client",
8
+ "vitest/globals"
9
+ ],
10
+ "paths": {
11
+ "#store-types": [
12
+ "./api-types/storeApiTypes.d.ts"
13
+ ]
14
+ },
15
+ "typeRoots": [
16
+ "./node_modules"
17
+ ],
18
+ },
19
+ "include": [
20
+ "src/**/*",
21
+ "../components/src/injects"
22
+ ]
23
+ }
package/vite.config.js ADDED
@@ -0,0 +1,39 @@
1
+ import vue from '@vitejs/plugin-vue'
2
+ import { resolve } from 'path'
3
+ import { defineConfig } from 'vite'
4
+ import dts from 'vite-plugin-dts'
5
+ import pkg from './package.json'
6
+
7
+ export default defineConfig({
8
+ plugins: [
9
+ vue(),
10
+ dts({
11
+ staticImport: true,
12
+ }),
13
+ ],
14
+ build: {
15
+ emptyOutDir: false,
16
+ lib: {
17
+ formats: ['es'],
18
+ // Could also be a dictionary or array of multiple entry points
19
+ entry: {
20
+ index: resolve(__dirname, 'src/index.ts'),
21
+ },
22
+ },
23
+ rollupOptions: {
24
+ // make sure to externalize deps that shouldn't be bundled
25
+ // into your library
26
+ external: [
27
+ ...Object.keys(pkg.dependencies ?? {}),
28
+ ...Object.keys(pkg.peerDependencies ?? {}),
29
+ ],
30
+ output: {
31
+ // Provide global variables to use in the UMD build
32
+ // for externalized deps
33
+ globals: {
34
+ vue: 'Vue',
35
+ },
36
+ },
37
+ },
38
+ },
39
+ })
@@ -0,0 +1,10 @@
1
+ import { loadEnv } from 'vite'
2
+ import { defineConfig } from 'vitest/config'
3
+
4
+ export default defineConfig(({ mode }) => ({
5
+ test: {
6
+ include: ['**/*.test.ts'],
7
+ globals: true,
8
+ env: loadEnv(mode, __dirname, ''),
9
+ },
10
+ }))