ApiResource.ts 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 (
  31. !this.id || (typeof this.id === "string" && this.id.slice(0, 3) === "tmp")
  32. );
  33. }
  34. }
  35. export default ApiResource;