Http

Retrieve state from an HTTP Rest API.

Overview

Requests can only be made to servers that are pre-registered in nuxt.config.

As an exception, the Nuxt Server API protocol is preset for access to /api.

If you need an API key or want to hide a URL, please use the Nuxt Server API instead.

URI {protocol}://{**name}
Path

Specify the path that follows the prefix registered in nuxt.config.ts.

Example reqres://users

nuxt.config.ts

Register the base URL as a custom protocol in nuxt.config.ts in advance.

This is an example of registering the test API reqres.in .

ts
export default defineNuxtConfig({
 usebootstrap: {
  integration: {
   protocol: {
    reqres: {
     type: 'fetch',
     prefix: 'https://reqres.in/api/',
    },
   },
  },
 },
});

ViewState

Basic

This is an example using the test API reqres.in .

vue
<template>
 <ViewState
  v-slot="users"
  src="reqres://users"
 >
  <JsonView :data="users.data" />
 </ViewState>
</template>
{
 "page": 1,
 "per_page": 6,
 "total": 12,
 "total_pages": 2,
 "data": [
  {
   "id": 1,
   "email": "george.bluth@reqres.in",
   "first_name": "George",
   "last_name": "Bluth",
   "avatar": "https://reqres.in/img/faces/1-image.jpg"
  },
  {
   "id": 2,
   "email": "janet.weaver@reqres.in",
   "first_name": "Janet",
   "last_name": "Weaver",
   "avatar": "https://reqres.in/img/faces/2-image.jpg"
  },
  {
   "id": 3,
   "email": "emma.wong@reqres.in",
   "first_name": "Emma",
   "last_name": "Wong",
   "avatar": "https://reqres.in/img/faces/3-image.jpg"
  },
  {
   "id": 4,
   "email": "eve.holt@reqres.in",
   "first_name": "Eve",
   "last_name": "Holt",
   "avatar": "https://reqres.in/img/faces/4-image.jpg"
  },
  {
   "id": 5,
   "email": "charles.morris@reqres.in",
   "first_name": "Charles",
   "last_name": "Morris",
   "avatar": "https://reqres.in/img/faces/5-image.jpg"
  },
  {
   "id": 6,
   "email": "tracey.ramos@reqres.in",
   "first_name": "Tracey",
   "last_name": "Ramos",
   "avatar": "https://reqres.in/img/faces/6-image.jpg"
  }
 ],
 "support": {
  "url": "https://contentcaddy.io?utm_source=reqres&utm_medium=json&utm_campaign=referral",
  "text": "Tired of writing endless social media content? Let Content Caddy generate it for you."
 }
}
  

Path

Since only the internal data property of the response data is used, the path attribute specifies data.

vue
<template>
 <ViewState
  v-slot="users"
  src="reqres://users"
  path="data"
 >
  <JsonView :data="users.data" />
 </ViewState>
</template>
  

List Example

Once the data retrieval is confirmed, the data will be assigned to elements using Vue.

id email first_name last_name avatar
vue
<template>
 <ViewState
  v-slot="users"
  src="reqres://users"
  path="data"
 >
  <b-table striped>
   <thead>
    <tr>
     <th scope="col">
      id
     </th>
     <th scope="col">
      email
     </th>
     <th scope="col">
      first_name
     </th>
     <th scope="col">
      last_name
     </th>
     <th scope="col">
      avatar
     </th>
    </tr>
   </thead>
   <tbody>
    <tr
     v-for="item in users.data"
     :key="item"
    >
     <td>{{ item.id }}</td>
     <td>{{ item.email }}</td>
     <td>{{ item.first_name }}</td>
     <td>{{ item.last_name }}</td>
     <td>
      <Avatar
       circle
       :img-src="item.avatar"
       :img-alt="item.first_name"
      />
     </td>
    </tr>
   </tbody>
  </b-table>
 </ViewState>
</template>

Detail Example

The edits will be reflected in the data.

By default, a PUT request is not sent to the server.

{
 "id": 5,
 "email": "charles.morris@reqres.in",
 "first_name": "Charles",
 "last_name": "Morris",
 "avatar": "https://reqres.in/img/faces/5-image.jpg"
}
  
vue
<template>
 <ViewState
  v-slot="user"
  src="reqres://users/5"
  path="data"
 >
  <Row margin="b-3">
   <BColFormLabel
    for="staticEmail"
    class="col-sm-2"
   >
    Email
   </BColFormLabel>
   <Col col="sm-10">
    <BFormInput
     type="text"
     :state-src="user"
     state-path="email"
    />
   </Col>
  </Row>
  <Row margin="b-3">
   <BColFormLabel
    for="inputPassword"
    class="col-sm-2"
   >
    first_name
   </BColFormLabel>
   <Col col="sm-10">
    <BFormInput
     type="text"
     :state-src="user"
     state-path="first_name"
    />
   </Col>
  </Row>
  <JsonView :data="user.data" />
 </ViewState>
</template>

Data Synchronization

If you register it as a synchronization protocol in nuxt.config.ts, a PUT request will be sent with each edit, synchronizing the data with the server.

You can set Delay and MaxWait.

The synchronization results can be checked with syncStatus and syncResult.

ts
export default defineNuxtConfig({
 usebootstrap: {
  viewState: {
   protocol: {
    'reqres-sync': {
     type: 'fetch',
     prefix: 'https://reqres.in/api/',
     sync: {
      method: 'put',
      delay: 1000,
      maxWait: 3000,
     },
    },
   },
  },
 },
});
{
 "valid": false,
 "location": {},
 "summury": {
  "errors": [],
  "keywords": []
 },
 "schema": {}
}
  
vue
<template>
 <ViewState
  v-slot="user"
  src="reqres-sync://users/5"
  path="data"
  schema-src="app-config://usebootstrap/schemas/basic"
 >
  <Row margin="b-3">
   <BColFormLabel
    for="staticEmail"
    class="col-sm-2"
   >
    Email
   </BColFormLabel>
   <Col col="sm-10">
    <BFormInput
     type="text"
     :state-src="user"
     state-path="email"
    />
   </Col>
  </Row>
  <Row margin="b-3">
   <BColFormLabel
    for="inputPassword"
    class="col-sm-2"
   >
    first_name
   </BColFormLabel>
   <Col col="sm-10">
    <BInputGroup margin="b-3">
     <BFormInput
      type="text"
      :state-src="user"
      state-path="first_name"
     />
     <BInputGroupText style="width:3em;">
      <Spinner
       v-show="user.syncStatus == 100"
       spinner="grow"
       sm
      />
     </BInputGroupText>
    </BInputGroup>
   </Col>
  </Row>
  <JsonView
   v-if="user.validationResult && !user.validationResult.valid"
   :data="user.validationResult"
  />
  <JsonView
   v-else
   :data="user.syncResult"
  />
 </ViewState>
</template>

ActionState

This is an example of sending data by clicking a button.

Specify the HttpMethod and Body in the parameters of the action method.

vue
<template>
 <ActionState src="reqres://login?delay=1">
  <template #default="{ action }">
   <b-button
    color="primary"
    @click="action('post', { email: 'eve.holt@reqres.in', password: 'cityslicka' })"
   >
    Action
   </b-button>
  </template>
  <template #fallback>
   <b-button
    color="primary"
    disabled
   >
    <Spinner
     spinner="grow"
     sm
     aria-hidden="true"
    />
    Loading...
   </b-button>
  </template>
  <template #complete="{ data, status }">
   <div v-if="status.value == 200">
    Response: {{ data }}
   </div>
   <div v-else>
    ErrorMessages: {{ data }}
   </div>
   <b-button
    color="success-subtle"
    @click="status.value = 0"
   >
    Retry
   </b-button>
  </template>
 </ActionState>
</template>