-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathutils.ts
More file actions
55 lines (50 loc) · 2.09 KB
/
utils.ts
File metadata and controls
55 lines (50 loc) · 2.09 KB
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
import { isTruthy } from '@clerk/shared/underscore';
import type { Request as ExpressRequest } from 'express';
import type { ExpressRequestWithAuth } from './types';
export const requestHasAuthObject = (req: ExpressRequest): req is ExpressRequestWithAuth => {
return 'auth' in req;
};
export const loadClientEnv = () => {
return {
publishableKey: process.env.CLERK_PUBLISHABLE_KEY || '',
clerkJSUrl: process.env.CLERK_JS || process.env.CLERK_JS_URL || '',
clerkJSVersion: process.env.CLERK_JS_VERSION || '',
clerkUIUrl: process.env.CLERK_UI_URL || '',
clerkUIVersion: process.env.CLERK_UI_VERSION || '',
prefetchUI: process.env.CLERK_PREFETCH_UI === 'false' ? false : undefined,
};
};
export const loadApiEnv = () => {
return {
secretKey: process.env.CLERK_SECRET_KEY || '',
machineSecretKey: process.env.CLERK_MACHINE_SECRET_KEY || '',
apiUrl: process.env.CLERK_API_URL || 'https://api.clerk.com',
apiVersion: process.env.CLERK_API_VERSION || 'v1',
domain: process.env.CLERK_DOMAIN || '',
proxyUrl: process.env.CLERK_PROXY_URL || '',
signInUrl: process.env.CLERK_SIGN_IN_URL || '',
isSatellite: isTruthy(process.env.CLERK_IS_SATELLITE),
jwtKey: process.env.CLERK_JWT_KEY || '',
sdkMetadata: {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
environment: process.env.NODE_ENV,
},
telemetry: {
disabled: isTruthy(process.env.CLERK_TELEMETRY_DISABLED),
debug: isTruthy(process.env.CLERK_TELEMETRY_DEBUG),
},
};
};
export const incomingMessageToRequest = (req: ExpressRequest): Request => {
const headers = Object.keys(req.headers).reduce((acc, key) => Object.assign(acc, { [key]: req?.headers[key] }), {});
// @ts-ignore Optimistic attempt to get the protocol in case
// req extends IncomingMessage in a useful way. No guarantee
// it'll work.
const protocol = req.connection?.encrypted ? 'https' : 'http';
const dummyOriginReqUrl = new URL(req.originalUrl || req.url || '', `${protocol}://clerk-dummy`);
return new Request(dummyOriginReqUrl, {
method: req.method,
headers: new Headers(headers),
});
};