import { RipplingSDK } from '../../../client'; import type { FunctionContext } from '../../../functions'; import { COManager, type CustomObjectRecordsClient, type TypedCustomObjectRecordManager } from './manager'; import type { AnyCustomObjectDefinition } from './types'; /** SDK-shaped client surface consumed by {@link createCustomObjectFunctionRuntime}. */ export interface CustomObjectFunctionRuntimeClient { readonly customObjects: { readonly records: CustomObjectRecordsClient; }; } /** Constructor options for {@link createCustomObjectFunctionRuntime}. */ export interface CreateCustomObjectFunctionRuntimeOptions { /** Optional prebuilt SDK-shaped client used instead of constructing a new one from the function context. */ readonly client?: CustomObjectFunctionRuntimeClient; /** Optional bearer-token override used when constructing the default SDK client. */ readonly bearerToken?: string; } /** Shared runtime returned by {@link createCustomObjectFunctionRuntime}. */ export interface CustomObjectFunctionRuntime { /** SDK-shaped client used for low-level access beyond the typed manager lane. */ readonly client: CustomObjectFunctionRuntimeClient; /** Underlying records resource used by the typed manager lane. */ readonly records: CustomObjectRecordsClient; /** Root typed custom-object manager bound to the chosen client. */ readonly manager: COManager; /** Returns a typed record manager bound to one object definition. */ for( definition: TDefinition, ): TypedCustomObjectRecordManager; /** Alias for {@link for}. */ forObject( definition: TDefinition, ): TypedCustomObjectRecordManager; } /** * Creates one shared custom-object function runtime from a Rippling function context. * * This is the small bootstrap helper for custom-object-backed functions: it * constructs one SDK client from `context.env.rippling_user_bearer_token`, binds * a `COManager`, and exposes definition-scoped managers through `for(...)`. * * @param context - Function context whose bearer token should back the runtime. * @param options - Optional client injection or bearer-token override. * @returns Shared runtime containing the SDK client plus typed record managers. */ export function createCustomObjectFunctionRuntime( context: Pick, options: CreateCustomObjectFunctionRuntimeOptions = {}, ): CustomObjectFunctionRuntime { if (options.client != null && options.bearerToken != null) { throw new Error('Pass either client or bearerToken to createCustomObjectFunctionRuntime(...), not both.'); } const client = options.client ?? new RipplingSDK({ bearerToken: options.bearerToken ?? context.env.rippling_user_bearer_token, }); const manager = COManager.fromRecords(client.customObjects.records); return Object.freeze({ client, records: client.customObjects.records, manager, for(definition: TDefinition) { return manager.for(definition); }, forObject(definition: TDefinition) { return manager.forObject(definition); }, }); }