-
Notifications
You must be signed in to change notification settings - Fork 2
/
static.ts
321 lines (297 loc) · 8.78 KB
/
static.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import _STATIC from "./static.json";
import { defaultsDeep, get as _get } from "lodash";
import { ThemeSettings } from "@/theme";
import type { ResultListingComponentOptions } from "@/components/ResultListing";
import type { ResultComponentOptions } from "@/components/Result";
import { NavigationOptions } from "@/components/Navigation";
/**
* The base type for a `static.json` file.
*/
export type Base = {
_static: {
generator: {
/**
* The name of the generator used to build the `static.json` file.
* This should be a reference to the package name of the generator.
* @example "@globus/static-data-portal"
*/
name: string;
};
/**
* GitHub Action-injected environment variables.
* @see https://github.com/from-static/actions
*/
host?: {
base_url: string;
origin: string;
host: string;
base_path: string;
};
};
data: {
version: string;
attributes: Record<string, unknown>;
};
};
/**
* The type used for `data` by the [@globus/static-search-portal generator](https://github.com/globus/static-search-portal).
*/
export type Data = {
/**
* The version of the `data` object, which is used to determine how
* the generator will render its `attributes`.
* @example "1.0.0"
*/
version: string;
attributes: {
features?: {
/**
* Enable JSONata support for processing the `static.json` file.
* @see https://jsonata.org/
*/
jsonata?: boolean;
/**
* Enable the Globus Auth functionality in the portal.
*/
authentication?: boolean;
/**
* Force users to authenticate before accessing the portal, regardless of whether or not the
* configured Globus Index is private.
*/
requireAuthentication?: boolean;
/**
* Enables the Globus Transfer functionality in the portal.
*/
transfer?: boolean;
/**
* Whether or not authorization data should be stored in LocalStorage
*/
useLocalStorage?: boolean;
/**
* Enable SEO results in the portal; This feature is not yet available.
* @private
*/
seo_results?: boolean;
};
theme?: ThemeSettings;
metadata?: {
title?: string;
description?: string;
};
/**
* The Content Security Policy (CSP) for the portal that will be included in a `<meta>` tag.
* If no value is provided, a default CSP will be used.
* If `false` is provided as the value, no CSP `<meta>` tag will be included.
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
*/
contentSecurityPolicy?: string | false;
content?: {
headline?: string;
logo?: {
src: string;
alt?: string;
};
navigation?: NavigationOptions;
};
components?: {
Result?: ResultComponentOptions;
ResultListing?: ResultListingComponentOptions;
};
globus: {
/**
* The Globus platform environment.
* @private
*/
environment?: string;
/**
* Information about your registered Globus Auth Application (Client)
* @see https://docs.globus.org/api/auth/developer-guide/#developing-apps
*/
application?: {
/**
* The UUID of the client application.
*/
client_id: string;
/**
* The redirect URI for the Globus Auth login page to complete the OAuth2 flow.
* The portal will make a reasonable effort to determine this URI, but this field is provided as a fallback.
* To use the portal's built-in authorization handling, redirects should be sent to `/authenticate` on the host.
* @example "https://example.com/data-portal/authenticate"
*/
redirect_uri?: string;
/**
* Additional scopes to request from the Globus Auth service when authenticating.
*/
scopes?: string[];
};
/**
* Configuration for Search-related functionality in the portal.
*/
search: {
/**
* The UUID of the Globus Search Index that will be used as the data source.
*/
index: string;
facets?: {
name?: string;
field_name: string;
type: string;
size: number;
}[];
};
};
};
};
export type Static = Base & {
data: Data;
};
/**
* Reference to the `static.json` file.
* @private
*/
export const STATIC: Static = _STATIC;
defaultsDeep(STATIC, {
data: {
attributes: {
features: {
/**
* If a Globus Auth client ID is provided, `authentication` is enabled by default.
*/
authentication: Boolean(
STATIC.data.attributes?.globus?.application?.client_id,
),
},
components: {
/**
* For `summary` and `heading` properties the `Result` and `ResultListing` components
* fallback to one another if not provided...
*/
Result: {
heading:
STATIC.data.attributes?.components?.ResultListing?.heading ||
"subject",
summary:
STATIC.data.attributes?.components?.ResultListing?.summary || null,
},
ResultListing: {
heading:
STATIC.data.attributes?.components?.Result?.heading || "subject",
summary: STATIC.data.attributes?.components?.Result?.summary || null,
},
},
},
},
});
const {
data: { attributes },
} = STATIC;
export function getEnvironment() {
return attributes.globus.environment || null;
}
/**
* @returns The redirect URI for the Globus Auth login page.
* @private
*/
export function getRedirectUri() {
/**
* If the `redirect_uri` is specified in the `static.json`, use it.
*/
if (attributes.globus.application?.redirect_uri) {
return attributes.globus.application.redirect_uri;
}
/**
* If this is a static-managed deployment, use the `base_url` from the `static.json`.
*/
if (STATIC._static.host?.base_url) {
return `${STATIC._static.host?.base_url}/authenticate`;
}
/**
* If all else fails, try to construct the redirect URI from the current location.
* The fallback here is mostly to account for SSR.
* @todo This could likely be configured to get `basePath` and host information for the Next.js configuration or environment.
*/
const baseURL = globalThis.location
? `${globalThis.location.protocol}//${globalThis.location.host}`
: "";
return `${baseURL}/authenticate`;
}
/**
* Get a value by key (JSONPath) from the `static.json`.
* @private
*/
export function get(key: string, defaultValue?: any) {
return _get(STATIC, key, defaultValue);
}
/**
* Get an attribute (`data.attributes` member) by key from the `static.json`.
* @private
*/
export function getAttribute(key: string, defaultValue?: any) {
return get(`data.attributes.${key}`, defaultValue);
}
/**
* Whether or not a feature is enabled in the `static.json`.
* @private
*/
export function isFeatureEnabled(key: string, defaultValue?: boolean) {
return Boolean(get(`data.attributes.features.${key}`, defaultValue));
}
/**
* @private
*/
export function withFeature<T>(
key: string,
a: () => T,
b: () => T | null = () => null,
) {
return isFeatureEnabled(key) ? a() : b();
}
let jsonata: typeof import("jsonata") | null = null;
/**
* - Resolve a value for the provided attribute`key` from the `static.json` file.
* - Call `getValueFrom` with the resolved key.
* @private
*/
export async function getValueFromAttribute<T>(
obj: Record<string, unknown>,
key: string,
defaultValue?: T,
): Promise<T | undefined> {
const resolvedKey = getAttribute(key);
return await getValueFrom<T>(obj, resolvedKey, defaultValue);
}
export async function getValueFrom<T>(
obj: Record<string, any>,
key: string,
defaultValue?: T,
): Promise<T | undefined> {
const useJSONata = isFeatureEnabled("jsonata");
if (useJSONata && !jsonata) {
jsonata = (await import("jsonata")).default;
}
if (useJSONata && jsonata && key) {
const expression = jsonata(key);
return (await expression.evaluate(obj)) || defaultValue;
}
return _get(obj, key, defaultValue);
}
/**
* Whether or not the Globus Transfer is enabled based on the state of the `static.json`.
*/
export const isTransferEnabled = Boolean(
getAttribute(
"features.transfer",
getAttribute("components.Result.globus.transfer"),
),
);
/**
* Whether or not a user can "Sign In" to the portal.
* If Transfer functionality is enabled (`isTransferEnabled`), then authentication is enabled.
*/
export const isAuthenticationEnabled =
isTransferEnabled || isFeatureEnabled("authentication");
export const areSEOResultsEnabled = isFeatureEnabled("seo_results");
export const METADATA = {
title: getAttribute("metadata.title", "Search Portal"),
description: getAttribute("metadata.description", ""),
};