Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ NEW: add array type for Search Params #583

Closed
wants to merge 1 commit into from
Closed
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/smooth-planes-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-kit-routes': patch
---

add array type for search params
10 changes: 9 additions & 1 deletion packages/vite-plugin-kit-routes/src/lib/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ const PAGES = {
'/sp': (sp?: Record<string, string | number>) => {
return `/sp${appendSp(sp)}`
},
'/spArray': (params: { ids: number[] }) => {
return `/spArray${appendSp({ ids: params.ids })}`
},
}

/**
Expand Down Expand Up @@ -179,7 +182,10 @@ const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (
sp?: Record<string, string | number | string[] | number[] | undefined>,
prefix: '?' | '&' = '?',
) => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -275,6 +281,7 @@ export type KIT_ROUTES = {
'/lay/root-layout': never
'/lay/skip': never
'/sp': never
'/spArray': never
}
SERVERS: {
'GET /server_func_get': never
Expand Down Expand Up @@ -304,6 +311,7 @@ export type KIT_ROUTES = {
siteId: never
contractId: never
rest: never
ids: never
locale: never
redirectTo: never
extra: never
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-kit-routes/src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const format = (margin: { left?: number; top?: number; bottom?: number },
export const appendSp = `/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down
9 changes: 9 additions & 0 deletions packages/vite-plugin-kit-routes/src/lib/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,11 @@ describe('run()', async () => {
site_contract_siteId_contractId: {
explicit_search_params: { limit: { type: 'number' } },
},
spArray: {
explicit_search_params: {
ids: { type: 'number[]', required: true },
},
},
},
SERVERS: {},
ACTIONS: {
Expand Down Expand Up @@ -824,6 +829,7 @@ describe('rmv Helper', () => {
"lay/+layout.svelte",
"page_server_woAction/+page.server.ts",
"sp/+page.svelte",
"spArray/+page.svelte",
]
`)
})
Expand Down Expand Up @@ -870,6 +876,7 @@ describe('rmv Helper', () => {
"lay/+layout.svelte",
"page_server_woAction/+page.server.ts",
"sp/+page.svelte",
"spArray/+page.svelte",
]
`)

Expand Down Expand Up @@ -909,6 +916,7 @@ describe('rmv Helper', () => {
"lay/+layout.svelte",
"page_server_woAction/+page.server.ts",
"sp/+page.svelte",
"spArray/+page.svelte",
]
`)
})
Expand Down Expand Up @@ -951,6 +959,7 @@ describe('rmv Helper', () => {
"lay/+layout.svelte",
"page_server_woAction/+page.server.ts",
"sp/+page.svelte",
"spArray/+page.svelte",
]
`)
})
Expand Down
3 changes: 2 additions & 1 deletion packages/vite-plugin-kit-routes/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
<a href={route('/lay/skip')}>Layout Skip</a>
</li>
<li>
<a href={route('/sp')}>Search Params</a>
<a href={route('/sp')}>Search Params</a> |
<a href={route('/spArray', { ids: [7, 11] })}>Search Params (Array)</a>
</li>
</ul>

Expand Down
13 changes: 13 additions & 0 deletions packages/vite-plugin-kit-routes/src/routes/spArray/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import { page } from '$app/stores'

const ids: number[] =
$page.url.searchParams
.get('ids')
?.split(',')
?.map(id => parseInt(id)) ?? []
</script>

<h2>Search Params (Array)</h2>

<pre>{ids}</pre>
7 changes: 4 additions & 3 deletions packages/vite-plugin-kit-routes/src/test/ROUTES_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const PAGES = {
"/lay/normal": `${base}/lay/normal`,
"/lay/root-layout": `${base}/lay/root-layout`,
"/lay/skip": `${base}/lay/skip`,
"/sp": `${base}/sp`
"/sp": `${base}/sp`,
"/spArray": `${base}/spArray`
}

/**
Expand Down Expand Up @@ -109,7 +110,7 @@ const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -182,7 +183,7 @@ export function route<T extends keyof AllTypes>(key: T, ...params: any[]): strin
* ```
*/
export type KIT_ROUTES = {
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'id' | 'lang', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'id' | 'lang', '/match/[id=int]': 'id' | 'lang', '/site': 'lang', '/site/[id]': 'id' | 'lang', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never }
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'id' | 'lang', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'id' | 'lang', '/match/[id=int]': 'id' | 'lang', '/site': 'lang', '/site/[id]': 'id' | 'lang', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never, '/spArray': never }
SERVERS: { 'GET /server_func_get': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'id' | 'lang', 'create /site': 'lang', 'update /site/[id]': 'id' | 'lang', 'delete /site/[id]': 'id' | 'lang', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: Record<string, never>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const PAGES = {
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
"/lay/skip": `/lay/skip`,
"/sp": `/sp`
"/sp": `/sp`,
"/spArray": `/spArray`
}

/**
Expand Down Expand Up @@ -122,7 +123,7 @@ export const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -168,7 +169,7 @@ export const currentSp = () => {
* ```
*/
export type KIT_ROUTES = {
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'id' | 'lang', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'id' | 'lang', '/match/[id=int]': 'id' | 'lang', '/site': 'lang', '/site/[id]': 'lang' | 'id', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never }
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'id' | 'lang', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'id' | 'lang', '/match/[id=int]': 'id' | 'lang', '/site': 'lang', '/site/[id]': 'lang' | 'id', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never, '/spArray': never }
SERVERS: { 'GET /server_func_get': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'id' | 'lang', 'create /site': 'lang', 'update /site/[id]': 'id' | 'lang', 'delete /site/[id]': 'id' | 'lang', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const PAGES = {
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
"/lay/skip": `/lay/skip`,
"/sp": `/sp`
"/sp": `/sp`,
"/spArray": `/spArray`
}

/**
Expand Down Expand Up @@ -123,7 +124,7 @@ export const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -169,7 +170,7 @@ export const currentSp = () => {
* ```
*/
export type KIT_ROUTES = {
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'lang' | 'id', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'lang' | 'id', '/match/[id=int]': 'lang' | 'id', '/site': 'lang', '/site/[id]': 'lang' | 'id', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never }
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'lang' | 'id', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'lang' | 'id', '/match/[id=int]': 'lang' | 'id', '/site': 'lang', '/site/[id]': 'lang' | 'id', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never, '/spArray': never }
SERVERS: { 'GET /server_func_get': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'lang' | 'id', 'create /site': 'lang', 'update /site/[id]': 'lang' | 'id', 'delete /site/[id]': 'lang' | 'id', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export const PAGES = {
"lay_normal": `/lay/normal`,
"lay_root_layout": `/lay/root-layout`,
"lay_skip": `/lay/skip`,
"sp": `/sp`
"sp": `/sp`,
"spArray": (params: { ids: (number[]) }) => {
return `/spArray${appendSp({ ids: params.ids })}`
}
}

/**
Expand Down Expand Up @@ -122,7 +125,7 @@ export const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -168,9 +171,9 @@ export const currentSp = () => {
* ```
*/
export type KIT_ROUTES = {
PAGES: { '_ROOT': never, 'subGroup': never, 'subGroup_user': never, 'subGroup2': never, 'contract': 'lang', 'contract_id': 'id' | 'lang', 'gp_one': 'lang', 'gp_two': 'lang', 'main': 'lang', 'match_id_ab': 'id' | 'lang', 'match_id_int': 'id' | 'lang', 'site': 'lang', 'site_id': 'lang' | 'id', 'site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang', 'a_rest_z': 'rest', 'lay_normal': never, 'lay_root_layout': never, 'lay_skip': never, 'sp': never }
PAGES: { '_ROOT': never, 'subGroup': never, 'subGroup_user': never, 'subGroup2': never, 'contract': 'lang', 'contract_id': 'id' | 'lang', 'gp_one': 'lang', 'gp_two': 'lang', 'main': 'lang', 'match_id_ab': 'id' | 'lang', 'match_id_int': 'id' | 'lang', 'site': 'lang', 'site_id': 'lang' | 'id', 'site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang', 'a_rest_z': 'rest', 'lay_normal': never, 'lay_root_layout': never, 'lay_skip': never, 'sp': never, 'spArray': never }
SERVERS: { 'GET_server_func_get': never, 'POST_server_func_post': never, 'GET_contract': 'lang', 'POST_contract': 'lang', 'GET_site': 'lang', 'GET_api_graphql': never, 'POST_api_graphql': never, 'GET_data_errors_locale_json': 'locale' }
ACTIONS: { 'default_contract_id': 'id' | 'lang', 'create_site': 'lang', 'update_site_id': 'id' | 'lang', 'delete_site_id': 'id' | 'lang', 'noSatisfies_site_contract': 'lang', 'send_site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { first: never, lang: never, id: never, limit: never, demo: never, siteId: never, contractId: never, rest: never, locale: never, extra: never, name: never, str: never, s: never, d: never }
Params: { first: never, lang: never, id: never, limit: never, demo: never, siteId: never, contractId: never, rest: never, ids: never, locale: never, extra: never, name: never, str: never, s: never, d: never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ export const PAGES = {
"lay_normal": `/lay/normal`,
"lay_root_layout": `/lay/root-layout`,
"lay_skip": `/lay/skip`,
"sp": `/sp`
"sp": `/sp`,
"spArray": (ids: (number[]), params?: { }) => {
return `/spArray${appendSp({ ids })}`
}
}

/**
Expand Down Expand Up @@ -123,7 +126,7 @@ export const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -169,9 +172,9 @@ export const currentSp = () => {
* ```
*/
export type KIT_ROUTES = {
PAGES: { '_ROOT': never, 'subGroup': never, 'subGroup_user': never, 'subGroup2': never, 'contract': 'lang', 'contract_id': 'lang' | 'id', 'gp_one': 'lang', 'gp_two': 'lang', 'main': 'lang', 'match_id_ab': 'lang' | 'id', 'match_id_int': 'lang' | 'id', 'site': 'lang', 'site_id': 'lang' | 'id', 'site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang', 'a_rest_z': 'rest', 'lay_normal': never, 'lay_root_layout': never, 'lay_skip': never, 'sp': never }
PAGES: { '_ROOT': never, 'subGroup': never, 'subGroup_user': never, 'subGroup2': never, 'contract': 'lang', 'contract_id': 'lang' | 'id', 'gp_one': 'lang', 'gp_two': 'lang', 'main': 'lang', 'match_id_ab': 'lang' | 'id', 'match_id_int': 'lang' | 'id', 'site': 'lang', 'site_id': 'lang' | 'id', 'site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang', 'a_rest_z': 'rest', 'lay_normal': never, 'lay_root_layout': never, 'lay_skip': never, 'sp': never, 'spArray': never }
SERVERS: { 'GET_server_func_get': never, 'POST_server_func_post': never, 'GET_contract': 'lang', 'POST_contract': 'lang', 'GET_site': 'lang', 'GET_api_graphql': never, 'POST_api_graphql': never, 'GET_data_errors_locale_json': 'locale' }
ACTIONS: { 'default_contract_id': 'lang' | 'id', 'create_site': 'lang', 'update_site_id': 'lang' | 'id', 'delete_site_id': 'lang' | 'id', 'noSatisfies_site_contract': 'lang', 'send_site_contract_siteId_contractId': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Params: { first: never, lang: never, id: never, limit: never, demo: never, siteId: never, contractId: never, rest: never, locale: never, extra: never, name: never, str: never, s: never, d: never }
Params: { first: never, lang: never, id: never, limit: never, demo: never, siteId: never, contractId: never, rest: never, ids: never, locale: never, extra: never, name: never, str: never, s: never, d: never }
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const PAGES = {
"/lay/normal": `/lay/normal`,
"/lay/root-layout": `/lay/root-layout`,
"/lay/skip": `/lay/skip`,
"/sp": `/sp`
"/sp": `/sp`,
"/spArray": `/spArray`
}

/**
Expand Down Expand Up @@ -122,7 +123,7 @@ export const LINKS = {
/**
* Append search params to a string
*/
const appendSp = (sp?: Record<string, string | number | undefined>, prefix: '?' | '&' = '?') => {
const appendSp = (sp?: Record<string, string | number| string[] | number[] | undefined>, prefix: '?' | '&' = '?') => {
if (sp === undefined) return ''
const mapping = Object.entries(sp)
.filter(c => c[1] !== undefined)
Expand Down Expand Up @@ -195,7 +196,7 @@ export function route<T extends keyof AllTypes>(key: T, ...params: any[]): strin
* ```
*/
export type KIT_ROUTES = {
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'id' | 'lang', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'id' | 'lang', '/match/[id=int]': 'id' | 'lang', '/site': 'lang', '/site/[id]': 'lang' | 'id', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never }
PAGES: { '/': never, '/subGroup': never, '/subGroup/user': never, '/subGroup2': never, '/contract': 'lang', '/contract/[id]': 'id' | 'lang', '/gp/one': 'lang', '/gp/two': 'lang', '/main': 'lang', '/match/[id=ab]': 'id' | 'lang', '/match/[id=int]': 'id' | 'lang', '/site': 'lang', '/site/[id]': 'lang' | 'id', '/site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang', '/a/[...rest]/z': 'rest', '/lay/normal': never, '/lay/root-layout': never, '/lay/skip': never, '/sp': never, '/spArray': never }
SERVERS: { 'GET /server_func_get': never, 'POST /server_func_post': never, 'GET /contract': 'lang', 'POST /contract': 'lang', 'GET /site': 'lang', 'GET /api/graphql': never, 'POST /api/graphql': never, 'GET /data/errors/[locale].json': 'locale' }
ACTIONS: { 'default /contract/[id]': 'id' | 'lang', 'create /site': 'lang', 'update /site/[id]': 'id' | 'lang', 'delete /site/[id]': 'id' | 'lang', 'noSatisfies /site_contract': 'lang', 'send /site_contract/[siteId]-[contractId]': 'siteId' | 'contractId' | 'lang' }
LINKS: { 'twitter': never, 'twitter_post': 'name' | 'id', 'gravatar': 'str' }
Expand Down
Loading