Skip to content

Commit

Permalink
chore: export types
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaAmaju committed May 20, 2022
1 parent bf04872 commit 6522baf
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
44 changes: 21 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<T extends object, D, E, Es> = Pick<
Ctx<T, D, E, Es>,
Expand All @@ -34,18 +36,24 @@ export type Actions<T = any, D = any> = {
cancelSubmit: () => void;
kill: (id: string) => void;
submitAsync: () => Promise<D>;
// clearError: (id: string) => void;
set: <N extends keyof T>(name: N, value: T[N]) => void;
validate: <N extends keyof T>(name: N, value?: T[N]) => void;
spawn: (id: string, value: unknown | null, validator: Validator) => void;
};

export type Extra<T extends object, D, E, FE> = {
__service: Interpreter<Ctx<T, D, E, FE>, any, Events, States>;
subscribe: (
subscriber: (state: SubscriptionValue<T, D, E, FE>) => void
) => void;
};

export type Config<T extends object, D> = {
onSubmit: (value: T) => D | Promise<D>;
initialValues?: { [K in keyof T]?: T[K] };
};

export const createForm = <
export const create = <
T extends object = any,
D = any,
E = any,
Expand All @@ -54,13 +62,8 @@ export const createForm = <
>({
onSubmit,
initialValues,
}: Config<T, D>): Actions<T, TData> & {
__service: Interpreter<Ctx<T, D, E, FE>, any, Events, States>;
subscribe: (
subscriber: (state: SubscriptionValue<T, D, E, FE>) => void
) => void;
} => {
const service = interpret(config<T>(initialValues as T, onSubmit));
}: Config<T, D>): Actions<T, TData> & Extra<T, TData, E, FE> => {
const service = interpret(machine<T>(initialValues as T, onSubmit));

const reset: Actions['reset'] = () => {
service.send('reset');
Expand All @@ -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');
};
Expand Down Expand Up @@ -123,7 +122,6 @@ export const createForm = <
reset,
submit,
validate,
// clearError,
submitAsync,
cancelSubmit,
__service: service,
Expand Down
2 changes: 1 addition & 1 deletion src/machine/actor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type States = {
context: Ctx;
};

export const config = (
export const actor = (
id: string,
initialValue: unknown,
validator: Validator
Expand Down
4 changes: 2 additions & 2 deletions src/machine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const setState = (state: ActorState) => {
});
};

export const config = <T extends object>(
export const machine = <T extends object>(
initialValues: T,
onSubmit: Submitter<T>
) => {
Expand Down Expand Up @@ -326,7 +326,7 @@ export const config = <T extends object>(
},
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 };
},
}),
Expand Down
10 changes: 10 additions & 0 deletions src/machine/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ type RecursiveValueOfInner<T> = {
type RecursiveValueOfHandleValue<TValue> = TValue extends object
? RecursiveValueOfInner<TValue>
: TValue;

type Form = {
name: {
firstName: string;
lastName: string;
};
age: number;
};

type m = FlattenKeys<Form>;
8 changes: 4 additions & 4 deletions test/actor.test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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) => {
Expand Down
10 changes: 5 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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<Ctx, any, Events, States> | null;

const def = config({}, async () => {});
const def = machine({}, async () => {});

describe('machine', () => {
beforeEach(() => {
Expand All @@ -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 }) => {
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('submission', () => {
});

it('should submit with error', (done) => {
const def = config({}, async () => {
const def = machine({}, async () => {
throw new Error('error');
});

Expand Down Expand Up @@ -195,7 +195,7 @@ describe('nested schemas', () => {
};

beforeEach(() => {
const def = config(values, async () => {});
const def = machine(values, async () => {});
service = interpret(def).start();
});

Expand Down

0 comments on commit 6522baf

Please sign in to comment.