Skip to content

Commit 363e023

Browse files
committed
[refactor] simplify Lark BI Table API with Koa router (fix #6)
[optimize] update Upstream packages
1 parent ad52aa3 commit 363e023

File tree

5 files changed

+390
-295
lines changed

5 files changed

+390
-295
lines changed

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@idea2app/lark-next-bootstrap-ts",
3-
"version": "2.1.0",
3+
"version": "2.4.0",
44
"description": "Lark project scaffold based on TypeScript, React, Next.js, Bootstrap & Workbox.",
55
"private": true,
66
"engines": {
@@ -24,9 +24,10 @@
2424
"copy-webpack-plugin": "^13.0.0",
2525
"core-js": "^3.41.0",
2626
"editorjs-html": "^4.0.5",
27-
"file-type": "^20.4.1",
27+
"file-type": "^20.5.0",
2828
"formidable": "^3.5.4",
2929
"idea-react": "^2.0.0-rc.10",
30+
"jsonwebtoken": "^9.0.2",
3031
"koa": "^2.16.1",
3132
"koajax": "^3.1.2",
3233
"less": "^4.3.0",
@@ -63,7 +64,7 @@
6364
"@babel/plugin-proposal-decorators": "^7.25.9",
6465
"@babel/plugin-transform-typescript": "^7.27.0",
6566
"@babel/preset-react": "^7.26.3",
66-
"@cspell/eslint-plugin": "^8.19.2",
67+
"@cspell/eslint-plugin": "^8.19.3",
6768
"@eslint/compat": "^1.2.8",
6869
"@eslint/eslintrc": "^3.3.1",
6970
"@eslint/js": "^9.25.1",
@@ -76,7 +77,7 @@
7677
"@types/koa__router": "^12.0.4",
7778
"@types/lodash": "^4.17.16",
7879
"@types/next-pwa": "^5.6.9",
79-
"@types/node": "^22.15.2",
80+
"@types/node": "^22.15.3",
8081
"@types/react": "^19.1.2",
8182
"eslint": "^9.25.1",
8283
"eslint-config-next": "^15.3.1",
@@ -90,7 +91,7 @@
9091
"prettier": "^3.5.3",
9192
"prettier-plugin-css-order": "^2.1.2",
9293
"typescript": "~5.8.3",
93-
"typescript-eslint": "^8.31.0"
94+
"typescript-eslint": "^8.31.1"
9495
},
9596
"resolutions": {
9697
"next": "$next"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { withSafeKoa } from '../../../core';
2-
import { proxyLark } from '../../core';
2+
import { proxyLarkAll } from '../../core';
33

44
export const config = { api: { bodyParser: false } };
55

6-
export default withSafeKoa(proxyLark());
6+
export default withSafeKoa(proxyLarkAll);
Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,43 @@
11
import { LarkPageData, TableRecord, TableRecordData } from 'mobx-lark';
22
import { DataObject } from 'mobx-restful';
3+
import { createKoaRouter } from 'next-ssr-middleware';
34

4-
import { withSafeKoa } from '../../../core';
5-
import { proxyLark } from '../../core';
5+
import { withSafeKoaRouter } from '../../../core';
6+
import { proxyLark, proxyLarkAll } from '../../core';
67

78
export const config = { api: { bodyParser: false } };
89

10+
const router = createKoaRouter(import.meta.url);
11+
912
function filterData(fields: DataObject) {
1013
for (const key of Object.keys(fields))
1114
if (!/^\w+$/.test(key)) delete fields[key];
1215
}
1316

14-
const middleware = proxyLark((URI, data) => {
15-
const [path] = URI.split('?');
17+
router.get('/apps/:app/tables/:table/records/:record', async context => {
18+
const { status, body } =
19+
await proxyLark<TableRecordData<DataObject>>(context);
20+
21+
const { fields } = body!.data!.record;
22+
23+
filterData(fields);
1624

17-
if (path.endsWith('/records')) {
18-
const list =
19-
(data as LarkPageData<TableRecord<DataObject>>).data!.items || [];
25+
context.status = status;
26+
context.body = body;
27+
});
28+
29+
router.get('/apps/:app/tables/:table/records', async context => {
30+
const { status, body } =
31+
await proxyLark<LarkPageData<TableRecord<DataObject>>>(context);
2032

21-
for (const { fields } of list) filterData(fields);
22-
} else if (path.split('/').at(-2) === 'records') {
23-
const { record } = (data as TableRecordData<DataObject>).data!;
33+
const list = body!.data!.items || [];
2434

25-
filterData(record.fields);
26-
}
35+
for (const { fields } of list) filterData(fields);
2736

28-
return data;
37+
context.status = status;
38+
context.body = body;
2939
});
3040

31-
export default withSafeKoa(middleware);
41+
router.all('/(.*)', proxyLarkAll);
42+
43+
export default withSafeKoaRouter(router);

pages/api/Lark/core.ts

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Middleware } from 'koa';
1+
import { Context, Middleware } from 'koa';
22
import { marked } from 'marked';
33
import {
44
LarkApp,
@@ -18,30 +18,27 @@ export const lark = new LarkApp(larkAppMeta);
1818
export const normalizeMarkdownArray = (list: TableCellText[]) =>
1919
normalizeTextArray(list).map(text => marked(text) as string);
2020

21-
export const proxyLark =
22-
<T extends LarkData>(dataFilter?: (path: string, data: T) => T): Middleware =>
23-
async context => {
24-
const { method, url, headers, request } = context;
21+
export const proxyLark = async <T extends LarkData>({
22+
method,
23+
url,
24+
headers: { host, authorization, ...headers },
25+
request,
26+
}: Context) => {
27+
await lark.getAccessToken();
2528

26-
if (!headers.authorization) await lark.getAccessToken();
29+
const path = url!.slice(`/api/Lark/`.length),
30+
body = Reflect.get(request, 'body');
2731

28-
delete headers.host;
29-
30-
const path = url!.slice(`/api/Lark/`.length);
31-
32-
const { status, body } = await lark.client.request<T>({
33-
// @ts-expect-error Type compatibility issue
34-
method,
35-
path,
36-
// @ts-expect-error Type compatibility issue
37-
headers,
38-
body: Reflect.get(request, 'body'),
39-
});
32+
// @ts-expect-error Type compatibility issue
33+
return lark.client.request<T>({ method, path, headers, body });
34+
};
4035

41-
context.status = status;
36+
export const proxyLarkAll: Middleware = async context => {
37+
const { status, body } = await proxyLark(context);
4238

43-
context.body = dataFilter?.(path, body!) || body;
44-
};
39+
context.status = status;
40+
context.body = body;
41+
};
4542

4643
export const larkOauth2 = oauth2Signer({
4744
signInURL: URI => new LarkApp(larkAppMeta).getWebSignInURL(URI),

0 commit comments

Comments
 (0)