typia icon indicating copy to clipboard operation
typia copied to clipboard

How to use typia with Vite/React

Open hudmarc opened this issue 1 year ago • 1 comments

I'm integrating typia into my React project. I followed the instructions for installing Typia for use with Vite: https://github.com/ryoppippi/unplugin-typia/tree/main/packages/unplugin-typia but now I get the following error when running "npm run build"

✓ 5 modules transformed.
x Build failed in 1.15s
error during build:
[vite:esbuild] Transform failed with 1 error:
~/frontend/src/services/JobService.ts:68:0: ERROR: Unexpected "}"
file: ~/frontend/src/services/JobService.ts:68:0

Unexpected "}"
66 |      }
67 |  }
68 |  }
   |  ^

The file is as follows:

addJob<T>(job: Job, validator?: (input: string) => Primitive<T>, delegate?: (result: T) => unknown) {

// Some lines skipped

fetch(job.request)
            .then((response) => {
                if (response.status === 200) {
                    if (response.headers.get("content-type") === "application/json") {
                        return response.text();
                    }
                    else {
                        setJob(job.timestamp, job.withResponseText("Content type not application/json").withState(JobState.Failed));
                        return undefined;
                    }

                } else {
                    console.log(response.statusText)
                    setJob(job.timestamp, job.withResponseText(response.statusText).withState(JobState.Failed));
                    return undefined;
                }
            })
            .then((text) => {
                if (text != undefined) {
                    setJob(job.timestamp, job.withResponseText(text));
                    if (validator != undefined) {
                    
                        //This seems to fail

try {
                            const deserialized = validator(text) as T;

                            console.log(deserialized);

                            if (delegate != undefined) {
                                delegate(deserialized);
                            }
                            setJob(job.timestamp, job.withState(JobState.Complete));

                        } catch (error) {
                            setJob(job.timestamp, job.withState(JobState.Failed).withResponseText(String(error)));
                        }
                    }
                }
            })

I call it as follows (in a.tsx file, inside of an onclick)

services.get(JobService).addJob(new Job(Date.now(), new Request(choice), "Loading dataset"), createAssertParse<Dataset>(), (result => { services.get(SystemService).setDataset(result); }))

hudmarc avatar Jul 23 '24 17:07 hudmarc

@hudmarc please create an issue at unplugin-typia. https://github.com/ryoppippi/unplugin-typia/issues?q=sort:updated-desc+is:issue+is:open Also, provide minimal reproduction repository

ryoppippi avatar Jul 24 '24 17:07 ryoppippi

I tracked this issue here: https://github.com/ryoppippi/unplugin-typia/issues/325

There seems to be many other problems preventing typia from being used with vite/react other than just this, source mapping is broken as well

creikey avatar Sep 12 '24 11:09 creikey

@samchon We can close this

ryoppippi avatar Sep 19 '24 10:09 ryoppippi

Source mapping fixed

ryoppippi avatar Sep 19 '24 10:09 ryoppippi