Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | 2x 362x 2x 2x 1x 1x 1x 24x 24x 24x 24x 26x 14x 24x 10x 10x 10x 10x 13x 7x 28x 28x 25x 24x 111x 111x 108x 19x 17x 21x 21x 2x 19x 19x 16x 21x 4x 4x 4x 12x 6x 4x 3x 62x 62x 59x 59x 38x 38x 36x 35x 34x 1x 1x 36x 38x 38x 32x 32x 21x 8x 8x 4x 6x 6x 3x 21x 21x 21x 10x | import { createPersistentCache, createSessionCache } from "./index";
import type { CacheOptions, CacheValidation, LoadResult, ResourceCache } from "./types";
/** Namespaces reserved for the OOMOL response factories. */
export const oomolNamespaces = {
providers: "oomol:providers",
actions: "oomol:actions",
appCatalog: "oomol:app-catalog",
connections: "oomol:connections",
} as const;
export interface OomolQuery {
readonly locale: string;
}
export interface ActionsQuery extends OomolQuery {
readonly service: string;
}
export interface ConnectionsQuery {
readonly ownerId: string;
}
interface OomolCacheBaseOptions<T> {
/** Deployment environment used to separate hosts such as production and staging. Defaults to `production`. */
readonly environment?: string;
/** Locale sent to the host loader and included in the cache identity. */
readonly locale: string;
readonly schemaVersion: number;
readonly maxAge: number;
readonly decode: (value: unknown) => T;
}
export interface ProvidersCacheOptions extends OomolCacheBaseOptions<ProvidersResponse> {
/** Load the provider catalog; use request credentials only when the host endpoint requires them. */
readonly load: (query: OomolQuery, validation: CacheValidation) => Promise<LoadResult<ProvidersResponse>>;
}
export interface ActionsCacheOptions extends OomolCacheBaseOptions<ActionsResponse> {
/** Load the action catalog; use request credentials only when the host endpoint requires them. */
readonly load: (query: ActionsQuery, validation: CacheValidation) => Promise<LoadResult<ActionsResponse>>;
}
export interface AppCatalogCacheOptions extends OomolCacheBaseOptions<AppCatalogResponse> {
/** Load the public app catalog. This request does not require credentials. */
readonly load: (query: OomolQuery, validation: CacheValidation) => Promise<LoadResult<AppCatalogResponse>>;
}
export interface ConnectionsCacheOptions {
/** Deployment environment used to separate hosts such as production and staging. Defaults to `production`. */
readonly environment?: string;
/** Nonempty, non-secret identifier for the current login session. */
readonly sessionId: string;
/** Stable owner identity sent to the host loader. */
readonly ownerId: string;
readonly schemaVersion: number;
readonly maxAge: number;
readonly decode: (value: unknown) => ConnectionsResponse;
/** Load the connection list; request credentials belong in the host request layer. */
readonly load: (query: ConnectionsQuery, validation: CacheValidation) => Promise<LoadResult<ConnectionsResponse>>;
}
type OomolFactoryOptions<T> = OomolCacheBaseOptions<T> & {
readonly load: (query: OomolQuery, validation: CacheValidation) => Promise<LoadResult<T>>;
};
export type OomolCache<T> = ResourceCache<T, void>;
export type ActionsCache = ResourceCache<ActionsResponse, string>;
export type ConnectionsCache = ResourceCache<ConnectionsResponse, void>;
type SingletonRegistry = Map<string, ResourceCache<unknown, unknown>>;
/** Join identity fields without nesting JSON in the storage key. */
function readableKey(...parts: Array<string | number>): string {
return parts.map((part) => encodeURIComponent(String(part)).replaceAll("%3A", ":")).join("/");
}
const singletonRegistrySymbol = Symbol.for("oomol-lab.resource-cache.oomol-singletons");
const singletonCaches =
(Reflect.get(globalThis, singletonRegistrySymbol) as SingletonRegistry | undefined) ??
(() => {
const registry: SingletonRegistry = new Map();
Reflect.set(globalThis, singletonRegistrySymbol, registry);
return registry;
})();
function createOomolCache<T>(resource: string, options: OomolFactoryOptions<T>, query: OomolQuery): OomolCache<T> {
const environment = validateOomolOptions(options);
const { load } = options;
const key = readableKey(query.locale);
const cacheOptions: CacheOptions<T, void> = {
namespace: readableKey(resource, environment),
schemaVersion: options.schemaVersion,
maxAge: options.maxAge,
key: () => key,
decode: options.decode,
load: (_query, validation) => load({ ...query }, validation),
};
return createPersistentCache(cacheOptions);
}
function createConnectionsCacheInternal(
options: ConnectionsCacheOptions,
validated: { environment: string; ownerId: string },
): ConnectionsCache {
const { environment, ownerId } = validated;
const { load } = options;
const key = readableKey(ownerId);
return createSessionCache({
namespace: readableKey(oomolNamespaces.connections, environment),
schemaVersion: options.schemaVersion,
maxAge: options.maxAge,
sessionId: options.sessionId,
key: () => key,
decode: options.decode,
load: (_query, validation) => load({ ownerId }, validation),
});
}
function validateOomolOptions(options: {
readonly environment?: unknown;
readonly locale?: unknown;
readonly load?: unknown;
}): string {
const environment = normalizeEnvironment(options.environment);
if (typeof options.locale !== "string" || !options.locale.trim()) throw new TypeError("locale must be nonempty");
if (typeof options.load !== "function") throw new TypeError("load must be a function");
return environment;
}
function normalizeEnvironment(value: unknown): string {
const environment = value === undefined ? "production" : value;
if (typeof environment !== "string" || !environment.trim()) throw new TypeError("environment must be nonempty");
return environment;
}
function normalizeOwnerId(value: unknown): string {
if (typeof value !== "string" || !value.trim()) throw new TypeError("ownerId must be nonempty");
return value;
}
function validateConnectionsOptions(options: {
readonly environment?: unknown;
readonly sessionId?: unknown;
readonly ownerId: unknown;
readonly load?: unknown;
}): { environment: string; ownerId: string } {
const environment = normalizeEnvironment(options.environment);
if (typeof options.sessionId !== "string" || !options.sessionId.trim())
throw new TypeError("sessionId must be nonempty");
const ownerId = normalizeOwnerId(options.ownerId);
if (typeof options.load !== "function") throw new TypeError("load must be a function");
return { environment, ownerId };
}
/** Create an IndexedDB cache for the account-independent GET /v1/providers response, keyed by environment and locale. */
function createProvidersCacheInternal(options: ProvidersCacheOptions): OomolCache<ProvidersResponse> {
return createOomolCache(oomolNamespaces.providers, options, { locale: options.locale });
}
/** Create an IndexedDB cache for account-independent GET /v1/actions?service=…, keyed by environment, locale and service. */
function createActionsCacheInternal(options: ActionsCacheOptions): ActionsCache {
const environment = validateOomolOptions(options);
const { locale, load } = options;
return createPersistentCache<ActionsResponse, string>({
namespace: readableKey(oomolNamespaces.actions, environment),
schemaVersion: options.schemaVersion,
maxAge: options.maxAge,
key: (service) => {
if (typeof service !== "string" || !service.trim()) throw new TypeError("service must be nonempty");
return readableKey(locale, service);
},
decode: options.decode,
load: (service, validation) => load({ locale, service }, validation),
});
}
/** Create an IndexedDB cache for the account-independent GET /public/v1/apps response, keyed by environment and locale. */
function createAppCatalogCacheInternal(options: AppCatalogCacheOptions): OomolCache<AppCatalogResponse> {
return createOomolCache(oomolNamespaces.appCatalog, options, { locale: options.locale });
}
function singletonKey(
resource: string,
options: { readonly environment?: string; readonly schemaVersion: number },
query: readonly (string | number)[],
sessionId?: string,
): string {
const environment = normalizeEnvironment(options.environment);
return sessionId === undefined
? readableKey(resource, environment, options.schemaVersion, ...query)
: readableKey(resource, environment, options.schemaVersion, sessionId, ...query);
}
function getSingleton<T, Q>(key: string, create: () => ResourceCache<T, Q>): ResourceCache<T, Q> {
const existing = singletonCaches.get(key);
if (existing) return existing as ResourceCache<T, Q>;
const cache = create();
let disposal: Promise<void> | undefined;
const singleton: ResourceCache<T, Q> = {
...cache,
dispose() {
if (!disposal) {
disposal = cache.dispose().then(
() => {
singletonCaches.delete(key);
},
(error) => {
disposal = undefined;
throw error;
},
);
}
return disposal;
},
};
singletonCaches.set(key, singleton as ResourceCache<unknown, unknown>);
return singleton;
}
/** Return the shared Providers cache for one environment, locale and schema version. */
export function getProvidersCache(options: ProvidersCacheOptions): OomolCache<ProvidersResponse> {
const query = { locale: options.locale };
return getSingleton(singletonKey(oomolNamespaces.providers, options, [query.locale]), () =>
createProvidersCacheInternal(options),
);
}
/** Return the shared Actions cache for one environment, locale and schema version. */
export function getActionsCache(options: ActionsCacheOptions): ActionsCache {
const query = { locale: options.locale };
return getSingleton(singletonKey(oomolNamespaces.actions, options, [query.locale]), () =>
createActionsCacheInternal(options),
);
}
/** Return the shared App Catalog cache for one environment, locale and schema version. */
export function getAppCatalogCache(options: AppCatalogCacheOptions): OomolCache<AppCatalogResponse> {
const query = { locale: options.locale };
return getSingleton(singletonKey(oomolNamespaces.appCatalog, options, [query.locale]), () =>
createAppCatalogCacheInternal(options),
);
}
/** Return the shared sessionStorage cache for one login session and owner scope. */
export function getConnectionsCache(options: ConnectionsCacheOptions): ConnectionsCache {
const validated = validateConnectionsOptions(options);
const query = { ownerId: validated.ownerId };
return getSingleton(singletonKey(oomolNamespaces.connections, options, [query.ownerId], options.sessionId), () =>
createConnectionsCacheInternal(options, validated),
);
}
export type CredentialAuthType = "oauth2" | "api_key" | "custom_credential" | "no_auth" | "federated";
export interface ProviderIconSprite {
version: string;
pixelRatio: number;
iconSize: number;
bleed: number;
width: number;
height: number;
lightUrl: string;
darkUrl: string;
}
export interface ProviderListItem {
service: string;
displayName: string;
searchAliases: string[];
iconUrl: string | null;
homepageUrl: string | null;
categories: { id: string; displayName: string }[];
authTypes: CredentialAuthType[];
iconSpritePosition: { x: number; y: number } | null;
}
/** Complete GET /v1/providers success response, including sprite metadata. */
export interface ProvidersResponse {
success: true;
message: string;
data: ProviderListItem[];
meta: { totalCount: number; iconSprite: ProviderIconSprite | null };
}
export type ActionAsyncLifecycle =
| {
role: "submit";
resultAction: string;
handle: { outputField: string; inputField: string };
}
| {
role: "result";
wait: {
intervalSeconds: number;
state: { field: string; running: string[]; success: string[]; failure: string[] };
resultField?: string;
};
};
export interface ActionCatalogItem {
id: string;
service: string;
name: string;
description: string;
operationType: "read" | "write" | "destructive";
requiredScopes: string[];
providerPermissions: string[];
followUpActions?: {
actionId: string;
description?: string;
recommended?: boolean;
inputFromOutput?: Record<string, string>;
}[];
asyncLifecycle?: ActionAsyncLifecycle;
inputSchema: Record<string, unknown>;
outputSchema: Record<string, unknown>;
}
/** Complete GET /v1/actions?service=… success response, excluding the service index. */
export interface ActionsResponse {
success: true;
message: string;
data: ActionCatalogItem[];
}
export interface AppCatalogItem extends ProviderListItem {
actionCount: number;
featuredActions: { id: string; name: string; displayName: string }[];
status: "available" | "degraded" | "incident" | "unknown";
healthScore?: number | null;
lastCheckedAt?: string;
}
export interface AppCatalogSummary {
providerCount: number;
actionCount: number;
categoryCount: number;
oauthProviderCount: number;
apiKeyProviderCount: number;
healthyProviderCount: number | null;
}
/** Complete GET /public/v1/apps success response, including catalog aggregates and sprite metadata. */
export interface AppCatalogResponse {
success: true;
message: string;
data: AppCatalogItem[];
meta: {
summary: AppCatalogSummary;
categories: { id: string; displayName: string; providerCount: number; actionCount: number }[];
authTypes: { id: CredentialAuthType; displayName: string; providerCount: number }[];
iconSprite: ProviderIconSprite | null;
};
}
export type ConnectionStatus = "active" | "reauth_required" | "error" | "disconnected";
export type ConnectionAuthType = CredentialAuthType | "marketplace";
export interface ConnectionCredentialField {
key: string;
label: string;
displayValue: string;
secret: boolean;
}
export interface ConnectionCredentialSummary {
authType: "api_key" | "custom_credential";
fields: Record<
string,
{
configured: boolean;
displayValue?: string;
maskedValue?: string;
}
>;
}
export interface ConnectionTriggerCallbackUrl {
service: string;
deliveryMode: "push";
url: string;
}
export interface ConnectionAppRecord {
id: string;
service: string;
providerAccountId: string;
accountLabel: string;
alias: string | null;
aliasNormalized: string | null;
comment: string | null;
scopes: string[];
status: ConnectionStatus;
createdAt: number;
updatedAt: number;
userId: string;
createdByUserId: string;
ownerType: "team";
ownerTeamId: string;
authType: ConnectionAuthType | null;
providerScopes?: string[];
displayName: string;
isDefault: boolean;
credentialFields?: ConnectionCredentialField[];
credentialSummary?: ConnectionCredentialSummary;
triggerCallbackUrls?: ConnectionTriggerCallbackUrl[];
marketplace?: {
id: string;
pricing: "free" | "metered";
};
}
export interface ConnectionsSummary {
providerCount: number;
connectableProviderCount: number;
connectedProviderCount: number;
activeConnectedProviderCount: number;
connectedAppCount: number;
activeConnectedAppCount: number;
filteredAppCount: number;
}
/** Complete GET /v1/connections success response, including connection summary metadata. */
export interface ConnectionsResponse {
success: true;
message: string;
data: ConnectionAppRecord[];
meta: { summary: ConnectionsSummary };
}
|