Skip to content

Commit

Permalink
refactor(api): devide request building code from each page to `api/…
Browse files Browse the repository at this point in the history
…index.ts`
  • Loading branch information
yoshinorin committed Jul 29, 2024
1 parent 6312284 commit 9a4964b
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 226 deletions.
144 changes: 144 additions & 0 deletions src/api/index.ts
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);
}
20 changes: 3 additions & 17 deletions src/app/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,21 @@ import { Metadata } from "next";
import { headers } from "next/headers";
import { permanentRedirect } from "next/navigation";
import { cache } from "react";
import { api } from "../../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../../api/request";
import { fetchContent } from "../../api";
import {
Content,
ContentResponse,
ContentResponseWithFetchResponse
} from "../../models/models";
import { asInsight } from "../../utils/insight";
import { requestContextFrom } from "../../utils/requestContext";
import { buildUrl, sluggize } from "../../utils/url";
import { sluggize } from "../../utils/url";
import { runWithHandleErrorIf, throwIfError } from "../handler";
import { generateForArticleOrPage } from "../metadata";
import { Renderer } from "./renderer";

// TODO: move somewhere if possible
const cachedFindByPath = cache(async (path: string) => {
// TODO: devide into another `function` and move `api` dir.
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
};
const response = await fetchFromApi(url, options);
const response = await fetchContent(headers(), path);
throwIfError(response);
const content = (await response.json()) as ContentResponse;
return {
Expand Down
17 changes: 2 additions & 15 deletions src/app/archives/page.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
"use server";

import { headers } from "next/headers";
import { api } from "../../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../../api/request";
import { Archive, ArchiveResponse } from "../../models/models";
import { requestContextFrom } from "../../utils/requestContext";
import { buildUrl } from "../../utils/url";
import { runWithHandleErrorIf, throwIfError } from "../handler";
import { Renderer } from "./renderer";
import { fetchArchives } from "../../api";

export default async function Page(req: any) {
return runWithHandleErrorIf(await run(req));
Expand All @@ -23,13 +16,7 @@ async function run(req: any): Promise<any> {
}

async function handler(req: any) {
// TODO: devide into another `function` and move `api` dir.
const url = buildUrl(api.url, "v1/archives", true);
const ctx = requestContextFrom(headers());
const options: RequestOptions = {
headers: requestHeaderFrom(ctx)
};
const response: Response = await fetchFromApi(url, options);
const response: Response = await fetchArchives(headers());
throwIfError(response);

const archiveResponse: Array<ArchiveResponse> =
Expand Down
20 changes: 3 additions & 17 deletions src/app/articles/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ import { Metadata } from "next";
import { headers } from "next/headers";
import { notFound } from "next/navigation";
import { cache } from "react";
import { api } from "../../../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../../../api/request";
import { fetchContent } from "../../../api";
import {
Content,
ContentResponse,
ContentResponseWithFetchResponse
} from "../../../models/models";
import { asInsight } from "../../../utils/insight";
import { isMatch } from "../../../utils/match";
import { requestContextFrom } from "../../../utils/requestContext";
import { buildUrl, sluggize } from "../../../utils/url";
import { sluggize } from "../../../utils/url";
import { runWithHandleErrorIf, throwIfError } from "../../handler";
import { generateForArticleOrPage } from "../../metadata";
import { Renderer } from "./renderer";
Expand All @@ -27,15 +21,7 @@ const PREFIX_URL = "articles";

// TODO: move somewhere if possible
const cachedFindByPath = cache(async (path: string) => {
// TODO: devide into another `function` and move `api` dir.
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
};
const response = await fetchFromApi(url, options);
const response = await fetchContent(headers(), path);
throwIfError(response);
const content = (await response.json()) as ContentResponse;
return {
Expand Down
20 changes: 2 additions & 18 deletions src/app/articles/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
"use server";

import { headers } from "next/headers";
import { api } from "../../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../../api/request";
import { fetchArticles } from "../../api";
import { Article, ArticleResponseWithCount } from "../../models/models";
import { requestContextFrom } from "../../utils/requestContext";
import { buildQueryParams, buildUrl } from "../../utils/url";
import { runWithHandleErrorIf, throwIfError } from "../handler";
import { Renderer } from "./renderer";

Expand All @@ -24,16 +17,7 @@ async function run(req: any): Promise<any> {

async function handler(req: any) {
const currentPage = req.searchParams["p"] ? req.searchParams["p"] : 1;
// TODO: devide into another `function` and move `api` dir.
const url = buildUrl(api.url, "v1/articles", true);
const ctx = requestContextFrom(headers());
const options: RequestOptions = {
headers: requestHeaderFrom(ctx),
queryParams: buildQueryParams({
pagination: { page: currentPage, limit: 10 }
})
};
const response: Response = await fetchFromApi(url, options);
const response: Response = await fetchArticles(headers(), currentPage, 10);
throwIfError(response);

const articlesResponseWithCount: ArticleResponseWithCount =
Expand Down
18 changes: 3 additions & 15 deletions src/app/feeds/index.xml/route.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
import { headers } from "next/headers";
import { api, mainAuthor, siteName, url } from "../../../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../../../api/request";
import { mainAuthor, siteName, url } from "../../../../config";
import { Feed } from "../../../models/models";
import { generateFeedsString } from "../../../services/feeds";
import { requestContextFrom } from "../../../utils/requestContext";
import { buildUrl, sluggize } from "../../../utils/url";
import { fetchFeeds } from "../../../api";

//export async function get(ctx: any) {
export async function GET() {
// TODO: devide into another `function` and move `api` dir.
const apiUrl = buildUrl(api.url, sluggize(["v1", "feeds", "index"]), false);
const ctx = requestContextFrom(headers());
const options: RequestOptions = {
headers: requestHeaderFrom(ctx)
};
const response: Response = await fetchFromApi(apiUrl, options);
const response: Response = await fetchFeeds(headers());

if (response.status !== 200) {
/* NOTE:
Expand Down
18 changes: 2 additions & 16 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
"use server";

import { headers } from "next/headers";
import { api } from "../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../api/request";
import { fetchArticles } from "../api";
import { Article, ArticleResponseWithCount } from "../models/models";
import { requestContextFrom } from "../utils/requestContext";
import { buildQueryParams, buildUrl } from "../utils/url";
import { runWithHandleErrorIf, throwIfError } from "./handler";
import { Renderer } from "./renderer";

Expand All @@ -23,14 +16,7 @@ async function run(req: any): Promise<any> {
}

async function handler(req: any) {
// TODO: devide into another `function` and move `api` dir.
const url = buildUrl(api.url, "v1/articles", true);
const ctx = requestContextFrom(headers());
const options: RequestOptions = {
headers: requestHeaderFrom(ctx),
queryParams: buildQueryParams({ pagination: { page: 1, limit: 5 } })
};
const response: Response = await fetchFromApi(url, options);
const response: Response = await fetchArticles(headers(), 1, 5);
throwIfError(response);

const articlesResponseWithCount: ArticleResponseWithCount =
Expand Down
21 changes: 2 additions & 19 deletions src/app/search/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
"use server";

import { headers } from "next/headers";
import { api } from "../../../config";
import {
RequestOptions,
fetchFromApi,
requestHeaderFrom
} from "../../api/request";
import { fetchSearch } from "../../api";
import {
SearchResponse,
SearchResponseWithCount,
SearchSuccessResult
} from "../../models/models";
import { ProblemDetails, isProblemDetails } from "../../models/problemDetails";
import { requestContextFrom } from "../../utils/requestContext";
import { buildQueryParams, buildUrl, sluggize } from "../../utils/url";
import { Renderer } from "./renderer";

const emptyResult = {
Expand Down Expand Up @@ -87,17 +80,7 @@ async function execute(
req,
words: Array<string>
): Promise<SearchResponseWithCount | ProblemDetails> {
// TODO: devide into another `function` and move `api` dir.
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 }
})
};

const response = await fetchFromApi(url, options);
const response = await fetchSearch(headers(), words);
const responseBody = await response.json();

if (response.status !== 200) {
Expand Down
Loading

0 comments on commit 9a4964b

Please sign in to comment.