From 858f520aef7585cfe6850750ce4f579f66350378 Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Mon, 27 May 2024 21:55:37 -0400 Subject: [PATCH 1/8] Improve typescript types + minor fixes --- package-lock.json | 57 +++++++++--------- packages/svelte/package.json | 29 ++++----- packages/svelte/src/lib/components/App.svelte | 6 +- .../svelte/src/lib/components/Render.svelte | 23 ++++--- packages/svelte/src/lib/components/SSR.svelte | 12 +++- packages/svelte/src/lib/createInertiaApp.ts | 39 ++++++------ packages/svelte/src/lib/index.ts | 2 +- packages/svelte/src/lib/store.ts | 4 +- packages/svelte/src/lib/types.ts | 10 ++-- packages/svelte/src/lib/useForm.ts | 60 ++++++++----------- packages/svelte/tsconfig.json | 14 +---- 11 files changed, 114 insertions(+), 142 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1fd9eaf1f..7dc5dedb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3535,26 +3535,6 @@ "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" }, - "node_modules/publint": { - "version": "0.2.8", - "resolved": "https://registry.npmjs.org/publint/-/publint-0.2.8.tgz", - "integrity": "sha512-C5MjGJ7gpanqaDpgBN+6QhjvXcoj0/YpbucoW29oO5729CGTMzfr3wZTIYcpzB1xl9ZfEqj4KL86P2Z50pt/JA==", - "dev": true, - "dependencies": { - "npm-packlist": "^5.1.3", - "picocolors": "^1.0.1", - "sade": "^1.8.1" - }, - "bin": { - "publint": "lib/cli.js" - }, - "engines": { - "node": ">=16" - }, - "funding": { - "url": "https://bjornlu.com/sponsor" - } - }, "node_modules/qs": { "version": "6.11.2", "resolved": "https://registry.npmjs.org/qs/-/qs-6.11.2.tgz", @@ -5387,15 +5367,16 @@ }, "devDependencies": { "@sveltejs/adapter-auto": "^3.2.0", - "@sveltejs/kit": "^2.5.5", - "@sveltejs/package": "^2.3.0", - "@sveltejs/vite-plugin-svelte": "^3.0.2", - "publint": "^0.2.7", - "svelte": "^4.2.12", - "svelte-check": "^3.6.8", + "@sveltejs/kit": "^2.5.7", + "@sveltejs/package": "^2.3.1", + "@sveltejs/vite-plugin-svelte": "^3.1.0", + "axios": "^1.6.8", + "publint": "^0.1.16", + "svelte": "^4.2.16", + "svelte-check": "^3.7.1", "tslib": "^2.6.2", - "typescript": "^5.4.3", - "vite": "^5.2.7" + "typescript": "^5.4.5", + "vite": "^5.2.11" }, "peerDependencies": { "svelte": "^3.20.0 || ^4.0.0" @@ -5894,6 +5875,26 @@ "@jridgewell/sourcemap-codec": "^1.4.15" } }, + "packages/svelte/node_modules/publint": { + "version": "0.1.16", + "resolved": "https://registry.npmjs.org/publint/-/publint-0.1.16.tgz", + "integrity": "sha512-wJgk7HnXDT5Ap0DjFYbGz78kPkN44iQvDiaq8P63IEEyNU9mYXvaMd2cAyIM6OgqXM/IA3CK6XWIsRq+wjNpgw==", + "dev": true, + "dependencies": { + "npm-packlist": "^5.1.3", + "picocolors": "^1.0.0", + "sade": "^1.8.1" + }, + "bin": { + "publint": "lib/cli.js" + }, + "engines": { + "node": ">=16" + }, + "funding": { + "url": "https://bjornlu.com/sponsor" + } + }, "packages/svelte/node_modules/rollup": { "version": "4.18.0", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.18.0.tgz", diff --git a/packages/svelte/package.json b/packages/svelte/package.json index d7c07db18..c35df9b86 100755 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -5,8 +5,7 @@ "description": "The Svelte adapter for Inertia.js", "contributors": [ "Jonathan Reinink ", - "Pedro Borges ", - "Dmytro Morozov " + "Pedro Borges " ], "homepage": "https://inertiajs.com/", "repository": { @@ -35,16 +34,17 @@ "svelte": "^3.20.0 || ^4.0.0" }, "devDependencies": { + "axios": "^1.6.8", "@sveltejs/adapter-auto": "^3.2.0", - "@sveltejs/kit": "^2.5.5", - "@sveltejs/package": "^2.3.0", - "@sveltejs/vite-plugin-svelte": "^3.0.2", - "publint": "^0.2.7", - "svelte": "^4.2.12", - "svelte-check": "^3.6.8", + "@sveltejs/kit": "^2.5.7", + "@sveltejs/package": "^2.3.1", + "@sveltejs/vite-plugin-svelte": "^3.1.0", + "publint": "^0.1.16", + "svelte": "^4.2.16", + "svelte-check": "^3.7.1", "tslib": "^2.6.2", - "typescript": "^5.4.3", - "vite": "^5.2.7" + "typescript": "^5.4.5", + "vite": "^5.2.11" }, "exports": { ".": { @@ -56,16 +56,9 @@ "svelte": "./dist/server.js" } }, - "import": "./dist/index.js", + "svelte": "./dist/index.js", "types": "./dist/index.d.ts", "type": "module", - "typesVersions": { - ">4.0": { - ".": [ - "./dist/index.d.ts" - ] - } - }, "dependencies": { "@inertiajs/core": "1.2.0", "lodash": "^4.5.0" diff --git a/packages/svelte/src/lib/components/App.svelte b/packages/svelte/src/lib/components/App.svelte index 0d6e10094..82c5a452e 100644 --- a/packages/svelte/src/lib/components/App.svelte +++ b/packages/svelte/src/lib/components/App.svelte @@ -2,9 +2,7 @@ import Render, { h } from './Render.svelte' import store from '../store' - $$props - - $: child = $store.component && h($store.component.default, $store.page?.props) + $: child = $store.component?.default && h($store.component.default, $store.page?.props) $: layout = $store.component && $store.component.layout $: components = layout ? Array.isArray(layout) @@ -12,7 +10,7 @@ .concat(child) .reverse() .reduce((child, layout) => h(layout, $store.page?.props, [child])) - : h(layout, $store.page?.props, [child]) + : h(layout, $store.page?.props, child ? [child] : []) : child diff --git a/packages/svelte/src/lib/components/Render.svelte b/packages/svelte/src/lib/components/Render.svelte index 6a2b72690..8e100f4d3 100644 --- a/packages/svelte/src/lib/components/Render.svelte +++ b/packages/svelte/src/lib/components/Render.svelte @@ -1,14 +1,14 @@ {#if $store.component} diff --git a/packages/svelte/src/lib/components/SSR.svelte b/packages/svelte/src/lib/components/SSR.svelte index 22c69713a..f056a30a2 100644 --- a/packages/svelte/src/lib/components/SSR.svelte +++ b/packages/svelte/src/lib/components/SSR.svelte @@ -1,9 +1,15 @@ - + +
diff --git a/packages/svelte/src/lib/createInertiaApp.ts b/packages/svelte/src/lib/createInertiaApp.ts index 5e9b8a080..26d269964 100644 --- a/packages/svelte/src/lib/createInertiaApp.ts +++ b/packages/svelte/src/lib/createInertiaApp.ts @@ -1,22 +1,24 @@ import { router, setupProgress, type InertiaAppResponse, type Page } from '@inertiajs/core' import type { ComponentType } from 'svelte' -import SvelteApp from './components/App.svelte' -import SSR from './components/SSR.svelte' +import App from './components/App.svelte' +import SSR, { type SSRProps } from './components/SSR.svelte' import store from './store' -import type { ComponentResolver, InertiaComponentType } from './types' +import type { ComponentsResolver, ResolvedComponents } from './types' + +type SvelteRenderResult = { html: string; head: string; css: { code: string } } +type SSRComponent = ComponentType & { render: (props: SSRProps) => SvelteRenderResult } interface CreateInertiaAppProps { id?: string - resolve: ComponentResolver + resolve: ComponentsResolver setup: (props: { el: Element - // @ts-ignore - App: ComponentType + App: ComponentType props: { initialPage: Page - resolveComponent: ComponentResolver + resolveComponent: ComponentsResolver } - }) => void | SvelteApp + }) => void | App progress?: | false | { @@ -37,12 +39,12 @@ export default async function createInertiaApp({ }: CreateInertiaAppProps): InertiaAppResponse { const isServer = typeof window === 'undefined' const el = isServer ? null : document.getElementById(id) - const initialPage = page || JSON.parse(el?.dataset.page ?? '{}') + const initialPage: Page = page || JSON.parse(el?.dataset?.page || '{}') const resolveComponent = (name: string) => Promise.resolve(resolve(name)) await resolveComponent(initialPage.component).then((initialComponent) => { store.set({ - component: initialComponent as unknown as InertiaComponentType, + component: initialComponent, page: initialPage, }) }) @@ -57,7 +59,7 @@ export default async function createInertiaApp({ resolveComponent, swapComponent: async ({ component, page, preserveState }) => { store.update((current) => ({ - component: component as InertiaComponentType, + component: component as ResolvedComponents, page, key: preserveState ? current.key : Date.now(), })) @@ -70,21 +72,20 @@ export default async function createInertiaApp({ setup({ el, - App: SvelteApp, + App, props: { initialPage, resolveComponent, }, }) - - return } - // Svelte types are written for the DOM API and not the SSR API. - const { html, head, css } = (SSR as any).render({ id, initialPage }) + if (isServer) { + const { html, head, css } = (SSR as SSRComponent).render({ id, initialPage }) - return { - body: html, - head: [head, ``], + return { + body: html, + head: [head, ``], + } } } diff --git a/packages/svelte/src/lib/index.ts b/packages/svelte/src/lib/index.ts index ab5d232c7..707eed72a 100644 --- a/packages/svelte/src/lib/index.ts +++ b/packages/svelte/src/lib/index.ts @@ -4,4 +4,4 @@ export { default as createInertiaApp } from './createInertiaApp' export { default as inertia } from './link' export { default as page } from './page' export { default as remember } from './remember' -export { default as useForm } from './useForm' +export { default as useForm, type InertiaForm } from './useForm' diff --git a/packages/svelte/src/lib/store.ts b/packages/svelte/src/lib/store.ts index 505fa6d59..36cf25a79 100644 --- a/packages/svelte/src/lib/store.ts +++ b/packages/svelte/src/lib/store.ts @@ -1,9 +1,9 @@ import type { Page } from '@inertiajs/core' import { writable } from 'svelte/store' -import type { InertiaComponentType } from './types' +import type { ResolvedComponents } from './types' interface Store { - component: InertiaComponentType | null + component: ResolvedComponents | null page: Page | null key?: number | null } diff --git a/packages/svelte/src/lib/types.ts b/packages/svelte/src/lib/types.ts index 55374d34c..3aeeb0903 100644 --- a/packages/svelte/src/lib/types.ts +++ b/packages/svelte/src/lib/types.ts @@ -1,10 +1,10 @@ import type { PageProps } from '@inertiajs/core' import type { ComponentType } from 'svelte' -export type ComponentResolver = (name: string) => ComponentType | Promise +export type ComponentsResolver = (name: string) => ResolvedComponents | Promise -export interface InertiaComponentType extends ComponentType { - default: InertiaComponentType - layout: InertiaComponentType - props: PageProps +export type ResolvedComponents = { + default?: ComponentType + layout?: ComponentType, + props?: PageProps } diff --git a/packages/svelte/src/lib/useForm.ts b/packages/svelte/src/lib/useForm.ts index d9d0b7c94..26f210dfb 100644 --- a/packages/svelte/src/lib/useForm.ts +++ b/packages/svelte/src/lib/useForm.ts @@ -1,6 +1,7 @@ import type { ActiveVisit, Errors, + FormDataConvertible, Method, Page, PendingVisit, @@ -14,7 +15,9 @@ import cloneDeep from 'lodash/cloneDeep' import isEqual from 'lodash/isEqual' import { writable, type Writable } from 'svelte/store' -interface InertiaFormProps> { +type FormDataType = Record + +interface InertiaFormProps { isDirty: boolean errors: Partial> hasErrors: boolean @@ -22,13 +25,13 @@ interface InertiaFormProps> { wasSuccessful: boolean recentlySuccessful: boolean processing: boolean - setStore(data: InertiaFormProps): void - setStore(key: keyof InertiaFormProps, value?: any): void + setStore(data: TForm): void + setStore(key: keyof TForm, value?: FormDataConvertible): void data(): TForm transform(callback: (data: TForm) => object): this defaults(): this - defaults(fields: Record): this - defaults(field?: keyof TForm, value?: string): this + defaults(fields: Partial): this + defaults(field?: keyof TForm, value?: FormDataConvertible): this reset(...fields: (keyof TForm)[]): this clearErrors(...fields: (keyof TForm)[]): this setError(field: keyof TForm, value: string): this @@ -42,16 +45,14 @@ interface InertiaFormProps> { cancel(): void } -export type InertiaForm> = TForm & InertiaFormProps +export type InertiaForm = InertiaFormProps & TForm -export default function useForm>( - data: TForm | (() => TForm), -): Writable> -export default function useForm>( +export default function useForm(data: TForm | (() => TForm)): Writable> +export default function useForm( rememberKey: string, data: TForm | (() => TForm), ): Writable> -export default function useForm>( +export default function useForm( rememberKeyOrData: string | TForm | (() => TForm), maybeData?: TForm | (() => TForm), ): Writable> { @@ -65,7 +66,7 @@ export default function useForm>( let recentlySuccessfulTimeoutId: ReturnType | null = null let transform = (data: TForm) => data as object - const store = writable>({ + const store = writable>({ ...(restored ? restored.data : data), isDirty: false, errors: restored ? restored.errors : {}, @@ -74,20 +75,16 @@ export default function useForm>( wasSuccessful: false, recentlySuccessful: false, processing: false, - setStore(keyOrData: keyof InertiaFormProps | InertiaFormProps, maybeData?: InertiaFormProps) { + setStore(keyOrData, maybeData = undefined) { store.update((store) => { return Object.assign(store, typeof keyOrData === 'string' ? { [keyOrData]: maybeData } : keyOrData) }) }, data() { - return Object.keys((typeof data === 'function' ? data() : data) as Record).reduce( - (carry, key) => { - // @ts-ignore - carry[key] = this[key] - return carry - }, - {} as Record, - ) as TForm + return Object.keys((typeof data === 'function' ? data() : data) as FormDataType).reduce((carry, key) => { + carry[key] = this[key] + return carry + }, {} as FormDataType) as TForm }, transform(callback) { transform = callback @@ -96,15 +93,12 @@ export default function useForm>( }, defaults(fieldOrFields?: keyof TForm | Record, maybeValue?: string) { if (typeof fieldOrFields === 'undefined') { - // @ts-ignore defaults = Object.assign(defaults, cloneDeep(this.data())) return this } - // @ts-ignore defaults = Object.assign( - // @ts-ignore cloneDeep(defaults), cloneDeep(typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields), ) @@ -112,22 +106,18 @@ export default function useForm>( return this }, reset(...fields) { - // @ts-ignore - const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data()) + const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data!()) const clonedData = cloneDeep(resolvedData) if (fields.length === 0) { - // @ts-ignore this.setStore(clonedData) } else { this.setStore( - // @ts-ignore Object.keys(clonedData) .filter((key) => fields.includes(key)) .reduce((carry, key) => { - // @ts-ignore carry[key] = clonedData[key] return carry - }, {} as InertiaFormProps), + }, {} as FormDataType) as TForm, ) } @@ -157,7 +147,7 @@ export default function useForm>( }, submit(method, url, options = {}) { const data = transform(this.data()) as RequestPayload - const _options = { + const _options: Omit = { ...options, onCancelToken: (token: { cancel: () => void }) => { cancelToken = token @@ -184,8 +174,8 @@ export default function useForm>( return options.onStart(visit) } }, - onProgress: (event: AxiosProgressEvent) => { - this.setStore('progress', event) + onProgress: (event?: AxiosProgressEvent) => { + this.setStore('progress', event as any) if (options.onProgress) { return options.onProgress(event) @@ -232,10 +222,8 @@ export default function useForm>( } if (method === 'delete') { - // @ts-ignore router.delete(url, { ..._options, data }) } else { - // @ts-ignore router[method](url, data, _options) } }, @@ -257,7 +245,7 @@ export default function useForm>( cancel() { cancelToken?.cancel() }, - }) + } as InertiaForm) store.subscribe((form) => { if (form.isDirty === isEqual(form.data(), defaults)) { diff --git a/packages/svelte/tsconfig.json b/packages/svelte/tsconfig.json index c9499c0fa..5c56cee33 100644 --- a/packages/svelte/tsconfig.json +++ b/packages/svelte/tsconfig.json @@ -8,18 +8,6 @@ "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, - "strict": true, - "declaration": true, - "declarationDir": "dist", - - "module": "ES2020", - "moduleResolution": "Node", - "allowSyntheticDefaultImports": true, - - "noImplicitThis": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "preserveConstEnums": true, - "removeComments": true + "strict": true } } From aac53d4b18258dcc9430d3d052aa9bdae462df19 Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Wed, 26 Jun 2024 17:09:50 -0400 Subject: [PATCH 2/8] QA: Remove unused props type + add extra types just in case --- packages/svelte/src/lib/types.ts | 4 +--- packages/svelte/src/lib/useForm.ts | 4 ++-- packages/vue2/src/createInertiaApp.ts | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/svelte/src/lib/types.ts b/packages/svelte/src/lib/types.ts index 3aeeb0903..585407937 100644 --- a/packages/svelte/src/lib/types.ts +++ b/packages/svelte/src/lib/types.ts @@ -1,10 +1,8 @@ -import type { PageProps } from '@inertiajs/core' import type { ComponentType } from 'svelte' export type ComponentsResolver = (name: string) => ResolvedComponents | Promise export type ResolvedComponents = { default?: ComponentType - layout?: ComponentType, - props?: PageProps + layout?: ComponentType } diff --git a/packages/svelte/src/lib/useForm.ts b/packages/svelte/src/lib/useForm.ts index 26f210dfb..82aa20aa1 100644 --- a/packages/svelte/src/lib/useForm.ts +++ b/packages/svelte/src/lib/useForm.ts @@ -106,8 +106,8 @@ export default function useForm( return this }, reset(...fields) { - const resolvedData = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data!()) - const clonedData = cloneDeep(resolvedData) + const resolvedData: TForm = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data!()) + const clonedData = cloneDeep(resolvedData) as TForm if (fields.length === 0) { this.setStore(clonedData) } else { diff --git a/packages/vue2/src/createInertiaApp.ts b/packages/vue2/src/createInertiaApp.ts index 8c38ef85b..0c91803d0 100644 --- a/packages/vue2/src/createInertiaApp.ts +++ b/packages/vue2/src/createInertiaApp.ts @@ -40,7 +40,7 @@ export default async function createInertiaApp({ const el = isServer ? null : document.getElementById(id) const initialPage = page || JSON.parse(el.dataset.page) // @ts-expect-error - const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module) + const resolveComponent = (name: string) => Promise.resolve(resolve(name)).then((module) => module.default || module) let head = [] From cafa16e53a4383714e57c9f07f88dfaeaa5473c7 Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Wed, 26 Jun 2024 17:13:07 -0400 Subject: [PATCH 3/8] QA: Remove plural --- packages/svelte/src/lib/createInertiaApp.ts | 8 ++++---- packages/svelte/src/lib/store.ts | 4 ++-- packages/svelte/src/lib/types.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/svelte/src/lib/createInertiaApp.ts b/packages/svelte/src/lib/createInertiaApp.ts index 26d269964..64ec754e4 100644 --- a/packages/svelte/src/lib/createInertiaApp.ts +++ b/packages/svelte/src/lib/createInertiaApp.ts @@ -3,20 +3,20 @@ import type { ComponentType } from 'svelte' import App from './components/App.svelte' import SSR, { type SSRProps } from './components/SSR.svelte' import store from './store' -import type { ComponentsResolver, ResolvedComponents } from './types' +import type { ComponentResolver, ResolvedComponent } from './types' type SvelteRenderResult = { html: string; head: string; css: { code: string } } type SSRComponent = ComponentType & { render: (props: SSRProps) => SvelteRenderResult } interface CreateInertiaAppProps { id?: string - resolve: ComponentsResolver + resolve: ComponentResolver setup: (props: { el: Element App: ComponentType props: { initialPage: Page - resolveComponent: ComponentsResolver + resolveComponent: ComponentResolver } }) => void | App progress?: @@ -59,7 +59,7 @@ export default async function createInertiaApp({ resolveComponent, swapComponent: async ({ component, page, preserveState }) => { store.update((current) => ({ - component: component as ResolvedComponents, + component: component as ResolvedComponent, page, key: preserveState ? current.key : Date.now(), })) diff --git a/packages/svelte/src/lib/store.ts b/packages/svelte/src/lib/store.ts index 36cf25a79..bcdc7cc2b 100644 --- a/packages/svelte/src/lib/store.ts +++ b/packages/svelte/src/lib/store.ts @@ -1,9 +1,9 @@ import type { Page } from '@inertiajs/core' import { writable } from 'svelte/store' -import type { ResolvedComponents } from './types' +import type { ResolvedComponent } from './types' interface Store { - component: ResolvedComponents | null + component: ResolvedComponent | null page: Page | null key?: number | null } diff --git a/packages/svelte/src/lib/types.ts b/packages/svelte/src/lib/types.ts index 585407937..147ee2901 100644 --- a/packages/svelte/src/lib/types.ts +++ b/packages/svelte/src/lib/types.ts @@ -1,8 +1,8 @@ import type { ComponentType } from 'svelte' -export type ComponentsResolver = (name: string) => ResolvedComponents | Promise +export type ComponentResolver = (name: string) => ResolvedComponent | Promise -export type ResolvedComponents = { +export type ResolvedComponent = { default?: ComponentType layout?: ComponentType } From e14cfb8d2d2d27cbb6a68e68bf6b426ae345779d Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Wed, 26 Jun 2024 17:15:21 -0400 Subject: [PATCH 4/8] QA: Revert tsconfig change --- packages/svelte/tsconfig.json | 14 +++++++++++++- packages/vue2/src/createInertiaApp.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/svelte/tsconfig.json b/packages/svelte/tsconfig.json index 5c56cee33..c9499c0fa 100644 --- a/packages/svelte/tsconfig.json +++ b/packages/svelte/tsconfig.json @@ -8,6 +8,18 @@ "resolveJsonModule": true, "skipLibCheck": true, "sourceMap": true, - "strict": true + "strict": true, + "declaration": true, + "declarationDir": "dist", + + "module": "ES2020", + "moduleResolution": "Node", + "allowSyntheticDefaultImports": true, + + "noImplicitThis": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "preserveConstEnums": true, + "removeComments": true } } diff --git a/packages/vue2/src/createInertiaApp.ts b/packages/vue2/src/createInertiaApp.ts index 0c91803d0..8c38ef85b 100644 --- a/packages/vue2/src/createInertiaApp.ts +++ b/packages/vue2/src/createInertiaApp.ts @@ -40,7 +40,7 @@ export default async function createInertiaApp({ const el = isServer ? null : document.getElementById(id) const initialPage = page || JSON.parse(el.dataset.page) // @ts-expect-error - const resolveComponent = (name: string) => Promise.resolve(resolve(name)).then((module) => module.default || module) + const resolveComponent = (name) => Promise.resolve(resolve(name)).then((module) => module.default || module) let head = [] From ea6cb40f2047e0953b5bc48df8d137145336af3d Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Tue, 30 Jul 2024 23:33:57 -0400 Subject: [PATCH 5/8] QA: Move the if server up --- packages/svelte/src/lib/createInertiaApp.ts | 60 ++++++++++----------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/packages/svelte/src/lib/createInertiaApp.ts b/packages/svelte/src/lib/createInertiaApp.ts index 64ec754e4..94e644e63 100644 --- a/packages/svelte/src/lib/createInertiaApp.ts +++ b/packages/svelte/src/lib/createInertiaApp.ts @@ -49,37 +49,6 @@ export default async function createInertiaApp({ }) }) - if (!isServer) { - if (!el) { - throw new Error(`Element with ID "${id}" not found.`) - } - - router.init({ - initialPage, - resolveComponent, - swapComponent: async ({ component, page, preserveState }) => { - store.update((current) => ({ - component: component as ResolvedComponent, - page, - key: preserveState ? current.key : Date.now(), - })) - }, - }) - - if (progress) { - setupProgress(progress) - } - - setup({ - el, - App, - props: { - initialPage, - resolveComponent, - }, - }) - } - if (isServer) { const { html, head, css } = (SSR as SSRComponent).render({ id, initialPage }) @@ -88,4 +57,33 @@ export default async function createInertiaApp({ head: [head, ``], } } + + if (!el) { + throw new Error(`Element with ID "${id}" not found.`) + } + + router.init({ + initialPage, + resolveComponent, + swapComponent: async ({ component, page, preserveState }) => { + store.update((current) => ({ + component: component as ResolvedComponent, + page, + key: preserveState ? current.key : Date.now(), + })) + }, + }) + + if (progress) { + setupProgress(progress) + } + + setup({ + el, + App, + props: { + initialPage, + resolveComponent, + }, + }) } From 19c0efce733d0508cdab06423cb95cb6260aa7a8 Mon Sep 17 00:00:00 2001 From: Pedro Borges Date: Fri, 9 Aug 2024 21:41:33 -0300 Subject: [PATCH 6/8] Review useForm types --- packages/svelte/src/lib/useForm.ts | 46 +++++++++++++----------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/packages/svelte/src/lib/useForm.ts b/packages/svelte/src/lib/useForm.ts index 82aa20aa1..853984b1b 100644 --- a/packages/svelte/src/lib/useForm.ts +++ b/packages/svelte/src/lib/useForm.ts @@ -57,11 +57,12 @@ export default function useForm( maybeData?: TForm | (() => TForm), ): Writable> { const rememberKey = typeof rememberKeyOrData === 'string' ? rememberKeyOrData : null - const data = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData + const inputData = typeof rememberKeyOrData === 'string' ? maybeData : rememberKeyOrData + const data: TForm = typeof inputData === 'function' ? inputData() : inputData as TForm const restored = rememberKey - ? (router.restore(rememberKey) as { data: TForm; errors: Record }) + ? (router.restore(rememberKey) as { data: TForm; errors: Record } | null) : null - let defaults = typeof data === 'function' ? cloneDeep(data()) : cloneDeep(data) + let defaults = cloneDeep(data) let cancelToken: { cancel: () => void } | null = null let recentlySuccessfulTimeoutId: ReturnType | null = null let transform = (data: TForm) => data as object @@ -75,39 +76,33 @@ export default function useForm( wasSuccessful: false, recentlySuccessful: false, processing: false, - setStore(keyOrData, maybeData = undefined) { + setStore(keyOrData, maybeValue = undefined) { store.update((store) => { - return Object.assign(store, typeof keyOrData === 'string' ? { [keyOrData]: maybeData } : keyOrData) + return Object.assign(store, typeof keyOrData === 'string' ? { [keyOrData]: maybeValue } : keyOrData) }) }, data() { - return Object.keys((typeof data === 'function' ? data() : data) as FormDataType).reduce((carry, key) => { + return Object.keys(data).reduce((carry, key) => { carry[key] = this[key] return carry }, {} as FormDataType) as TForm }, transform(callback) { transform = callback - return this }, - defaults(fieldOrFields?: keyof TForm | Record, maybeValue?: string) { - if (typeof fieldOrFields === 'undefined') { - defaults = Object.assign(defaults, cloneDeep(this.data())) - - return this - } - - defaults = Object.assign( - cloneDeep(defaults), - cloneDeep(typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields), - ) + defaults(fieldOrFields?: keyof TForm | Partial, maybeValue?: FormDataConvertible) { + defaults = typeof fieldOrFields === 'undefined' + ? cloneDeep(this.data()) + : Object.assign( + cloneDeep(defaults), + typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields, + ) return this }, reset(...fields) { - const resolvedData: TForm = typeof data === 'object' ? cloneDeep(defaults) : cloneDeep(data!()) - const clonedData = cloneDeep(resolvedData) as TForm + const clonedData = cloneDeep(defaults) if (fields.length === 0) { this.setStore(clonedData) } else { @@ -126,7 +121,7 @@ export default function useForm( setError(fieldOrFields: keyof TForm | Errors, maybeValue?: string) { this.setStore('errors', { ...this.errors, - ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as any), + ...((typeof fieldOrFields === 'string' ? { [fieldOrFields]: maybeValue } : fieldOrFields) as Errors), }) return this @@ -140,12 +135,11 @@ export default function useForm( ...(fields.length > 0 && !fields.includes(field) ? { [field]: this.errors[field] } : {}), }), {}, - ), + ) as Errors, ) - return this }, - submit(method, url, options = {}) { + submit(method, url, options: Partial = {}) { const data = transform(this.data()) as RequestPayload const _options: Omit = { ...options, @@ -262,5 +256,5 @@ export default function useForm( } }) - return store as Writable> -} + return store +} \ No newline at end of file From eb00eea21b26c1f0798a0c110548198d3118d7d2 Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Fri, 9 Aug 2024 21:22:25 -0400 Subject: [PATCH 7/8] QA: Update reactive if statement --- packages/svelte/src/lib/components/Render.svelte | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/svelte/src/lib/components/Render.svelte b/packages/svelte/src/lib/components/Render.svelte index 8e100f4d3..3cb4fa5ed 100644 --- a/packages/svelte/src/lib/components/Render.svelte +++ b/packages/svelte/src/lib/components/Render.svelte @@ -26,11 +26,9 @@ let prevComponent: ComponentType let key: number - $: { - if (prevComponent !== component) { - key = Date.now() - prevComponent = component - } + $: if (prevComponent !== component) { + key = Date.now() + prevComponent = component } From 5187a68a9737cc1a6f33785d8d5edd726afd25cf Mon Sep 17 00:00:00 2001 From: James St-Pierre Date: Sun, 11 Aug 2024 00:43:46 -0400 Subject: [PATCH 8/8] QA: Add @types/lodash to fix svelte-check --- package-lock.json | 26 ++++++++++++++++++-------- packages/svelte/package.json | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7dc5dedb2..0883ffbd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1196,6 +1196,13 @@ "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==" }, + "node_modules/@types/lodash": { + "version": "4.17.7", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.7.tgz", + "integrity": "sha512-8wTvZawATi/lsmNu10/j2hk1KEP0IvjubqPE3cu1Xz7xfXXt5oCq3SNUz4fMIP4XGF9Ky+Ue2tBA3hcS7LSBlA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "14.18.63", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.18.63.tgz", @@ -1599,11 +1606,12 @@ } }, "node_modules/axios": { - "version": "1.6.7", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", - "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.3.tgz", + "integrity": "sha512-Ar7ND9pU99eJ9GpoGQKhKf58GpUOgnzuaB7ueNQ5BMi0p+LZ5oaEnfF999fAArcTIBwXTCHAmGcHOZJaWPq9Nw==", + "license": "MIT", "dependencies": { - "follow-redirects": "^1.15.4", + "follow-redirects": "^1.15.6", "form-data": "^4.0.0", "proxy-from-env": "^1.1.0" } @@ -4099,9 +4107,10 @@ } }, "node_modules/svelte": { - "version": "4.2.14", - "resolved": "https://registry.npmjs.org/svelte/-/svelte-4.2.14.tgz", - "integrity": "sha512-ry3+YlWqZpHxLy45MW4MZIxNdvB+Wl7p2nnstWKbOAewaJyNJuOtivSbRChcfIej6wFBjWqyKmf/NgK1uW2JAA==", + "version": "4.2.18", + "resolved": "https://registry.npmjs.org/svelte/-/svelte-4.2.18.tgz", + "integrity": "sha512-d0FdzYIiAePqRJEb90WlJDkjUEx42xhivxN8muUBmfZnP+tzUgz12DJ2hRJi8sIHCME7jeK1PTMgKPSfTd8JrA==", + "license": "MIT", "dependencies": { "@ampproject/remapping": "^2.2.1", "@jridgewell/sourcemap-codec": "^1.4.15", @@ -5370,6 +5379,7 @@ "@sveltejs/kit": "^2.5.7", "@sveltejs/package": "^2.3.1", "@sveltejs/vite-plugin-svelte": "^3.1.0", + "@types/lodash": "^4.17.7", "axios": "^1.6.8", "publint": "^0.1.16", "svelte": "^4.2.16", @@ -6173,7 +6183,7 @@ "typescript": "^4.9.4", "vite": "^4.5.3", "vue": "^3.3.4", - "vue-tsc": "^2.0.22" + "vue-tsc": "^2.0.0" } }, "playgrounds/vue3/node_modules/@vitejs/plugin-vue": { diff --git a/packages/svelte/package.json b/packages/svelte/package.json index c35df9b86..29fc99cf5 100755 --- a/packages/svelte/package.json +++ b/packages/svelte/package.json @@ -34,11 +34,12 @@ "svelte": "^3.20.0 || ^4.0.0" }, "devDependencies": { - "axios": "^1.6.8", "@sveltejs/adapter-auto": "^3.2.0", "@sveltejs/kit": "^2.5.7", "@sveltejs/package": "^2.3.1", "@sveltejs/vite-plugin-svelte": "^3.1.0", + "@types/lodash": "^4.17.7", + "axios": "^1.6.8", "publint": "^0.1.16", "svelte": "^4.2.16", "svelte-check": "^3.7.1",