ApiResource.ts 1.1 KB

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