-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
|
||
import useSignedVideoToken from "./useSignedVideoToken"; | ||
|
||
const mockUseSWR = jest.fn<{ data: unknown; error: unknown }, []>(() => ({ | ||
data: null, | ||
error: null, | ||
})); | ||
|
||
jest.mock("swr", () => ({ | ||
__esModule: true, | ||
default: (...args: []) => mockUseSWR(...args), | ||
})); | ||
|
||
describe("useSignedVideoToken", () => { | ||
test("'loading' should default to false on public video", () => { | ||
const { result } = renderHook(() => | ||
useSignedVideoToken({ | ||
playbackId: "123", | ||
playbackPolicy: "public", | ||
}) | ||
); | ||
const { loading } = result.current; | ||
|
||
expect(loading).toBe(false); | ||
}); | ||
test("'loading' should default to true on signed video", () => { | ||
const { result } = renderHook(() => | ||
useSignedVideoToken({ | ||
playbackId: "123", | ||
playbackPolicy: "signed", | ||
}) | ||
); | ||
const { loading } = result.current; | ||
|
||
expect(loading).toBe(true); | ||
}); | ||
test("should return correct state on error ", () => { | ||
mockUseSWR.mockImplementationOnce(() => ({ data: null, error: "error" })); | ||
const { result } = renderHook(() => | ||
useSignedVideoToken({ | ||
playbackId: "123", | ||
playbackPolicy: "signed", | ||
}) | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
loading: false, | ||
playbackToken: null, | ||
error: "error", | ||
}); | ||
}); | ||
test("should return correct signed playback token ", () => { | ||
mockUseSWR.mockImplementationOnce(() => ({ | ||
data: { token: "1234" }, | ||
error: null, | ||
})); | ||
const { result } = renderHook(() => | ||
useSignedVideoToken({ | ||
playbackId: "123", | ||
playbackPolicy: "signed", | ||
}) | ||
); | ||
|
||
expect(result.current).toEqual({ | ||
loading: false, | ||
playbackToken: "1234", | ||
error: null, | ||
}); | ||
}); | ||
}); |
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,107 @@ | ||
import { useEffect } from "react"; | ||
import useSWR from "swr"; | ||
|
||
import errorReporter from "../../common-lib/error-reporter"; | ||
import OakError from "../../errors/OakError"; | ||
|
||
const reportError = errorReporter("useSignedPlaybackId"); | ||
export const apiEndpoint = | ||
"https://api.thenational.academy/api/signed-video-token"; | ||
|
||
export const options = { | ||
revalidateOnFocus: false, | ||
dedupingInterval: 6 * 60 * 60 * 1000, // Don't generate a new signedUrl unless the old one has expired | ||
}; | ||
|
||
export type PlaybackPolicy = "public" | "signed"; | ||
|
||
type UseSignedPlaybackIdProps = { | ||
playbackId: string; | ||
playbackPolicy: PlaybackPolicy; | ||
}; | ||
|
||
type UseSignedPlaybackIdReturnProps = { | ||
loading: boolean; | ||
error: Error | null; | ||
playbackToken: string | null | undefined; | ||
}; | ||
|
||
const useSignedVideoToken = ({ | ||
playbackId, | ||
playbackPolicy, | ||
}: UseSignedPlaybackIdProps): UseSignedPlaybackIdReturnProps => { | ||
const fetcher = async (url: string) => { | ||
const res = await fetch(url); | ||
if (!res.ok) { | ||
const json = await res.json(); | ||
const error = new OakError({ | ||
code: "video/fetch-signed-token", | ||
meta: { | ||
json, | ||
status: res.status, | ||
statusText: res.statusText, | ||
playbackId, | ||
playbackPolicy, | ||
}, | ||
}); | ||
reportError(error); | ||
throw error; | ||
} | ||
return res.json(); | ||
}; | ||
|
||
const { data, error } = useSWR( | ||
playbackPolicy === "signed" | ||
? `${apiEndpoint}?id=${playbackId}&type=video` | ||
: null, | ||
fetcher, | ||
options | ||
); | ||
const token = data?.token; | ||
|
||
useEffect(() => { | ||
if (data && !data.token) { | ||
const error = new OakError({ | ||
code: "video/fetch-signed-token", | ||
meta: { | ||
data, | ||
playbackId, | ||
playbackPolicy, | ||
}, | ||
}); | ||
reportError(error); | ||
} | ||
}, [data, playbackId, playbackPolicy]); | ||
|
||
if (playbackPolicy === "public") { | ||
return { | ||
loading: false, | ||
error: null, | ||
playbackToken: null, | ||
}; | ||
} | ||
|
||
if (error) { | ||
return { | ||
loading: false, | ||
error: error, | ||
playbackToken: null, | ||
}; | ||
} | ||
|
||
if (token) { | ||
return { | ||
loading: false, | ||
error: null, | ||
playbackToken: playbackPolicy === "signed" ? token : undefined, | ||
}; | ||
} | ||
|
||
return { | ||
loading: true, | ||
error: null, | ||
playbackToken: null, | ||
}; | ||
}; | ||
|
||
export default useSignedVideoToken; |
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