Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:接口适配器 #6

Closed
terwer opened this issue Aug 2, 2022 · 0 comments
Closed

feat:接口适配器 #6

terwer opened this issue Aug 2, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@terwer
Copy link
Owner

terwer commented Aug 2, 2022

使用接口适配器适配各种平台,只需要传入参数 ?t= 即可

服务端属性

request阶段生成,非常适合复杂交互

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: {
            posts: result
        }
    }
}

客户端属性

构建阶段生成,不适合动态变化的页面

export const getStaticProps: GetStaticProps<Props, Params> = async (context) => {
    // 本地测试
    let pageId = "20220724172444-16a2oc1"

    let page = await getPage(pageId)
    if (!page) {
        page = {}
    }

    // ! is a non-null assertion
    const params = context.params! || {}
    return {
        props: {
            page: page,
            params: params
        }
    }
}
@terwer terwer self-assigned this Aug 2, 2022
@terwer terwer added the enhancement New feature or request label Aug 2, 2022
terwer added a commit that referenced this issue Aug 2, 2022
terwer added a commit that referenced this issue Aug 2, 2022
@terwer terwer closed this as completed Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant