Skip to content

Commit a2681aa

Browse files
committed
feat: add lyrics api
Signed-off-by: Innei <[email protected]>
1 parent b7c51a4 commit a2681aa

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

global.d.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable @typescript-eslint/no-empty-interface */
2-
/* eslint-disable @typescript-eslint/no-unused-vars */
31
// THIS FILE FOR MONACO EDITOR CODE COMPLETION
42

53
declare interface FunctionContextResponse {
@@ -36,26 +34,34 @@ declare interface Context {
3634
readAsset: (path: string, options: any) => void
3735
}
3836

37+
declare interface IDb {
38+
get<T = any>(key: string): Promise<T>
39+
set(key: string, value: any): Promise<void>
40+
find<T = any>(condition: Record<string, any>): Promise<T[]>
41+
del(key: string): Promise<void>
42+
insert(key: string, value: any): Promise<void>
43+
update(key: string, value: any): Promise<void>
44+
}
45+
3946
declare interface IStorage {
40-
db: any
47+
db: IDb
4148
cache: {
4249
get(key: string): Promise<string>
4350
set(key: string, value: string | object): Promise<string>
44-
dek(key: string): Promise<string>
51+
del(key: string): Promise<string>
4552
}
53+
dangerousAccessDbInstance: () => any
4654
}
4755

4856
declare const __dirname: string
4957
declare const __filename: ''
5058

5159
declare class Buffer {
52-
constructor(...args: any[]): Buffer
60+
constructor(...args: any[])
5361
from(...args: any[]): Buffer
5462
[x: string]: any
5563
}
5664

57-
declare const console: Console
58-
5965
declare const logger: Console
6066

6167
declare const process: {
@@ -105,4 +111,5 @@ declare class UserModel {
105111

106112
socialIds?: any
107113
}
114+
108115
declare function require(id: string, useCache?: boolean): Promise<any>

kami/functions/lyrics.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import extra from '@mx-space/extra'
2+
3+
const cacheKey = 'netease-lyrics'
4+
async function handler() {
5+
const { NeteaseCloudMusicApi } = extra
6+
const {
7+
req: { query },
8+
9+
storage: { cache },
10+
} = context
11+
12+
if (!query.id) {
13+
context.throws(400, 'id is required')
14+
}
15+
16+
const { get, set } = cache
17+
const fromCache = await get(cacheKey + ':' + query.id)
18+
if (fromCache) {
19+
return fromCache
20+
}
21+
22+
const { lyric } = NeteaseCloudMusicApi
23+
const result =
24+
(
25+
await lyric({
26+
id: query.id,
27+
})
28+
).body?.lrc?.lyric || ''
29+
result && (await set(`${cacheKey}:${query.id}`, result))
30+
return result
31+
}

0 commit comments

Comments
 (0)