Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/little-crews-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-kit-routes': patch
---

fix: manage well "//" in values
9 changes: 6 additions & 3 deletions packages/vite-plugin-kit-routes/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ export const ACTIONS = {
* LINKS
*/
export const LINKS = {
twitter: `https:/twitter.com/jycouet`,
twitter: `https://twitter.com/jycouet`,
twitter_post: (params: { name: string | number; id: string | number }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
gravatar: (params: { str: string; s?: number; d?: 'retro' | 'identicon' }) => {
params.s = params.s ?? 75
params.d = params.d ?? 'identicon'
return `https:/www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${params.str}${appendSp({
s: params?.s,
d: params?.d,
})}`
},
}

Expand Down
78 changes: 75 additions & 3 deletions packages/vite-plugin-kit-routes/src/lib/fs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,82 @@ describe('fs', () => {
})
})

describe('rmvOptional', () => {
it('getFilesUnder', async () => {
describe('rmv Helper', () => {
it('rmvOptional', async () => {
const location = `${process.cwd()}/src/routes/`
expect(getFilesUnder(location).map(c => rmvOptional(c))).toMatchInlineSnapshot(`
[
"(rootGroup)/subGroup/+page.svelte",
"(rootGroup)/subGroup2/+page.svelte",
"+layout.svelte",
"+page.svelte",
"/contract/+page.svelte",
"/contract/+server.ts",
"/contract/[id]/+page.server.ts",
"/contract/[id]/+page.svelte",
"/gp/(logged)/one/+page.svelte",
"/gp/(public)/two/+page.svelte",
"/main/+page.svelte",
"/match/[id=int]/+page.svelte",
"/site/+page.server.ts",
"/site/+page.svelte",
"/site/+server.ts",
"/site/[id]/+page.server.ts",
"/site/[id]/+page.svelte",
"/site_contract/+page.server.ts",
"/site_contract/[siteId]-[contractId]/+page.server.ts",
"/site_contract/[siteId]-[contractId]/+page.svelte",
"a/[...rest]/z/+page.svelte",
"api/graphql/+server.ts",
"lay/(layVerySpecial)/+layout.svelte",
"lay/(layVerySpecial)/normal/+page.svelte",
"lay/(layVerySpecial)/root-layout/[email protected]",
"lay/(layVerySpecial)/skip/[email protected]",
"lay/+layout.svelte",
"page_server_woAction/+page.server.ts",
]
`)
})

it('rmvGroups', async () => {
const location = `${process.cwd()}/src/routes/`
expect(getFilesUnder(location).map(c => rmvGroups(c))).toMatchInlineSnapshot(`
[
"/subGroup/+page.svelte",
"/subGroup2/+page.svelte",
"+layout.svelte",
"+page.svelte",
"[[lang]]/contract/+page.svelte",
"[[lang]]/contract/+server.ts",
"[[lang]]/contract/[id]/+page.server.ts",
"[[lang]]/contract/[id]/+page.svelte",
"[[lang]]/gp/one/+page.svelte",
"[[lang]]/gp/two/+page.svelte",
"[[lang]]/main/+page.svelte",
"[[lang]]/match/[id=int]/+page.svelte",
"[[lang]]/site/+page.server.ts",
"[[lang]]/site/+page.svelte",
"[[lang]]/site/+server.ts",
"[[lang]]/site/[id]/+page.server.ts",
"[[lang]]/site/[id]/+page.svelte",
"[[lang]]/site_contract/+page.server.ts",
"[[lang]]/site_contract/[siteId]-[contractId]/+page.server.ts",
"[[lang]]/site_contract/[siteId]-[contractId]/+page.svelte",
"a/[...rest]/z/+page.svelte",
"api/graphql/+server.ts",
"lay/+layout.svelte",
"lay/normal/+page.svelte",
"lay/root-layout/[email protected]",
"lay/skip/[email protected]",
"lay/+layout.svelte",
"page_server_woAction/+page.server.ts",
]
`)
})

it('rmvGroups & Optional', async () => {
const location = `${process.cwd()}/src/routes/`
expect(getFilesUnder(location).map(c => rmvOptional(rmvGroups(c)))).toMatchInlineSnapshot(`
expect(getFilesUnder(location).map(c => rmvGroups(rmvOptional(c)))).toMatchInlineSnapshot(`
[
"/subGroup/+page.svelte",
"/subGroup2/+page.svelte",
Expand Down
24 changes: 16 additions & 8 deletions packages/vite-plugin-kit-routes/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export type Options<T extends ExtendTypes = ExtendTypes> = {
* If you have only 1 required param, it will be a direct arg (not part of an object).
*
* ```ts
* route("/site/[id]", 7)
* route("site_id", 7)
* PAGE_site_id(7)
* PAGES["/site/[id]"](7)
* PAGES.site_id(7)
* route("/site/[id]", 7) // format: route(path)
* route("site_id", 7) // format: route(symbol)
* PAGE_site_id(7) // format: variables
* PAGES["/site/[id]"](7) // format: object[path]
* PAGES.site_id(7) // format: object[symbol]
* ```
*/
shorten_args_if_one_required?: boolean
Expand Down Expand Up @@ -218,23 +218,31 @@ export function routes_path() {

export function rmvGroups(key: string) {
let toRet = key
// rmv /(groups)
.replace(/\/\([^)]*\)/, '')
// rmv (groups)
.replace(/\([^)]*\)/g, '')
.replace(/\/+/g, '/')
return toRet
}

export function rmvOptional(key: string) {
let toRet = key
// rmv (Optional)
.replace(/\[{2}.*?\]{2}/g, '')
// rmv /[[Optional]]
.replace(/\/\[\[.*?\]\]/, '')
// rmv [[Optional]]
.replace(/\[\[.*?\]\]/, '')
return toRet
}

export function formatKey(key: string, o: Options) {
const options = getDefaultOption(o)
let toRet = rmvGroups(rmvOptional(key))

// In case we have only an optional param
if (toRet === '') {
toRet = '/'
}

if (options.format!.includes('path')) {
return toRet
}
Expand Down
6 changes: 3 additions & 3 deletions packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ describe('run()', async () => {
{
name: 'direct link',
kind: 'LINKS',
results: 'https:/twitter.com/jycouet',
results: 'https://twitter.com/jycouet',
key_path: 'twitter',
key_symbol: 'twitter',
params: [],
Expand Down Expand Up @@ -615,7 +615,7 @@ describe('run()', async () => {
} else if (element.results === '/contract?yop=hello') {
nbVariablesDone++
expect(vars.PAGE_contract({}, { yop: 'hello' }), element.name).toBe(element.results)
} else if (element.results === 'https:/twitter.com/jycouet') {
} else if (element.results === 'https://twitter.com/jycouet') {
nbVariablesDone++
expect(vars.LINK_twitter, element.name).toBe(element.results)
} else if (element.results === '/fr/site_contract/Paris-abc?limit=2') {
Expand Down Expand Up @@ -643,7 +643,7 @@ describe('run()', async () => {
} else if (element.results === '/contract?yop=hello') {
nbVariablesShortenedDone++
expect(vars.PAGE_contract({}, { yop: 'hello' }), element.name).toBe(element.results)
} else if (element.results === 'https:/twitter.com/jycouet') {
} else if (element.results === 'https://twitter.com/jycouet') {
nbVariablesShortenedDone++
expect(vars.LINK_twitter, element.name).toBe(element.results)
} else if (element.results === '/fr/site_contract/Paris-abc?limit=2') {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type AllTypes = typeof AllObjs
export function route<T extends FunctionKeys<AllTypes>>(key: T, ...params: FunctionParams<AllTypes[T]>): string
export function route<T extends NonFunctionKeys<AllTypes>>(key: T): string
export function route<T extends keyof AllTypes>(key: T, ...params: any[]): string {
if (AllObjs[key] instanceof Function) {
if (AllObjs[key] as any instanceof Function) {
const element = (AllObjs as any)[key] as (...args: any[]) => string
return element(...params)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export const ACTION_send_site_contract_siteId_contractId = (params: { siteId: (s
/**
* LINKS
*/
export const LINK_twitter = `https:/twitter.com/jycouet`
export const LINK_twitter = `https://twitter.com/jycouet`
export const LINK_twitter_post = (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
}
export const LINK_gravatar = (params: { str: (string | number), s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ export const ACTION_send_site_contract_siteId_contractId = (params: { siteId: (s
/**
* LINKS
*/
export const LINK_twitter = `https:/twitter.com/jycouet`
export const LINK_twitter = `https://twitter.com/jycouet`
export const LINK_twitter_post = (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
}
export const LINK_gravatar = (str: (string | number), params: { s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export const ACTIONS = {
* LINKS
*/
export const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (params: { str: (string | number), s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export const ACTIONS = {
* LINKS
*/
export const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (str: (string | number), params: { s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export const ACTIONS = {
* LINKS
*/
export const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (params: { str: (string | number), s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export const ACTIONS = {
* LINKS
*/
export const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (str: (string | number), params: { s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ const ACTIONS = {
* LINKS
*/
const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (params: { str: (string | number), s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs
export function route<T extends FunctionKeys<AllTypes>>(key: T, ...params: FunctionParams<AllTypes[T]>): string
export function route<T extends NonFunctionKeys<AllTypes>>(key: T): string
export function route<T extends keyof AllTypes>(key: T, ...params: any[]): string {
if (AllObjs[key] instanceof Function) {
if (AllObjs[key] as any instanceof Function) {
const element = (AllObjs as any)[key] as (...args: any[]) => string
return element(...params)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ const ACTIONS = {
* LINKS
*/
const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (str: (string | number), params: { s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs
export function route<T extends FunctionKeys<AllTypes>>(key: T, ...params: FunctionParams<AllTypes[T]>): string
export function route<T extends NonFunctionKeys<AllTypes>>(key: T): string
export function route<T extends keyof AllTypes>(key: T, ...params: any[]): string {
if (AllObjs[key] instanceof Function) {
if (AllObjs[key] as any instanceof Function) {
const element = (AllObjs as any)[key] as (...args: any[]) => string
return element(...params)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ const ACTIONS = {
* LINKS
*/
const LINKS = {
"twitter": `https:/twitter.com/jycouet`,
"twitter": `https://twitter.com/jycouet`,
"twitter_post": (params: { name: (string | number), id: (string | number) }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
return `https://twitter.com/${params.name}/status/${params.id}`
},
"gravatar": (params: { str: (string | number), s?: (number), d?: ("retro" | "identicon") }) => {
params.s = params.s ?? 75;
params.d = params.d ?? "identicon";
return `https:/www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
return `https://www.gravatar.com/avatar/${params.str}${appendSp({ s: params?.s, d: params?.d })}`
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs
export function route<T extends FunctionKeys<AllTypes>>(key: T, ...params: FunctionParams<AllTypes[T]>): string
export function route<T extends NonFunctionKeys<AllTypes>>(key: T): string
export function route<T extends keyof AllTypes>(key: T, ...params: any[]): string {
if (AllObjs[key] instanceof Function) {
if (AllObjs[key] as any instanceof Function) {
const element = (AllObjs as any)[key] as (...args: any[]) => string
return element(...params)
} else {
Expand Down
Loading