Skip to content

Commit

Permalink
Merge pull request #7 from terwer/dev
Browse files Browse the repository at this point in the history
feat:#6 接口适配器
  • Loading branch information
terwer authored Aug 2, 2022
2 parents f1e23c0 + 2e99470 commit cde364f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 53 deletions.
53 changes: 21 additions & 32 deletions lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,27 @@
/**
* 获取挂件所在的块ID
* @returns {Promise<string>}
*/
import {getBlockByID, getBlockAttrs, setBlockAttrs, exportMdContent} from "./siyuan/siYuanApi";
import {SiYuanApiAdaptor} from "./siyuan/siYuanApiAdaptor";
import {API_TYPE_CONSTANTS} from "./constants";

/**
* 获取页面
* @param pageId 页面ID
*/
export async function getPage(pageId: string) {
return await getBlockByID(pageId)
export interface IApi {
getRecentPosts(numOfPosts: number): Promise<Array<any>>
}

/**
* 获取页面属性
* @param pageId 页面ID
*/
export async function getPageAttrs(pageId: string) {
return await getBlockAttrs(pageId)
}
export class API implements IApi {
type: string
private apiAdaptor: IApi

constructor(type: string) {
this.type = type;
switch (this.type) {
case API_TYPE_CONSTANTS.API_TYPE_SIYUAN:
this.apiAdaptor = new SiYuanApiAdaptor()
break;
default:
throw new Error("未找到接口适配器,请检查参数")
}
}

/**
* 保存页面属性
* @param pageId 页面ID
* @param attrs 属性对象
*/
export async function setPageAttrs(pageId: string, attrs: any) {
return await setBlockAttrs(pageId, attrs)
async getRecentPosts(numOfPosts: number): Promise<Array<any>> {
return this.apiAdaptor.getRecentPosts(numOfPosts);
}
}

/**
* 获取页面的Markdown
* @param pageId
*/
export async function getPageMd(pageId: string) {
return await exportMdContent(pageId);
}
8 changes: 8 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const API_TYPE_SIYUAN = "siyuan"

/**
* API类型常量定义
*/
export const API_TYPE_CONSTANTS = {
API_TYPE_SIYUAN
}
17 changes: 17 additions & 0 deletions lib/siyuan/siYuanApiAdaptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {IApi} from "../api";
import {getBlockByID} from "./siYuanApi";

export class SiYuanApiAdaptor implements IApi {
async getRecentPosts(numOfPosts: number): Promise<Array<any>> {
let result = []

// 本地测试
let pageId = "20220724172444-16a2oc1"

let page = await getBlockByID(pageId)
if (page) {
result.push(page)
}
return Promise.resolve(result);
}
}
41 changes: 20 additions & 21 deletions pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type {GetStaticProps, NextPage} from 'next'
import type {GetServerSideProps, NextPage} from 'next'
import Head from 'next/head'
import Image from 'next/image'
import styles from '../styles/Home.module.css'
import {getPage} from "../lib/api";
import {API} from "../lib/api";
import {API_TYPE_CONSTANTS} from "../lib/constants";

type PageProps = {
hello: string,
page: any,
random: any
type Props = {
posts: any[]
}

const Home: NextPage<PageProps> = (props, context) => {
const Home: NextPage<Props> = (props, context) => {
return (
<div className={styles.container}>
<Head>
Expand All @@ -24,9 +23,7 @@ const Home: NextPage<PageProps> = (props, context) => {
Welcome to <a href="https://nextjs.org">Next.js!</a>
</h1>

<p>{JSON.stringify(props.hello)}</p>
<p>{JSON.stringify(props.page)}</p>
<p>{JSON.stringify(props.random)}</p>
<p>{JSON.stringify(props.posts)}</p>

<p className={styles.description}>
Get started by editing{' '}
Expand Down Expand Up @@ -84,19 +81,21 @@ export default Home

// https://github.com/siyuan-note/siyuan/blob/master/API_zh_CN.md#%E9%89%B4%E6%9D%83
// https://github.com/vercel/next.js/blob/canary/examples/cms-wordpress/pages/index.js
export const getStaticProps: GetStaticProps<PageProps> = async (context) => {
// 本地测试
let pageId = "20220724172444-16a2oc1"

let page = await getPage(pageId)
if (!page) {
page = {}
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
const query = context.query || {}
if (query.t instanceof Array) {
throw new Error("参数类型错误")
}

let result = []
const type = query.t || API_TYPE_CONSTANTS.API_TYPE_SIYUAN
const api = new API(type)

result = await api.getRecentPosts(10)

return {
props: {
hello: 'world',
page: page,
random: Math.random()
posts: result
}
}
}
}

1 comment on commit cde364f

@vercel
Copy link

@vercel vercel bot commented on cde364f Aug 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.