-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk-identifier.ts
More file actions
41 lines (35 loc) · 1.27 KB
/
sdk-identifier.ts
File metadata and controls
41 lines (35 loc) · 1.27 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
import type { BeforeRequestContext, BeforeRequestHook } from "./types.js";
let cachedVersion: string | null = null;
function loadVersion(): string {
if (cachedVersion) {
return cachedVersion;
}
try {
// NOTES: require is being used for now, using import demands appropriate
// compiler configuration, which won't be enabled yet due to Speakeasy's
// particular way of enabling persistent edits conflicting with workflow.
// Ticket sc-56960 created for this investigation.
const packageJsonPath = require.resolve("galileo-generated/package.json");
// eslint-disable-next-line @typescript-eslint/no-require-imports
const packageJson = require(packageJsonPath);
cachedVersion = packageJson.version;
return cachedVersion ?? "unknown";
} catch {
return "unknown";
}
}
function getSdkIdentifier(version: string): string {
return `galileo-generated/${version}`;
}
export class SDKIdentifierHook implements BeforeRequestHook {
async beforeRequest(
_hookCtx: BeforeRequestContext,
request: Request,
): Promise<Request> {
const newRequest = request.clone();
const version = loadVersion();
const sdkIdentifier = getSdkIdentifier(version);
newRequest.headers.set("X-Galileo-SDK", sdkIdentifier);
return newRequest;
}
}