Giter Site home page Giter Site logo

Use Apicase about beauty-vuejs-boilerplate HOT 4 CLOSED

zmts avatar zmts commented on July 21, 2024
Use Apicase

from beauty-vuejs-boilerplate.

Comments (4)

zmts avatar zmts commented on July 21, 2024

@Kelin2025 Tnx for suggestion. I will check your way.

from beauty-vuejs-boilerplate.

zmts avatar zmts commented on July 21, 2024

At first this boilerplate created for maximum transparent example how to structure vue.js projects.
Axios fortunately is great, well known and tested many users library. I think Axios doing a great job in my case.

I took a look at your services section. To be honest, it looks awful.

I wholly disagreed with yours opinion. Work with API in this project based on ES6 classes, and very flexible to next extension. Each api call can be modified to each case and encapsulated to one convenient interface. Using pure axios inside components, yes it really awful, but not as class based services.

you don't need to write tons of boilerplate code.

Unfortunately, we're not going anywhere.

And yes i will look at Apicase more closely when I have time.

from beauty-vuejs-boilerplate.

Kelin2025 avatar Kelin2025 commented on July 21, 2024

No-no-no-no-no, you didn't understand why I called it awful.

Using pure axios inside components, yes it really awful, but not as class based services.

I meant just this :)
If you want to use just requests, of course, there are no difference between Apicase and axios

BUT! Axios doesn't have flexible and scalable services. That's why you have services/auth.js with a hand-made classes and tons of boilerplate.

Simple example of auth service:

/* Auth service */
const AuthService = new ApiService({
  url: '/api/auth',
  method: 'POST'
})

/* Save token on success auth */
AuthService.on('done', result => { 
  localStorage.setItem('token', result.body.token) 
})

/* Calling service */
AuthService.doRequest({
  body: { login: 'Hello', password: 'foobar' }
})

Example of services that depends on auth

/* Root services with auth check */
const Root = new ApiService(fetch, {
  url: '/api',
  hooks: {
    /* 
      Check for auth and inject auth token. 
      If it fails, abort request with 401 error 
    */
    before ({ payload, meta, next }) {
      const token = localStorage.getItem("token")
      if (!token) {
        if (meta.requiresAuth) {
          fail({ status: 401 })
        } else {
          next(payload)
        }
      } else {
        payload.headers = { ...payload.headers, Authorization: token }
        next(payload)
      }
    }
  }
})

/* Just array of services that will inherit Root hook and url */
const API = new ApiTree(Root, [
  { 
    name: 'getPosts', 
    url: 'posts' 
  },
  { 
    name: 'changeProfile', 
    url: 'profile/edit', 
    meta: { requiresAuth: true } 
  }
])

And there are a lot of features which I can't explain in a single message. Just try it ;)

from beauty-vuejs-boilerplate.

zmts avatar zmts commented on July 21, 2024

I'll try it! Tnx

from beauty-vuejs-boilerplate.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.