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/soft-lions-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-kit-routes': patch
---

fix: default params in LINKS are now taken into account
32 changes: 17 additions & 15 deletions packages/vite-plugin-kit-routes/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}` : ''}/site${appendSp({ limit: params.limit })}`
},
lang_site_id: (
params: { lang?: 'fr' | 'hu' | undefined; id?: string; limit?: number; demo?: string } = {},
params: { lang?: 'fr' | 'hu' | undefined; id?: number; limit?: number; demo?: string } = {},
) => {
params.lang = params.lang ?? 'fr'
params.id = params.id ?? '7'
params.id = params.id ?? 7
return `${params?.lang ? `/${params?.lang}` : ''}/site/${params.id}${appendSp({
limit: params.limit,
demo: params.demo,
Expand Down Expand Up @@ -96,6 +96,7 @@ export const ACTIONS = {
extra?: 'A' | 'B'
},
) => {
params.extra = params.extra ?? 'A'
return `${params?.lang ? `/${params?.lang}` : ''}/site_contract/${params.siteId}-${
params.contractId
}?/${action}${appendSp({ extra: params.extra })}`
Expand All @@ -104,13 +105,13 @@ export const ACTIONS = {

export const LINKS = {
twitter: `https:/twitter.com/jycouet`,
mailto: (params: { email: string | number }) => {
return `mailto:${params.email}`
twitter_post: (params: { name: string | number; id: string | number }) => {
return `https:/twitter.com/${params.name}/status/${params.id}`
},
twitter_post: (params: { name: string | number; id: string | number; limit?: number }) => {
return `https:/twitter.com/${params.name}/status/${params.id}${appendSp({
limit: params.limit,
})}`
gravatar: (params: { id: string; s?: number; d?: 'retro' | 'identicon' }) => {
params.s = params.s ?? 75
params.d = params.d ?? 'identicon'
return `https:/www.gravatar.com/avatar/${params.id}${appendSp({ s: params.s, d: params.d })}`
},
}

Expand Down Expand Up @@ -146,24 +147,24 @@ export type KIT_ROUTES = {
PAGES: {
_ROOT: never
subGroup: never
subGroup2: 'first'
subGroup2: never
lang_contract: 'lang'
lang_contract_id: 'lang' | 'id'
lang_gp_one: 'lang'
lang_gp_two: 'lang'
lang_main: 'lang'
lang_match_id_int: 'lang' | 'id'
lang_site: 'lang' | 'limit'
lang_site_id: 'lang' | 'id' | 'limit' | 'demo'
lang_site_contract_siteId_contractId: 'lang' | 'siteId' | 'contractId' | 'limit'
lang_site: 'lang'
lang_site_id: 'lang' | 'id'
lang_site_contract_siteId_contractId: 'lang' | 'siteId' | 'contractId'
}
SERVERS: { lang_contract: 'lang'; lang_site: 'lang'; api_graphql: never }
ACTIONS: {
lang_contract_id: 'lang' | 'id'
lang_site: 'lang'
lang_site_contract_siteId_contractId: 'lang' | 'siteId' | 'contractId' | 'extra'
lang_site_contract_siteId_contractId: 'lang' | 'siteId' | 'contractId'
}
LINKS: { twitter: never; mailto: 'email'; twitter_post: 'name' | 'id' | 'limit' }
LINKS: { twitter: never; twitter_post: 'name' | 'id'; gravatar: 'id' }
Params: {
first: never
lang: never
Expand All @@ -173,7 +174,8 @@ export type KIT_ROUTES = {
siteId: never
contractId: never
extra: never
email: never
name: never
s: never
d: never
}
}
11 changes: 4 additions & 7 deletions packages/vite-plugin-kit-routes/src/lib/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export function read(pathFile: string) {

export function write(pathFile: string, data: string[]) {
const fullDataToWrite = Array.isArray(data) ? data.join('\n') : data
createFolderIfNotExists(dirname(pathFile))

// createFolderIfNotExists
mkdirSync(dirname(pathFile), { recursive: true })

// Don't write if nothing changed!
if (existsSync(pathFile)) {
const currentFileData = read(pathFile)
Expand All @@ -21,9 +24,3 @@ export function write(pathFile: string, data: string[]) {
writeFileSync(join(pathFile), fullDataToWrite)
return true
}

function createFolderIfNotExists(dir: string) {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true })
}
}
67 changes: 52 additions & 15 deletions packages/vite-plugin-kit-routes/src/lib/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse } from '@babel/parser'
import { green, Log, red, yellow } from '@kitql/helpers'
import { gray, green, Log, red, yellow } from '@kitql/helpers'
import { readdirSync } from 'fs'
import { spawn } from 'node:child_process'
import * as recast from 'recast'
Expand Down Expand Up @@ -73,15 +73,18 @@ export type Options<T extends ExtendTypes = ExtendTypes> = {
* // ✅ <a href={LINKS.twitter}>Twitter</a>
*
* // reference to link with params! (Like svelteKit routes add [ ] to specify params)
* mailto: 'mailto:[email]',
* // ✅ <a href={LINKS.mailto({ email: 'me@super.dev' })}>Mail</a>
* twitter_post: 'https://twitter.com/[name]/status/[id]',
* // ✅ <a href={LINKS.twitter_post({ name: 'jycouet', id: '1727089217707159569' })}>Twitter Post</a>
*
* // reference to link with params & search params!
* twitter_post: {
* href: 'https://twitter.com/[name]/status/[id]',
* explicit_search_params: { limit: { type: 'number' } }
* }
* // ✅ <a href={LINKS.twitter_post({ name: 'jycouet', id: '1727089217707159569', limit: 12 })}>Twitter Post</a>
* gravatar: {
* href: 'https://www.gravatar.com/avatar/[id]',
* explicit_search_params: {
* s: { type: 'number', default: 75 },
* d: { type: '"retro" | "identicon"', default: '"identicon"' },
* },
* },
* // ✅ <img src={LINKS.gravatar({ id: 'jycouet', s: 20 })} alt="logo" />
* }
* }
* ```
Expand Down Expand Up @@ -112,7 +115,7 @@ export type CustomPath<Params extends string | never = string> = {
* limit: { // name of the search param
* required?: true | false, // default: false
* type: 'number', // default: 'string | number'
* default: '12', // default: undefined
* default: 12, // default: undefined
* }
* }
*/
Expand All @@ -123,7 +126,7 @@ export type CustomPath<Params extends string | never = string> = {
* params {
* id: { // name of the param (if you set the plugin `kitRoutes<KIT_ROUTES>`, it will be typed!)
* type: 'number', // default: 'string | number'
* default: '12', // default: undefined
* default: 12, // default: undefined
* }
* }
*/
Expand All @@ -148,7 +151,15 @@ export type OverrideParam = {

export type ExtendParam = {
type?: string
default?: string
/**
* You have to double escape strings.
*
* @example
* { type: 'number', default: 75 }
* of
* { type: 'string', default: '"jycouet"' }
*/
default?: any
}

export type ExplicitSearchParam = ExtendParam & {
Expand Down Expand Up @@ -243,7 +254,14 @@ const getFileKeys = (
}[]
}

type Param = { name: string; optional: boolean; matcher?: string; type?: string; default?: string }
type Param = {
name: string
optional: boolean
matcher?: string
type?: string
default?: any
fromPath?: boolean
}

export const fileToMetadata = (
original: string,
Expand Down Expand Up @@ -334,7 +352,12 @@ export const fileToMetadata = (
const explicit_search_params_to_function: string[] = []
if (customConf.explicit_search_params) {
Object.entries(customConf.explicit_search_params).forEach(sp => {
paramsFromPath.push({ name: sp[0], optional: !sp[1].required, type: sp[1].type })
paramsFromPath.push({
name: sp[0],
optional: !sp[1].required,
type: sp[1].type,
default: sp[1].default,
})
explicit_search_params_to_function.push(`${sp[0]}: params.${sp[0]}`)
})
}
Expand Down Expand Up @@ -374,7 +397,7 @@ export const fileToMetadata = (
// }
// }

return `params.${c.name} = params.${c.name} ?? ${additionalByStore}'${c.default}'; `
return `params.${c.name} = params.${c.name} ?? ${additionalByStore}${c.default}; `
})

let prop = ''
Expand Down Expand Up @@ -405,11 +428,13 @@ export function extractParamsFromPath(path: string): Param[] {
name: matcher[0],
optional: isOptional,
matcher: matcher[1],
fromPath: true,
})
} else {
params.push({
name: match[1],
optional: isOptional,
fromPath: true,
})
}
}
Expand Down Expand Up @@ -602,9 +627,10 @@ ${objTypes
return ` ${c.type}: { ${c.files
.map(d => {
return `'${d.keyToUse}': ${
d.paramsFromPath.length === 0
d.paramsFromPath.filter(e => e.fromPath === true).length === 0
? 'never'
: d.paramsFromPath
.filter(e => e.fromPath === true)
.map(e => {
return `'${e.name}'`
})
Expand Down Expand Up @@ -711,6 +737,17 @@ ${objTypes
child.on('close', code => {
if (result) {
log.success(`${yellow(generated_file_path(options))} updated`)
// TODO later
// log.info(
// `⚠️ Warning ${yellow(`href="/about"`)} detected ` +
// `in ${gray('/src/lib/component/menu.svelte')} is not safe. ` +
// `You could use: ${green(`href={PAGES['/about']}`)}`,
// )
// log.info(
// `⚠️ Warning ${yellow(`action="?/save"`)} detected ` +
// `in ${gray('/routes/card/+page.svelte')} is not safe. ` +
// `You could use: ${green(`href={ACTION['/card']('save')}`)}`,
// )
}
})
} else {
Expand Down
Loading