@leancodepl/axios-cqrs-client
v9.6.6
Published
HTTP CQRS client implementation using Axios
Readme
@leancodepl/axios-cqrs-client
CQRS client with Axios for HTTP communication and command/query handling.
Installation
npm install @leancodepl/axios-cqrs-client
# or
yarn add @leancodepl/axios-cqrs-clientAPI
mkCqrsClient(cqrsEndpoint, tokenProvider, axiosOptions, tokenHeader)
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
mkUncapitalizedCqrsClient(params)
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
Usage Examples
Basic Setup
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),
},
})Query Operations
interface GetUserQuery {
userId: string
}
interface UserResult {
id: string
name: string
email: string
}
const getUser = client.createQuery<GetUserQuery, UserResult>("GetUser")
const response = await getUser({ userId: "123" })
if (response.isSuccess) {
console.log("User:", response.result)
}Command Operations
interface CreateUserCommand {
name: string
email: string
}
const errorCodes = { EmailExists: 1, InvalidEmail: 2 } as const
const createUser = client.createCommand<CreateUserCommand, typeof errorCodes>("CreateUser", errorCodes)
const response = await createUser({ name: "John", email: "[email protected]" })
createUser
.handle({ name: "John", email: "[email protected]" })
.handle("success", () => console.log("User created"))
.handle("EmailExists", () => console.log("Email already exists"))
.check()Uncapitalized Client
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" })