mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
feat(global) compatibility milestone
This commit is contained in:
19 files changed
+347
-119
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,96 @@
|
||||
import { fetchNocturneJson } from '@/src/api/endpoint';
|
||||
|
||||
const DEFAULT_PAGE_SIZE = 6;
|
||||
|
||||
export interface PostListResponse {
|
||||
data: {
|
||||
posts: {
|
||||
id: string,
|
||||
identifier: string,
|
||||
title: string,
|
||||
subtitle: string,
|
||||
tags: string[]
|
||||
category: string,
|
||||
date_created: Date,
|
||||
date_updated: Date | null,
|
||||
header_img: string | null
|
||||
}[],
|
||||
pagination: {
|
||||
page: number,
|
||||
page_count: number,
|
||||
page_size: number
|
||||
}
|
||||
},
|
||||
status: string
|
||||
}
|
||||
|
||||
|
||||
export async function fetchPostList(page: number, page_size: number = DEFAULT_PAGE_SIZE): Promise<PostListResponse> {
|
||||
const url = `/blog/posts?page=${ page }&page_size=${ page_size }`;
|
||||
return await fetchNocturneJson(url);
|
||||
}
|
||||
|
||||
|
||||
export interface PostListPaginationResponseData {
|
||||
page_count: number,
|
||||
post_count: number,
|
||||
}
|
||||
|
||||
export interface PostListPaginationResponse {
|
||||
data: PostListPaginationResponseData;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export async function fetchPostListPagination(): Promise<PostListPaginationResponse> {
|
||||
const url = `/blog/posts/pagination`;
|
||||
return await fetchNocturneJson(url);
|
||||
}
|
||||
|
||||
|
||||
export interface FetchPostResponseData {
|
||||
post: {
|
||||
id: string,
|
||||
identifier: string,
|
||||
title: string,
|
||||
subtitle: string,
|
||||
status: string,
|
||||
date_created: string,
|
||||
date_updated: string | null,
|
||||
category: string,
|
||||
tags: string[],
|
||||
content: string,
|
||||
metadata: any | null,
|
||||
},
|
||||
adjacent: {
|
||||
prev: {
|
||||
id: string,
|
||||
identifier: string,
|
||||
title: string,
|
||||
subtitle: string,
|
||||
date_created: string,
|
||||
category: string,
|
||||
tags: string[],
|
||||
header_img: string | null,
|
||||
} | null,
|
||||
next: {
|
||||
id: string,
|
||||
identifier: string,
|
||||
title: string,
|
||||
subtitle: string,
|
||||
date_created: string,
|
||||
category: string,
|
||||
tags: string[],
|
||||
header_img: string | null,
|
||||
} | null,
|
||||
}
|
||||
}
|
||||
|
||||
export interface FetchPostResponse {
|
||||
data: FetchPostResponseData;
|
||||
status: string;
|
||||
}
|
||||
|
||||
export async function fetchPost(id: string): Promise<FetchPostResponse> {
|
||||
const url = `/blog/posts/${ id }`;
|
||||
return await fetchNocturneJson(url);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
const LOCAL_ENDPOINT = process.env.API_LOCAL_URL;
|
||||
const REMOTE_ENDPOINT = process.env.API_REMOTE_URL;
|
||||
const FALLBACK_ENDPOINT = 'http://localhost:3001';
|
||||
|
||||
const NOCTURNE_ENDPOINT = ((isServerSide = true) => {
|
||||
const get_endpoint = () => {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
if (isServerSide) {
|
||||
return LOCAL_ENDPOINT;
|
||||
} else {
|
||||
return REMOTE_ENDPOINT;
|
||||
}
|
||||
} else {
|
||||
return LOCAL_ENDPOINT;
|
||||
}
|
||||
};
|
||||
|
||||
return get_endpoint() ?? FALLBACK_ENDPOINT;
|
||||
});
|
||||
|
||||
export async function fetchNocturneJson<T>(
|
||||
path: string,
|
||||
isServerSideRequest = true,
|
||||
method: string = 'GET'
|
||||
): Promise<T> {
|
||||
const url = `${ NOCTURNE_ENDPOINT(isServerSideRequest) }${ path }`;
|
||||
|
||||
const future = fetch(url, {
|
||||
cache: 'no-cache',
|
||||
method: method
|
||||
});
|
||||
|
||||
const res = (await future);
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Failed to fetch "${ url }" [${ res.status }]: ${ res.statusText }`);
|
||||
}
|
||||
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
Reference in new issue
Block a user