@quanticdigit/web-services
v1.0.2
Published
Typed reads and writes against HTTP services: response envelope, model reconstruction and error contracts.
Downloads
499
Readme
@quanticdigit/web-services
Typed reads and writes against HTTP services: response envelope, model reconstruction and error contracts.
Two base classes, same shape, different other end:
BaseService— for services that answer with the QuanticDigit envelope{ result, output, errors };EasyServiceBase— for services that return the object directly and report failures with HTTP status codes.
Both give you the same eight verbs with the same signatures, so moving between them does not change how you write a call.
Install
npm install @quanticdigit/web-services @quanticdigit/web-modelPeer dependencies: @angular/common, @angular/core, rxjs, @quanticdigit/web-model.
The one thing to wire
Both classes resolve endpoints from a table your product owns. Declare it once, in the root injector:
import { ENDPOINT_TABLE } from '@quanticdigit/web-services';
providers: [
{ provide: ENDPOINT_TABLE, useValue: environment.endpoint },
]The table is { name, endpoint }[]. Forget it and the first call tells you which key it was looking
for, not just that a token was missing.
A service
@Injectable({ providedIn: 'root' })
export class UserService extends BaseService {
constructor(service: WebApiService) { super(service); }
byId(id: number, errors: IErrorInfoAppender | null = null): Observable<User> {
return this.get<User>(User, this.formatEndpoint('user', id), errors);
}
}getEndpoint appends its arguments to the address; formatEndpoint substitutes {0}, {1} … and
URL-encodes them.
Errors are never swallowed
If you pass an IErrorInfoAppender, failures are handed to it and the stream completes.
If you pass nothing, the error propagates to your subscriber. There is no silent mode: an operation that fails without anyone to report it must not look like an operation that returned nothing.
service.byId(7).subscribe({
next: (user) => …,
error: (err) => …, // reached when no appender was given
});The three outcomes are siblings, so the order of your instanceof checks does not matter:
WebApiException
├── WebApiFatal the service could not answer
├── WebApiError the request was understood and refused
└── WebApiWarning<T> succeeded with warnings — carries the resultPlain REST services
EasyServiceBase reads the response body as the result. Errors are built from the HTTP response:
id is the status code, message is the response body or its status text, and isBusiness is
always false — without an envelope there is no way to know whether a refusal is a domain rule, and
guessing from a 409 would be inventing it.
The family
web-utils, web-model, web-services, web-component and web-errors are released together and
always share the same version. Install them at the same version.
License
Commercial. See LICENSE.txt in the package.
