@spoosh/core 0.15.0 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +104 -13
- package/dist/index.d.ts +104 -13
- package/dist/index.js +6 -0
- package/dist/index.mjs +6 -0
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1053,21 +1053,21 @@ type ExtractWriteResult<T> = T extends SpooshPlugin<infer Types> ? Types extends
|
|
|
1053
1053
|
type ExtractInstanceApi<T> = T extends SpooshPlugin<infer Types> ? Types extends {
|
|
1054
1054
|
instanceApi: infer A;
|
|
1055
1055
|
} ? A : object : object;
|
|
1056
|
-
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
1056
|
+
type UnionToIntersection$1<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
1057
1057
|
type MergePluginOptions<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
1058
|
-
read: UnionToIntersection<ExtractReadOptions<TPlugins[number]>>;
|
|
1059
|
-
write: UnionToIntersection<ExtractWriteOptions<TPlugins[number]>>;
|
|
1060
|
-
pages: UnionToIntersection<ExtractPagesOptions<TPlugins[number]>>;
|
|
1061
|
-
writeTrigger: UnionToIntersection<ExtractWriteTriggerOptions<TPlugins[number]>>;
|
|
1062
|
-
queue: UnionToIntersection<ExtractQueueOptions<TPlugins[number]>>;
|
|
1063
|
-
queueTrigger: UnionToIntersection<ExtractQueueTriggerOptions<TPlugins[number]>>;
|
|
1058
|
+
read: UnionToIntersection$1<ExtractReadOptions<TPlugins[number]>>;
|
|
1059
|
+
write: UnionToIntersection$1<ExtractWriteOptions<TPlugins[number]>>;
|
|
1060
|
+
pages: UnionToIntersection$1<ExtractPagesOptions<TPlugins[number]>>;
|
|
1061
|
+
writeTrigger: UnionToIntersection$1<ExtractWriteTriggerOptions<TPlugins[number]>>;
|
|
1062
|
+
queue: UnionToIntersection$1<ExtractQueueOptions<TPlugins[number]>>;
|
|
1063
|
+
queueTrigger: UnionToIntersection$1<ExtractQueueTriggerOptions<TPlugins[number]>>;
|
|
1064
1064
|
};
|
|
1065
1065
|
type MergePluginResults<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
1066
|
-
read: UnionToIntersection<ExtractReadResult<TPlugins[number]>>;
|
|
1067
|
-
write: UnionToIntersection<ExtractWriteResult<TPlugins[number]>>;
|
|
1068
|
-
queue: UnionToIntersection<ExtractQueueResult<TPlugins[number]>>;
|
|
1066
|
+
read: UnionToIntersection$1<ExtractReadResult<TPlugins[number]>>;
|
|
1067
|
+
write: UnionToIntersection$1<ExtractWriteResult<TPlugins[number]>>;
|
|
1068
|
+
queue: UnionToIntersection$1<ExtractQueueResult<TPlugins[number]>>;
|
|
1069
1069
|
};
|
|
1070
|
-
type MergePluginInstanceApi<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[], TSchema = unknown> = ResolveInstanceApi<UnionToIntersection<ExtractInstanceApi<TPlugins[number]>>, TSchema, MergePluginOptions<TPlugins>["read"]>;
|
|
1070
|
+
type MergePluginInstanceApi<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[], TSchema = unknown> = ResolveInstanceApi<UnionToIntersection$1<ExtractInstanceApi<TPlugins[number]>>, TSchema, MergePluginOptions<TPlugins>["read"]>;
|
|
1071
1071
|
type PluginRegistry<TPlugins extends SpooshPlugin<PluginTypeConfig>[]> = {
|
|
1072
1072
|
plugins: TPlugins;
|
|
1073
1073
|
_options: MergePluginOptions<TPlugins>;
|
|
@@ -1311,12 +1311,103 @@ type WritePathMethods$1<TSchema, TPath extends string> = FindMatchingKey<TSchema
|
|
|
1311
1311
|
*/
|
|
1312
1312
|
type WriteSchemaHelper<TSchema> = <TPath extends WritePaths<TSchema> | (string & {})>(path: TPath) => HasWriteMethod<TSchema, TPath> extends true ? WritePathMethods$1<TSchema, TPath> : never;
|
|
1313
1313
|
|
|
1314
|
+
/**
|
|
1315
|
+
* Check for exact type equality using function type assignability.
|
|
1316
|
+
* Two types are equal if functions returning them are mutually assignable.
|
|
1317
|
+
*/
|
|
1318
|
+
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
1319
|
+
/**
|
|
1320
|
+
* Convert bare `object` type to `never` to exclude it from the union.
|
|
1321
|
+
* Preserves all other types including interfaces and types with actual properties.
|
|
1322
|
+
*/
|
|
1323
|
+
type FilterObjectType<T> = Equals<T, object> extends true ? never : T;
|
|
1324
|
+
/**
|
|
1325
|
+
* Convert a union type to an intersection type.
|
|
1326
|
+
* This merges all properties from all types in the union.
|
|
1327
|
+
*/
|
|
1328
|
+
type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
|
|
1329
|
+
/**
|
|
1330
|
+
* Extract all option types from plugin configuration and create a union, then intersect.
|
|
1331
|
+
* This allows middleware to access all properties from all option types.
|
|
1332
|
+
* object types are converted to unknown which doesn't affect the intersection.
|
|
1333
|
+
*/
|
|
1334
|
+
type ExtractPluginOptionsUnion<T extends PluginTypeConfig> = UnionToIntersection<FilterObjectType<T extends {
|
|
1335
|
+
readOptions: infer R;
|
|
1336
|
+
} ? R : never> | FilterObjectType<T extends {
|
|
1337
|
+
writeOptions: infer W;
|
|
1338
|
+
} ? W : never> | FilterObjectType<T extends {
|
|
1339
|
+
writeTriggerOptions: infer WT;
|
|
1340
|
+
} ? WT : never> | FilterObjectType<T extends {
|
|
1341
|
+
queueTriggerOptions: infer QT;
|
|
1342
|
+
} ? QT : never> | FilterObjectType<T extends {
|
|
1343
|
+
pagesOptions: infer P;
|
|
1344
|
+
} ? P : never> | FilterObjectType<T extends {
|
|
1345
|
+
queueOptions: infer Q;
|
|
1346
|
+
} ? Q : never>>;
|
|
1347
|
+
/**
|
|
1348
|
+
* Plugin context with typed pluginOptions based on plugin configuration.
|
|
1349
|
+
*/
|
|
1350
|
+
type TypedPluginContext<T extends PluginTypeConfig> = Omit<PluginContext, "pluginOptions"> & {
|
|
1351
|
+
pluginOptions?: ExtractPluginOptionsUnion<T>;
|
|
1352
|
+
};
|
|
1353
|
+
/**
|
|
1354
|
+
* Plugin definition with typed context for middleware and handlers.
|
|
1355
|
+
*/
|
|
1356
|
+
type TypedPluginDefinition<T extends PluginTypeConfig> = Omit<SpooshPlugin<T>, "middleware" | "afterResponse" | "lifecycle"> & {
|
|
1357
|
+
middleware?: (context: TypedPluginContext<T>, next: () => Promise<any>) => Promise<any>;
|
|
1358
|
+
afterResponse?: (context: TypedPluginContext<T>, response: any) => any;
|
|
1359
|
+
lifecycle?: {
|
|
1360
|
+
onMount?: (context: TypedPluginContext<T>) => void | Promise<void>;
|
|
1361
|
+
onUpdate?: (context: TypedPluginContext<T>, previousContext: TypedPluginContext<T>) => void | Promise<void>;
|
|
1362
|
+
onUnmount?: (context: TypedPluginContext<T>) => void | Promise<void>;
|
|
1363
|
+
};
|
|
1364
|
+
};
|
|
1365
|
+
/**
|
|
1366
|
+
* Helper to create a Spoosh plugin with automatic type inference for plugin options.
|
|
1367
|
+
*
|
|
1368
|
+
* This eliminates the need for manual type assertions in middleware by automatically
|
|
1369
|
+
* intersecting all option types, making all properties accessible:
|
|
1370
|
+
*
|
|
1371
|
+
* ```ts
|
|
1372
|
+
* // Before:
|
|
1373
|
+
* const pluginOptions = context.pluginOptions as CacheReadOptions | undefined;
|
|
1374
|
+
* const staleTime = pluginOptions?.staleTime ?? defaultStaleTime;
|
|
1375
|
+
*
|
|
1376
|
+
* // After (with createSpooshPlugin):
|
|
1377
|
+
* const staleTime = context.pluginOptions?.staleTime ?? defaultStaleTime;
|
|
1378
|
+
* ```
|
|
1379
|
+
*
|
|
1380
|
+
* @typeParam T - Plugin type configuration (readOptions, writeOptions, etc.)
|
|
1381
|
+
* @param definition - Plugin definition with typed context
|
|
1382
|
+
* @returns Typed Spoosh plugin
|
|
1383
|
+
*
|
|
1384
|
+
* @example
|
|
1385
|
+
* ```ts
|
|
1386
|
+
* export const cachePlugin = (config: CachePluginConfig = {}) =>
|
|
1387
|
+
* createSpooshPlugin<{
|
|
1388
|
+
* readOptions: CacheReadOptions;
|
|
1389
|
+
* writeOptions: CacheWriteOptions;
|
|
1390
|
+
* pagesOptions: CachePagesOptions;
|
|
1391
|
+
* }>({
|
|
1392
|
+
* name: "spoosh:cache",
|
|
1393
|
+
* operations: ["read", "write", "pages"],
|
|
1394
|
+
* middleware: async (context, next) => {
|
|
1395
|
+
* // context.pluginOptions is automatically typed as an intersection:
|
|
1396
|
+
* // CacheReadOptions & CachePagesOptions (CacheWriteOptions filtered as it's just 'object')
|
|
1397
|
+
* // All properties from all option types are accessible:
|
|
1398
|
+
* const staleTime = context.pluginOptions?.staleTime ?? defaultStaleTime;
|
|
1399
|
+
* return next();
|
|
1400
|
+
* },
|
|
1401
|
+
* });
|
|
1402
|
+
* ```
|
|
1403
|
+
*/
|
|
1404
|
+
declare function createSpooshPlugin<T extends PluginTypeConfig>(definition: TypedPluginDefinition<T>): SpooshPlugin<T>;
|
|
1405
|
+
|
|
1314
1406
|
/**
|
|
1315
1407
|
* Base request options available on all methods.
|
|
1316
1408
|
*/
|
|
1317
1409
|
type BaseRequestOptions = {
|
|
1318
1410
|
headers?: HeadersInitOrGetter;
|
|
1319
|
-
cache?: RequestCache;
|
|
1320
1411
|
signal?: AbortSignal;
|
|
1321
1412
|
};
|
|
1322
1413
|
/**
|
|
@@ -2212,4 +2303,4 @@ declare class Semaphore {
|
|
|
2212
2303
|
getWaitingCount(): number;
|
|
2213
2304
|
}
|
|
2214
2305
|
|
|
2215
|
-
export { type AnyRequestOptions, type ApiSchema, type BuiltInEvents, type CacheEntry, type CacheEntryWithKey, type CapturedCall, type ComputeRequestOptions, type CoreRequestOptionsBase, type CreateInfiniteReadOptions, type CreateOperationOptions, type DataAwareCallback, type DataAwareTransform, type DataChangeCallback, type DevtoolEvents, type EventEmitter, type EventListener, type EventOptions, type EventTracer, type ExecuteOptions, type ExtractBody$1 as ExtractBody, type ExtractData, type ExtractError, type ExtractMethodOptions, type ExtractParamNames, type ExtractQuery$1 as ExtractQuery, type ExtractTriggerBody, type ExtractTriggerParams, type ExtractTriggerQuery, type FetchDirection, type FetchExecutor, type FindMatchingKey, HTTP_METHODS, type HasParams, type HasReadMethod, type HasWriteMethod, type HeadersInitOrGetter, type HttpMethod, type HttpMethodKey, type InfiniteNextContext, type InfinitePage, type InfinitePageStatus, type InfinitePrevContext, type InfiniteReadController, type InfiniteReadState, type InfiniteRequestOptions, type InfiniteTriggerOptions, type InstanceApiContext, type InstanceApiResolvers, type InstancePluginExecutor, type LifecyclePhase, type MergePluginInstanceApi, type MergePluginOptions, type MergePluginResults, type MethodOptionsMap, type OperationController, type OperationState, type OperationType, type PluginAccessor, type PluginArray, type PluginContext, type PluginContextBase, type PluginContextExtensions, type PluginContextInput, type PluginExecutor, type PluginExportsRegistry, type PluginFactory, type PluginHandler, type PluginLifecycle, type PluginMiddleware, type PluginRegistry, type PluginRequestOptions, type PluginResolvers, type PluginResponseHandler, type PluginResultResolvers, type PluginTypeConfig, type PluginUpdateHandler, type QueueController, type QueueControllerConfig, type QueueControllerContext, type QueueItem, type QueueItemStatus, type QueueSelectorClient, type QueueStats, type QueueTriggerInput, type ReadClient, type ReadPaths, type ReadSchemaHelper, type RefetchEvent, type RequestCompleteEvent, type RequestOptions$1 as RequestOptions, type RequestTracer, type ResolveInstanceApi, type ResolveResultTypes, type ResolveSchemaTypes, type ResolveTypes, type ResolverContext, type SchemaPaths, type SelectedEndpoint, type SelectorFunction, type SelectorResult, Semaphore, type SetupContext, type Simplify, Spoosh, type SpooshBody, type SpooshClient, type SpooshConfig, type SpooshInstance, type SpooshOptions, type SpooshOptionsInput, type SpooshPlugin, type SpooshResponse, type SpooshSchema, type StandaloneEvent, type StateManager, type StripPrefix, type Subscriber, type TagMode, type TagOptions, type Trace, type TraceColor, type TraceEvent, type TraceInfo, type TraceListener, type TraceOptions, type TraceStage, type Transport, type TransportOption, type TransportOptionsMap, type TransportResponse, type WriteClient, type WriteMethod, type WritePaths, type WriteSchemaHelper, type WriteSelectorClient, __DEV__, buildUrl, clone, containsFile, createClient, createEventEmitter, createInfiniteReadController, createInitialState, createOperationController, createPluginExecutor, createPluginRegistry, createProxyHandler, createQueueController, createSelectorProxy, createStateManager, createTracer, executeFetch, extractMethodFromSelector, extractPathFromSelector, fetchTransport, form, generateTags, getContentType, isAbortError, isJsonBody, isNetworkError, isSpooshBody, json, mergeHeaders, objectToFormData, objectToUrlEncoded, removeHeaderKeys, resolveHeadersToRecord, resolvePath, resolvePathString, resolveRequestBody, resolveTags, setHeaders, sortObjectKeys, urlencoded, xhrTransport };
|
|
2306
|
+
export { type AnyRequestOptions, type ApiSchema, type BuiltInEvents, type CacheEntry, type CacheEntryWithKey, type CapturedCall, type ComputeRequestOptions, type CoreRequestOptionsBase, type CreateInfiniteReadOptions, type CreateOperationOptions, type DataAwareCallback, type DataAwareTransform, type DataChangeCallback, type DevtoolEvents, type EventEmitter, type EventListener, type EventOptions, type EventTracer, type ExecuteOptions, type ExtractBody$1 as ExtractBody, type ExtractData, type ExtractError, type ExtractMethodOptions, type ExtractParamNames, type ExtractQuery$1 as ExtractQuery, type ExtractTriggerBody, type ExtractTriggerParams, type ExtractTriggerQuery, type FetchDirection, type FetchExecutor, type FindMatchingKey, HTTP_METHODS, type HasParams, type HasReadMethod, type HasWriteMethod, type HeadersInitOrGetter, type HttpMethod, type HttpMethodKey, type InfiniteNextContext, type InfinitePage, type InfinitePageStatus, type InfinitePrevContext, type InfiniteReadController, type InfiniteReadState, type InfiniteRequestOptions, type InfiniteTriggerOptions, type InstanceApiContext, type InstanceApiResolvers, type InstancePluginExecutor, type LifecyclePhase, type MergePluginInstanceApi, type MergePluginOptions, type MergePluginResults, type MethodOptionsMap, type OperationController, type OperationState, type OperationType, type PluginAccessor, type PluginArray, type PluginContext, type PluginContextBase, type PluginContextExtensions, type PluginContextInput, type PluginExecutor, type PluginExportsRegistry, type PluginFactory, type PluginHandler, type PluginLifecycle, type PluginMiddleware, type PluginRegistry, type PluginRequestOptions, type PluginResolvers, type PluginResponseHandler, type PluginResultResolvers, type PluginTypeConfig, type PluginUpdateHandler, type QueueController, type QueueControllerConfig, type QueueControllerContext, type QueueItem, type QueueItemStatus, type QueueSelectorClient, type QueueStats, type QueueTriggerInput, type ReadClient, type ReadPaths, type ReadSchemaHelper, type RefetchEvent, type RequestCompleteEvent, type RequestOptions$1 as RequestOptions, type RequestTracer, type ResolveInstanceApi, type ResolveResultTypes, type ResolveSchemaTypes, type ResolveTypes, type ResolverContext, type SchemaPaths, type SelectedEndpoint, type SelectorFunction, type SelectorResult, Semaphore, type SetupContext, type Simplify, Spoosh, type SpooshBody, type SpooshClient, type SpooshConfig, type SpooshInstance, type SpooshOptions, type SpooshOptionsInput, type SpooshPlugin, type SpooshResponse, type SpooshSchema, type StandaloneEvent, type StateManager, type StripPrefix, type Subscriber, type TagMode, type TagOptions, type Trace, type TraceColor, type TraceEvent, type TraceInfo, type TraceListener, type TraceOptions, type TraceStage, type Transport, type TransportOption, type TransportOptionsMap, type TransportResponse, type TypedPluginContext, type TypedPluginDefinition, type WriteClient, type WriteMethod, type WritePaths, type WriteSchemaHelper, type WriteSelectorClient, __DEV__, buildUrl, clone, containsFile, createClient, createEventEmitter, createInfiniteReadController, createInitialState, createOperationController, createPluginExecutor, createPluginRegistry, createProxyHandler, createQueueController, createSelectorProxy, createSpooshPlugin, createStateManager, createTracer, executeFetch, extractMethodFromSelector, extractPathFromSelector, fetchTransport, form, generateTags, getContentType, isAbortError, isJsonBody, isNetworkError, isSpooshBody, json, mergeHeaders, objectToFormData, objectToUrlEncoded, removeHeaderKeys, resolveHeadersToRecord, resolvePath, resolvePathString, resolveRequestBody, resolveTags, setHeaders, sortObjectKeys, urlencoded, xhrTransport };
|
package/dist/index.d.ts
CHANGED
|
@@ -1053,21 +1053,21 @@ type ExtractWriteResult<T> = T extends SpooshPlugin<infer Types> ? Types extends
|
|
|
1053
1053
|
type ExtractInstanceApi<T> = T extends SpooshPlugin<infer Types> ? Types extends {
|
|
1054
1054
|
instanceApi: infer A;
|
|
1055
1055
|
} ? A : object : object;
|
|
1056
|
-
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
1056
|
+
type UnionToIntersection$1<U> = (U extends unknown ? (x: U) => void : never) extends (x: infer I) => void ? I : never;
|
|
1057
1057
|
type MergePluginOptions<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
1058
|
-
read: UnionToIntersection<ExtractReadOptions<TPlugins[number]>>;
|
|
1059
|
-
write: UnionToIntersection<ExtractWriteOptions<TPlugins[number]>>;
|
|
1060
|
-
pages: UnionToIntersection<ExtractPagesOptions<TPlugins[number]>>;
|
|
1061
|
-
writeTrigger: UnionToIntersection<ExtractWriteTriggerOptions<TPlugins[number]>>;
|
|
1062
|
-
queue: UnionToIntersection<ExtractQueueOptions<TPlugins[number]>>;
|
|
1063
|
-
queueTrigger: UnionToIntersection<ExtractQueueTriggerOptions<TPlugins[number]>>;
|
|
1058
|
+
read: UnionToIntersection$1<ExtractReadOptions<TPlugins[number]>>;
|
|
1059
|
+
write: UnionToIntersection$1<ExtractWriteOptions<TPlugins[number]>>;
|
|
1060
|
+
pages: UnionToIntersection$1<ExtractPagesOptions<TPlugins[number]>>;
|
|
1061
|
+
writeTrigger: UnionToIntersection$1<ExtractWriteTriggerOptions<TPlugins[number]>>;
|
|
1062
|
+
queue: UnionToIntersection$1<ExtractQueueOptions<TPlugins[number]>>;
|
|
1063
|
+
queueTrigger: UnionToIntersection$1<ExtractQueueTriggerOptions<TPlugins[number]>>;
|
|
1064
1064
|
};
|
|
1065
1065
|
type MergePluginResults<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[]> = {
|
|
1066
|
-
read: UnionToIntersection<ExtractReadResult<TPlugins[number]>>;
|
|
1067
|
-
write: UnionToIntersection<ExtractWriteResult<TPlugins[number]>>;
|
|
1068
|
-
queue: UnionToIntersection<ExtractQueueResult<TPlugins[number]>>;
|
|
1066
|
+
read: UnionToIntersection$1<ExtractReadResult<TPlugins[number]>>;
|
|
1067
|
+
write: UnionToIntersection$1<ExtractWriteResult<TPlugins[number]>>;
|
|
1068
|
+
queue: UnionToIntersection$1<ExtractQueueResult<TPlugins[number]>>;
|
|
1069
1069
|
};
|
|
1070
|
-
type MergePluginInstanceApi<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[], TSchema = unknown> = ResolveInstanceApi<UnionToIntersection<ExtractInstanceApi<TPlugins[number]>>, TSchema, MergePluginOptions<TPlugins>["read"]>;
|
|
1070
|
+
type MergePluginInstanceApi<TPlugins extends readonly SpooshPlugin<PluginTypeConfig>[], TSchema = unknown> = ResolveInstanceApi<UnionToIntersection$1<ExtractInstanceApi<TPlugins[number]>>, TSchema, MergePluginOptions<TPlugins>["read"]>;
|
|
1071
1071
|
type PluginRegistry<TPlugins extends SpooshPlugin<PluginTypeConfig>[]> = {
|
|
1072
1072
|
plugins: TPlugins;
|
|
1073
1073
|
_options: MergePluginOptions<TPlugins>;
|
|
@@ -1311,12 +1311,103 @@ type WritePathMethods$1<TSchema, TPath extends string> = FindMatchingKey<TSchema
|
|
|
1311
1311
|
*/
|
|
1312
1312
|
type WriteSchemaHelper<TSchema> = <TPath extends WritePaths<TSchema> | (string & {})>(path: TPath) => HasWriteMethod<TSchema, TPath> extends true ? WritePathMethods$1<TSchema, TPath> : never;
|
|
1313
1313
|
|
|
1314
|
+
/**
|
|
1315
|
+
* Check for exact type equality using function type assignability.
|
|
1316
|
+
* Two types are equal if functions returning them are mutually assignable.
|
|
1317
|
+
*/
|
|
1318
|
+
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
1319
|
+
/**
|
|
1320
|
+
* Convert bare `object` type to `never` to exclude it from the union.
|
|
1321
|
+
* Preserves all other types including interfaces and types with actual properties.
|
|
1322
|
+
*/
|
|
1323
|
+
type FilterObjectType<T> = Equals<T, object> extends true ? never : T;
|
|
1324
|
+
/**
|
|
1325
|
+
* Convert a union type to an intersection type.
|
|
1326
|
+
* This merges all properties from all types in the union.
|
|
1327
|
+
*/
|
|
1328
|
+
type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends (arg: infer I) => void ? I : never;
|
|
1329
|
+
/**
|
|
1330
|
+
* Extract all option types from plugin configuration and create a union, then intersect.
|
|
1331
|
+
* This allows middleware to access all properties from all option types.
|
|
1332
|
+
* object types are converted to unknown which doesn't affect the intersection.
|
|
1333
|
+
*/
|
|
1334
|
+
type ExtractPluginOptionsUnion<T extends PluginTypeConfig> = UnionToIntersection<FilterObjectType<T extends {
|
|
1335
|
+
readOptions: infer R;
|
|
1336
|
+
} ? R : never> | FilterObjectType<T extends {
|
|
1337
|
+
writeOptions: infer W;
|
|
1338
|
+
} ? W : never> | FilterObjectType<T extends {
|
|
1339
|
+
writeTriggerOptions: infer WT;
|
|
1340
|
+
} ? WT : never> | FilterObjectType<T extends {
|
|
1341
|
+
queueTriggerOptions: infer QT;
|
|
1342
|
+
} ? QT : never> | FilterObjectType<T extends {
|
|
1343
|
+
pagesOptions: infer P;
|
|
1344
|
+
} ? P : never> | FilterObjectType<T extends {
|
|
1345
|
+
queueOptions: infer Q;
|
|
1346
|
+
} ? Q : never>>;
|
|
1347
|
+
/**
|
|
1348
|
+
* Plugin context with typed pluginOptions based on plugin configuration.
|
|
1349
|
+
*/
|
|
1350
|
+
type TypedPluginContext<T extends PluginTypeConfig> = Omit<PluginContext, "pluginOptions"> & {
|
|
1351
|
+
pluginOptions?: ExtractPluginOptionsUnion<T>;
|
|
1352
|
+
};
|
|
1353
|
+
/**
|
|
1354
|
+
* Plugin definition with typed context for middleware and handlers.
|
|
1355
|
+
*/
|
|
1356
|
+
type TypedPluginDefinition<T extends PluginTypeConfig> = Omit<SpooshPlugin<T>, "middleware" | "afterResponse" | "lifecycle"> & {
|
|
1357
|
+
middleware?: (context: TypedPluginContext<T>, next: () => Promise<any>) => Promise<any>;
|
|
1358
|
+
afterResponse?: (context: TypedPluginContext<T>, response: any) => any;
|
|
1359
|
+
lifecycle?: {
|
|
1360
|
+
onMount?: (context: TypedPluginContext<T>) => void | Promise<void>;
|
|
1361
|
+
onUpdate?: (context: TypedPluginContext<T>, previousContext: TypedPluginContext<T>) => void | Promise<void>;
|
|
1362
|
+
onUnmount?: (context: TypedPluginContext<T>) => void | Promise<void>;
|
|
1363
|
+
};
|
|
1364
|
+
};
|
|
1365
|
+
/**
|
|
1366
|
+
* Helper to create a Spoosh plugin with automatic type inference for plugin options.
|
|
1367
|
+
*
|
|
1368
|
+
* This eliminates the need for manual type assertions in middleware by automatically
|
|
1369
|
+
* intersecting all option types, making all properties accessible:
|
|
1370
|
+
*
|
|
1371
|
+
* ```ts
|
|
1372
|
+
* // Before:
|
|
1373
|
+
* const pluginOptions = context.pluginOptions as CacheReadOptions | undefined;
|
|
1374
|
+
* const staleTime = pluginOptions?.staleTime ?? defaultStaleTime;
|
|
1375
|
+
*
|
|
1376
|
+
* // After (with createSpooshPlugin):
|
|
1377
|
+
* const staleTime = context.pluginOptions?.staleTime ?? defaultStaleTime;
|
|
1378
|
+
* ```
|
|
1379
|
+
*
|
|
1380
|
+
* @typeParam T - Plugin type configuration (readOptions, writeOptions, etc.)
|
|
1381
|
+
* @param definition - Plugin definition with typed context
|
|
1382
|
+
* @returns Typed Spoosh plugin
|
|
1383
|
+
*
|
|
1384
|
+
* @example
|
|
1385
|
+
* ```ts
|
|
1386
|
+
* export const cachePlugin = (config: CachePluginConfig = {}) =>
|
|
1387
|
+
* createSpooshPlugin<{
|
|
1388
|
+
* readOptions: CacheReadOptions;
|
|
1389
|
+
* writeOptions: CacheWriteOptions;
|
|
1390
|
+
* pagesOptions: CachePagesOptions;
|
|
1391
|
+
* }>({
|
|
1392
|
+
* name: "spoosh:cache",
|
|
1393
|
+
* operations: ["read", "write", "pages"],
|
|
1394
|
+
* middleware: async (context, next) => {
|
|
1395
|
+
* // context.pluginOptions is automatically typed as an intersection:
|
|
1396
|
+
* // CacheReadOptions & CachePagesOptions (CacheWriteOptions filtered as it's just 'object')
|
|
1397
|
+
* // All properties from all option types are accessible:
|
|
1398
|
+
* const staleTime = context.pluginOptions?.staleTime ?? defaultStaleTime;
|
|
1399
|
+
* return next();
|
|
1400
|
+
* },
|
|
1401
|
+
* });
|
|
1402
|
+
* ```
|
|
1403
|
+
*/
|
|
1404
|
+
declare function createSpooshPlugin<T extends PluginTypeConfig>(definition: TypedPluginDefinition<T>): SpooshPlugin<T>;
|
|
1405
|
+
|
|
1314
1406
|
/**
|
|
1315
1407
|
* Base request options available on all methods.
|
|
1316
1408
|
*/
|
|
1317
1409
|
type BaseRequestOptions = {
|
|
1318
1410
|
headers?: HeadersInitOrGetter;
|
|
1319
|
-
cache?: RequestCache;
|
|
1320
1411
|
signal?: AbortSignal;
|
|
1321
1412
|
};
|
|
1322
1413
|
/**
|
|
@@ -2212,4 +2303,4 @@ declare class Semaphore {
|
|
|
2212
2303
|
getWaitingCount(): number;
|
|
2213
2304
|
}
|
|
2214
2305
|
|
|
2215
|
-
export { type AnyRequestOptions, type ApiSchema, type BuiltInEvents, type CacheEntry, type CacheEntryWithKey, type CapturedCall, type ComputeRequestOptions, type CoreRequestOptionsBase, type CreateInfiniteReadOptions, type CreateOperationOptions, type DataAwareCallback, type DataAwareTransform, type DataChangeCallback, type DevtoolEvents, type EventEmitter, type EventListener, type EventOptions, type EventTracer, type ExecuteOptions, type ExtractBody$1 as ExtractBody, type ExtractData, type ExtractError, type ExtractMethodOptions, type ExtractParamNames, type ExtractQuery$1 as ExtractQuery, type ExtractTriggerBody, type ExtractTriggerParams, type ExtractTriggerQuery, type FetchDirection, type FetchExecutor, type FindMatchingKey, HTTP_METHODS, type HasParams, type HasReadMethod, type HasWriteMethod, type HeadersInitOrGetter, type HttpMethod, type HttpMethodKey, type InfiniteNextContext, type InfinitePage, type InfinitePageStatus, type InfinitePrevContext, type InfiniteReadController, type InfiniteReadState, type InfiniteRequestOptions, type InfiniteTriggerOptions, type InstanceApiContext, type InstanceApiResolvers, type InstancePluginExecutor, type LifecyclePhase, type MergePluginInstanceApi, type MergePluginOptions, type MergePluginResults, type MethodOptionsMap, type OperationController, type OperationState, type OperationType, type PluginAccessor, type PluginArray, type PluginContext, type PluginContextBase, type PluginContextExtensions, type PluginContextInput, type PluginExecutor, type PluginExportsRegistry, type PluginFactory, type PluginHandler, type PluginLifecycle, type PluginMiddleware, type PluginRegistry, type PluginRequestOptions, type PluginResolvers, type PluginResponseHandler, type PluginResultResolvers, type PluginTypeConfig, type PluginUpdateHandler, type QueueController, type QueueControllerConfig, type QueueControllerContext, type QueueItem, type QueueItemStatus, type QueueSelectorClient, type QueueStats, type QueueTriggerInput, type ReadClient, type ReadPaths, type ReadSchemaHelper, type RefetchEvent, type RequestCompleteEvent, type RequestOptions$1 as RequestOptions, type RequestTracer, type ResolveInstanceApi, type ResolveResultTypes, type ResolveSchemaTypes, type ResolveTypes, type ResolverContext, type SchemaPaths, type SelectedEndpoint, type SelectorFunction, type SelectorResult, Semaphore, type SetupContext, type Simplify, Spoosh, type SpooshBody, type SpooshClient, type SpooshConfig, type SpooshInstance, type SpooshOptions, type SpooshOptionsInput, type SpooshPlugin, type SpooshResponse, type SpooshSchema, type StandaloneEvent, type StateManager, type StripPrefix, type Subscriber, type TagMode, type TagOptions, type Trace, type TraceColor, type TraceEvent, type TraceInfo, type TraceListener, type TraceOptions, type TraceStage, type Transport, type TransportOption, type TransportOptionsMap, type TransportResponse, type WriteClient, type WriteMethod, type WritePaths, type WriteSchemaHelper, type WriteSelectorClient, __DEV__, buildUrl, clone, containsFile, createClient, createEventEmitter, createInfiniteReadController, createInitialState, createOperationController, createPluginExecutor, createPluginRegistry, createProxyHandler, createQueueController, createSelectorProxy, createStateManager, createTracer, executeFetch, extractMethodFromSelector, extractPathFromSelector, fetchTransport, form, generateTags, getContentType, isAbortError, isJsonBody, isNetworkError, isSpooshBody, json, mergeHeaders, objectToFormData, objectToUrlEncoded, removeHeaderKeys, resolveHeadersToRecord, resolvePath, resolvePathString, resolveRequestBody, resolveTags, setHeaders, sortObjectKeys, urlencoded, xhrTransport };
|
|
2306
|
+
export { type AnyRequestOptions, type ApiSchema, type BuiltInEvents, type CacheEntry, type CacheEntryWithKey, type CapturedCall, type ComputeRequestOptions, type CoreRequestOptionsBase, type CreateInfiniteReadOptions, type CreateOperationOptions, type DataAwareCallback, type DataAwareTransform, type DataChangeCallback, type DevtoolEvents, type EventEmitter, type EventListener, type EventOptions, type EventTracer, type ExecuteOptions, type ExtractBody$1 as ExtractBody, type ExtractData, type ExtractError, type ExtractMethodOptions, type ExtractParamNames, type ExtractQuery$1 as ExtractQuery, type ExtractTriggerBody, type ExtractTriggerParams, type ExtractTriggerQuery, type FetchDirection, type FetchExecutor, type FindMatchingKey, HTTP_METHODS, type HasParams, type HasReadMethod, type HasWriteMethod, type HeadersInitOrGetter, type HttpMethod, type HttpMethodKey, type InfiniteNextContext, type InfinitePage, type InfinitePageStatus, type InfinitePrevContext, type InfiniteReadController, type InfiniteReadState, type InfiniteRequestOptions, type InfiniteTriggerOptions, type InstanceApiContext, type InstanceApiResolvers, type InstancePluginExecutor, type LifecyclePhase, type MergePluginInstanceApi, type MergePluginOptions, type MergePluginResults, type MethodOptionsMap, type OperationController, type OperationState, type OperationType, type PluginAccessor, type PluginArray, type PluginContext, type PluginContextBase, type PluginContextExtensions, type PluginContextInput, type PluginExecutor, type PluginExportsRegistry, type PluginFactory, type PluginHandler, type PluginLifecycle, type PluginMiddleware, type PluginRegistry, type PluginRequestOptions, type PluginResolvers, type PluginResponseHandler, type PluginResultResolvers, type PluginTypeConfig, type PluginUpdateHandler, type QueueController, type QueueControllerConfig, type QueueControllerContext, type QueueItem, type QueueItemStatus, type QueueSelectorClient, type QueueStats, type QueueTriggerInput, type ReadClient, type ReadPaths, type ReadSchemaHelper, type RefetchEvent, type RequestCompleteEvent, type RequestOptions$1 as RequestOptions, type RequestTracer, type ResolveInstanceApi, type ResolveResultTypes, type ResolveSchemaTypes, type ResolveTypes, type ResolverContext, type SchemaPaths, type SelectedEndpoint, type SelectorFunction, type SelectorResult, Semaphore, type SetupContext, type Simplify, Spoosh, type SpooshBody, type SpooshClient, type SpooshConfig, type SpooshInstance, type SpooshOptions, type SpooshOptionsInput, type SpooshPlugin, type SpooshResponse, type SpooshSchema, type StandaloneEvent, type StateManager, type StripPrefix, type Subscriber, type TagMode, type TagOptions, type Trace, type TraceColor, type TraceEvent, type TraceInfo, type TraceListener, type TraceOptions, type TraceStage, type Transport, type TransportOption, type TransportOptionsMap, type TransportResponse, type TypedPluginContext, type TypedPluginDefinition, type WriteClient, type WriteMethod, type WritePaths, type WriteSchemaHelper, type WriteSelectorClient, __DEV__, buildUrl, clone, containsFile, createClient, createEventEmitter, createInfiniteReadController, createInitialState, createOperationController, createPluginExecutor, createPluginRegistry, createProxyHandler, createQueueController, createSelectorProxy, createSpooshPlugin, createStateManager, createTracer, executeFetch, extractMethodFromSelector, extractPathFromSelector, fetchTransport, form, generateTags, getContentType, isAbortError, isJsonBody, isNetworkError, isSpooshBody, json, mergeHeaders, objectToFormData, objectToUrlEncoded, removeHeaderKeys, resolveHeadersToRecord, resolvePath, resolvePathString, resolveRequestBody, resolveTags, setHeaders, sortObjectKeys, urlencoded, xhrTransport };
|
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ __export(src_exports, {
|
|
|
37
37
|
createProxyHandler: () => createProxyHandler,
|
|
38
38
|
createQueueController: () => createQueueController,
|
|
39
39
|
createSelectorProxy: () => createSelectorProxy,
|
|
40
|
+
createSpooshPlugin: () => createSpooshPlugin,
|
|
40
41
|
createStateManager: () => createStateManager,
|
|
41
42
|
createTracer: () => createTracer,
|
|
42
43
|
executeFetch: () => executeFetch,
|
|
@@ -1084,6 +1085,11 @@ function createPluginRegistry(plugins) {
|
|
|
1084
1085
|
};
|
|
1085
1086
|
}
|
|
1086
1087
|
|
|
1088
|
+
// src/plugins/create.ts
|
|
1089
|
+
function createSpooshPlugin(definition) {
|
|
1090
|
+
return definition;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1087
1093
|
// src/Spoosh.ts
|
|
1088
1094
|
var Spoosh = class _Spoosh {
|
|
1089
1095
|
baseUrl;
|
package/dist/index.mjs
CHANGED
|
@@ -1015,6 +1015,11 @@ function createPluginRegistry(plugins) {
|
|
|
1015
1015
|
};
|
|
1016
1016
|
}
|
|
1017
1017
|
|
|
1018
|
+
// src/plugins/create.ts
|
|
1019
|
+
function createSpooshPlugin(definition) {
|
|
1020
|
+
return definition;
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1018
1023
|
// src/Spoosh.ts
|
|
1019
1024
|
var Spoosh = class _Spoosh {
|
|
1020
1025
|
baseUrl;
|
|
@@ -2188,6 +2193,7 @@ export {
|
|
|
2188
2193
|
createProxyHandler,
|
|
2189
2194
|
createQueueController,
|
|
2190
2195
|
createSelectorProxy,
|
|
2196
|
+
createSpooshPlugin,
|
|
2191
2197
|
createStateManager,
|
|
2192
2198
|
createTracer,
|
|
2193
2199
|
executeFetch,
|