HTTP CQRS client implementation using Axios
npm install @leancodepl/axios-cqrs-clientCQRS client with Axios for HTTP communication and command/query handling.
``bash`
npm install @leancodepl/axios-cqrs-clientor
yarn add @leancodepl/axios-cqrs-client
Creates CQRS client with Axios for HTTP communication and command/query handling.
Parameters:
- cqrsEndpoint: string - Base URL for CQRS API endpointstokenProvider?: TokenProvider
- - Optional token provider for authenticationaxiosOptions?: CreateAxiosDefaults
- - Optional Axios configuration optionstokenHeader?: string
- - Header name for authentication token (default: "Authorization")
Returns: Object with createQuery, createOperation, and createCommand methods
Creates CQRS client with automatic response key uncapitalization using "@leancodepl/utils".
Parameters:
- params: MkCqrsClientParameters - Configuration object for CQRS clientparams.cqrsEndpoint: string
- - Base URL for CQRS API endpointsparams.tokenProvider?: TokenProvider
- - Optional token provider for authenticationparams.axiosOptions?: CreateAxiosDefaults
- - Optional Axios configuration optionsparams.tokenHeader?: string
- - Header name for authentication token (default: "Authorization")
Returns: CQRS client with response key transformation
`typescript
import { mkCqrsClient } from "@leancodepl/axios-cqrs-client"
const client = mkCqrsClient({
cqrsEndpoint: "https://api.example.com",
tokenProvider: {
getToken: () => Promise.resolve(localStorage.getItem("token")),
invalidateToken: () => Promise.resolve(true),
},
})
`
`typescript
interface GetUserQuery {
userId: string
}
interface UserResult {
id: string
name: string
email: string
}
const getUser = client.createQuery
const response = await getUser({ userId: "123" })
if (response.isSuccess) {
console.log("User:", response.result)
}
`
`typescript
interface CreateUserCommand {
name: string
email: string
}
const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const
const createUser = client.createCommand
const response = await createUser({ name: "John", email: "john@example.com" })
createUser
.handle({ name: "John", email: "john@example.com" })
.handle("success", () => console.log("User created"))
.handle("EmailExists", () => console.log("Email already exists"))
.check()
`
`typescript
import { mkUncapitalizedCqrsClient } from "@leancodepl/axios-cqrs-client"
const client = mkUncapitalizedCqrsClient({
cqrsEndpoint: "https://api.example.com",
})
// Automatically transforms { UserId: '123' } to { userId: '123' }
const response = await client.createQuery("GetUser")({ userId: "123" })
``