@equinor/apollo-utils 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,36 +0,0 @@
1
- import { renderHook } from '@testing-library/react-hooks'
2
- import { useAtomValue } from 'jotai'
3
- import { describe, expect, it } from 'vitest'
4
- import { examplePerson, Person } from '../test-data/person'
5
- import { createFormFamily, useFormFamilyUtils } from './formUtils'
6
-
7
- describe('Test createFormFamily', () => {
8
- it('should create a family', () => {
9
- const family = createFormFamily<Person>()
10
- expect(family).toBeTruthy()
11
- })
12
- it('should initialize atom as undefined', () => {
13
- const family = createFormFamily<Person>()
14
- const { result } = renderHook(() => useAtomValue(family(examplePerson)))
15
- expect(result.current).toBeUndefined()
16
- })
17
- })
18
-
19
- function initFormUtils() {
20
- const family = createFormFamily<Person>()
21
- return useFormFamilyUtils(family)
22
- }
23
-
24
- describe('Test useFormFamilyUtils', () => {
25
- it('should initialize form as valid with values', () => {
26
- const utils = initFormUtils()
27
- renderHook(() => utils.useFormMutations(examplePerson).initializeForm(examplePerson))
28
- const { result } = renderHook(() => utils.useFormState(examplePerson))
29
- expect(result.current?.isValid).toBeTruthy()
30
- expect(result.current).toStrictEqual({
31
- status: 'editing',
32
- values: examplePerson,
33
- isValid: true,
34
- })
35
- })
36
- })
@@ -1,73 +0,0 @@
1
- import { atom, PrimitiveAtom, useAtom, useAtomValue, useSetAtom } from 'jotai'
2
- import { atomFamily } from 'jotai/utils'
3
- import { type AtomFamily } from 'jotai/vanilla/utils/atomFamily'
4
- import { createValidator } from '../zod-validation'
5
- import { FormFamilyParam, FormState } from './types'
6
-
7
- export function createFormFamily<E extends Record<string, unknown>>() {
8
- return atomFamily(
9
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
- (_param: FormFamilyParam) => atom<FormState<E> | undefined>(undefined),
11
- (a, b) => a.id === b.id
12
- )
13
- }
14
-
15
- type FormFamily<T> = AtomFamily<FormFamilyParam, PrimitiveAtom<FormState<T> | undefined>>
16
- type FormValidator = ReturnType<typeof createValidator>
17
-
18
- type UseFormFamilyUtilsOptions = {
19
- validator?: FormValidator
20
- }
21
-
22
- export function useFormFamilyUtils<T>(
23
- family: FormFamily<T>,
24
- options: UseFormFamilyUtilsOptions = {}
25
- ) {
26
- return {
27
- useFormStateAtom: (param: FormFamilyParam) => useAtom(family(param)),
28
- useFormState: (param: FormFamilyParam) => useAtomValue(family(param)),
29
- useUpdateFormMutation: (param: FormFamilyParam) => {
30
- return useFormFamilyMutation(family, param, options.validator)
31
- },
32
- useFormMutations(param: FormFamilyParam) {
33
- const mutateAtom = useSetAtom(family(param))
34
-
35
- return {
36
- update: useFormFamilyMutation(family, param, options.validator),
37
- initializeForm: (entity: T) =>
38
- mutateAtom({
39
- status: 'editing',
40
- values: entity,
41
- isValid: true,
42
- }),
43
- resetForm: () => mutateAtom(undefined),
44
- }
45
- },
46
- }
47
- }
48
-
49
- export function useFormFamilyMutation<T>(
50
- family: FormFamily<T>,
51
- param: FormFamilyParam,
52
- validator?: FormValidator
53
- ) {
54
- const mutate = useSetAtom(family(param))
55
- return (update: Partial<T>) => {
56
- return mutate((previous) => {
57
- if (!previous) return
58
-
59
- const updatedValues = {
60
- ...previous.values,
61
- ...update,
62
- }
63
- const errors = validator?.validate(updatedValues)
64
-
65
- return {
66
- status: 'editing',
67
- values: updatedValues,
68
- errors,
69
- isValid: !errors,
70
- }
71
- })
72
- }
73
- }
@@ -1,2 +0,0 @@
1
- export * from './formUtils'
2
- export * from './types'
@@ -1,12 +0,0 @@
1
- import { ValidationErrorMap } from '../zod-validation'
2
-
3
- export type FormState<T> = {
4
- status: 'editing' | 'pending'
5
- values: T
6
- errors?: ValidationErrorMap<T>
7
- isValid?: boolean
8
- }
9
-
10
- export type FormFamilyParam = {
11
- id: number | string
12
- } & Record<string, unknown>
@@ -1,15 +0,0 @@
1
- import { z } from 'zod'
2
-
3
- export const personSchema = z.object({
4
- id: z.number(),
5
- name: z.string().min(1),
6
- age: z.number().min(0).max(150),
7
- })
8
-
9
- export type Person = z.infer<typeof personSchema>
10
-
11
- export const examplePerson: Person = {
12
- id: 0,
13
- name: 'Apollo',
14
- age: 25,
15
- }
@@ -1,2 +0,0 @@
1
- export * from './types'
2
- export * from './utils'
@@ -1 +0,0 @@
1
- export type ValidationErrorMap<T> = Map<keyof T, { message: string; code: string }>
@@ -1,17 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { examplePerson, personSchema } from '../test-data/person'
3
- import { createValidator } from './utils'
4
-
5
- const validator = createValidator(personSchema)
6
-
7
- describe('Test validator creation', () => {
8
- it('should return undefined on valid data', () => {
9
- const errors = validator.validate(examplePerson)
10
- expect(errors).toBeUndefined()
11
- })
12
- it('should return errors on invalid name and age', () => {
13
- const errors = validator.validate({ ...examplePerson, name: '', age: 200 })
14
- expect(errors?.has('name')).toBeTruthy()
15
- expect(errors?.has('age')).toBeTruthy()
16
- })
17
- })
@@ -1,29 +0,0 @@
1
- import { z } from 'zod'
2
- import { ValidationErrorMap } from './types'
3
-
4
- export function createValidator<S extends z.ZodTypeAny>(schema: S) {
5
- return {
6
- validate: <E extends z.infer<typeof schema>>(entity: E) => {
7
- const validation = schema.safeParse(entity)
8
- if (validation.success) return undefined
9
- return prepareErrors<E>(validation)
10
- },
11
- validateAsync: async <E extends z.infer<typeof schema>>(entity: z.infer<typeof schema>) => {
12
- const validation = await schema.safeParseAsync(entity)
13
- if (validation.success) return undefined
14
- return prepareErrors<E>(validation)
15
- },
16
- getSchema() {
17
- return schema
18
- },
19
- }
20
- }
21
-
22
- function prepareErrors<E extends Record<string, unknown>>(errorValidation: z.SafeParseError<E>) {
23
- return new Map(
24
- errorValidation.error.errors.map((error) => [
25
- error.path[0] as keyof E,
26
- { message: error.message, code: error.code },
27
- ])
28
- ) as ValidationErrorMap<E>
29
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "tsconfig/vite.json",
3
- "include": ["src", "setupTest.ts"],
4
- "compilerOptions": { "types": ["vitest/importMeta"] }
5
- }
package/vitest.config.ts DELETED
@@ -1,11 +0,0 @@
1
- import { defineConfig } from 'vitest/config'
2
-
3
- export default defineConfig({
4
- define: {
5
- 'import.meta.vitest': 'undefined',
6
- },
7
- test: {
8
- includeSource: ['src/**/*.ts'],
9
- environment: 'jsdom',
10
- },
11
- })