ApiResource.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { Model } from 'pinia-orm'
  2. /**
  3. * Base class for resources that can be fetched from the API
  4. */
  5. class ApiResource extends Model {
  6. // eslint-disable-next-line no-use-before-define
  7. protected static _iriEncodedFields: Record<string, ApiResource>
  8. public static addIriEncodedField(name: string, target: ApiResource) {
  9. if (!this._iriEncodedFields) {
  10. this._iriEncodedFields = {}
  11. }
  12. this._iriEncodedFields[name] = target
  13. }
  14. public static getIriEncodedFields() {
  15. return this._iriEncodedFields
  16. }
  17. /**
  18. * Fix the 'Cannot stringify arbitrary non-POJOs' warning, meaning server can not parse the store
  19. *
  20. * @see https://github.com/vuex-orm/vuex-orm/issues/255#issuecomment-876378684
  21. */
  22. toJSON() {
  23. return { ...this }
  24. }
  25. /**
  26. * Is it a newly created entity?
  27. *
  28. * If it is, it means this entity does not exist in the data source and that it has a temporary id
  29. */
  30. public isNew(): boolean {
  31. return (
  32. !this.id || (typeof this.id === 'string' && this.id.slice(0, 3) === 'tmp')
  33. )
  34. }
  35. }
  36. export default ApiResource