helpful effector utils
npm install @42px/effector-extraHelpful effector utils.
``sh`
npm install @42px/effector-extra
Often you need to create a derivative effect with wrapped params, result or error:
`ts
import { attachWrapper } from '@42px/effector-extra'
import { requestFx, Res } from '../request'
type LoginParams = {
username: string
password: string
}
type LoginResult = {
token: string
}
const loginReqFx = attachWrapper({
effect: requestFx,
mapParams: (params: LoginParams) => ({
method: "POST",
body: params,
}),
mapResult: ({ result }): Res
mapError: ({ error }) => {
if (error.code === 404) {
const err = new Error()
err.name = "InvalidCredentials"
return err
}
return error
},
})
`
`ts
const domain = createDomain()
// real request effect
const requestFx = domain.effect
const myService = createService({
domain: createDomain(),
effect: requestFx,
})
// some method
const getNewsFx = myService.createMethod({
mapParams: (newsId: number) => ({
url: /news/${newsId},`
}),
mapResult: ({ result }) => result.data,
})
`ts
import { batchEvents } from '@42px/effector-extra'
import { matrixDomain } from './domain'
export const roomMessage = matrixDomain.event
export const roomMessageBatch = batchEvents(roomMessage, 500)
`
Type-safe helper for mock effects when using fork (useful for testing):
`ts
const mockCartStorage = () => {
let cartStorage: CartItem[] = []
return mockEffects()
.set(writeCartFx, (cart) => {
cartStorage = cart
})
.set(readCartFx, () => cartStorage)
}
let scope: Scope
test('add to cart', async () => {
scope = fork(root, {
handlers: mockCartStorage(),
})
})
``