Skip to content

Commit

Permalink
feat: Allow augmented Route & RouteContext interfaces (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
willybrauner authored Sep 15, 2024
1 parent 23a2762 commit cfb9bf4
Show file tree
Hide file tree
Showing 9 changed files with 4,253 additions and 3,577 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-rocks-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wbe/low-router-preact": minor
---

Allow augmented Route & RouteContext interfaces
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const routes = [
name: "not-found",
action: () => NotFoundPage,
},
]
] as const satisfies Route[]

/**
* Locale
Expand Down
1 change: 0 additions & 1 deletion packages/low-router-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"files": [
"dist"
],
"sideEffects": false,
"main": "./dist/low-router-preact.js",
"module": "./dist/low-router-preact.js",
"types": "./dist/low-router-preact.d.ts",
Expand Down
5 changes: 2 additions & 3 deletions packages/low-router-preact/src/core/composeUrlByRouteName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ export function composeUrlByRouteName(
console.error("composeUrlByRouteName > name is invalid, return", name)
return
}
// prettier-ignore

const next = (routes: Route[], params: RouteParams, curBase: string): string => {
for (let route of routes) {
const lang = params?.lang || params?.code || i18n?.currentLocale.code
const selectPath = route.translations?.[lang] || route.path
const compiledPath = compilePath(curBase + selectPath)(params)
if (route?.name === name) {
return compiledPath
}
else if (route.children?.length > 0) {
} else if (route.children?.length > 0) {
const match = next(route.children, params, compiledPath)
if (match) {
return match
Expand Down
3 changes: 3 additions & 0 deletions packages/low-router-preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ export { composeUrlByRouteName } from "./core/composeUrlByRouteName"
export { getStaticPropsFromUrl } from "./core/getStaticPropsFromUrl"
export type { InitialStaticProps } from "./core/getStaticPropsFromUrl"
export { setLocation } from "./core/setLocation"

// import extended interfaces
import "./low-router.d.ts"
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import "@wbe/low-router"
import { Locale } from "~/core/router"
import { Locale } from "./services/I18n"

/**
* Augmentation of Route & RouteContext interfaces
*/
declare module "@wbe/low-router" {
interface Route {
export interface Route {
path?: string
name: string
getStaticProps?: (props: Record<any, any>, locale: Locale) => Promise<any>
translations?: Record<string, string>
path?: string
}

export interface RouteContext {
routeId: number
route: Route
Expand Down
8 changes: 4 additions & 4 deletions packages/low-router-preact/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"declaration": true,
"outDir": "dist",
"strict": false,
"declaration": true,
"moduleResolution": "node",
"types": ["preact"]
"types": ["preact", "node", "./src/low-router.d.ts"]
},
"types": ["node", "src/router.d.ts"],
"include": ["src/**/*"],

"include": ["./src/**/*"],
"exclude": ["node_modules", "dist"]
}
29 changes: 0 additions & 29 deletions packages/low-router-preact/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import { defineConfig } from "tsup"
// import fs from "node:fs/promises"

export default defineConfig([
// {
// entry: {
// "preact-deps": "src/preact-deps.ts",
// },
// splitting: true,
// clean: true,
// dts: true,
// format: ["esm"],
// minify: true,
// external: ["preact", "preact/hooks", "preact/compat"],
// },

{
entry: {
"low-router-preact": "src/index.ts",
Expand All @@ -32,21 +19,5 @@ export default defineConfig([
"@wbe/low-router",
// /preact-deps/,
],

// esbuildPlugins: [
// {
// name: "replace-import",
// setup(build) {
// build.onLoad({ filter: /\.ts$/ }, async (args) => {
// let contents = await fs.readFile(args.path, "utf8")
// contents = contents.replace(/from\s+['"]\.\.\/preact-deps['"]/g, 'from "./preact-deps"')
// return {
// contents,
// loader: "default",
// }
// })
// },
// },
// ],
},
])
Loading

0 comments on commit cfb9bf4

Please sign in to comment.