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

Typed fetch function, where the types are generated using the endpoints #5700

Closed
Tracked by #11108
pedeEli opened this issue Jul 25, 2022 · 2 comments
Closed
Tracked by #11108

Comments

@pedeEli
Copy link

pedeEli commented Jul 25, 2022

Describe the problem

if you have the following endpoint

// src/routes/test.ts
import type { RequestHandler } from './__types/test'

export const GET: RequestHandler = async () => {
    return {
        body: {
            name: 'elias',
            age: 21
        }
    }
}

and you try to fetch the endpoint

// src/routes/page.svelte
fetch('/test')
    .then(data => data.json())
    .then(json => json.name)

the return object from data.json() should have the type

{
    name: string,
    age: number
}

Describe the proposed solution

like for the Load and the RequestHandler type, you could generate another file, which overrides the type for the fetch function

// .svelte-kit/types/fetch.d.ts
declare global {
    export function fetch(...): ...
}

and then reference this file in app.d.ts

// src/app.d.ts
/// <reference types="../.svelte-kit/types/fetch" />

And to know what the types are for the endpoint you could just export them in the endpoints and import then in the fetch.d.ts file

//src/routes/test.ts
import type { RequestHandler } from './__types/test'

export interface GETType {
    name: string,
    age: number
}

export const GET: RequestHandler<GETType> = async () => {
    return {
        body: {
            name: 'elias',
            age: 21
        }
    }
}

Alternatives considered

No response

Importance

would make my life easier

Additional Information

No response

@pedeEli
Copy link
Author

pedeEli commented Jul 25, 2022

i would have an implementation for this too, if this sounds like a feature worth having

@dummdidumm
Copy link
Member

Subset / duplicate of #647. There's a similar proposal in one of the comments. Feel free to add your thoughts in this issue instead. We will look at this in a holistic manner once we are sure the rest of SvelteKit is in a stable place.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Jul 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants