-
Notifications
You must be signed in to change notification settings - Fork 42
[0.6.x] Add svelte 5 #104
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
Draft
renatomoor
wants to merge
10
commits into
laravel:main
Choose a base branch
from
renatomoor:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[0.6.x] Add svelte 5 #104
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d28c7e
feat: add svelte 5 package
renatomoor a7603ab
fix: use resolveName inside validate function
renatomoor 6a020a7
fix: make submit function async
renatomoor 72045ab
chore: add preserveWatchOutput to watch command
renatomoor 3f81902
chore: update .gitignore
renatomoor 9e37c4a
fix: remove unused types
renatomoor 3b340e7
fix: use $derived for hasErrors
renatomoor 999836f
fix: remove resolveName
renatomoor 1cbd6e0
chore: add package.json best practices for svelte packages
renatomoor dbf9dc7
chore: add getters and setters for each input
renatomoor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
/node_modules | ||
/package-lock.json | ||
|
||
.idea | ||
pnpm-lock.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
node_modules | ||
|
||
# Output | ||
.output | ||
.vercel | ||
/.svelte-kit | ||
/build | ||
/dist | ||
|
||
# OS | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Env | ||
.env | ||
.env.* | ||
!.env.example | ||
!.env.test | ||
|
||
# Vite | ||
vite.config.js.timestamp-* | ||
vite.config.ts.timestamp-* | ||
|
||
pnpm-lock.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) Taylor Otwell | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Laravel Precognition | ||
|
||
<a href="https://github.com/laravel/precognition/actions"><img src="https://github.com/laravel/precognition/workflows/tests/badge.svg" alt="Test Status"></a> | ||
<a href="https://github.com/laravel/precognition/actions"><img src="https://github.com/laravel/precognition/workflows/build/badge.svg" alt="Build Status"></a> | ||
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/dt/laravel-precognition" alt="Total Downloads"></a> | ||
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/v/laravel-precognition" alt="Latest Stable Version"></a> | ||
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/l/laravel-precognition" alt="License"></a> | ||
|
||
## Introduction | ||
|
||
Laravel Precognition allows you to anticipate the outcome of a future HTTP request. One of the primary use cases of Precognition is the ability to provide "live" validation in your frontend application. | ||
|
||
## Official Documentation | ||
|
||
Documentation for Laravel Precognition can be found on the [Laravel website](https://laravel.com/docs/precognition). | ||
|
||
## Contributing | ||
|
||
Thank you for considering contributing to Laravel Precognition! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions). | ||
|
||
## Code of Conduct | ||
|
||
In order to ensure that the Laravel community is welcoming to all, please review and abide by the [Code of Conduct](https://laravel.com/docs/contributions#code-of-conduct). | ||
|
||
## Security Vulnerabilities | ||
|
||
Please review [our security policy](https://github.com/laravel/precognition/security/policy) on how to report security vulnerabilities. | ||
|
||
## License | ||
|
||
Laravel Precognition is open-sourced software licensed under the [MIT license](LICENSE.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "laravel-precognition-svelte", | ||
"version": "0.0.1", | ||
"description": "Laravel Precognition (Svelte).", | ||
"keywords": [ | ||
"laravel", | ||
"precognition", | ||
"svelte" | ||
], | ||
"homepage": "https://github.com/laravel/precognition", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/laravel/precognition" | ||
}, | ||
"license": "MIT", | ||
"author": "Laravel", | ||
"main": "dist/index.svelte.js", | ||
"files": [ | ||
"dist", | ||
"!dist/**/*.test.*", | ||
"!dist/**/*.spec.*" | ||
], | ||
"svelte": "./dist/index.svelte.js", | ||
"types": "./dist/index.svelte.d.ts", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.svelte.d.ts", | ||
"svelte": "./dist/index.svelte.js" | ||
} | ||
}, | ||
"scripts": { | ||
"watch": "rm -rf dist && tsc --watch --preserveWatchOutput", | ||
"build": "rm -rf dist && tsc", | ||
"typeCheck": "tsc --noEmit", | ||
"prepublishOnly": "npm run build", | ||
"version": "npm pkg set dependencies.laravel-precognition=$npm_package_version" | ||
}, | ||
"peerDependencies": { | ||
"svelte": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"laravel-precognition": "0.5.11", | ||
"lodash-es": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@types/lodash-es": "^4.17.12", | ||
"typescript": "^5.0.0" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { Config, RequestMethod, client, createValidator, toSimpleValidationErrors, ValidationConfig, resolveUrl, resolveMethod , resolveName } from 'laravel-precognition' | ||
import { Form } from './types.js' | ||
import { get, cloneDeep, set } from 'lodash-es' | ||
|
||
export { client } | ||
|
||
export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod | (() => RequestMethod), url: string | (() => string), inputs: Data, config: ValidationConfig = {}): Data & Form<Data> => { | ||
/** | ||
* The original data. | ||
*/ | ||
const originalData = cloneDeep(inputs) | ||
|
||
/** | ||
* The original input names. | ||
*/ | ||
const originalInputs: (keyof Data)[] = Object.keys(originalData) | ||
|
||
/** | ||
* Reactive valid state. | ||
*/ | ||
// @ts-ignore | ||
let valid = $state<(keyof Data)[]>([]) | ||
|
||
/** | ||
* Reactive touched state. | ||
*/ | ||
// @ts-ignore | ||
let touched = $state<(keyof Partial<Data>)[]>([]) | ||
|
||
/** | ||
* Reactive errors. | ||
*/ | ||
// @ts-ignore | ||
let errors = $state<Record<keyof Data , any>>({}) | ||
|
||
/** | ||
* Reactive hasErrors. | ||
*/ | ||
// @ts-ignore | ||
const hasErrors = $derived<boolean>(Object.keys(errors).length > 0) | ||
|
||
/** | ||
* Reactive Validating. | ||
*/ | ||
// @ts-ignore | ||
let validating = $state<boolean>(false) | ||
|
||
/** | ||
* Reactive Processing. | ||
*/ | ||
// @ts-ignore | ||
let processing = $state<boolean>(false) | ||
|
||
/** | ||
* Reactive Data state | ||
*/ | ||
// @ts-ignore | ||
const data = $state<Data>(cloneDeep(originalData)) | ||
|
||
/** | ||
* The validator instance. | ||
*/ | ||
const validator = createValidator((client) => client[resolveMethod(method)](resolveUrl(url), form.data(), config), originalData) | ||
.on('validatingChanged', () => { | ||
validating = validator.validating() | ||
}) | ||
.on('validatedChanged', () => { | ||
valid = validator.valid() | ||
}) | ||
.on('touchedChanged', () => { | ||
touched = validator.touched() | ||
}) | ||
.on('errorsChanged', () => { | ||
errors = toSimpleValidationErrors(validator.errors()) as Record<keyof Data, any> | ||
valid = validator.valid() | ||
}) | ||
|
||
/** | ||
* Resolve the config for a form submission. | ||
*/ | ||
const resolveSubmitConfig = (config: Config): Config => ({ | ||
...config, | ||
precognitive: false, | ||
onStart: () => { | ||
processing = true | ||
|
||
config.onStart?.() | ||
}, | ||
onFinish: () => { | ||
processing = false | ||
|
||
config.onFinish?.() | ||
}, | ||
onValidationError: (response: any, error: any) => { | ||
validator.setErrors(response.data.errors) | ||
|
||
return config.onValidationError | ||
? config.onValidationError(response) | ||
// @ts-ignore | ||
: Promise.reject(error) | ||
}, | ||
}) | ||
|
||
/** | ||
* Create a new form instance. | ||
*/ | ||
const form: Data & Form<Data> = { | ||
...cloneDeep(originalData), | ||
data() { | ||
// @ts-ignore | ||
return $state.snapshot(data) as Data | ||
}, | ||
setData(newData: Record<string, unknown>) { | ||
Object.keys(newData).forEach((input) => { | ||
// @ts-ignore | ||
data[input] = newData[input] | ||
}) | ||
return form | ||
}, | ||
touched(name: string) { | ||
return touched.includes(name) | ||
}, | ||
touch(name: string) { | ||
validator.touch(name) | ||
|
||
return form | ||
}, | ||
validate(name: string | undefined, config: Config) { | ||
if (typeof name === 'object' && !('target' in name)) { | ||
config = name | ||
name = undefined | ||
} | ||
|
||
if (typeof name === 'undefined') { | ||
validator.validate(config) | ||
} else { | ||
name = resolveName(name) | ||
|
||
validator.validate(name, get(data, name), config) | ||
} | ||
|
||
return form | ||
}, | ||
get validating() { | ||
return validating | ||
}, | ||
valid(name: string) { | ||
return valid.includes(resolveName(name)) | ||
renatomoor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
invalid(name: string) { | ||
return typeof form.errors[name] !== 'undefined' | ||
}, | ||
get errors() { | ||
return errors | ||
}, | ||
get hasErrors() { | ||
return hasErrors | ||
}, | ||
setErrors(newErrors: any) { | ||
validator.setErrors(newErrors) | ||
return form | ||
}, | ||
forgetError(name: string) { | ||
validator.forgetError(name) | ||
|
||
return form | ||
}, | ||
reset(...names: string[]) { | ||
const original = cloneDeep(originalData) | ||
|
||
if (names.length === 0) { | ||
originalInputs.forEach((name) => (data[name] = original[name])) | ||
} else { | ||
names.forEach((name : string) => set(data, name, get(original, name))) | ||
} | ||
|
||
validator.reset(...names) | ||
|
||
return form | ||
}, | ||
setValidationTimeout(duration: number) { | ||
validator.setTimeout(duration) | ||
|
||
return form | ||
}, | ||
get processing() { | ||
return processing | ||
}, | ||
async submit(config = {}) { | ||
return client[resolveMethod(method)](resolveUrl(url), form.data(), resolveSubmitConfig(config)) | ||
}, | ||
validateFiles() { | ||
validator.validateFiles() | ||
|
||
return form | ||
}, | ||
validator() { | ||
return validator | ||
}, | ||
}; | ||
|
||
(Object.keys(data) as Array<keyof Data>).forEach((key) => { | ||
Object.defineProperty(form, key, { | ||
get() { | ||
return data[key] | ||
}, | ||
set(value: Data[keyof Data]) { | ||
data[key] = value | ||
}, | ||
}) | ||
}) | ||
|
||
return form as Data & Form<Data> | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { type ValidationConfig, type Config, type NamedInputEvent, type Validator } from 'laravel-precognition' | ||
|
||
export interface Form<Data extends Record<string, unknown>> { | ||
processing: boolean; | ||
validating: boolean; | ||
touched(name: keyof Data): boolean; | ||
touch(name: string | NamedInputEvent | Array<string>): Data & Form<Data>; | ||
data(): Data, | ||
setData(data: Record<string, unknown>): Data & Form<Data>; | ||
errors: Record<keyof Data, string>; | ||
hasErrors: boolean; | ||
valid(name: keyof Data): boolean; | ||
invalid(name: keyof Data): boolean; | ||
validate(name?: (keyof Data | NamedInputEvent) | ValidationConfig, config?: ValidationConfig): Data & Form<Data>; | ||
setErrors(errors: Partial<Record<keyof Data, string | string[]>>): Data & Form<Data>; | ||
forgetError(string: keyof Data | NamedInputEvent): Data & Form<Data>; | ||
setValidationTimeout(duration: number): Data & Form<Data>; | ||
submit(config?: Config): Promise<unknown>; | ||
reset(...keys: (keyof Partial<Data>)[]): Data & Form<Data>; | ||
validateFiles(): Data & Form<Data>; | ||
validator(): Validator; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.