Bewinxed / svetch

Auto-Generated typesafe client & API docs generator for your Serverless Application (Svelte First)
https://svetch-dev.vercel.app/
67 stars 0 forks source link

Error detection for return statements not exhaustive enough #3

Closed jinbe closed 1 month ago

jinbe commented 8 months ago

Return statements in nested functions such as:

Promise.then(result => { return result; });

or something like this (kysley)

const { user } = await db.transaction().execute(async (txn) => {
            let user = await txn
                .selectFrom('User')
                .where('email', '=', rq.email)
                .select(['id', 'firstName', 'lastName'])
                .executeTakeFirst();
            if (!user) {
                user = await txn
                    .insertInto('User')
                    .values({
                        id: uuid(),
                        firstName: rq.firstName,
                        lastName: rq.lastName,
                    })
                    .returning(['id', 'firstName', 'lastName'])
                    .executeTakeFirstOrThrow();
            }

            return {
                user
            };
        });

Causes the generator to detect an error with a return statement not returning json or new Response().

Additionally if you had a function you call to generate a response. The return type of this function is not evaluated so that it can be validated against a valid json() or Response object.

E.g.

const handleApiError = (err: unknown) => {
    console.error(err);
    return json({ message: 'error message', { status: 400 });
};
export const POST = async (event: RequestEvent<Record<string, string>>): Promise<Response> => {
    try {
        throw 'on purpose';
    } catch (err) {
        return handleApiError(err);
    }

    return json('ok');
};
Bewinxed commented 2 months ago

Hello, I have done a huge rewrite for the library, Let me know if it picks it up now.

The easiest way for the library to pick up the return type is to assign it to a "result" or "results" variable, otherwise it will try to pick it up automatically.

Please try with the latest version npx svetch.ts@latest and let me know

Bewinxed commented 1 month ago

Closing this, Added a lot more detection mechanisms, whenever it's not able to detect it, the easiest solution is to assign the final result to a results const, then return that.

It will infer from that.

If there is a specific thing not being picked up, feel free to reopen and I'll try my best.