diff --git a/.changeset/little-crews-fail.md b/.changeset/little-crews-fail.md new file mode 100644 index 000000000..014d88e2a --- /dev/null +++ b/.changeset/little-crews-fail.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-kit-routes': patch +--- + +fix: manage well "//" in values diff --git a/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts b/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts index 85ab21e68..a31bed4cb 100644 --- a/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts +++ b/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts @@ -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, + })}` }, } diff --git a/packages/vite-plugin-kit-routes/src/lib/fs.spec.ts b/packages/vite-plugin-kit-routes/src/lib/fs.spec.ts index 1c34c1e09..b2f21f1ef 100644 --- a/packages/vite-plugin-kit-routes/src/lib/fs.spec.ts +++ b/packages/vite-plugin-kit-routes/src/lib/fs.spec.ts @@ -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/+page@.svelte", + "lay/(layVerySpecial)/skip/+page@lay.svelte", + "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/+page@.svelte", + "lay/skip/+page@lay.svelte", + "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", diff --git a/packages/vite-plugin-kit-routes/src/lib/plugin.ts b/packages/vite-plugin-kit-routes/src/lib/plugin.ts index e0978c9eb..cd39debd8 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugin.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugin.ts @@ -72,11 +72,11 @@ export type Options = { * 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 @@ -218,16 +218,19 @@ 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 } @@ -235,6 +238,11 @@ 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 } diff --git a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts index 918157c43..c8cd6e124 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts @@ -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: [], @@ -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') { @@ -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') { diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts index 575b85dec..d0430318f 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts @@ -130,7 +130,7 @@ type AllTypes = typeof AllObjs export function route>(key: T, ...params: FunctionParams): string export function route>(key: T): string export function route(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 { diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables.ts index f40c08248..1052d52d7 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables.ts @@ -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 })}` } /** diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables_shortened.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables_shortened.ts index e010a4977..bd25bcb2d 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables_shortened.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-format-variables_shortened.ts @@ -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 })}` } /** diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path.ts index 9360eb12e..0795ecca9 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path.ts @@ -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 })}` } } diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path_shortened.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path_shortened.ts index db56e4e67..ae1ef5b51 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path_shortened.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-path_shortened.ts @@ -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 })}` } } diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol.ts index 2c9e67928..9a102cb84 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol.ts @@ -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 })}` } } diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol_shortened.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol_shortened.ts index dfda562b1..9f1d1a4a0 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol_shortened.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-object-symbol_shortened.ts @@ -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 })}` } } diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path.ts index d00dd587c..d956c4ae1 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path.ts @@ -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 })}` } } @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs export function route>(key: T, ...params: FunctionParams): string export function route>(key: T): string export function route(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 { diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path_shortened.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path_shortened.ts index fa383a3ed..195ac5449 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path_shortened.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-path_shortened.ts @@ -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 })}` } } @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs export function route>(key: T, ...params: FunctionParams): string export function route>(key: T): string export function route(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 { diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol.ts index f947f961a..d897b4875 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol.ts @@ -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 })}` } } @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs export function route>(key: T, ...params: FunctionParams): string export function route>(key: T): string export function route(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 { diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol_shortened.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol_shortened.ts index 7522d07d8..6a9fdad3a 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol_shortened.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_format-route-symbol_shortened.ts @@ -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 })}` } } @@ -143,7 +143,7 @@ type AllTypes = typeof AllObjs export function route>(key: T, ...params: FunctionParams): string export function route>(key: T): string export function route(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 { diff --git a/packages/vite-plugin-kit-routes/src/test/ROUTES_post-update.ts b/packages/vite-plugin-kit-routes/src/test/ROUTES_post-update.ts index 94c47b41f..8a62f16fd 100644 --- a/packages/vite-plugin-kit-routes/src/test/ROUTES_post-update.ts +++ b/packages/vite-plugin-kit-routes/src/test/ROUTES_post-update.ts @@ -129,7 +129,7 @@ type AllTypes = typeof AllObjs export function route>(key: T, ...params: FunctionParams): string export function route>(key: T): string export function route(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 {