Skip to content

Commit 8f92d0d

Browse files
authored
[refactor] make GitHub & Lark API clearer & safer with Koa 2 & Next-SSR-middleware 0.10 (#77)
1 parent 9d4b58a commit 8f92d0d

File tree

24 files changed

+3268
-1964
lines changed

24 files changed

+3268
-1964
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
public-hoist-pattern[] = *import-in-the-middle*
2+
public-hoist-pattern[] = *require-in-the-middle*
13
auto-install-peers = false

components/Client/Partner.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Tooltip } from '@mui/material';
22
import { FC, ReactNode } from 'react';
33

4+
import { fileURLOf } from '../../models/Base';
45
import { Client } from '../../models/Client';
5-
import { fileURLOf } from '../../pages/api/Lark/file/[id]';
66
import { LarkImage } from '../LarkImage';
77

88
export interface PartnerProps extends Client {
@@ -41,7 +41,7 @@ export const LogoWithLink: FC<Omit<PartnerOverviewProps, 'tooltip'>> = ({
4141
address,
4242
logo,
4343
logoDark,
44-
className
44+
className,
4545
}) => (
4646
<a
4747
key={name}

components/Icon.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { FC, HTMLAttributes, PropsWithRef } from 'react';
1+
import { FC, HTMLAttributes } from 'react';
22

3-
export type IconProps = PropsWithRef<
4-
HTMLAttributes<HTMLSpanElement> & {
5-
name: string;
6-
variant?: 'outlined' | 'rounded' | 'sharp';
7-
}
8-
>;
3+
export interface IconProps extends HTMLAttributes<HTMLSpanElement> {
4+
name: string;
5+
variant?: 'outlined' | 'rounded' | 'sharp';
6+
}
97

108
export const SymbolIcon: FC<IconProps> = ({ className, name, variant = 'outlined', ...props }) => (
119
<span

components/LarkImage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { TableCellValue } from 'mobx-lark';
22
import { ImageProps } from 'next/image';
33
import { FC } from 'react';
44

5-
import { DefaultImage, fileURLOf } from '../pages/api/Lark/file/[id]';
5+
import { fileURLOf } from '../models/Base';
6+
import { DefaultImage } from '../models/configuration';
67

78
export interface LarkImageProps extends Omit<ImageProps, 'src'> {
89
src?: TableCellValue;

components/PageHead.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import Head from 'next/head';
22
import type { FC, PropsWithChildren } from 'react';
33

4+
import { Name, Summary } from '../models/configuration';
5+
46
export type PageHeadProps = PropsWithChildren<{
57
title?: string;
68
description?: string;
79
}>;
810

9-
const Name = process.env.NEXT_PUBLIC_SITE_NAME,
10-
Summary = process.env.NEXT_PUBLIC_SITE_SUMMARY;
11-
1211
export const PageHead: FC<PageHeadProps> = ({ title, description = Summary, children }) => (
1312
<Head>
1413
<title>{(title ? `${title} - ` : '') + Name}</title>
File renamed without changes.

instrumentation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const { NEXT_RUNTIME } = process.env;
2+
3+
export async function register() {
4+
if (NEXT_RUNTIME === 'nodejs') await import('./sentry.server.config');
5+
6+
if (NEXT_RUNTIME === 'edge') await import('./sentry.edge.config');
7+
}

models/Base.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import { HTTPClient } from 'koajax';
2+
import MIME from 'mime';
3+
import { TableCellValue, TableCellMedia, TableCellAttachment } from 'mobx-lark';
24

3-
export const isServer = () => typeof window === 'undefined';
4-
5-
export const VercelHost = process.env.VERCEL_URL,
6-
LarkBaseId = process.env.NEXT_PUBLIC_LARK_BASE!;
7-
8-
export const API_Host = isServer()
9-
? VercelHost
10-
? `https://${VercelHost}`
11-
: 'http://localhost:3000'
12-
: globalThis.location.origin;
5+
import { API_Host } from './configuration';
136

147
export const larkClient = new HTTPClient({
158
baseURI: `${API_Host}/api/Lark/`,
169
responseType: 'json',
1710
});
11+
12+
export function fileURLOf(field: TableCellValue, cache = false) {
13+
if (!(field instanceof Array) || !field[0]) return field + '';
14+
15+
const file = field[0] as TableCellMedia | TableCellAttachment;
16+
17+
let URI = `/api/Lark/file/${'file_token' in file ? file.file_token : file.attachmentToken}`;
18+
19+
if (cache) URI += '.' + MIME.getExtension('type' in file ? file.type : file.mimeType);
20+
21+
return URI;
22+
}

models/Client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BiDataQueryOptions, BiDataTable, TableCellValue } from 'mobx-lark';
22

3-
import { LarkBaseId, larkClient } from './Base';
3+
import { larkClient } from './Base';
4+
import { LarkBaseId } from './configuration';
45

56
export type Client = Record<
67
'id' | 'name' | 'type' | 'partnership' | 'image' | 'summary' | 'address',

models/Member.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BiDataQueryOptions, BiDataTable, TableCellValue } from 'mobx-lark';
22

3-
import { LarkBaseId, larkClient } from './Base';
3+
import { larkClient } from './Base';
4+
import { LarkBaseId } from './configuration';
45

56
export type Member = Record<
67
'id' | 'nickname' | 'type' | 'skill' | 'position' | 'summary' | 'github' | 'joinedAt',

0 commit comments

Comments
 (0)