@@ -15,6 +15,19 @@ import type {
1515 VersionEntry ,
1616} from "@/entities/docs/model/types" ;
1717import { dedupeVersionEntriesById } from "../lib/dedupe-version-entries" ;
18+ import {
19+ DEFAULT_LAYOUTS_PATH ,
20+ DEFAULT_TEMPLATES_BASE_PATH ,
21+ OFFICIAL_LAYOUTS_CONFIG_URL ,
22+ OFFICIAL_LAYOUTS_TEMPLATES_URL ,
23+ } from "@/shared/config/remote-urls" ;
24+ import {
25+ tryFetchText ,
26+ fetchRepoText ,
27+ fetchUrlText ,
28+ fetchRepoJson ,
29+ fetchUrlJson ,
30+ } from "@/shared/api/fetch-client" ;
1831
1932type VersionConfig = {
2033 routes ?: RouteConfig [ ] ;
@@ -24,13 +37,6 @@ type VersionConfig = {
2437} ;
2538export type SupportedLanguage = "en" | "pt" | "es" ;
2639
27- const REQUEST_TIMEOUT_MS = 12000 ;
28- const DEFAULT_LAYOUTS_PATH = "gitpagedocs/layouts/layoutsConfig.json" ;
29- const DEFAULT_TEMPLATES_BASE_PATH = "gitpagedocs/layouts/" ;
30- const OFFICIAL_LAYOUTS_CONFIG_URL =
31- "https://github.com/Vidigal-code/git-page-docs/blob/main/gitpagedocs/layouts/layoutsConfig.json" ;
32- const OFFICIAL_LAYOUTS_TEMPLATES_URL =
33- "https://github.com/Vidigal-code/git-page-docs/blob/main/gitpagedocs/layouts/templates" ;
3440export async function loadStandaloneLayoutsAndThemes ( ) : Promise < {
3541 layoutsConfig : LayoutsConfig ;
3642 themes : Record < string , ThemeTemplate > ;
@@ -109,61 +115,6 @@ function stripFrontMatter(markdown: string): string {
109115 return markdown . replace ( / ^ - - - \r ? \n [ \s \S ] * ?\r ? \n - - - \r ? \n / , "" ) ;
110116}
111117
112- async function tryFetchText ( url : string ) : Promise < string | null > {
113- const controller = new AbortController ( ) ;
114- const timer = setTimeout ( ( ) => controller . abort ( ) , REQUEST_TIMEOUT_MS ) ;
115- try {
116- const response = await fetch ( url , { cache : "no-store" , signal : controller . signal } ) ;
117- if ( ! response . ok ) {
118- return null ;
119- }
120- return await response . text ( ) ;
121- } catch {
122- return null ;
123- } finally {
124- clearTimeout ( timer ) ;
125- }
126- }
127-
128- async function fetchRepoText ( owner : string , repo : string , relativePath : string ) : Promise < string | null > {
129- const candidates = buildGithubRawCandidates ( owner , repo , relativePath ) ;
130- for ( const candidate of candidates ) {
131- const content = await tryFetchText ( candidate ) ;
132- if ( content !== null ) {
133- return content ;
134- }
135- }
136- return null ;
137- }
138-
139- async function fetchUrlText ( url : string ) : Promise < string | null > {
140- return tryFetchText ( toRawGithubUrl ( url ) ) ;
141- }
142-
143- async function fetchRepoJson < T > ( owner : string , repo : string , relativePath : string ) : Promise < T | null > {
144- const text = await fetchRepoText ( owner , repo , relativePath ) ;
145- if ( ! text ) {
146- return null ;
147- }
148- try {
149- return JSON . parse ( text ) as T ;
150- } catch {
151- return null ;
152- }
153- }
154-
155- async function fetchUrlJson < T > ( url : string ) : Promise < T | null > {
156- const text = await fetchUrlText ( url ) ;
157- if ( ! text ) {
158- return null ;
159- }
160- try {
161- return JSON . parse ( text ) as T ;
162- } catch {
163- return null ;
164- }
165- }
166-
167118function getAvailableLanguages (
168119 routes : Array < { path ?: Record < LanguageCode , string > } > ,
169120 fallbackLanguage : LanguageCode ,
0 commit comments