modelsUtils.ts 880 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {models} from "~/models/_import";
  2. export default class ModelsUtils {
  3. /**
  4. * Extrait l'ID de l'URI passée en paramètre
  5. * @param uri
  6. */
  7. static extractIdFromUri (uri: string): number|null {
  8. if(!uri) return null
  9. const partUri: Array<string> = uri.split('/')
  10. const id:any = partUri.pop()
  11. if(isNaN(id))
  12. throw new Error('id is not a number')
  13. return parseInt(id)
  14. }
  15. /**
  16. * Extrait l'ID de l'URI passée en paramètre
  17. * @param iri
  18. */
  19. static getModelFromIri (iri: string): any {
  20. const matches = iri.match(/^\/api\/(\w+)\/.*/)
  21. if (!matches || !matches[1]) {
  22. throw new Error('cannot parse the IRI')
  23. }
  24. const entityName = matches[1]
  25. let model = models.find(candidate => { return candidate.entity === entityName })
  26. if (!model) {
  27. throw new Error('no model found')
  28. }
  29. return model
  30. }
  31. }