diff --git a/.changeset/popular-squids-compare.md b/.changeset/popular-squids-compare.md new file mode 100644 index 00000000..87e9902e --- /dev/null +++ b/.changeset/popular-squids-compare.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-kit-routes': patch +--- + +when we have only 1 method, let's not add the arg diff --git a/.changeset/warm-melons-argue.md b/.changeset/warm-melons-argue.md new file mode 100644 index 00000000..69ca5f23 --- /dev/null +++ b/.changeset/warm-melons-argue.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-kit-routes': patch +--- + +using nicer keys by default diff --git a/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts b/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts index 46e44219..0fd15dc9 100644 --- a/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts +++ b/packages/vite-plugin-kit-routes/src/lib/ROUTES.ts @@ -1,36 +1,48 @@ export const PAGES = { - '/': (sp?: Record) => { + _ROOT: (sp?: Record) => { return `/${appendSp(sp)}` }, - '/site/[id]': (params: { id: string }, sp?: Record) => { + contract: (sp?: Record) => { + return `/contract${appendSp(sp)}` + }, + contract_id: (params: { id: string }, sp?: Record) => { + return `/contract/${params.id}${appendSp(sp)}` + }, + site: (sp?: Record) => { + return `/site${appendSp(sp)}` + }, + site_id: (params: { id: string }, sp?: Record) => { return `/site/${params.id}${appendSp(sp)}` }, - '/site/[param]/[yop]': (params: { param: string; yop: string }, sp?: Record) => { - return `/site/${params.param}/${params.yop}${appendSp(sp)}` + site_contract_siteId_contractId: ( + params: { siteId: string; contractId: string }, + sp?: Record, + ) => { + return `/site_contract/${params.siteId}-${params.contractId}${appendSp(sp)}` }, } export const SERVERS = { - '/site/[id]/one': ( - method: 'GET' | 'POST', - params: { id: string }, - sp?: Record, - ) => { - return `/site/${params.id}/one${appendSp(sp)}` + contract: (method: 'GET' | 'POST', sp?: Record) => { + return `/contract${appendSp(sp)}` + }, + site: (method: 'GET', sp?: Record) => { + return `/site${appendSp(sp)}` }, } export const ACTIONS = { - '/site/[id]/two': (params: { id: string }) => { - return `/site/${params.id}/two` + contract_id: (params: { id: string }) => { + return `/contract/${params.id}` + }, + site: (action: 'action1' | 'action2') => { + return `/site?/${action}` }, - '/site/[id]/two/[hello]': ( - action: 'default' | 'login' | 'register', - params: { id: string; hello: string }, + site_contract_siteId_contractId: ( + action: 'sendSomething', + params: { siteId: string; contractId: string }, ) => { - return `/site/${params.id}/two/${params.hello}${ - String(action) === 'default' ? '' : `?/${action}` - }` + return `/site_contract/${params.siteId}-${params.contractId}?/${action}` }, } diff --git a/packages/vite-plugin-kit-routes/src/lib/index.ts b/packages/vite-plugin-kit-routes/src/lib/index.ts index 715f7259..6bac8ff6 100644 --- a/packages/vite-plugin-kit-routes/src/lib/index.ts +++ b/packages/vite-plugin-kit-routes/src/lib/index.ts @@ -24,10 +24,44 @@ export type Options = { * @default 'src/lib/ROUTES.ts' */ generated_file_path?: string + + /** + * when `false` _(default)_, object keys look like this: `site_id_two_hello` + * + * when `true`, object keys look like this: `/site/[id]/two/[hello]` + */ + keep_path_param_format?: boolean } -function generated_file_path(params?: Options) { - return params?.generated_file_path ?? 'src/lib/ROUTES.ts' +function generated_file_path(options?: Options) { + return options?.generated_file_path ?? 'src/lib/ROUTES.ts' +} + +export function formatKey(key: string, options?: Options) { + if (options?.keep_path_param_format) { + return key + } + + const toReplace = ['/', '[', ']', '(', ')', '-'] + let toRet = key + .split('') + .map(c => (toReplace.includes(c) ? '_' : c)) + .join('') + .replaceAll('__', '_') + .replaceAll('__', '_') + .replaceAll('__', '_') + if (toRet.startsWith('_')) { + toRet = toRet.slice(1) + } + if (toRet.endsWith('_')) { + toRet = toRet.slice(0, -1) + } + + if (toRet === '') { + toRet = '_ROOT' + } + + return toRet } function routes_path() { @@ -37,11 +71,21 @@ function routes_path() { // const routes_path = 'src/lib/ROUTES.ts' const log = new Log('Kit Routes') -const getFiles = (lookFor: '+page.svelte' | '+page.server.ts' | '+server.ts') => { +const getFileKeys = ( + lookFor: '+page.svelte' | '+page.server.ts' | '+server.ts', + options?: Options, +) => { const files = readdirSync(routes_path(), { recursive: true }) as string[] - return files - .filter(file => file.endsWith(lookFor)) - .map(file => `/` + file.replace(`/${lookFor}`, '').replace(lookFor, '')) + return ( + files + .filter(file => file.endsWith(lookFor)) + .map(file => `/` + file.replace(`/${lookFor}`, '').replace(lookFor, '')) + // Keep the sorting at this level, it will make more sense + .sort() + .map(file => { + return { toUse: formatKey(file, options), original: file } + }) + ) } function formatExtractParamsFromPath(file_path: string) { @@ -119,24 +163,24 @@ const getActionsOfServerPages = (path: string) => { return exportedNames } -const run = (params?: Options) => { - const files_pages = getFiles('+page.svelte') - const files_server_pages = getFiles('+page.server.ts') - const files_server = getFiles('+server.ts') +const run = (options?: Options) => { + const pages = getFileKeys('+page.svelte', options) + const server = getFileKeys('+page.server.ts', options) + const files_server = getFileKeys('+server.ts', options) - const result = write(generated_file_path(params), [ + const result = write(generated_file_path(options), [ `export const PAGES = { - ${files_pages - .map(file_path => { + ${pages + .map(key => { const params = [] - const pFormPath = formatExtractParamsFromPath(file_path) + const pFormPath = formatExtractParamsFromPath(key.original) if (pFormPath.length > 0) { params.push(`params: {${pFormPath}}`) } params.push(`sp?: Record`) return ( - `"${file_path}": (${params.join(', ')}) => ` + - `{ return \`${file_path + `"${key.toUse}": (${params.join(', ')}) => ` + + `{ return \`${key.original .replaceAll('[', '${params.') .replaceAll(']', '}')}\${appendSp(sp)}\` }` ) @@ -146,21 +190,20 @@ const run = (params?: Options) => { export const SERVERS = { ${files_server - .map(file_path => { + .map(key => { const params = [] - params.push( - `method: ${getMethodsOfServerFiles(file_path) - .map(c => `'${c}'`) - .join(' | ')}`, - ) - const pFormPath = formatExtractParamsFromPath(file_path) + const methods = getMethodsOfServerFiles(key.original) + if (methods.length > 0) { + params.push(`method: ${methods.map(c => `'${c}'`).join(' | ')}`) + } + const pFormPath = formatExtractParamsFromPath(key.original) if (pFormPath.length > 0) { params.push(`params: {${pFormPath}}`) } params.push(`sp?: Record`) return ( - `"${file_path}": (${params.join(', ')}) => ` + - `{ return \`${file_path + `"${key.toUse}": (${params.join(', ')}) => ` + + `{ return \`${key.original .replaceAll('[', '${params.') .replaceAll(']', '}')}\${appendSp(sp)}\` }` ) @@ -169,27 +212,28 @@ export const SERVERS = { } export const ACTIONS = { - ${files_server_pages - .map(file_path => { + ${server + .map(key => { const params = [] - const actions = getActionsOfServerPages(file_path) - let actionsSpecified = false + const actions = getActionsOfServerPages(key.original) + let actionsFormat = '' if (actions.length === 0) { - // Don't do anything... } else if (actions.length === 1 && actions[0] === 'default') { - // Don't do anything... } else { params.push(`action: ${actions.map(c => `'${c}'`).join(' | ')}`) - actionsSpecified = true + actionsFormat = `?/\${action}` + // actionsFormat = `coucou` } - const pFormPath = formatExtractParamsFromPath(file_path) + const pFormPath = formatExtractParamsFromPath(key.original) if (pFormPath.length > 0) { params.push(`params: {${pFormPath}}`) } return ( - `"${file_path}": (${params.join(', ')}) => ` + - `{ return \`${file_path.replaceAll('[', '${params.').replaceAll(']', '}')}` + - `${actionsSpecified ? `\${String(action) === 'default' ? '' : \`?/\${action}\`}` : ``}\` }` + `"${key.toUse}": (${params.join(', ')}) => ` + + `{ return \`` + + `${key.original.replaceAll('[', '${params.').replaceAll(']', '}')}` + + `${actionsFormat}` + + `\`}` ) }) .join(',\n ')} @@ -203,9 +247,9 @@ const appendSp = (sp?: Record) => { ]) // TODO: optimize this later. We want to write the new file only if different after prettier?! (having a tmp file somewhere?) - if (params?.post_update_run) { - log.info(`${yellow(`post_update_run`)} "${green(params?.post_update_run)}" running...`) - const child = spawn(params.post_update_run, { shell: true }) + if (options?.post_update_run) { + log.info(`${yellow(`post_update_run`)} "${green(options?.post_update_run)}" running...`) + const child = spawn(options.post_update_run, { shell: true }) child.stdout.on('data', data => { if (data.toString()) { log.info(data.toString()) @@ -216,23 +260,23 @@ const appendSp = (sp?: Record) => { }) child.on('close', code => { if (result) { - log.success(`${yellow(generated_file_path(params))} updated`) + log.success(`${yellow(generated_file_path(options))} updated`) } }) } else { if (result) { - log.success(`${yellow(generated_file_path(params))} updated`) + log.success(`${yellow(generated_file_path(options))} updated`) } } } -export function kitRoutes(params?: Options): Plugin[] { +export function kitRoutes(options?: Options): Plugin[] { return [ // Run the thing at startup { name: 'kit-routes', configureServer() { - run(params) + run(options) }, }, @@ -242,7 +286,7 @@ export function kitRoutes(params?: Options): Plugin[] { name: 'kit-routes-watch', logs: [], watch: ['**/+page.svelte', '**/+page.server.ts', '**/+server.ts'], - run: () => run(params), + run: () => run(options), }, ]), ] 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 83eae417..74eaf4d4 100644 --- a/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts +++ b/packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { extractParamsFromPath } from './index.js' +import { extractParamsFromPath, formatKey } from './index.js' describe('vite-plugin-kit-routes', () => { it('get id', async () => { @@ -28,4 +28,20 @@ describe('vite-plugin-kit-routes', () => { ] `) }) + + it('formatKey', async () => { + expect(formatKey('/[param]site/[yop](group)/[id]')).toMatchInlineSnapshot( + '"param_site_yop_group_id"', + ) + }) + + it('formatKey', async () => { + expect( + formatKey('/[param]site/[yop](group)/[id]', { keep_path_param_format: true }), + ).toMatchInlineSnapshot('"/[param]site/[yop](group)/[id]"') + }) + + it('formatKey', async () => { + expect(formatKey('/')).toMatchInlineSnapshot('"_ROOT"') + }) }) diff --git a/packages/vite-plugin-kit-routes/src/routes/+layout.svelte b/packages/vite-plugin-kit-routes/src/routes/+layout.svelte index 6b566c82..edb17d01 100644 --- a/packages/vite-plugin-kit-routes/src/routes/+layout.svelte +++ b/packages/vite-plugin-kit-routes/src/routes/+layout.svelte @@ -1,8 +1,5 @@ @@ -14,10 +11,14 @@

diff --git a/packages/vite-plugin-kit-routes/src/routes/contract/+page.svelte b/packages/vite-plugin-kit-routes/src/routes/contract/+page.svelte new file mode 100644 index 00000000..25199476 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/contract/+page.svelte @@ -0,0 +1 @@ +

Contracts

diff --git a/packages/vite-plugin-kit-routes/src/routes/site/[id]/one/+server.ts b/packages/vite-plugin-kit-routes/src/routes/contract/+server.ts similarity index 61% rename from packages/vite-plugin-kit-routes/src/routes/site/[id]/one/+server.ts rename to packages/vite-plugin-kit-routes/src/routes/contract/+server.ts index 54f73a0c..f4e3ac28 100644 --- a/packages/vite-plugin-kit-routes/src/routes/site/[id]/one/+server.ts +++ b/packages/vite-plugin-kit-routes/src/routes/contract/+server.ts @@ -1,9 +1,9 @@ -import type { RequestHandler } from './$types' +import type { RequestHandler } from './$types.d.ts' export const GET: RequestHandler = async () => { return new Response() } export const POST: RequestHandler = () => { - // do something + return new Response() } diff --git a/packages/vite-plugin-kit-routes/src/routes/contract/[id]/+page.server.ts b/packages/vite-plugin-kit-routes/src/routes/contract/[id]/+page.server.ts new file mode 100644 index 00000000..ecffda80 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/contract/[id]/+page.server.ts @@ -0,0 +1,5 @@ +import type { Actions } from './$types.d.ts' + +export const actions = { + default: async () => {}, +} satisfies Actions diff --git a/packages/vite-plugin-kit-routes/src/routes/contract/[id]/+page.svelte b/packages/vite-plugin-kit-routes/src/routes/contract/[id]/+page.svelte new file mode 100644 index 00000000..8ffbea39 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/contract/[id]/+page.svelte @@ -0,0 +1 @@ +

Contract [id]

diff --git a/packages/vite-plugin-kit-routes/src/routes/site/+page.server.ts b/packages/vite-plugin-kit-routes/src/routes/site/+page.server.ts new file mode 100644 index 00000000..ce64d4fa --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/site/+page.server.ts @@ -0,0 +1,6 @@ +import type { Actions } from './$types.d.ts' + +export const actions = { + action1: async () => {}, + action2: async () => {}, +} satisfies Actions diff --git a/packages/vite-plugin-kit-routes/src/routes/site/+page.svelte b/packages/vite-plugin-kit-routes/src/routes/site/+page.svelte new file mode 100644 index 00000000..c10c6a1a --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/site/+page.svelte @@ -0,0 +1 @@ +

Sites

diff --git a/packages/vite-plugin-kit-routes/src/routes/site/+server.ts b/packages/vite-plugin-kit-routes/src/routes/site/+server.ts new file mode 100644 index 00000000..21d3e019 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/site/+server.ts @@ -0,0 +1,5 @@ +import type { RequestHandler } from './$types.d.ts' + +export const GET: RequestHandler = async () => { + return new Response() +} diff --git a/packages/vite-plugin-kit-routes/src/routes/site/[id]/two/+page.server.ts b/packages/vite-plugin-kit-routes/src/routes/site/[id]/two/+page.server.ts deleted file mode 100644 index b7b038d6..00000000 --- a/packages/vite-plugin-kit-routes/src/routes/site/[id]/two/+page.server.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { Actions } from './$types' - -export const actions = { - default: async event => { - // TODO log the user in - }, -} satisfies Actions diff --git a/packages/vite-plugin-kit-routes/src/routes/site/[id]/two/[hello]/+page.server.ts b/packages/vite-plugin-kit-routes/src/routes/site/[id]/two/[hello]/+page.server.ts deleted file mode 100644 index e0d460db..00000000 --- a/packages/vite-plugin-kit-routes/src/routes/site/[id]/two/[hello]/+page.server.ts +++ /dev/null @@ -1,13 +0,0 @@ -import type { Actions } from './$types' - -export const actions = { - default: async event => { - // TODO log the user in - }, - login: async event => { - // TODO log the user in - }, - register: async event => { - // TODO register the user - }, -} satisfies Actions diff --git a/packages/vite-plugin-kit-routes/src/routes/site/[param]/[yop]/+page.svelte b/packages/vite-plugin-kit-routes/src/routes/site/[param]/[yop]/+page.svelte deleted file mode 100644 index 6779db80..00000000 --- a/packages/vite-plugin-kit-routes/src/routes/site/[param]/[yop]/+page.svelte +++ /dev/null @@ -1 +0,0 @@ -

site [param] [yop]

diff --git a/packages/vite-plugin-kit-routes/src/routes/site_contract/[siteId]-[contractId]/+page.server.ts b/packages/vite-plugin-kit-routes/src/routes/site_contract/[siteId]-[contractId]/+page.server.ts new file mode 100644 index 00000000..083e7a4d --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/site_contract/[siteId]-[contractId]/+page.server.ts @@ -0,0 +1,11 @@ +import type { Actions } from './$types.d.ts' + +export const actions = { + sendSomething: async () => { + return { + body: { + message: 'Yes, you sent me something! ✨ Thank you!', + }, + } + }, +} satisfies Actions diff --git a/packages/vite-plugin-kit-routes/src/routes/site_contract/[siteId]-[contractId]/+page.svelte b/packages/vite-plugin-kit-routes/src/routes/site_contract/[siteId]-[contractId]/+page.svelte new file mode 100644 index 00000000..4e377ba6 --- /dev/null +++ b/packages/vite-plugin-kit-routes/src/routes/site_contract/[siteId]-[contractId]/+page.svelte @@ -0,0 +1,19 @@ + + +

Site & Contract [siteId] - [Contract]

+
+ Action used:
{action}
+ +
+ +
{JSON.stringify(form)}