added into generated client response handlers
This commit is contained in:
18
README.md
18
README.md
@ -240,16 +240,29 @@ You can also download generated frontend clients:
|
||||
|
||||
Both generated clients support a shared Bearer token helper. Call `bearerSet(token)` once, the token is stored in `localStorage` under `apilite_bearer_token`, and subsequent requests automatically send `Authorization: Bearer ...`.
|
||||
|
||||
Both generated clients also support response hooks:
|
||||
|
||||
* `addResponseHandler(handler)` registers a handler called for every API response.
|
||||
* `addErrorHandler(handler)` registers a handler called only when the API response has `status === 'ERROR'`.
|
||||
* Both methods return an unsubscribe function.
|
||||
* Handlers receive `(response, context)`, where `context` contains `method`, `data`, and `httpStatus`.
|
||||
* Handler exceptions are ignored, so they do not change the original API callback or promise result.
|
||||
|
||||
JavaScript usage example:
|
||||
|
||||
```js
|
||||
import backend from './backend.js';
|
||||
|
||||
backend.bearerSet('your-access-token');
|
||||
const removeErrorHandler = backend.addErrorHandler((response, context) => {
|
||||
console.error(context.method, response.msg);
|
||||
});
|
||||
|
||||
backend.add(1, 2).then((response) => {
|
||||
console.log(response.data);
|
||||
});
|
||||
|
||||
// removeErrorHandler();
|
||||
```
|
||||
|
||||
TypeScript usage example (Vue + TS):
|
||||
@ -258,10 +271,15 @@ TypeScript usage example (Vue + TS):
|
||||
import backend from './backend';
|
||||
|
||||
backend.bearerSet('your-access-token');
|
||||
const removeResponseHandler = backend.addResponseHandler((response, context) => {
|
||||
console.log(context.httpStatus, response.status);
|
||||
});
|
||||
|
||||
backend.add(1, 2).then((response) => {
|
||||
console.log(response.data); // typed value based on PHP return type
|
||||
});
|
||||
|
||||
// removeResponseHandler();
|
||||
```
|
||||
|
||||
If a method return type is a PHP class with public properties, JSON help now also includes `return_structure`, and the generated TypeScript client maps that class to an object shape based on those public properties.
|
||||
|
||||
Reference in New Issue
Block a user