Skip to content

Commit

Permalink
extract string or undefined helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fehnomenal committed Dec 30, 2023
1 parent 23f3eb3 commit 8d671f2
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 63 deletions.
20 changes: 14 additions & 6 deletions packages/vite-plugin-kit-routes/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const PAGES = {
) => {
return `${params?.lang ? `/${params?.lang}` : ''}/site${appendSp({
...sp,
limit: params?.limit !== undefined ? String(params?.limit) : undefined,
limit: StringOrUndefined(params?.limit),
})}`
},
'/site/[id]': (params?: {
Expand All @@ -61,8 +61,8 @@ const PAGES = {
params.lang = params.lang ?? 'fr'
params.id = params.id ?? 'Vienna'
return `${params?.lang ? `/${params?.lang}` : ''}/site/${params.id}${appendSp({
limit: params.limit !== undefined ? String(params.limit) : undefined,
demo: params.demo !== undefined ? String(params.demo) : undefined,
limit: StringOrUndefined(params.limit),
demo: StringOrUndefined(params.demo),
})}`
},
'/site_contract/[siteId]-[contractId]': (params: {
Expand All @@ -73,7 +73,7 @@ const PAGES = {
}) => {
return `${params?.lang ? `/${params?.lang}` : ''}/site_contract/${params.siteId}-${
params.contractId
}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
'/a/[...rest]/z': (params: { rest: (string | number)[] }) => {
return `/a/${params.rest?.join('/')}/z`
Expand Down Expand Up @@ -124,15 +124,15 @@ const ACTIONS = {
limit?: number
}) => {
return `${params?.lang ? `/${params?.lang}` : ''}/contract/${params.id}${appendSp({
limit: params.limit !== undefined ? String(params.limit) : undefined,
limit: StringOrUndefined(params.limit),
})}`
},
'create /site': (params?: {
lang?: 'fr' | 'en' | 'hu' | 'at' | string
redirectTo?: 'list' | 'new' | 'detail'
}) => {
return `${params?.lang ? `/${params?.lang}` : ''}/site?/create${appendSp(
{ redirectTo: params?.redirectTo !== undefined ? String(params?.redirectTo) : undefined },
{ redirectTo: StringOrUndefined(params?.redirectTo) },
'&',
)}`
},
Expand Down Expand Up @@ -234,6 +234,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

// route function helpers
type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
type FunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]
Expand Down
8 changes: 8 additions & 0 deletions packages/vite-plugin-kit-routes/src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export const currentSp = () => {
record[key] = value
}
return record
}
function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}
return String(val)
}`

export const routeFn = `// route function helpers
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-kit-routes/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ function getSpValue(rawValue: string, param: ExplicitSearchParam) {
return `String(${rawValue})`
}

return `${rawValue} !== undefined ? String(${rawValue}) : undefined`
return `StringOrUndefined(${rawValue})`
}

return rawValue
Expand Down
8 changes: 8 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 @@ -157,6 +157,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

// route function helpers
type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
type FunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}`: ''}/match/${params.id}`
},
"/site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }, sp?: Record<string, string | number>) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: StringOrUndefined(params?.limit) })}`
},
"/site/[id]": (params?: { lang?: ('fr' | 'hu' | undefined), id?: (string), limit?: (number), demo?: (string) }) => {
params = params ?? {}
params.lang = params.lang ?? "fr";
params.id = params.id ?? "Vienna";
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined, demo: params.demo !== undefined ? String(params.demo) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: StringOrUndefined(params.limit), demo: StringOrUndefined(params.demo) })}`
},
"/site_contract/[siteId]-[contractId]": (params: { siteId: (string | number), contractId: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"/a/[...rest]/z": (params: { rest: (string | number)[] }) => {
return `/a/${params.rest?.join('/')}/z`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SERVERS = {
*/
export const ACTIONS = {
"default /contract/[id]": (params: { id: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${params.id}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"create /site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site?/create`
Expand Down Expand Up @@ -170,6 +170,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

/**
* Add this type as a generic of the vite plugin `kitRoutes<KIT_ROUTES>`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}`: ''}/match/${id}`
},
"/site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }, sp?: Record<string, string | number>) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: StringOrUndefined(params?.limit) })}`
},
"/site/[id]": (params?: { lang?: ('fr' | 'hu' | undefined), id?: (string), limit?: (number), demo?: (string) }) => {
params = params ?? {}
params.lang = params.lang ?? "fr";
params.id = params.id ?? "Vienna";
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined, demo: params.demo !== undefined ? String(params.demo) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: StringOrUndefined(params.limit), demo: StringOrUndefined(params.demo) })}`
},
"/site_contract/[siteId]-[contractId]": (params: { siteId: (string | number), contractId: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"/a/[...rest]/z": (rest: (string | number)[], params?: { }) => {
return `/a/${rest?.join('/')}/z`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SERVERS = {
*/
export const ACTIONS = {
"default /contract/[id]": (id: (string | number), params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${id}${appendSp({ limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${id}${appendSp({ limit: StringOrUndefined(params?.limit) })}`
},
"create /site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site?/create`
Expand Down Expand Up @@ -171,6 +171,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

/**
* Add this type as a generic of the vite plugin `kitRoutes<KIT_ROUTES>`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}`: ''}/match/${params.id}`
},
"site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }, sp?: Record<string, string | number>) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: StringOrUndefined(params?.limit) })}`
},
"site_id": (params?: { lang?: ('fr' | 'hu' | undefined), id?: (string), limit?: (number), demo?: (string) }) => {
params = params ?? {}
params.lang = params.lang ?? "fr";
params.id = params.id ?? "Vienna";
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined, demo: params.demo !== undefined ? String(params.demo) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: StringOrUndefined(params.limit), demo: StringOrUndefined(params.demo) })}`
},
"site_contract_siteId_contractId": (params: { siteId: (string | number), contractId: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"a_rest_z": (params: { rest: (string | number)[] }) => {
return `/a/${params.rest?.join('/')}/z`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SERVERS = {
*/
export const ACTIONS = {
"default_contract_id": (params: { id: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${params.id}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"create_site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site?/create`
Expand Down Expand Up @@ -170,6 +170,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

/**
* Add this type as a generic of the vite plugin `kitRoutes<KIT_ROUTES>`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}`: ''}/match/${id}`
},
"site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }, sp?: Record<string, string | number>) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: StringOrUndefined(params?.limit) })}`
},
"site_id": (params?: { lang?: ('fr' | 'hu' | undefined), id?: (string), limit?: (number), demo?: (string) }) => {
params = params ?? {}
params.lang = params.lang ?? "fr";
params.id = params.id ?? "Vienna";
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined, demo: params.demo !== undefined ? String(params.demo) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: StringOrUndefined(params.limit), demo: StringOrUndefined(params.demo) })}`
},
"site_contract_siteId_contractId": (params: { siteId: (string | number), contractId: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"a_rest_z": (rest: (string | number)[], params?: { }) => {
return `/a/${rest?.join('/')}/z`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SERVERS = {
*/
export const ACTIONS = {
"default_contract_id": (id: (string | number), params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${id}${appendSp({ limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${id}${appendSp({ limit: StringOrUndefined(params?.limit) })}`
},
"create_site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site?/create`
Expand Down Expand Up @@ -171,6 +171,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

/**
* Add this type as a generic of the vite plugin `kitRoutes<KIT_ROUTES>`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}`: ''}/match/${params.id}`
},
"/site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }, sp?: Record<string, string | number>) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: StringOrUndefined(params?.limit) })}`
},
"/site/[id]": (params?: { lang?: ('fr' | 'hu' | undefined), id?: (string), limit?: (number), demo?: (string) }) => {
params = params ?? {}
params.lang = params.lang ?? "fr";
params.id = params.id ?? "Vienna";
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined, demo: params.demo !== undefined ? String(params.demo) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: StringOrUndefined(params.limit), demo: StringOrUndefined(params.demo) })}`
},
"/site_contract/[siteId]-[contractId]": (params: { siteId: (string | number), contractId: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"/a/[...rest]/z": (params: { rest: (string | number)[] }) => {
return `/a/${params.rest?.join('/')}/z`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SERVERS = {
*/
export const ACTIONS = {
"default /contract/[id]": (params: { id: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${params.id}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"create /site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site?/create`
Expand Down Expand Up @@ -170,6 +170,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

// route function helpers
type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
type FunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export const PAGES = {
return `${params?.lang ? `/${params?.lang}`: ''}/match/${id}`
},
"/site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }, sp?: Record<string, string | number>) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site${appendSp({ ...sp, limit: StringOrUndefined(params?.limit) })}`
},
"/site/[id]": (params?: { lang?: ('fr' | 'hu' | undefined), id?: (string), limit?: (number), demo?: (string) }) => {
params = params ?? {}
params.lang = params.lang ?? "fr";
params.id = params.id ?? "Vienna";
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined, demo: params.demo !== undefined ? String(params.demo) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site/${params.id}${appendSp({ limit: StringOrUndefined(params.limit), demo: StringOrUndefined(params.demo) })}`
},
"/site_contract/[siteId]-[contractId]": (params: { siteId: (string | number), contractId: (string | number), lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: params.limit !== undefined ? String(params.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/site_contract/${params.siteId}-${params.contractId}${appendSp({ limit: StringOrUndefined(params.limit) })}`
},
"/a/[...rest]/z": (rest: (string | number)[], params?: { }) => {
return `/a/${rest?.join('/')}/z`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const SERVERS = {
*/
export const ACTIONS = {
"default /contract/[id]": (id: (string | number), params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string), limit?: (number) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${id}${appendSp({ limit: params?.limit !== undefined ? String(params?.limit) : undefined })}`
return `${params?.lang ? `/${params?.lang}`: ''}/contract/${id}${appendSp({ limit: StringOrUndefined(params?.limit) })}`
},
"create /site": (params?: { lang?: ('fr' | 'en' | 'hu' | 'at' | string) }) => {
return `${params?.lang ? `/${params?.lang}`: ''}/site?/create`
Expand Down Expand Up @@ -171,6 +171,14 @@ export const currentSp = () => {
return record
}

function StringOrUndefined(val: any) {
if (val === undefined) {
return undefined
}

return String(val)
}

// route function helpers
type NonFunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T]
type FunctionKeys<T> = { [K in keyof T]: T[K] extends Function ? K : never }[keyof T]
Expand Down
Loading

0 comments on commit 8d671f2

Please sign in to comment.