-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: pass the Cybozu data via
CustomeEvent
- Loading branch information
Showing
12 changed files
with
265 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(() => { | ||
document.dispatchEvent( | ||
new CustomEvent("cybozuDataPass", { | ||
detail: JSON.stringify(cybozu.data), | ||
}), | ||
); | ||
})(); |
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,73 @@ | ||
import { | ||
CybozuDataInitializationTimeoutError, | ||
UninitializedCybozuDataError, | ||
} from "@/apis/cybozu/errors.ts"; | ||
import { type CybozuData, CybozuDataSchema } from "@/models/CybozuData"; | ||
import type { LoginUser } from "@/models/LoginUser.ts"; | ||
|
||
declare global { | ||
// noinspection JSUnusedGlobalSymbols | ||
interface DocumentEventMap { | ||
cybozuDataPass: CustomEvent<CybozuData>; | ||
} | ||
} | ||
|
||
const dataReceptionTimeoutMilliseconds = 1000; | ||
|
||
let cybozuData: CybozuData | undefined; | ||
|
||
export async function initializeCybozuData(): Promise<CybozuData> { | ||
return new Promise((resolve, reject) => { | ||
const timeoutTimer = setTimeout(() => { | ||
document.removeEventListener("cybozuDataPass", handler); | ||
reject( | ||
new CybozuDataInitializationTimeoutError( | ||
dataReceptionTimeoutMilliseconds, | ||
), | ||
); | ||
}, dataReceptionTimeoutMilliseconds); | ||
|
||
function handler(event: CustomEvent) { | ||
clearTimeout(timeoutTimer); | ||
const data = CybozuDataSchema.parse(JSON.parse(event.detail)); | ||
setCybozuData(data); | ||
resolve(data); | ||
} | ||
|
||
document.addEventListener("cybozuDataPass", handler, { | ||
once: true, | ||
}); | ||
|
||
const scriptEl = document.createElement("script"); | ||
scriptEl.src = chrome.runtime.getURL("js/passCybozuData.js"); | ||
document.body.appendChild(scriptEl); | ||
}); | ||
} | ||
|
||
export function setCybozuData(data: CybozuData): void { | ||
cybozuData = data; | ||
} | ||
|
||
export function clearCybozuData(): void { | ||
cybozuData = undefined; | ||
} | ||
|
||
export function getLoginUser(): LoginUser { | ||
return getCybozuData().LOGIN_USER; | ||
} | ||
|
||
export function getRequestToken(): string { | ||
return getCybozuData().REQUEST_TOKEN; | ||
} | ||
|
||
export function getDisplayLocale(): string { | ||
return getCybozuData().DISPLAY_LOCALE; | ||
} | ||
|
||
function getCybozuData(): CybozuData { | ||
if (cybozuData === undefined) { | ||
throw new UninitializedCybozuDataError(); | ||
} | ||
|
||
return cybozuData; | ||
} |
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 @@ | ||
export class CybozuDataInitializationTimeoutError extends Error { | ||
constructor(timeoutMilliseconds: number) { | ||
super( | ||
`Could not receive the Cybozu data within ${timeoutMilliseconds} milliseconds`, | ||
); | ||
this.name = "CybozuDataInitializationTimeoutError"; | ||
Object.setPrototypeOf(this, CybozuDataInitializationTimeoutError.prototype); | ||
} | ||
} | ||
|
||
export class UninitializedCybozuDataError extends Error { | ||
constructor() { | ||
super("Cybozu data is not initialized yet"); | ||
this.name = "UninitializedCybozuDataError"; | ||
Object.setPrototypeOf(this, UninitializedCybozuDataError.prototype); | ||
} | ||
} |
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,9 @@ | ||
import { LoginUserSchema } from "@/models/LoginUser.ts"; | ||
import { z } from "zod"; | ||
|
||
export const CybozuDataSchema = z.object({ | ||
DISPLAY_LOCALE: z.string(), | ||
LOGIN_USER: LoginUserSchema, | ||
REQUEST_TOKEN: z.string(), | ||
}); | ||
export type CybozuData = z.infer<typeof CybozuDataSchema>; |
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,6 @@ | ||
import { z } from "zod"; | ||
|
||
export const LoginUserSchema = z.object({ | ||
code: z.string(), | ||
}); | ||
export type LoginUser = z.infer<typeof LoginUserSchema>; |
Oops, something went wrong.