Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions .devcontainer/README.md

This file was deleted.

45 changes: 0 additions & 45 deletions .devcontainer/devcontainer.json

This file was deleted.

30 changes: 0 additions & 30 deletions .devcontainer/setup.sh

This file was deleted.

4 changes: 2 additions & 2 deletions .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
configVersion: 2.0.0
generation:
devContainers:
enabled: true
enabled: false
schemaPath: .speakeasy/out.openapi.yaml
sdkClassName: GalileoGenerated
maintainOpenAPIOrder: true
Expand All @@ -27,7 +27,7 @@ generation:
allOfMergeStrategy: shallowMerge
requestBodyFieldName: body
persistentEdits:
enabled: "true"
enabled: "false"
tests:
generateTests: false
generateNewTests: true
Expand Down
16 changes: 9 additions & 7 deletions src/entities/base-entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthConfigStore, GalileoGenerated } from "../index.js";
import { EnvConfigStore, GalileoGenerated, SDKOptions } from "../index.js";
import { Token } from "../models/token.js";
import type { Result } from "../types/fp.js";
import { OK, ERR } from "../types/fp.js";
Expand All @@ -20,26 +20,28 @@ export class BaseEntity {
}

static getCLient(): GalileoGenerated {
BaseEntity.client ??= new GalileoGenerated();
const envConfig = EnvConfigStore.get();
const clientConfig: SDKOptions = envConfig?.apiUrl ? { serverURL: envConfig.apiUrl } : {};
BaseEntity.client ??= new GalileoGenerated(clientConfig);
return BaseEntity.client;
}

static async authenticate(): Promise<string | null> {
const authConfig = AuthConfigStore.get();
const authConfig = EnvConfigStore.get();

let result: Token | undefined;
if(authConfig?.apiKey){
result = await BaseEntity.client?.auth.loginApiKeyLoginApiKeyPost({
result = await BaseEntity.getCLient().auth.loginApiKeyLoginApiKeyPost({
apiKey:authConfig.apiKey,
});
}else if(authConfig?.login?.username && authConfig?.login?.password){
result = await BaseEntity.client?.auth.loginEmailLoginPost({
result = await BaseEntity.getCLient().auth.loginEmailLoginPost({
username:authConfig.login.username,
password:authConfig.login.password,
});
}
/*else if(authConfig?.sso?.idToken && authConfig?.sso?.provider){
result = await BaseEntity.client?.auth.ssoLoginPost({
result = await BaseEntity.getCLient().auth.ssoLoginPost({
idToken:authConfig.sso.idToken,
provider:authConfig.sso.provider,
});
Expand All @@ -50,7 +52,7 @@ export class BaseEntity {

protected ensureNotDeleted(): void {
if (this.deleted) {
throw new Error("Cannot perform operation on deleted project");
throw new Error("Cannot perform operation on deleted entity");
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/hooks/error-cleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export class ErrorCleanerHook implements AfterErrorHook {
): Promise<{ response: Response | null; error: unknown }> {
void hookCtx;


console.log("#####################################################");
console.log("eRROR CLEANER HOOK - ERROR:", error);
console.log("eRROR CLEANER HOOK - RESPONSE:", response);
console.log("eRROR CLEANER HOOK - HOOK CTX:", hookCtx);


try{
if (!response) return { response, error };

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/registration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorCleanerHook } from "./error-cleaner.js";
import { TokenManagementHook } from "./token-management.js";
import type { Hooks } from "./types.js";
//import { ErrorCleanerHook } from "./error-cleaner.js";

/*
* This file is only ever generated once on the first generation and then is free to be modified.
Expand All @@ -15,6 +15,6 @@ export function initHooks(hooks: Hooks) {
hooks.registerAfterSuccessHook(tokenHook);

// Register error cleaning hook
//const errorCleanerHook = new ErrorCleanerHook();
//hooks.registerAfterErrorHook(errorCleanerHook);
const errorCleanerHook = new ErrorCleanerHook();
hooks.registerAfterErrorHook(errorCleanerHook);
}
2 changes: 1 addition & 1 deletion src/hooks/token-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class TokenManagementHook

// Skip if no token is stored
const token = await BaseEntity.getToken();

if (!token) {
return request;
}
Expand Down Expand Up @@ -119,4 +120,3 @@ export class TokenManagementHook
return response;
}
}

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

export * from "./lib/config.js";
export * from "./lib/auth-config.js";
export * from "./lib/env-config.js";
export * as files from "./lib/files.js";
export { HTTPClient } from "./lib/http.js";
export type { Fetcher, HTTPClientOptions } from "./lib/http.js";
Expand Down
60 changes: 34 additions & 26 deletions src/lib/auth-config.ts → src/lib/env-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import { isBrowserLike, isDeno, isNodeLike } from "./runtime.js";

export type AuthConfig = {
export type EnvConfig = {
apiUrl?: string;
consoleUrl?: string;
apiKey?: string;
login?: { username?: string; password?: string };
sso?: Record<string, unknown>;
Expand All @@ -17,39 +19,41 @@ const ENV_USERNAME = "GALILEO_USERNAME";
const ENV_PASSWORD = "GALILEO_PASSWORD";
const ENV_SSO_ID_TOKEN = "GALILEO_SSO_ID_TOKEN";
const ENV_SSO_PROVIDER = "GALILEO_SSO_PROVIDER";
const ENV_GALILEO_API_URL = "GALILEO_API_URL";
const ENV_GALILEO_CONSOLE_URL = "GALILEO_CONSOLE_URL";

// biome-ignore lint/complexity/noStaticOnlyClass: Singleton-like store.
export class AuthConfigStore {
static #config: AuthConfig | null = null;
export class EnvConfigStore {
static #config: EnvConfig | null = null;

static set(config: AuthConfig): void {
AuthConfigStore.#config = normalizeAuthConfig(config);
static set(config: EnvConfig): void {
EnvConfigStore.#config = normalizeEnvConfig(config);
}

static clear(): void {
AuthConfigStore.#config = null;
EnvConfigStore.#config = null;
}

static refreshFromEnvironment(): AuthConfig | null {
static refreshFromEnvironment(): EnvConfig | null {
const config = resolveConfigFromEnvironment();
AuthConfigStore.#config = config;
EnvConfigStore.#config = config;
return config;
}

static get(): AuthConfig | null {
if (AuthConfigStore.#config) {
return AuthConfigStore.#config;
static get(): EnvConfig | null {
if (EnvConfigStore.#config) {
return EnvConfigStore.#config;
}

return AuthConfigStore.refreshFromEnvironment();
return EnvConfigStore.refreshFromEnvironment();
}

static getRedacted(): AuthConfig | null {
return redactAuthConfig(AuthConfigStore.get());
static getRedacted(): EnvConfig | null {
return redactEnvConfig(EnvConfigStore.get());
}
}

function resolveConfigFromEnvironment(): AuthConfig | null {
function resolveConfigFromEnvironment(): EnvConfig | null {
if (isBrowserLike()) {
return (
readFromBrowserGlobal(DEFAULT_BROWSER_GLOBAL)
Expand All @@ -64,18 +68,18 @@ function resolveConfigFromEnvironment(): AuthConfig | null {
return null;
}

function readFromBrowserGlobal(globalName: string): AuthConfig | null {
function readFromBrowserGlobal(globalName: string): EnvConfig | null {
const gt = typeof globalThis === "undefined"
? null
: (globalThis as Record<string, unknown>);
if (gt == null) {
return null;
}

return parseAuthConfigValue(gt[globalName]);
return parseEnvConfigValue(gt[globalName]);
}

function readFromLocalStorage(key: string): AuthConfig | null {
function readFromLocalStorage(key: string): EnvConfig | null {
if (typeof globalThis === "undefined") {
return null;
}
Expand All @@ -85,13 +89,13 @@ function readFromLocalStorage(key: string): AuthConfig | null {
return null;
}
const raw = globalThis.localStorage?.getItem(key);
return parseAuthConfigValue(raw);
return parseEnvConfigValue(raw);
} catch {
return null;
}
}

function readFromEnv(): AuthConfig | null {
function readFromEnv(): EnvConfig | null {
const env = readEnvObject();
if (!env) {
return null;
Expand All @@ -100,6 +104,8 @@ function readFromEnv(): AuthConfig | null {
const apiKey = env[ENV_API_KEY];
const username = env[ENV_USERNAME];
const password = env[ENV_PASSWORD];
const apiUrl = env[ENV_GALILEO_API_URL];
const consoleUrl = env[ENV_GALILEO_CONSOLE_URL];
const ssoIdToken = env[ENV_SSO_ID_TOKEN];
const ssoProvider = env[ENV_SSO_PROVIDER];

Expand All @@ -119,6 +125,8 @@ function readFromEnv(): AuthConfig | null {
? { login: { ...(username ? { username } : {}), ...(password ? { password } : {}) } }
: {}),
...(sso ? { sso } : {}),
...(apiUrl ? { apiUrl } : {}),
...(consoleUrl ? { consoleUrl } : {}),
};
}

Expand All @@ -140,17 +148,17 @@ function readEnvObject(): Record<string, string | undefined> | null {
return null;
}

function parseAuthConfigValue(value: unknown): AuthConfig | null {
function parseEnvConfigValue(value: unknown): EnvConfig | null {
if (value == null) {
return null;
}

if (typeof value === "string") {
const parsed = parseJSON(value);
return normalizeAuthConfig(parsed);
return normalizeEnvConfig(parsed);
}

return normalizeAuthConfig(value);
return normalizeEnvConfig(value);
}

function parseJSON(value: string): unknown {
Expand All @@ -161,7 +169,7 @@ function parseJSON(value: string): unknown {
}
}

function normalizeAuthConfig(value: unknown): AuthConfig | null {
function normalizeEnvConfig(value: unknown): EnvConfig | null {
if (!value || typeof value !== "object") {
return null;
}
Expand All @@ -182,7 +190,7 @@ function normalizeAuthConfig(value: unknown): AuthConfig | null {
};
}

function normalizeLogin(value: unknown): AuthConfig["login"] | undefined {
function normalizeLogin(value: unknown): EnvConfig["login"] | undefined {
if (!value || typeof value !== "object") {
return undefined;
}
Expand All @@ -206,7 +214,7 @@ function normalizeSSO(value: unknown): Record<string, unknown> | undefined {
return value as Record<string, unknown>;
}

function redactAuthConfig(config: AuthConfig | null): AuthConfig | null {
function redactEnvConfig(config: EnvConfig | null): EnvConfig | null {
if (!config) {
return null;
}
Expand Down
Loading