-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from EstrellaXD/fix/bangumi-search-api
fix: 修复 bangumi search API 中的流式接口,webui 对应接口定义方法改造
- Loading branch information
Showing
12 changed files
with
177 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"recommendations": [ | ||
// https://marketplace.visualstudio.com/items?itemName=antfu.unocss | ||
"antfu.unocss", | ||
// https://marketplace.visualstudio.com/items?itemName=formulahendry.auto-rename-tag | ||
"formulahendry.auto-rename-tag", | ||
// https://marketplace.visualstudio.com/items?itemName=streetsidesoftware.code-spell-checker | ||
"streetsidesoftware.code-spell-checker", | ||
// https://marketplace.visualstudio.com/items?itemName=naumovs.color-highlight | ||
"naumovs.color-highlight", | ||
// https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance | ||
"ms-python.vscode-pylance", | ||
// https://marketplace.visualstudio.com/items?itemName=ms-python.python | ||
"ms-python.python", | ||
// https://marketplace.visualstudio.com/items?itemName=vue.volar | ||
"vue.volar" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Dev Backend", | ||
"type": "python", | ||
"request": "launch", | ||
"cwd": "${workspaceFolder}/backend/src", | ||
"program": "main.py", | ||
"console": "integratedTerminal", | ||
"justMyCode": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,80 @@ | ||
import { | ||
Observable, | ||
} from 'rxjs'; | ||
|
||
import type { BangumiRule } from '#/bangumi'; | ||
|
||
export const apiSearch = { | ||
async get(keyword: string, site = 'mikan') { | ||
const { data } = await axios.get('api/v1/search', { | ||
params: { | ||
site, | ||
keyword, | ||
}, | ||
/** | ||
* 番剧搜索接口是 Server Send 流式数据,每条是一个 Bangumi JSON 字符串, | ||
* 使用接口方式需要订阅使用 | ||
* | ||
* Usage Example: | ||
* | ||
* ```ts | ||
* import { | ||
* Subject, | ||
* tap, | ||
* map, | ||
* switchMap, | ||
* debounceTime, | ||
* } from 'rxjs'; | ||
* | ||
* | ||
* const input$ = new Subject<string>(); | ||
* const onInput = (e: Event) => input$.next(e.target); | ||
* | ||
* // vue: <input @input="onInput"> | ||
* | ||
* const bangumiInfo$ = apiSearch.get('魔女之旅'); | ||
* | ||
* // vue: start loading animation | ||
* | ||
* input$.pipe( | ||
* debounceTime(1000), | ||
* tap((input: string) => { | ||
* console.log('input', input) | ||
* // clear Search Result List | ||
* }), | ||
* | ||
* // switchMap 把输入 keyword 查询为 bangumiInfo$ 流,多次输入停用前一次查询 | ||
* switchMap((input: string) => apiSearch(input, site)), | ||
* | ||
* tap((bangumi: BangumiRule) => console.log(bangumi)), | ||
* tap((bangumi: BangumiRule) => { | ||
* console.log('bangumi', bangumi) | ||
* // set bangumi info to Search Result List | ||
* }), | ||
* ).subscribe({ | ||
* complete() { | ||
* // end of stream, stop loading animation | ||
* }, | ||
* }) | ||
* ``` | ||
*/ | ||
get(keyword: string, site = 'mikan'): Observable<BangumiRule> { | ||
const bangumiInfo$ = new Observable<BangumiRule>(observer => { | ||
const eventSource = new EventSource( | ||
`api/v1/search?site=${site}&keyword=${encodeURIComponent(keyword)}`, | ||
{ withCredentials: true }, | ||
); | ||
|
||
eventSource.onmessage = ev => { | ||
try { | ||
const data: BangumiRule = JSON.parse(ev.data); | ||
observer.next(data); | ||
} catch (error) { | ||
observer.error(error); | ||
} | ||
}; | ||
|
||
eventSource.onerror = ev => observer.error(ev); | ||
|
||
return () => { | ||
eventSource.close(); | ||
}; | ||
}); | ||
return data!; | ||
|
||
return bangumiInfo$; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* https://unocss.dev/presets/attributify#vue-3 | ||
*/ | ||
|
||
import type { AttributifyAttributes } from '@unocss/preset-attributify' | ||
|
||
declare module '@vue/runtime-dom' { | ||
interface HTMLAttributes extends AttributifyAttributes {} | ||
} |