Dejo el código de la clase con Vite + TypeScript
api.ts
export const fetchCountries = async () => {
const response = await fetch('https://restcountries.com/v3.1/region/ame');
const data = await response.json();
return data;
};

archivo bookingMachine.ts
export type BookingEvent =
| { type: 'START' }
| { type: 'CONTINUE'; selectedCountry: string }
| { type: 'ADD'; newPassenger: string }
| { type: 'DONE' }
| { type: 'FINISH' }
| { type: 'RETRY' }
| { type: 'CANCEL' };
const bookingMachine = createMachine(
{
context: {
passengers: [],
selectedCountry: '',
countries: [],
error: '',
},
tsTypes: {} as import('./bookingMachine.typegen').Typegen0,
schema: {
events: {} as BookingEvent,
context: {} as BookingContext,
},
initial: 'initial',
id: 'buy plane tickets',
states: {
initial: {
on: {
START: {
target: 'search',
},
},
},
search: {
on: {
CONTINUE: {
target: 'passengers',
actions: assign({
selectedCountry: (_context, event) => event.selectedCountry,
}),
},
CANCEL: {
target: 'initial',
actions: 'clearState',
},
},
initial: 'loading',
states: {
loading: {
invoke: {
id: 'getContries',
src: () => fetchCountries,
onDone: {
target: 'success',
actions: assign({
countries: (_context, event) => event.data,
}),
},
onError: {
target: 'failure',
actions: assign({
error: 'Fallo el request',
}),
},
},
},
success: {},
failure: {
on: {
RETRY: 'loading',
},
},
},
},
passengers: {
on: {
DONE: 'tickets',
CANCEL: {
target: 'initial',
actions: 'clearState',
},
ADD: {
target: 'passengers',
actions: (context, event) =>
context.passengers.push(event.newPassenger),
},
},
},
tickets: {
after: {
5000: {
target: 'initial',
actions: 'clearState',
},
},
on: {
FINISH: {
target: 'initial',
actions: 'clearState',
},
},
},
},
predictableActionArguments: true,
},
{
actions: {
clearState: assign({
passengers: [],
selectedCountry: '',
}),
// addPassenger: assign({
// passengers: (context, event) => [
// ...context.passengers,
// event.newPassenger,
// ],
// }),
},
},
);
Tickets.ts
