Skip to content

Commit

Permalink
fix: Add defaults to generics to fallback to old behaviour (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmycallin authored Mar 15, 2024
1 parent dbbf15c commit 476be6e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
24 changes: 14 additions & 10 deletions source/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
* @namespace operation
*/

export interface CreateOperation<TEntityType> {
import type { DefaultEntityTypeMap } from "./types.js";

export interface CreateOperation<TEntityType = keyof DefaultEntityTypeMap> {
action: "create";
entity_type: TEntityType;
entity_data: any;
Expand All @@ -15,15 +17,17 @@ export interface QueryOperation {
expression: string;
}

export interface SearchOperationOptions<TEntityType> {
export interface SearchOperationOptions<
TEntityType = keyof DefaultEntityTypeMap,
> {
expression?: string;
entityType?: TEntityType;
terms?: string[];
contextId?: string;
objectTypeIds?: string[];
}

export interface SearchOperation<TEntityType> {
export interface SearchOperation<TEntityType = keyof DefaultEntityTypeMap> {
action: "search";
expression?: string;
entity_type?: TEntityType;
Expand All @@ -32,14 +36,14 @@ export interface SearchOperation<TEntityType> {
object_type_ids?: string[];
}

export interface UpdateOperation<TEntityType> {
export interface UpdateOperation<TEntityType = keyof DefaultEntityTypeMap> {
action: "update";
entity_type: TEntityType;
entity_key: string[] | string;
entity_data: any;
}

export interface DeleteOperation<TEntityType> {
export interface DeleteOperation<TEntityType = keyof DefaultEntityTypeMap> {
action: "delete";
entity_type: TEntityType;
entity_key: string[] | string;
Expand All @@ -61,7 +65,7 @@ export interface GetUploadMetadataOperation {
component_id: string;
}

export type Operation<TEntityType> =
export type Operation<TEntityType = keyof DefaultEntityTypeMap> =
| CreateOperation<TEntityType>
| QueryOperation
| SearchOperation<TEntityType>
Expand All @@ -81,7 +85,7 @@ export type Operation<TEntityType> =
* @param {Object} data Entity data to use for creation
* @return {Object} API operation
*/
export function create<TEntityType>(
export function create<TEntityType = keyof DefaultEntityTypeMap>(
type: TEntityType,
data: any,
): CreateOperation<TEntityType> {
Expand Down Expand Up @@ -112,7 +116,7 @@ export function query(expression: string): QueryOperation {
* @param {string} expression API query expression
* @return {Object} API operation
*/
export function search<TEntityType>({
export function search<TEntityType = keyof DefaultEntityTypeMap>({
expression,
entityType,
terms,
Expand All @@ -139,7 +143,7 @@ export function search<TEntityType>({
* @param {Object} data values to update
* @return {Object} API operation
*/
export function update<TEntityType>(
export function update<TEntityType = keyof DefaultEntityTypeMap>(
type: TEntityType,
keys: string[] | string,
data: any,
Expand All @@ -161,7 +165,7 @@ export function update<TEntityType>(
* @param {Array} keys Identifying keys, typically [<entity id>]
* @return {Object} API operation
*/
function deleteOperation<TEntityType>(
function deleteOperation<TEntityType = keyof DefaultEntityTypeMap>(
type: TEntityType,
keys: string[] | string,
): DeleteOperation<TEntityType> {
Expand Down
29 changes: 20 additions & 9 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,41 @@ interface ResponseMetadata {
offset: number | null;
};
}
export interface SearchOptions<TEntityType> {
export interface SearchOptions<TEntityType = keyof DefaultEntityTypeMap> {
expression: string;
entityType: TEntityType;
terms?: string[];
contextId?: string;
objectTypeIds?: string[];
}

export interface QueryResponse<TEntityData> {
export interface QueryResponse<
TEntityData = DefaultEntityTypeMap[keyof DefaultEntityTypeMap],
> {
data: TEntityData[];
action: "query";
metadata: ResponseMetadata;
}

export interface CreateResponse<TEntityData> {
export interface CreateResponse<
TEntityData = DefaultEntityTypeMap[keyof DefaultEntityTypeMap],
> {
data: TEntityData;
action: "create";
}
export interface UpdateResponse<TEntityData> {
export interface UpdateResponse<
TEntityData = DefaultEntityTypeMap[keyof DefaultEntityTypeMap],
> {
data: TEntityData;
action: "update";
}
export interface DeleteResponse {
data: true;
action: "delete";
}
export interface SearchResponse<TEntityData> {
export interface SearchResponse<
TEntityData = DefaultEntityTypeMap[keyof DefaultEntityTypeMap],
> {
data: TEntityData[];
action: "search";
metadata: ResponseMetadata;
Expand All @@ -65,7 +73,8 @@ export interface ResetRemoteResponse {
action: "reset_remote";
data: Data;
}
export type QuerySchemasResponse<TEntityTypeMap> = Schema<TEntityTypeMap>[];
export type QuerySchemasResponse<TEntityTypeMap = DefaultEntityTypeMap> =
Schema<TEntityTypeMap>[];

export type QueryServerInformationResponse = ServerInformation;
export interface ServerInformation {
Expand Down Expand Up @@ -146,7 +155,7 @@ export interface PermissionsResponse {
}

export type ActionResponse<
TEntityTypeMap,
TEntityTypeMap = DefaultEntityTypeMap,
TEntityType extends keyof TEntityTypeMap = keyof TEntityTypeMap,
TEntityData = TEntityTypeMap[TEntityType],
> =
Expand Down Expand Up @@ -201,15 +210,17 @@ export type TypedSchemaProperty =
export type RefSchemaProperty = {
["$ref"]: string;
};
export type SchemaProperties<TEntityData> = {
export type SchemaProperties<
TEntityData = DefaultEntityTypeMap[keyof DefaultEntityTypeMap],
> = {
[key in keyof TEntityData]: TypedSchemaProperty | RefSchemaProperty;
};
export type SchemaMixin = {
$ref: string;
};
export type SchemaMetadata = { entity_event: boolean };
export interface Schema<
TEntityTypeMap,
TEntityTypeMap = DefaultEntityTypeMap,
TEntityType extends keyof TEntityTypeMap = keyof TEntityTypeMap,
> {
properties: SchemaProperties<TEntityTypeMap[TEntityType]>;
Expand Down

0 comments on commit 476be6e

Please sign in to comment.