Interface IKosServiceRequestParams<Paths, Path, Method, Response, TransformedResponse>

Configuration parameters for

Kos Service Request

decorator Enables lifecycle-driven HTTP requests with declarative data transformation

Typeparam

Paths - The OpenAPI paths type (from your OpenAPI schema)

Typeparam

Path - The specific API path being requested

Typeparam

Method - The HTTP method type

Typeparam

Response - The raw response type from the API

Typeparam

TransformedResponse - The transformed response type after mappings

interface IKosServiceRequestParams<Paths, Path, Method, Response, TransformedResponse> {
    path: Path;
    method?: Method;
    lifecycle?: DependencyLifecycle;
    pathParams?: (ClientParams<Paths, Path, Method> extends {
            path?: P;
        }
        ? P
        : never) | ((model, ...args) => ClientParams<Paths, Path, Method> extends {
            path?: P;
        }
        ? P
        : never);
    queryParams?: (ClientParams<Paths, Path, Method> extends {
            query?: Q;
        }
        ? Q
        : never) | ((model, ...args) => ClientParams<Paths, Path, Method> extends {
            query?: Q;
        }
        ? Q
        : never);
    body?: ((model, ...args) => ClientBody<Paths, Path, Method>) | ClientBody<Paths, Path, Method>;
    iterateOver?: string;
    modelFactory?: KosModelRegistrationType<IKosDataModel, Record<string, unknown>> | ((id) => ((options) => IKosDataModel));
    idExtractor?: ((item) => string);
    mappings?: FieldMapping[];
    extensions?: ExtensionConfig[];
    transform?: ((response) => TransformedResponse);
    condition?: ((model) => boolean);
    errorHandler?: ErrorHandlerConfig;
    cache?: ResponseCacheConfig;
    requestOptions?: RequestOptions | ((model, ...args) => RequestOptions);
}

Type Parameters

  • Paths extends Record<string, any> = Record<string, any>
  • Path extends keyof Paths = string
  • Method extends HttpMethod = "get"
  • Response = any
  • TransformedResponse = Response

Properties

path: Path

API path to request

Example

path: '/api/kos/ingredients'
path: `/api/devices/${PROP_DEVICE_ID}/status` // with PropKey
method?: Method

HTTP method

Default

'get'
lifecycle?: DependencyLifecycle

When to execute this request during model lifecycle

If omitted, the request executes when the decorated method is called (method-driven). If specified, the request executes automatically during the given lifecycle phase (lifecycle-driven).

Example

// Lifecycle-driven (auto-execute during LOAD phase)
lifecycle: DependencyLifecycle.LOAD

Example

// Method-driven (execute when method is called)
lifecycle: undefined // or omit entirely
pathParams?: (ClientParams<Paths, Path, Method> extends {
        path?: P;
    }
    ? P
    : never) | ((model, ...args) => ClientParams<Paths, Path, Method> extends {
        path?: P;
    }
    ? P
    : never)

Path parameters to expand in the URL Strongly typed from OpenAPI schema Can be static values or PropKey references

Type declaration

    • (model, ...args): ClientParams<Paths, Path, Method> extends {
              path?: P;
          }
          ? P
          : never
    • Parameters

      • model: IKosDataModel
      • Rest ...args: any[]

      Returns ClientParams<Paths, Path, Method> extends {
              path?: P;
          }
          ? P
          : never

Example

// Static value
pathParams: { deviceId: 'device-123' }

Example

// PropKey reference (use PROP_ prefix)
pathParams: { deviceId: PROP_DEVICE_ID }

Example

// Dynamic from method arguments
pathParams: (model, deviceId, status) => ({ deviceId })
queryParams?: (ClientParams<Paths, Path, Method> extends {
        query?: Q;
    }
    ? Q
    : never) | ((model, ...args) => ClientParams<Paths, Path, Method> extends {
        query?: Q;
    }
    ? Q
    : never)

Query parameters Strongly typed from OpenAPI schema

Type declaration

    • (model, ...args): ClientParams<Paths, Path, Method> extends {
              query?: Q;
          }
          ? Q
          : never
    • Parameters

      • model: IKosDataModel
      • Rest ...args: any[]

      Returns ClientParams<Paths, Path, Method> extends {
              query?: Q;
          }
          ? Q
          : never

Example

// Static
queryParams: { includeMetadata: true, format: 'json' }

Example

// Dynamic from method arguments
queryParams: (model, deviceId, debug) => ({ debug })
body?: ((model, ...args) => ClientBody<Paths, Path, Method>) | ClientBody<Paths, Path, Method>

Request body (for POST/PUT/PATCH) Strongly typed from OpenAPI schema

Type declaration

Example

// Static body
body: { action: 'start' }

Example

// Dynamic from method arguments
body: (model, vehicleType, speed) => ({ vehicleType, speed })
iterateOver?: string

JSON path to iterate over in response Creates models for each item in the array

Example

iterateOver: 'data.ingredients' // Iterates response.data.ingredients array
modelFactory?: KosModelRegistrationType<IKosDataModel, Record<string, unknown>> | ((id) => ((options) => IKosDataModel))

Model registration object for creating instances from array items Used with iterateOver to create child models using the builder pattern

Type declaration

    • (id): ((options) => IKosDataModel)
    • Parameters

      • id: string

      Returns ((options) => IKosDataModel)

        • (options): IKosDataModel
        • Parameters

          • options: unknown

          Returns IKosDataModel

Example

modelFactory: IngredientModel  // The registration object itself

deprecated The old curried function signature is deprecated:
modelFactory?: (id: string) => (options: any) => IKosDataModel
idExtractor?: ((item) => string)

Function to extract the model ID from response data items Required when using modelFactory if items don't have an 'id' field

Type declaration

    • (item): string
    • Parameters

      • item: any

      Returns string

Example

idExtractor: (item) => item.ingredientId
idExtractor: (item) => `${item.type}-${item.index}`
mappings?: FieldMapping[]

Field mappings for data transformation Same format as

Kos Topic Handler

mappings

Example

mappings: [
{ from: 'id', to: 'id' },
{ from: 'ingredientId', to: 'ingredientId' },
{ from: 'metadata.calories', to: 'calories', transform: 'number' }
]
extensions?: ExtensionConfig[]

Extension points to execute during transformation

Example

extensions: [
{ type: 'data-mapper', extension: ExtensionType.IngredientMapper }
]
transform?: ((response) => TransformedResponse)

Transformation function for response data Applied before mappings

Type declaration

Example

transform: (response) => response.data
condition?: ((model) => boolean)

Condition to determine if request should execute

Type declaration

    • (model): boolean
    • Parameters

      • model: IKosDataModel

      Returns boolean

Example

condition: (model) => model.isInitialized
errorHandler?: ErrorHandlerConfig

Error handling strategy

Example

errorHandler: {
strategy: 'default',
defaultValue: [],
onError: (error, model) => model.logger.error('Failed to load', error)
}

Response caching configuration Controls how long responses are stored and when they're cleaned up

Default

{ retention: ResponseRetention.SINGLE }

Example

// No caching - clear immediately after handler
cache: { retention: ResponseRetention.IMMEDIATE }

Example

// Cache for 5 minutes
cache: { retention: ResponseRetention.TTL, ttl: 5 * 60 * 1000 }

Example

// Keep for model lifetime
cache: { retention: ResponseRetention.PERMANENT }
requestOptions?: RequestOptions | ((model, ...args) => RequestOptions)

Request options for controlling fetch behavior These are merged with runtime options passed to ctx.$request() Runtime options take precedence over decorator options

Type declaration

    • (model, ...args): RequestOptions
    • Parameters

      • model: IKosDataModel
      • Rest ...args: any[]

      Returns RequestOptions

Example

// Set default timeout and destination for all requests
requestOptions: {
destinationAddress: 'device-123',
timeout: 10000
}

Example

// Dynamic from method arguments
requestOptions: (model, ...args) => ({
destinationAddress: model.deviceId,
headers: { 'X-Session': model.sessionId }
})