Skip to content

Commit

Permalink
✨ GET /soundboard & POST /soundboard POST & /soundboard/play のサーバ接続処理を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
pikachu0310 committed Jan 25, 2025
1 parent f8ccdbd commit a875d17
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion src/composables/qall/useQall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ const { addQueue } = useTts()
const meStore = useMeStore()
const rtcSettings = useRtcSettings()

/**
* 認証付きFetch
* qallFetch(input, init) -> fetch(...) にBearerトークンやheadersを付与
*/
const qallFetch = async (
input: string | URL | globalThis.Request,
init?: RequestInit
Expand All @@ -83,6 +87,80 @@ const qallFetch = async (
return res
}

Check warning on line 88 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L74-L88

Added lines #L74 - L88 were not covered by tests

/** 内部で使うサウンドボード一覧の型(サーバレスポンス) */
type SoundboardItem = {
soundId: string
soundName: string
stampId: string
creatorId: string
}
/** サウンドボード一覧 */
type SoundboardListResponse = SoundboardItem[]

/**
* GET /soundboard
* サウンドボード一覧を取得する
*/
const getSoundboardList = async (): Promise<SoundboardListResponse> => {
const res = await qallFetch('/api/soundboard', { method: 'GET' })
if (!res.ok) {
const text = await res.text()
throw new Error(`GET /soundboard failed: ${text}`)
}
return (await res.json()) as SoundboardListResponse
}

Check warning on line 111 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L104-L111

Added lines #L104 - L111 were not covered by tests

/**
* POST /soundboard (multipart/form-data)
* @param file アップロードする音声ファイル (15秒~20秒以内)
* @param soundName ユーザがつける音声の名前
*/
const postSoundboard = async (
file: File,
soundName: string
): Promise<{ soundId: string }> => {

Check warning on line 121 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L118-L121

Added lines #L118 - L121 were not covered by tests
// multipart送信のため、FormDataを利用
const formData = new FormData()
formData.append('audio', file)
formData.append('soundName', soundName)

Check warning on line 125 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L123-L125

Added lines #L123 - L125 were not covered by tests

// Content-Typeは自動設定させたいので、init.headers で空文字を指定して上書きする
const res = await qallFetch('/api/soundboard', {
method: 'POST',

Check warning on line 129 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L128-L129

Added lines #L128 - L129 were not covered by tests
// 下記の書き方で 'application/json' を上書きしないようにする
headers: {
'Content-Type': ''
},
body: formData
})
if (!res.ok) {
const text = await res.text()
throw new Error(`POST /soundboard failed: ${text}`)
}
return await res.json()
}

Check warning on line 141 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L131-L141

Added lines #L131 - L141 were not covered by tests

/**
* POST /soundboard/play
* @param soundId 再生したい音声のID
* @param roomName 対象ルームのUUID文字列
*/
const postSoundboardPlay = async (soundId: string, roomName: string) => {
const body = { soundId, roomName }
const res = await qallFetch('/api/soundboard/play', {
method: 'POST',
body: JSON.stringify(body)

Check warning on line 152 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L148-L152

Added lines #L148 - L152 were not covered by tests
// headers: Content-Type は JSONでOK
// 問題なくqallFetch内で 'application/json' が付与される
})
if (!res.ok) {
const text = await res.text()
throw new Error(`POST /soundboard/play failed: ${text}`)
}

Check warning on line 159 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L155-L159

Added lines #L155 - L159 were not covered by tests
// 正常時に IngressInfo(ingressId, url, streamKey)が返る想定
return await res.json()
}

Check warning on line 162 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L161-L162

Added lines #L161 - L162 were not covered by tests

const purifyRoomData = async (data: RoomsWithParticipants): Promise<Rooms> => {
if (!data) return []
await bothChannelsMapInitialFetchPromise.value
Expand Down Expand Up @@ -181,6 +259,9 @@ export const useQall = () => {
tracksMap,
screenShareTrackSidMap,
screenShareTracks,
qallMitt
qallMitt,
getSoundboardList,
postSoundboard,
postSoundboardPlay

Check warning on line 265 in src/composables/qall/useQall.ts

View check run for this annotation

Codecov / codecov/patch

src/composables/qall/useQall.ts#L261-L265

Added lines #L261 - L265 were not covered by tests
}
}

0 comments on commit a875d17

Please sign in to comment.