Skip to content

Commit 19a9ee7

Browse files
committed
WIP Add strict pinejs mode
Change-type: patch
1 parent ec1285a commit 19a9ee7

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

src/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,7 @@ export type ResourceExpand<T extends Resource['Read'] = AnyResourceObject> = {
20392039
export type BaseExpand<T extends Resource['Read'] = AnyResourceObject> =
20402040
| ExpandableStringKeyOf<T>
20412041
| ResourceExpand<T>;
2042+
20422043
export type Expand<T extends Resource['Read'] = AnyResourceObject> =
20432044
| BaseExpand<T>
20442045
| ReadonlyArray<BaseExpand<T>>;
@@ -2163,3 +2164,65 @@ export interface UpsertParams<T extends Resource = AnyResource>
21632164
resource: string;
21642165
body: Partial<T['Write']>;
21652166
}
2167+
2168+
export type StrictODataOptions<T extends Resource['Read'] = AnyResourceObject> =
2169+
Omit<ODataOptions<T>, '$select' | '$expand'> & {
2170+
$expand?: StrictExpand<T>;
2171+
} & (
2172+
| {
2173+
$count: NonNullable<ODataOptions<T>['$count']>;
2174+
// TODO: Maybe never is the better type here?
2175+
// TODO: Ask Page if it makes any sense to have $count & $select together
2176+
$select?: ODataOptions<T>['$select'];
2177+
}
2178+
| {
2179+
// TODO: Maybe never is the better type here?
2180+
$count?: undefined;
2181+
$select: NonNullable<ODataOptions<T>['$select']>;
2182+
}
2183+
);
2184+
2185+
type StrictResourceExpand<T extends Resource['Read']> = {
2186+
[K in StringKeyOf<T> as ExtractExpand<T, K> extends never
2187+
? never
2188+
: K]?: ExtractExpand<T, K> extends Resource['Read']
2189+
? StrictODataOptions<ExtractExpand<T, K>>
2190+
: ODataOptions<ExtractExpand<T, K>>;
2191+
};
2192+
2193+
// StrictExpand does not allow to $expand with a string or list of string (resources)
2194+
// instead, it enforces a resource expand with $select
2195+
type StrictExpand<T extends Resource['Read']> =
2196+
| StrictResourceExpand<T>
2197+
| ReadonlyArray<StrictResourceExpand<T>>;
2198+
2199+
type StrictParams<T extends Resource = AnyResource> = Omit<
2200+
Params<T>,
2201+
'options'
2202+
> & {
2203+
options: StrictODataOptions<T['Read']>;
2204+
};
2205+
2206+
// Forces a $select to exist for get, subscribe and prepare
2207+
export type StrictPinejsClientCore<
2208+
Model extends { [key in keyof Model]: Resource } = {
2209+
[key in string]: AnyResource;
2210+
},
2211+
> = Omit<PinejsClientCore<Model>, 'get'> & {
2212+
get<
2213+
TResource extends StringKeyOf<Model>,
2214+
TParams extends StrictParams<Model[TResource]> & {
2215+
resource: TResource;
2216+
},
2217+
>(
2218+
params: { resource: TResource } & TParams,
2219+
): Promise<
2220+
NoInfer<
2221+
OptionsToResponse<
2222+
Model[TResource]['Read'],
2223+
NonNullable<StrictParams['options']>,
2224+
TParams['id']
2225+
>
2226+
>
2227+
>;
2228+
};

0 commit comments

Comments
 (0)