curd.ts 873 B

12345678910111213141516171819202122232425262728293031323334
  1. import http from '@/lib/http'
  2. class Curd {
  3. protected readonly baseUrl: string
  4. protected readonly plural: string
  5. get_list = this._get_list.bind(this)
  6. get = this._get.bind(this)
  7. save = this._save.bind(this)
  8. destroy = this._destroy.bind(this)
  9. constructor(baseUrl: string, plural: string | null = null) {
  10. this.baseUrl = baseUrl
  11. this.plural = plural ?? this.baseUrl + 's'
  12. }
  13. _get_list(params: any = null) {
  14. return http.get(this.plural, {params: params})
  15. }
  16. _get(id: any = null) {
  17. return http.get(this.baseUrl + (id ? '/' + id : ''))
  18. }
  19. _save(id: any = null, data: any, config: any = undefined) {
  20. return http.post(this.baseUrl + (id ? '/' + id : ''), data, config)
  21. }
  22. _destroy(id: any = null) {
  23. return http.delete(this.baseUrl + '/' + id)
  24. }
  25. }
  26. export default Curd