Class: RailsVueGenerator::Handlers::ApiHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/rails_vue_generator/handlers/api_handler.rb

Constant Summary

Constants inherited from BaseHandler

BaseHandler::IGNORED_COLUMNS

Instance Method Summary collapse

Methods inherited from BaseHandler

#initialize, #model_name

Constructor Details

This class inherits a constructor from RailsVueGenerator::Handlers::BaseHandler

Instance Method Details

#generateObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_vue_generator/handlers/api_handler.rb', line 6

def generate
  lines = []
  lines << "import axios from 'axios';"
  lines << ""
  lines << "export     const HTTP = axios.create({"
  lines << "  baseURL: process.env.VUE_APP_API_ENDPOINT || 'http://localhost:3000'"
  lines << "})"
  lines << ""
  lines << "export default {"

  lines << "  async getAll() {"
  lines << "    const response = await HTTP.get(`/#{model_name.pluralize}`)"
  lines << "    return response"
  lines << "  },"

  lines << "  async get(id) {"
  lines << "    const response = await HTTP.get(`/#{model_name.pluralize}/${id}`)"
  lines << "    return response"
  lines << "  },"
  
  lines << "  async create(data) {"
  lines << "    const response = await HTTP.post(`/#{model_name.pluralize}`, {#{model_name}: data})"
  lines << "    return response"
  lines << "  },"
  
  lines << "  async update(id, data) {"
  lines << "    const response = await HTTP.put(`/#{model_name.pluralize}/${id}`, {#{model_name}: data})"
  lines << "    return response"
  lines << "  },"
  
  lines << "  async delete(id) {"
  lines << "    const response = await HTTP.delete(`/#{model_name.pluralize}/${id}`)"
  lines << "    return response"
  lines << "  }"
  
  lines << "}"

  lines.join("\n")
end