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

feat: improve ssgParams flexibility #2024

Merged
merged 9 commits into from
Jan 19, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions deno_dist/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@ interface SSGParam {
[key: string]: string
}
type SSGParams = SSGParam[]

interface SSGParamsMiddleware {
(generateParams: (c: Context) => SSGParams | Promise<SSGParams>): MiddlewareHandler
<E extends Env = Env>(
generateParams: (c: Context<E>) => SSGParams | Promise<SSGParams>
): MiddlewareHandler<E>
<E extends Env = Env>(params: SSGParams): MiddlewareHandler<E>
}

type AddedSSGDataRequest = Request & {
ssgParams?: SSGParams
}
/**
* Define SSG Route
*/
export const ssgParams: SSGParamsMiddleware = (generateParams) => async (c, next) => {
yusukebe marked this conversation as resolved.
Show resolved Hide resolved
;(c.req.raw as AddedSSGDataRequest).ssgParams = await generateParams(c)
export const ssgParams: SSGParamsMiddleware = (params) => async (c, next) => {
;(c.req.raw as AddedSSGDataRequest).ssgParams = Array.isArray(params) ? params : await params(c)
await next()
}

Expand Down
29 changes: 29 additions & 0 deletions src/helper/ssg/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ describe('toSSG function', () => {
ssgParams(() => postParams),
(c) => c.html(<h1>{c.req.param('post')}</h1>)
)

app.get(
'/user/:user_id',
ssgParams([{ user_id: '1' }, { user_id: '2' }, { user_id: '3' }]),
(c) => c.html(<h1>{c.req.param('user_id')}</h1>)
)

type Env = {
Bindings: {
FOO_DB: string
}
Variables: {
FOO_VAR: string
}
}

app.get(
'/env-type-check',
ssgParams<Env>((c) => {
expectTypeOf<typeof c.env.FOO_DB>().toBeString()
expectTypeOf<typeof c.var.FOO_VAR>().toBeString()
return []
})
)
})
it('Should correctly generate static HTML files for Hono routes', async () => {
const fsMock: FileSystemModule = {
Expand All @@ -57,6 +81,11 @@ describe('toSSG function', () => {
expect(html?.content).toBe(`<h1>${postParam.post}</h1>`)
}

for (let i = 1; i <= 3; i++) {
const html = htmlMap.get(`/user/${i}`)
expect(html?.content).toBe(`<h1>${i}</h1>`)
}

const files = await saveContentToFiles(htmlMap, fsMock, './static')

expect(files.length).toBeGreaterThan(0)
Expand Down
11 changes: 8 additions & 3 deletions src/helper/ssg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,22 @@ interface SSGParam {
[key: string]: string
}
type SSGParams = SSGParam[]

interface SSGParamsMiddleware {
(generateParams: (c: Context) => SSGParams | Promise<SSGParams>): MiddlewareHandler
<E extends Env = Env>(
generateParams: (c: Context<E>) => SSGParams | Promise<SSGParams>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to add the line for overloading:

<E extends Env = Env>(params: SSGParams): MiddlewareHandler<E>

): MiddlewareHandler<E>
<E extends Env = Env>(params: SSGParams): MiddlewareHandler<E>
}

type AddedSSGDataRequest = Request & {
ssgParams?: SSGParams
}
/**
* Define SSG Route
*/
export const ssgParams: SSGParamsMiddleware = (generateParams) => async (c, next) => {
;(c.req.raw as AddedSSGDataRequest).ssgParams = await generateParams(c)
export const ssgParams: SSGParamsMiddleware = (params) => async (c, next) => {
;(c.req.raw as AddedSSGDataRequest).ssgParams = Array.isArray(params) ? params : await params(c)
Copy link
Member

@yusukebe yusukebe Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sor4chi Should you rewrite these lines? I think generateParams is always function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. If you want to pass an array, write the overload signature to the interface declaration!

await next()
}

Expand Down