-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): devide request building code from
each page
to `api/…
…index.ts`
- Loading branch information
1 parent
6312284
commit 9a4964b
Showing
15 changed files
with
176 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { api, publicApi } from "../../config"; | ||
import { requestContextFrom } from "../utils/requestContext"; | ||
import { buildQueryParams, buildUrl, sluggize } from "../utils/url"; | ||
import { RequestOptions, fetchFromApi, requestHeaderFrom } from "./request"; | ||
|
||
export function fetchArticles( | ||
headers: Headers, | ||
currentPage: number, | ||
limit: number | ||
): Promise<Response> { | ||
const url = buildUrl(api.url, "v1/articles", true); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx), | ||
queryParams: buildQueryParams({ | ||
pagination: { page: currentPage, limit: limit } | ||
}) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchArchives(headers: Headers): Promise<Response> { | ||
const url = buildUrl(api.url, "v1/archives", true); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchContent( | ||
headers: Headers, | ||
path: string | ||
): Promise<Response> { | ||
const slug = sluggize(["v1", "contents", path]); | ||
const url = buildUrl(api.url, slug, true); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx), | ||
interceptIfContainsIgnorePaths: true | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchFeeds(headers: Headers): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "feeds", "index"]), false); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchSearch( | ||
headers: Headers, | ||
words: Array<string> | ||
): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "search"]), false); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx), | ||
queryParams: buildQueryParams({ | ||
params: { key: "q", values: words } | ||
}) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchAllSeries(headers: Headers): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "series"]), true); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchSeries( | ||
headers: Headers, | ||
seriesName: string | ||
): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "series", seriesName]), false); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchSitemap(headers: Headers): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "sitemaps"]), true); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchStatus(headers: Headers): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "system", "health"]), false); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchAllTags(headers: Headers): Promise<Response> { | ||
const url = buildUrl(api.url, sluggize(["v1", "tags"]), true); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchTag( | ||
headers: Headers, | ||
tagName: string, | ||
currentPage: number | ||
): Promise<Response> { | ||
const url = buildUrl( | ||
api.url, | ||
sluggize(["v1", "tags", encodeURI(tagName)]), | ||
false | ||
); | ||
const ctx = requestContextFrom(headers); | ||
const options: RequestOptions = { | ||
headers: requestHeaderFrom(ctx), | ||
queryParams: buildQueryParams({ | ||
pagination: { page: currentPage, limit: 10 } | ||
}) | ||
}; | ||
return fetchFromApi(url, options); | ||
} | ||
|
||
export function fetchSystemMetadata(): Promise<Response> { | ||
const url = buildUrl( | ||
publicApi.url, | ||
sluggize(["v1", "system", "metadata"]), | ||
false | ||
); | ||
return fetchFromApi(url); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.