Skip to content

Commit

Permalink
feat: exporting AllRoutes set from generated ROUTES.ts (#676)
Browse files Browse the repository at this point in the history
* feat: exporting AllObjs from generated ROUTES.ts

This allows users to import 'AllObjs' which contains all routes.

* feat: added AllRoutes type and set

* fix: backslash as string

* feat: returning key case regex fail

* refactor: using better variable names

* add "routes"

* add changeset

---------

Co-authored-by: jycouet <[email protected]>
  • Loading branch information
duducpp and jycouet authored Jul 25, 2024
1 parent 2e2c8e6 commit 99c9786
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-pugs-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vite-plugin-kit-routes": patch
---

add routes as Set (with all routes options) & Routes type.
7 changes: 7 additions & 0 deletions packages/vite-plugin-kit-routes/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}`
? `/${Route}`
: keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-kit-routes/src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs
export type Routes = keyof AllTypes extends \`\${string}/\${infer Route}\` ? \`/\${Route}\` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\\/.*|[^ ]?\\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]
/**
* To be used like this:
* \`\`\`ts
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ type FunctionParams<T> = T extends (...args: infer P) => any ? P : never
const AllObjs = { ...PAGES, ...ACTIONS, ...SERVERS, ...LINKS }
type AllTypes = typeof AllObjs

export type Routes = keyof AllTypes extends `${string}/${infer Route}` ? `/${Route}` : keyof AllTypes
export const routes = [
...new Set(Object.keys(AllObjs).map((route) => /^\/.*|[^ ]?\/.*$/.exec(route)?.[0] ?? route)),
] as Routes[]

/**
* To be used like this:
* ```ts
Expand Down

0 comments on commit 99c9786

Please sign in to comment.