Skip to content

Commit 5301124

Browse files
committed
refactor: ExtensionAPI添加getExposedAPI方法
1 parent b5155a2 commit 5301124

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/extension/ExtensionAPI.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const apiMetadataKey = Symbol('APIMetadata')
77
type WithAPIMetadata = { [apiMetadataKey]: APIMetatdata }
88
type APIMetatdata = { version: string; schema: z.ZodType }
99

10+
/**标记一个类的方法是暴露的插件API。调用此函数会返回一个stage3装饰器。 */
1011
function exposedAPI<V extends string>(version: V, schema: z.ZodType) {
1112
return function <
1213
This,
@@ -27,24 +28,30 @@ function exposedAPI<V extends string>(version: V, schema: z.ZodType) {
2728
}
2829
}
2930

30-
export function isExposedAPI(value: unknown): value is WithAPIMetadata {
31-
return typeof value === 'function' && apiMetadataKey in value
32-
}
33-
3431
export class ExtensionAPI {
3532
constructor(protected runtime: ExtensionRuntime) {}
33+
getExposedAPI(
34+
key: string,
35+
):
36+
| (((this: typeof this, ...args: unknown[]) => unknown) & WithAPIMetadata)
37+
| null {
38+
const value = this[key as keyof this]
39+
if (typeof value === 'function' && apiMetadataKey in value)
40+
return value as ((this: this, ...args: unknown[]) => unknown) &
41+
WithAPIMetadata
42+
return null
43+
}
3644

37-
@exposedAPI('0.1.0', z.any())
45+
@exposedAPI('0.1.0', z.unknown())
3846
ping() {
3947
return 'pong'
4048
}
4149

4250
@exposedAPI('0.1.0', z.tuple([z.string().min(1)]))
4351
caniuse(identifier: string) {
44-
const func = this[identifier as keyof this] as WithAPIMetadata | undefined,
45-
metadata = func?.[apiMetadataKey]
46-
if (!metadata) return undefined
47-
const { version } = metadata
52+
const func = this.getExposedAPI(identifier)
53+
if (!func) return undefined
54+
const { version } = func[apiMetadataKey]
4855
return { identifier, version, available: true }
4956
}
5057

src/extension/WidgetExtensionRuntime.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { z } from 'zod'
33
import { afterDone, applyOn, PromiseAwaited } from '../utils/func'
44
import { BorrowedHandle } from './BorrowManager'
55
import { encodeReturn, ExtensionRuntime } from './ExtensionRuntime'
6-
import { isExposedAPI, WidgetExtensionAPI } from './ExtensionAPI'
6+
import { WidgetExtensionAPI } from './ExtensionAPI'
77

88
type ResolveHandle<H> = H extends BorrowedHandle<infer O> ? O : H
99
type WidgetExtensionRuntimeExposedAPIs = WidgetExtensionRuntime['exposedAPIs']
@@ -45,13 +45,9 @@ export class WidgetExtensionRuntime extends ExtensionRuntime {
4545
const { call, args } =
4646
WidgetExtensionRuntime.widgetMessageDataBaseSchema.parse(data)
4747

48-
const f = this.exposedAPIs[call as keyof typeof this.exposedAPIs]
49-
if (!isExposedAPI(f)) throw new Error(`No exposed API named ${call}`)
50-
const result = applyOn(
51-
f as (this: typeof this.exposedAPIs, ...args: unknown[]) => unknown,
52-
this.exposedAPIs,
53-
args,
54-
)
48+
const f = this.exposedAPIs.getExposedAPI(call)
49+
if (!f) throw new Error(`No exposed API named ${call}`)
50+
const result = applyOn(f, this.exposedAPIs, args)
5551

5652
afterDone(result, async (data: unknown) =>
5753
source.postMessage({ traceId, return: await encodeReturn(data) }, '*'),

widgets/credit234/credit234.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import nx from '../../extHelper'
22

3-
const r = await nx.newZjuamService({
3+
const service = await nx.newZjuamService({
44
service: 'http://sztz.zju.edu.cn/dekt/',
55
})
66

7-
const rs = (await (
8-
await r.nxFetch('http://sztz.zju.edu.cn/dekt/student/home/getMyInfo')
7+
const result = (await (
8+
await service.nxFetch('http://sztz.zju.edu.cn/dekt/student/home/getMyInfo')
99
).json()) as {
1010
extend: {
1111
myInfo: {
@@ -20,11 +20,11 @@ const rs = (await (
2020
}
2121
}
2222
}
23-
console.log(rs)
23+
console.log(result)
2424
const {
2525
extend: {
2626
myInfo: { dektJf: credit2, dsktJf: credit3, dsiktJf: credit4 },
2727
},
28-
} = rs
28+
} = result
2929
document.body.innerText = `二课分:${credit2}\n三课分:${credit3}\n四课分:${credit4}`
3030
await nx.setWidgetHeight(document.body.scrollHeight)

0 commit comments

Comments
 (0)