From 6522baf8a4934abb0828f0cfe01d1e357c31e7d4 Mon Sep 17 00:00:00 2001 From: Joshua Amaju Date: Fri, 20 May 2022 20:57:40 +0100 Subject: [PATCH] chore: export types --- src/index.ts | 44 +++++++++++++++++++++----------------------- src/machine/actor.ts | 2 +- src/machine/index.ts | 4 ++-- src/machine/utils.ts | 10 ++++++++++ test/actor.test.ts | 8 ++++---- test/index.test.ts | 10 +++++----- 6 files changed, 43 insertions(+), 35 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7d1e4d3..27ad2d3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,18 @@ -import type { Interpreter } from 'xstate'; -import { interpret, State } from 'xstate'; -import { config } from './machine'; +import type { Interpreter, State } from 'xstate'; +import { interpret } from 'xstate'; +import { machine } from './machine'; import type { Ctx, Events, States } from './machine'; -import type { Validator, FormState } from './machine/types'; +import type { + Validator, + FormState, + Submitter, + ActorState, +} from './machine/types'; -import * as types from './machine/types'; -import * as actor from './machine/actor'; - -export type { types }; export * from './tools'; export * from './machine'; -export { actor }; +export * as actor from './machine/actor'; +export { Validator, FormState, ActorState, Submitter }; export type SubscriptionValue = Pick< Ctx, @@ -34,18 +36,24 @@ export type Actions = { cancelSubmit: () => void; kill: (id: string) => void; submitAsync: () => Promise; - // clearError: (id: string) => void; set: (name: N, value: T[N]) => void; validate: (name: N, value?: T[N]) => void; spawn: (id: string, value: unknown | null, validator: Validator) => void; }; +export type Extra = { + __service: Interpreter, any, Events, States>; + subscribe: ( + subscriber: (state: SubscriptionValue) => void + ) => void; +}; + export type Config = { onSubmit: (value: T) => D | Promise; initialValues?: { [K in keyof T]?: T[K] }; }; -export const createForm = < +export const create = < T extends object = any, D = any, E = any, @@ -54,13 +62,8 @@ export const createForm = < >({ onSubmit, initialValues, -}: Config): Actions & { - __service: Interpreter, any, Events, States>; - subscribe: ( - subscriber: (state: SubscriptionValue) => void - ) => void; -} => { - const service = interpret(config(initialValues as T, onSubmit)); +}: Config): Actions & Extra => { + const service = interpret(machine(initialValues as T, onSubmit)); const reset: Actions['reset'] = () => { service.send('reset'); @@ -70,10 +73,6 @@ export const createForm = < service.send('submit'); }; - // const clearError: Actions['clearError'] = (id) => { - // service.send({ id, type: 'clear_error' }); - // }; - const cancelSubmit: Actions['cancelSubmit'] = () => { service.send('cancel'); }; @@ -123,7 +122,6 @@ export const createForm = < reset, submit, validate, - // clearError, submitAsync, cancelSubmit, __service: service, diff --git a/src/machine/actor.ts b/src/machine/actor.ts index 2aee5ef..d2643a7 100644 --- a/src/machine/actor.ts +++ b/src/machine/actor.ts @@ -18,7 +18,7 @@ export type States = { context: Ctx; }; -export const config = ( +export const actor = ( id: string, initialValue: unknown, validator: Validator diff --git a/src/machine/index.ts b/src/machine/index.ts index fc48086..14e30ea 100644 --- a/src/machine/index.ts +++ b/src/machine/index.ts @@ -51,7 +51,7 @@ const setState = (state: ActorState) => { }); }; -export const config = ( +export const machine = ( initialValues: T, onSubmit: Submitter ) => { @@ -326,7 +326,7 @@ export const config = ( }, actors: ({ actors }, { id, value, validator }: any) => { const v = value ?? get(initialValues, id); - const spawned = spawn(actor.config(id, v, validator), id); + const spawned = spawn(actor.actor(id, v, validator), id); return { ...actors, [id]: spawned }; }, }), diff --git a/src/machine/utils.ts b/src/machine/utils.ts index 59d8eab..fd0aaf8 100644 --- a/src/machine/utils.ts +++ b/src/machine/utils.ts @@ -26,3 +26,13 @@ type RecursiveValueOfInner = { type RecursiveValueOfHandleValue = TValue extends object ? RecursiveValueOfInner : TValue; + +type Form = { + name: { + firstName: string; + lastName: string; + }; + age: number; +}; + +type m = FlattenKeys
; diff --git a/test/actor.test.ts b/test/actor.test.ts index 50b53b7..2e8871f 100644 --- a/test/actor.test.ts +++ b/test/actor.test.ts @@ -1,4 +1,4 @@ -import { config, Ctx, Events, States } from '../src/machine/actor'; +import { actor, Ctx, Events, States } from '../src/machine/actor'; import { interpret, Interpreter } from 'xstate'; import { string } from 'zod'; @@ -13,7 +13,7 @@ describe('actor', () => { beforeEach(() => { service = interpret( - config('1', null, (v) => string().parseAsync(v)).withConfig({ + actor('1', null, (v) => string().parseAsync(v)).withConfig({ actions: mockActions, }) ).start(); @@ -27,7 +27,7 @@ describe('actor', () => { it('should create actor with initial value', (done) => { const service = interpret( - config('1', 'Joe', (v) => string().parseAsync(v)).withConfig({ + actor('1', 'Joe', (v) => string().parseAsync(v)).withConfig({ actions: mockActions, }) ).start(); @@ -64,7 +64,7 @@ describe('actor', () => { it('validation should pass and resolve with new value', (done) => { const service = interpret( - config('1', null, () => 'Jane').withConfig({ actions: mockActions }) + actor('1', null, () => 'Jane').withConfig({ actions: mockActions }) ).start(); service.onTransition((state) => { diff --git a/test/index.test.ts b/test/index.test.ts index 94cf6bc..9b503bd 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,10 +1,10 @@ import { interpret, Interpreter } from 'xstate'; import * as z from 'zod'; -import { config, Ctx, Events, States } from '../src/machine'; +import { machine, Ctx, Events, States } from '../src/machine'; let service: Interpreter | null; -const def = config({}, async () => {}); +const def = machine({}, async () => {}); describe('machine', () => { beforeEach(() => { @@ -22,7 +22,7 @@ describe('machine', () => { }); it('should initialize with default values', (done) => { - const def = config({ name: 'Joe' }, async () => {}); + const def = machine({ name: 'Joe' }, async () => {}); const service = interpret(def).start(); service?.onTransition(({ context: ctx }) => { @@ -100,7 +100,7 @@ describe('submission', () => { }); it('should submit with error', (done) => { - const def = config({}, async () => { + const def = machine({}, async () => { throw new Error('error'); }); @@ -195,7 +195,7 @@ describe('nested schemas', () => { }; beforeEach(() => { - const def = config(values, async () => {}); + const def = machine(values, async () => {}); service = interpret(def).start(); });