Base types and interfaces for CQRS client implementations
npm install @leancodepl/cqrs-client-baseBase types and interfaces for CQRS clients.
``bash`
npm install @leancodepl/cqrs-client-baseor
yarn add @leancodepl/cqrs-client-base
Interface for token providers used in CQRS clients.
Properties:
- getToken: () => Promise - Returns authentication tokeninvalidateToken: () => Promise
- - Invalidates and refreshes token
Represents validation errors from commands.
Properties:
- PropertyName: string - Property that failed validationErrorMessage: string
- - Human-readable error messageErrorCode: TErrorCodes[keyof TErrorCodes]
- - Error code from provided map
Union type for command results.
Types:
- SuccessfulCommandResult - When command succeedsFailedCommandResult
- - When command fails with validation errors
Union type for API responses.
Types:
- ApiSuccess - Successful response with resultApiError
- - Error response
`typescript
import { TokenProvider } from "@leancodepl/cqrs-client-base"
const tokenProvider: TokenProvider = {
getToken: async () => {
return localStorage.getItem("authToken")
},
invalidateToken: async () => {
localStorage.removeItem("authToken")
return true
},
}
`
`typescript
import { CommandResult, ValidationError } from "@leancodepl/cqrs-client-base"
interface UserErrorCodes {
EmailExists: 1
InvalidEmail: 2
}
function handleCommandResult(result: CommandResult
if (result.WasSuccessful) {
console.log("Command succeeded")
} else {
result.ValidationErrors.forEach((error: ValidationError
console.log(${error.PropertyName}: ${error.ErrorMessage})`
})
}
}
`typescript
import { ApiResponse } from "@leancodepl/cqrs-client-base"
function handleApiResponse
if (response.isSuccess) {
return response.result
} else {
throw new Error("API call failed")
}
}
``