Skip to content

Commit 2ad9d57

Browse files
committed
Add parseUrlSearchParams with failing tests
1 parent 6261ad0 commit 2ad9d57

File tree

11 files changed

+102
-41
lines changed

11 files changed

+102
-41
lines changed

examples/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import landlubber from 'landlubber'
44

5-
import * as todo from './todo.js'
5+
import * as parse from './parse.js'
66

7-
const commands = [todo]
7+
const commands = [parse]
88

99
await landlubber(commands).parse()

examples/parse.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { Builder, Command, Describe, Handler } from 'landlubber'
2+
import { z } from 'zod'
3+
4+
import { parseUrlSearchParams } from '@seamapi/url-search-params-parser'
5+
6+
interface Options {
7+
query: string
8+
}
9+
10+
export const command: Command = 'parse query'
11+
12+
export const describe: Describe = 'Parse query'
13+
14+
export const builder: Builder = {
15+
query: {
16+
type: 'string',
17+
describe: 'Query string',
18+
},
19+
}
20+
21+
export const handler: Handler<Options> = async ({ query, logger }) => {
22+
logger.info({ data: parseUrlSearchParams(query, schema) }, 'params')
23+
}
24+
25+
const schema = z
26+
.object({
27+
a: z.string(),
28+
b: z.number(),
29+
c: z.boolean(),
30+
d: z.null(),
31+
e: z.array(z.union([z.string(), z.number()])),
32+
f: z.array(z.string()),
33+
g: z.date(),
34+
h: z.date(),
35+
i: z
36+
.object({
37+
foo: z.number(),
38+
bar: z
39+
.object({
40+
baz: z.number(),
41+
fizz: z.array(z.union([z.string(), z.number()])),
42+
})
43+
.optional(),
44+
})
45+
.optional(),
46+
})
47+
.optional()

examples/todo.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
"node": ">=18.12.0",
6868
"npm": ">= 9.0.0"
6969
},
70+
"peerDependencies": {
71+
"zod": "^3.0.0"
72+
},
7073
"devDependencies": {
7174
"@seamapi/url-search-params-serializer": "^1.2.0",
7275
"@types/node": "^20.8.10",

src/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { todo } from './todo.js'
1+
export * from './parse.js'

src/lib/parse.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import test from 'ava'
2+
import { z } from 'zod'
3+
4+
import { parseUrlSearchParams } from './parse.js'
5+
6+
test.failing('parseUrlSearchParams: with string input', (t) => {
7+
t.deepEqual(
8+
parseUrlSearchParams(
9+
'foo=d&bar=2',
10+
z.object({ foo: z.string().optional(), bar: z.number().optional() }),
11+
),
12+
{ foo: 'd', bar: 2 },
13+
)
14+
})
15+
16+
test.failing('parseUrlSearchParams: with URLSearchParams input', (t) => {
17+
t.deepEqual(
18+
parseUrlSearchParams(
19+
new URLSearchParams('foo=d&bar=2'),
20+
z.object({ foo: z.string().optional(), bar: z.number().optional() }),
21+
),
22+
{ foo: 'd', bar: 2 },
23+
'with URLSearchParams input',
24+
)
25+
})

src/lib/parse.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { ZodSchema } from 'zod'
2+
3+
export const parseUrlSearchParams = (
4+
_query: URLSearchParams | string,
5+
_schema: ZodSchema,
6+
): Record<string, unknown> => {
7+
// const _searchParams =
8+
// typeof query === 'string' ? new URLSearchParams(query) : query
9+
return {}
10+
}

src/lib/todo.test.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lib/todo.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/parse.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { serializeUrlSearchParams } from '@seamapi/url-search-params-serializer'
2+
import test from 'ava'
3+
import { z } from 'zod'
4+
5+
import { parseUrlSearchParams } from '@seamapi/url-search-params-parser'
6+
7+
test('parses empty string', (t) => {
8+
const schema = z.object({ foo: z.string() })
9+
const input = {}
10+
t.deepEqual(
11+
parseUrlSearchParams(serializeUrlSearchParams(input), schema),
12+
input,
13+
)
14+
})

0 commit comments

Comments
 (0)