2026-07-12 02:03:28 +08:00
|
|
|
name: API
|
|
|
|
|
|
|
|
|
|
attributes:
|
|
|
|
|
auto:
|
|
|
|
|
type: boolean
|
|
|
|
|
behavior: Request after request changes when request.url exists.
|
|
|
|
|
|
|
|
|
|
properties:
|
|
|
|
|
request:
|
|
|
|
|
default:
|
|
|
|
|
url: ''
|
2026-07-13 22:24:23 +08:00
|
|
|
method: "GET when data is null; POST when data is present, unless explicitly set."
|
2026-07-12 02:03:28 +08:00
|
|
|
headers: {}
|
|
|
|
|
data: null
|
|
|
|
|
timeout: 10000
|
|
|
|
|
responseType: ''
|
|
|
|
|
response:
|
|
|
|
|
fields: [loading, ok, status, error, headers, responseType, result]
|
|
|
|
|
result:
|
|
|
|
|
behavior: Latest response result.
|
|
|
|
|
|
|
|
|
|
methods:
|
|
|
|
|
do:
|
|
|
|
|
signature: api.do(options)
|
|
|
|
|
behavior: Merge options with request and perform HTTP.request.
|
|
|
|
|
options:
|
|
|
|
|
noui: Suppress automatic error toast.
|
|
|
|
|
|
|
|
|
|
events:
|
|
|
|
|
response:
|
|
|
|
|
detail: Complete HTTP response object.
|
|
|
|
|
error:
|
|
|
|
|
detail: Error object.
|
|
|
|
|
|
|
|
|
|
rules:
|
|
|
|
|
- request.url is required before calling do.
|
2026-07-13 22:24:23 +08:00
|
|
|
- Without request.method, API uses POST when request.data is present and GET otherwise.
|
2026-07-12 02:03:28 +08:00
|
|
|
- Bind request with $.request when external state owns request configuration.
|
|
|
|
|
|
|
|
|
|
examples:
|
|
|
|
|
auto: |
|
|
|
|
|
<script>
|
|
|
|
|
const usersRequest = { url: '/api/users', method: 'GET' }
|
|
|
|
|
</script>
|
|
|
|
|
<API id="usersApi" auto $.request="usersRequest"></API>
|
|
|
|
|
<List $.state.list="usersApi.result.list"></List>
|
|
|
|
|
do: |
|
|
|
|
|
<script>
|
|
|
|
|
const response = await usersApi.do({ url: '/api/users', method: 'GET', noui: true })
|
|
|
|
|
if (response.ok) console.log(response.result)
|
2026-07-13 22:24:23 +08:00
|
|
|
post_default: |
|
|
|
|
|
<API auto .request.url="/admin/table" .request.data.action="tables"></API>
|
|
|
|
|
<!-- data is present, so this request uses POST unless method is explicitly set -->
|
2026-07-12 02:03:28 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
tests:
|
|
|
|
|
- API.test.html
|