Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support entity type mappings #206

Merged
merged 27 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4726583
feat: introduced basic type map
ffMathy Sep 12, 2023
11dd5ae
feat: use stronger types
ffMathy Sep 12, 2023
6a706f6
fix: tests pass
ffMathy Sep 12, 2023
666e155
fix: compile issues
ffMathy Sep 12, 2023
43a9422
fix: better `ensure` types
ffMathy Sep 12, 2023
7e754bf
Merge branch 'main' into feature/type-extensibility
ffMathy Sep 12, 2023
956d104
Merge branch 'main' into feature/type-extensibility
ffMathy Sep 14, 2023
92c3832
fix: keyof everywhere
ffMathy Sep 14, 2023
6fc0776
Merge branch 'main' into feature/type-extensibility
ffMathy Sep 18, 2023
d8aab39
Merge branch 'main' into feature/type-extensibility
ffMathy Oct 17, 2023
dd5d437
Merge branch 'main' into feature/type-extensibility
ffMathy Nov 9, 2023
f6aac8d
Update source/types.ts
ffMathy Nov 10, 2023
564da6b
Merge branch 'main' into feature/type-extensibility
ffMathy Nov 20, 2023
4f98e71
Merge branch 'main' into feature/type-extensibility
ffMathy Nov 22, 2023
63a89e0
Refinements
gismya Jan 5, 2024
259e993
K to TEntityType
gismya Jan 5, 2024
1ad712d
Make the entitytype key the base for the responses as well
gismya Jan 5, 2024
03a2242
Merge branch 'main' into feature/type-extensibility
ffMathy Jan 6, 2024
074e937
Merge branch 'main' into feature/type-extensibility
ffMathy Jan 17, 2024
8681d18
Review Fixes
gismya Feb 13, 2024
bd88b36
Merge branch 'main' into feature/type-extensibility
gismya Feb 13, 2024
1b651c3
TS/Prettier/Import fixes
gismya Feb 13, 2024
54e1ca9
Made EntityType generic naming consistent
gismya Feb 13, 2024
3a42230
WIP
gismya Feb 15, 2024
f943e02
send tentitytypemap as a generic
jimmycallin Feb 15, 2024
ae3672a
add documentation
jimmycallin Feb 21, 2024
15d678b
Merge branch 'main' into feature/more-type-extensibility
jimmycallin Feb 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions source/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @namespace operation
*/

export interface CreateOperation {
export interface CreateOperation<TEntityType> {
action: "create";
entity_type: string;
entity_type: TEntityType;
entity_data: any;
}

Expand All @@ -15,33 +15,33 @@ export interface QueryOperation {
expression: string;
}

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

export interface SearchOperation {
export interface SearchOperation<TEntityType> {
action: "search";
expression?: string;
entity_type?: string;
entity_type?: TEntityType;
terms?: string[];
context_id?: string;
object_type_ids?: string[];
}

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

export interface DeleteOperation {
export interface DeleteOperation<TEntityType> {
action: "delete";
entity_type: string;
entity_type: TEntityType;
entity_key: string[] | string;
}

Expand All @@ -61,12 +61,12 @@ export interface GetUploadMetadataOperation {
component_id: string;
}

export type Operation =
| CreateOperation
export type Operation<TEntityType> =
| CreateOperation<TEntityType>
| QueryOperation
| SearchOperation
| UpdateOperation
| DeleteOperation
| SearchOperation<TEntityType>
| UpdateOperation<TEntityType>
| DeleteOperation<TEntityType>
| QueryServerInformationOperation
| QuerySchemasOperation
| GetUploadMetadataOperation
Expand All @@ -81,7 +81,10 @@ export type Operation =
* @param {Object} data Entity data to use for creation
* @return {Object} API operation
*/
export function create(type: string, data: any): CreateOperation {
export function create<TEntityType>(
type: TEntityType,
data: any,
): CreateOperation<TEntityType> {
return {
action: "create",
entity_type: type,
Expand Down Expand Up @@ -109,13 +112,13 @@ export function query(expression: string): QueryOperation {
* @param {string} expression API query expression
* @return {Object} API operation
*/
export function search({
export function search<TEntityType>({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TEntityType is automatically inferred from the options type, so no need to specify it explicitly (please verify)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's clever if it works. Needs to be tested. The only issue I see is a lack of verification of the TEntitityType in the Options.

expression,
entityType,
terms,
contextId,
objectTypeIds,
}: SearchOperationOptions): SearchOperation {
}: SearchOperationOptions<TEntityType>): SearchOperation<TEntityType> {
return {
action: "search",
expression,
Expand All @@ -136,11 +139,11 @@ export function search({
* @param {Object} data values to update
* @return {Object} API operation
*/
export function update(
type: string,
export function update<TEntityType>(
type: TEntityType,
keys: string[] | string,
data: any,
): UpdateOperation {
): UpdateOperation<TEntityType> {
return {
action: "update",
entity_type: type,
Expand All @@ -158,10 +161,10 @@ export function update(
* @param {Array} keys Identifying keys, typically [<entity id>]
* @return {Object} API operation
*/
function deleteOperation(
type: string,
function deleteOperation<TEntityType>(
type: TEntityType,
keys: string[] | string,
): DeleteOperation {
): DeleteOperation<TEntityType> {
return {
action: "delete",
entity_type: type,
Expand Down
9 changes: 5 additions & 4 deletions source/project_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as operation from "./operation.js";
import { Session } from "./session.js";
import type { Data, QueryResponse } from "./types.js";
import type { QueryResponse } from "./types.js";
/**
* Project schema namespace
* @namespace project_schema
Expand All @@ -18,8 +18,8 @@ import type { Data, QueryResponse } from "./types.js";
*
* @memberof project_schema
*/
export function getStatuses(
session: Session,
export function getStatuses<TEntityTypeMap extends Record<string, any>>(
session: Session<TEntityTypeMap>,
Copy link
Contributor Author

@jimmycallin jimmycallin Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should automatically get the entitytypemap inferred from the provided session, make sure it works so you don't have to specify it explicitly

projectSchemaId: string,
entityType: string,
typeId: string | null = null,
Expand Down Expand Up @@ -71,7 +71,8 @@ export function getStatuses(
),
);

response = session.call<QueryResponse<Data>>(operations);
response =
session.call<QueryResponse<TEntityTypeMap["ProjectSchema"]>>(operations);
response = response.then((results) => {
// Since the operations where performed in one batched call,
// the result will be merged into a single entity.
Expand Down
Loading
Loading