kit: Module '"@sveltejs/kit"' declares 'RequestEvent' locally, but it is not exported
Describe the bug
Getting a TypeScript warning Module '"@sveltejs/kit"' declares 'RequestEvent' locally, but it is not exported
for this line of code…
import type { RequestEvent } from '@sveltejs/kit'
Why do I need it? I have two functions in my hooks.ts called from handle() that pass the event as a parameter…
// Attach authorization to each server request (role may have changed)
async function attachUserToRequest(sessionId: string, event: RequestEvent) {
const sql = `SELECT * FROM get_session($1);`
const { rows } = await query(sql, [sessionId])
if (rows?.length > 0) {
event.locals.user = rows[0].get_session
}
}
function deleteCookieIfNoUser(event: RequestEvent, response: Response) {
if (!event.locals.user) {
response.headers['Set-Cookie'] = `session=; Path=/; HttpOnly; SameSite=Lax; Expires=${new Date().toUTCString()}`
}
}
export const handle: Handle = async ({ event, resolve }) => {
const cookies = cookie.parse(event.request.headers.get('Cookie') || '')
if (cookies.session) {
await attachUserToRequest(cookies.session, event)
}
const response = await resolve(event)
deleteCookieIfNoUser(event, response)
return response
}
Not sure if there was a recent change but my code has been in place for some time.
Reproduction
See code above…
Logs
No response
System Info
System:
OS: macOS 12.3.1
CPU: (10) arm64 Apple M1 Pro
Memory: 1.53 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.15.0 - /opt/homebrew/bin/node
Yarn: 1.22.18 - /opt/homebrew/bin/yarn
npm: 8.8.0 - /opt/homebrew/bin/npm
Browsers:
Chrome: 101.0.4951.54
Firefox: 99.0.1
Safari: 15.4
npmPackages:
@sveltejs/adapter-node: latest => 1.0.0-next.73
@sveltejs/kit: latest => 1.0.0-next.324
svelte: ^3.48.0 => 3.48.0
Severity
annoyance
Additional Information
My code works but the warning is a little annoying. More importantly, I think there is value for the RequestEvent type. Would be great to have it exported like Handle and GetSession.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 18 (8 by maintainers)
Ben, I’ll take care of this and open a PR today.
More widely, it seems to me like the input and return types of the public API should all be exposed to the user. From a TypeScript perspective, it’s always a frustrating user experience to have access to an exported API but not have access to that API’s input and output types.
From a dependency perspective, I don’t think it makes a difference either way – whether you expose the type publicly or not, users are still depending on the implicit type, so why not make it easier on them? 🙂
Would you like me to audit the exported function types for functions with unexported input/output types and create an issue for discussion if there are any remaining?