| 32 | */ |
| 33 | @Service() |
| 34 | export class VersionManager { |
| 35 | private document = inject(DOCUMENT); |
| 36 | |
| 37 | readonly currentDocsVersionMode = computed<VersionMode>(() => { |
| 38 | const hostname = this.document.location.hostname; |
| 39 | if (hostname.startsWith('v')) return 'deprecated'; |
| 40 | if (hostname.startsWith('rc')) return 'rc'; |
| 41 | if (hostname.startsWith('next')) return 'next'; |
| 42 | |
| 43 | return 'stable'; |
| 44 | }); |
| 45 | |
| 46 | private localVersions = (versionJson as VersionJson[]).map((v) => { |
| 47 | return { |
| 48 | displayName: v.version, |
| 49 | url: v.url, |
| 50 | }; |
| 51 | }); |
| 52 | |
| 53 | // This handle the fallback if the resource fails. |
| 54 | readonly versions = computed(() => { |
| 55 | return this.remoteVersions.hasValue() ? this.remoteVersions.value() : this.localVersions; |
| 56 | }); |
| 57 | |
| 58 | // Yes this will trigger a cors error on localhost |
| 59 | // but this is fine as we'll fallback to the local versions.json |
| 60 | // which is the most up-to-date anyway. |
| 61 | remoteVersions = httpResource( |
| 62 | () => ({ |
| 63 | url: 'https://angular.dev/assets/others/versions.json', |
| 64 | transferCache: false, |
| 65 | cache: 'no-cache', |
| 66 | }), |
| 67 | { |
| 68 | parse: (json: unknown) => { |
| 69 | if (!Array.isArray(json)) { |
| 70 | throw new Error('Invalid version data'); |
| 71 | } |
| 72 | return json.map((v: unknown) => { |
| 73 | if ( |
| 74 | v === undefined || |
| 75 | v === null || |
| 76 | typeof v !== 'object' || |
| 77 | !('version' in v) || |
| 78 | !('url' in v) || |
| 79 | typeof v.version !== 'string' || |
| 80 | typeof v.url !== 'string' |
| 81 | ) { |
| 82 | throw new Error('Invalid version data'); |
| 83 | } |
| 84 | |
| 85 | return { |
| 86 | displayName: v.version, |
| 87 | url: v.url, |
| 88 | }; |
| 89 | }); |
| 90 | }, |
| 91 | }, |