Skip to content

Commit 75ea38b

Browse files
committed
wip
1 parent acbce17 commit 75ea38b

File tree

13 files changed

+209
-74
lines changed

13 files changed

+209
-74
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
22
"private": true,
33
"workspaces": [
4-
"packages/*"
4+
"packages/core",
5+
"packages/react",
6+
"packages/react-inertia",
7+
"packages/vue",
8+
"packages/vue-inertia"
59
],
610
"scripts": {
711
"lint": "eslint --ext .ts --ignore-pattern dist ./packages",

packages/core/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export { client } from './client'
2-
export { createValidator } from './validator'
3-
export { toSimpleValidationErrors, resolveName } from './utils'
2+
export { createValidator, toSimpleValidationErrors, resolveName } from './validator'
43
export * from './types'

packages/core/src/utils.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/core/src/validator.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import isequal from 'lodash.isequal'
33
import get from 'lodash.get'
44
import set from 'lodash.set'
55
import { ValidationCallback, Config, NamedInputEvent, SimpleValidationErrors, ValidationErrors, Validator as TValidator, ValidatorListeners, ValidationConfig } from './types'
6-
import { resolveName, toValidationErrors } from './utils'
76
import { client } from './client'
87
import { isAxiosError } from 'axios'
98

@@ -238,3 +237,26 @@ export const createValidator = (callback: ValidationCallback, initialData: Recor
238237
},
239238
}
240239
}
240+
241+
export const toSimpleValidationErrors = (errors: ValidationErrors|SimpleValidationErrors): SimpleValidationErrors => {
242+
return Object.keys(errors).reduce((carry, key) => ({
243+
...carry,
244+
[key]: Array.isArray(errors[key])
245+
? errors[key][0]
246+
: errors[key],
247+
}), {})
248+
}
249+
250+
export const toValidationErrors = (errors: ValidationErrors|SimpleValidationErrors): ValidationErrors => {
251+
return Object.keys(errors).reduce((carry, key) => ({
252+
...carry,
253+
[key]: typeof errors[key] === 'string' ? [errors[key]] : errors[key],
254+
}), {})
255+
}
256+
257+
export const resolveName = (name: string|NamedInputEvent): string => {
258+
return typeof name !== 'string'
259+
? name.target.name
260+
: name
261+
}
262+

packages/vue-inertia/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dist
2+
/node_modules

packages/vue-inertia/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Taylor Otwell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

packages/vue-inertia/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Laravel Precognition
2+
3+
<a href="https://github.com/laravel/precognition/actions"><img src="https://github.com/laravel/precognition/workflows/tests/badge.svg" alt="Test Status"></a>
4+
<a href="https://github.com/laravel/precognition/actions"><img src="https://github.com/laravel/precognition/workflows/build/badge.svg" alt="Build Status"></a>
5+
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/dt/laravel-precognition" alt="Total Downloads"></a>
6+
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/v/laravel-precognition" alt="Latest Stable Version"></a>
7+
<a href="https://www.npmjs.com/package/laravel-precognition"><img src="https://img.shields.io/npm/l/laravel-precognition" alt="License"></a>
8+
9+
## Introduction
10+
11+
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.
12+
13+
## Official Documentation
14+
15+
Documentation for Laravel Precognition can be found on the [Laravel website](https://laravel.com/docs/precognition).
16+
17+
## Contributing
18+
19+
Thank you for considering contributing to Laravel Precognition! The contribution guide can be found in the [Laravel documentation](https://laravel.com/docs/contributions).
20+
21+
## Code of Conduct
22+
23+
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).
24+
25+
## Security Vulnerabilities
26+
27+
Please review [our security policy](https://github.com/laravel/precognition/security/policy) on how to report security vulnerabilities.
28+
29+
## License
30+
31+
Laravel Precognition is open-sourced software licensed under the [MIT license](LICENSE.md).

packages/vue-inertia/package.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "laravel-precognition-vue-inertia",
3+
"version": "0.1.0",
4+
"description": "Laravel Precognition (Vue w/ Inertia).",
5+
"keywords": [
6+
"laravel",
7+
"precognition",
8+
"vue",
9+
"inertia"
10+
],
11+
"homepage": "https://github.com/laravel/precognition",
12+
"type": "module",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/laravel/precognition"
16+
},
17+
"license": "MIT",
18+
"author": "Laravel",
19+
"main": "dist/index.js",
20+
"files": [
21+
"/dist"
22+
],
23+
"scripts": {
24+
"build": "rm -rf dist && tsc",
25+
"prepublishOnly": "npm run build"
26+
},
27+
"engines": {
28+
"node": ">=14"
29+
},
30+
"peerDependencies": {
31+
"@inertiajs/vue3": "^1.0.0"
32+
},
33+
"dependencies": {
34+
"laravel-precognition-vue": "0.1.0"
35+
},
36+
"devDependencies": {
37+
"typescript": "^5.0.0"
38+
}
39+
}

packages/vue-inertia/src/index.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import {Config, RequestMethod, SimpleValidationErrors, toSimpleValidationErrors, ValidationConfig} from 'laravel-precognition'
2+
import { useForm as useVueForm } from 'laravel-precognition-vue'
3+
import { useForm as useInertiaForm } from '@inertiajs/vue3'
4+
5+
export const useForm = <Data extends Record<string, unknown>>(method: RequestMethod, url: string, inputs: Data, config: ValidationConfig = {}): any => {
6+
/**
7+
* The Inertia form.
8+
*/
9+
const inertiaForm = useInertiaForm(inputs)
10+
11+
/**
12+
* The Vue form.
13+
*/
14+
const vueForm = useVueForm(method, url, inputs, config)
15+
16+
/**
17+
* Setup event listeners.
18+
*/
19+
vueForm.validator().on('errorsChanged', () => inertiaForm.clearErrors().setError(
20+
// @ts-expect-error
21+
toSimpleValidationErrors(vueForm.validator().errors())
22+
))
23+
24+
/**
25+
* The Inertia submit function.
26+
*/
27+
const inertiaSubmit = inertiaForm.submit.bind(inertiaForm)
28+
29+
/**
30+
* Patch the form.
31+
*/
32+
return Object.assign(inertiaForm, {
33+
processing: vueForm.processing,
34+
validating: vueForm.validating,
35+
touched: vueForm.touched,
36+
valid: vueForm.valid,
37+
invalid: vueForm.invalid,
38+
validate: vueForm.validate,
39+
setValidationTimeout: vueForm.setValidationTimeout,
40+
submit: (submitMethod: RequestMethod|Config = {}, submitUrl?: string, submitOptions?: any): void => {
41+
const isPatchedCall = typeof submitMethod !== 'string'
42+
43+
const options = {
44+
...isPatchedCall
45+
? submitMethod
46+
: submitOptions,
47+
onError: (errors: SimpleValidationErrors): any => {
48+
vueForm.validator().setErrors(errors)
49+
50+
if (submitOptions.onError) {
51+
return submitOptions.onError(errors)
52+
}
53+
},
54+
}
55+
56+
inertiaSubmit(
57+
isPatchedCall ? method : submitMethod,
58+
// @ts-expect-error
59+
(isPatchedCall ? url : submitUrl),
60+
options
61+
)
62+
}
63+
})
64+
}

packages/vue-inertia/tsconfig.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist",
4+
"target": "ES2020",
5+
"module": "ES2020",
6+
"moduleResolution": "node",
7+
"resolveJsonModule": true,
8+
"strict": true,
9+
"declaration": true,
10+
"esModuleInterop": true,
11+
"types": [
12+
"vue"
13+
]
14+
},
15+
"include": [
16+
"./src/index.ts"
17+
]
18+
}

0 commit comments

Comments
 (0)