|
1 | | -import { logger } from "@coder/logger" |
| 1 | +import { logger, field } from "@coder/logger" |
2 | 2 |
|
3 | 3 | export interface Options { |
4 | 4 | base: string |
5 | 5 | commit: string |
6 | 6 | logLevel: number |
7 | | - sessionId?: string |
| 7 | + pid?: number |
8 | 8 | } |
9 | 9 |
|
10 | 10 | /** |
@@ -34,34 +34,44 @@ export const normalize = (url: string, keepTrailing = false): string => { |
34 | 34 | } |
35 | 35 |
|
36 | 36 | /** |
37 | | - * Get options embedded in the HTML from the server. |
| 37 | + * Get options embedded in the HTML or query params. |
38 | 38 | */ |
39 | 39 | export const getOptions = <T extends Options>(): T => { |
40 | | - if (typeof document === "undefined") { |
41 | | - return {} as T |
42 | | - } |
43 | | - const el = document.getElementById("coder-options") |
| 40 | + let options: T |
44 | 41 | try { |
| 42 | + const el = document.getElementById("coder-options") |
45 | 43 | if (!el) { |
46 | 44 | throw new Error("no options element") |
47 | 45 | } |
48 | 46 | const value = el.getAttribute("data-settings") |
49 | 47 | if (!value) { |
50 | 48 | throw new Error("no options value") |
51 | 49 | } |
52 | | - const options = JSON.parse(value) |
53 | | - if (typeof options.logLevel !== "undefined") { |
54 | | - logger.level = options.logLevel |
55 | | - } |
56 | | - const parts = window.location.pathname.replace(/^\//g, "").split("/") |
57 | | - parts[parts.length - 1] = options.base |
58 | | - const url = new URL(window.location.origin + "/" + parts.join("/")) |
59 | | - return { |
| 50 | + options = JSON.parse(value) |
| 51 | + } catch (error) { |
| 52 | + options = {} as T |
| 53 | + } |
| 54 | + |
| 55 | + const params = new URLSearchParams(location.search) |
| 56 | + const queryOpts = params.get("options") |
| 57 | + if (queryOpts) { |
| 58 | + options = { |
60 | 59 | ...options, |
61 | | - base: normalize(url.pathname, true), |
| 60 | + ...JSON.parse(queryOpts), |
62 | 61 | } |
63 | | - } catch (error) { |
64 | | - logger.warn(error.message) |
65 | | - return {} as T |
66 | 62 | } |
| 63 | + |
| 64 | + if (typeof options.logLevel !== "undefined") { |
| 65 | + logger.level = options.logLevel |
| 66 | + } |
| 67 | + if (options.base) { |
| 68 | + const parts = location.pathname.replace(/^\//g, "").split("/") |
| 69 | + parts[parts.length - 1] = options.base |
| 70 | + const url = new URL(location.origin + "/" + parts.join("/")) |
| 71 | + options.base = normalize(url.pathname, true) |
| 72 | + } |
| 73 | + |
| 74 | + logger.debug("got options", field("options", options)) |
| 75 | + |
| 76 | + return options |
67 | 77 | } |
0 commit comments