Skip to content

Commit 9cab49f

Browse files
jimmycallinffMathygismya
authored
feat: Support entity type mappings (#206)
* feat: introduced basic type map For later extensibility. * feat: use stronger types * fix: tests pass * fix: compile issues * fix: better `ensure` types * fix: keyof everywhere * Update source/types.ts Co-authored-by: Lars Johansson <[email protected]> * Refinements * K to TEntityType * Make the entitytype key the base for the responses as well * Review Fixes * TS/Prettier/Import fixes * Made EntityType generic naming consistent * WIP * send tentitytypemap as a generic * add documentation --------- Co-authored-by: Mathias Lorenzen <[email protected]> Co-authored-by: Mathias Lykkegaard Lorenzen <[email protected]> Co-authored-by: Lars Johansson <[email protected]> Co-authored-by: Lars Johansson <[email protected]>
1 parent 65cd894 commit 9cab49f

File tree

8 files changed

+209
-138
lines changed

8 files changed

+209
-138
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ yarn:
3434
yarn add @ftrack/api
3535
```
3636

37+
## TypeScript: Generated Schema Types
38+
39+
You can generate schema types for your own workspace with [@ftrack/ts-schema-generator](https://github.com/ftrackhq/ftrack-ts-schema-generator).
40+
41+
Once generated, you can integrate them with @ftrack/api by passing them as a type variable:
42+
43+
```ts
44+
import SchemaTypes from "./__generated__/schema.ts"
45+
import { Session } from @ftrack/api
46+
47+
const session = new Session<SchemaTypes>(...);
48+
49+
// user will now be of type User
50+
// and provide all available properties for its entity type.
51+
const user = await session.query<"User">("...");
52+
```
53+
3754
## Tutorial
3855

3956
The API uses sessions to manage communication with an ftrack server. Create a session that connects to your ftrack server (changing the passed values as appropriate):

source/operation.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* @namespace operation
55
*/
66

7-
export interface CreateOperation {
7+
export interface CreateOperation<TEntityType> {
88
action: "create";
9-
entity_type: string;
9+
entity_type: TEntityType;
1010
entity_data: any;
1111
}
1212

@@ -15,33 +15,33 @@ export interface QueryOperation {
1515
expression: string;
1616
}
1717

18-
export interface SearchOperationOptions {
18+
export interface SearchOperationOptions<TEntityType> {
1919
expression?: string;
20-
entityType?: string;
20+
entityType?: TEntityType;
2121
terms?: string[];
2222
contextId?: string;
2323
objectTypeIds?: string[];
2424
}
2525

26-
export interface SearchOperation {
26+
export interface SearchOperation<TEntityType> {
2727
action: "search";
2828
expression?: string;
29-
entity_type?: string;
29+
entity_type?: TEntityType;
3030
terms?: string[];
3131
context_id?: string;
3232
object_type_ids?: string[];
3333
}
3434

35-
export interface UpdateOperation {
35+
export interface UpdateOperation<TEntityType> {
3636
action: "update";
37-
entity_type: string;
37+
entity_type: TEntityType;
3838
entity_key: string[] | string;
3939
entity_data: any;
4040
}
4141

42-
export interface DeleteOperation {
42+
export interface DeleteOperation<TEntityType> {
4343
action: "delete";
44-
entity_type: string;
44+
entity_type: TEntityType;
4545
entity_key: string[] | string;
4646
}
4747

@@ -61,12 +61,12 @@ export interface GetUploadMetadataOperation {
6161
component_id: string;
6262
}
6363

64-
export type Operation =
65-
| CreateOperation
64+
export type Operation<TEntityType> =
65+
| CreateOperation<TEntityType>
6666
| QueryOperation
67-
| SearchOperation
68-
| UpdateOperation
69-
| DeleteOperation
67+
| SearchOperation<TEntityType>
68+
| UpdateOperation<TEntityType>
69+
| DeleteOperation<TEntityType>
7070
| QueryServerInformationOperation
7171
| QuerySchemasOperation
7272
| GetUploadMetadataOperation
@@ -81,7 +81,10 @@ export type Operation =
8181
* @param {Object} data Entity data to use for creation
8282
* @return {Object} API operation
8383
*/
84-
export function create(type: string, data: any): CreateOperation {
84+
export function create<TEntityType>(
85+
type: TEntityType,
86+
data: any,
87+
): CreateOperation<TEntityType> {
8588
return {
8689
action: "create",
8790
entity_type: type,
@@ -109,13 +112,13 @@ export function query(expression: string): QueryOperation {
109112
* @param {string} expression API query expression
110113
* @return {Object} API operation
111114
*/
112-
export function search({
115+
export function search<TEntityType>({
113116
expression,
114117
entityType,
115118
terms,
116119
contextId,
117120
objectTypeIds,
118-
}: SearchOperationOptions): SearchOperation {
121+
}: SearchOperationOptions<TEntityType>): SearchOperation<TEntityType> {
119122
return {
120123
action: "search",
121124
expression,
@@ -136,11 +139,11 @@ export function search({
136139
* @param {Object} data values to update
137140
* @return {Object} API operation
138141
*/
139-
export function update(
140-
type: string,
142+
export function update<TEntityType>(
143+
type: TEntityType,
141144
keys: string[] | string,
142145
data: any,
143-
): UpdateOperation {
146+
): UpdateOperation<TEntityType> {
144147
return {
145148
action: "update",
146149
entity_type: type,
@@ -158,10 +161,10 @@ export function update(
158161
* @param {Array} keys Identifying keys, typically [<entity id>]
159162
* @return {Object} API operation
160163
*/
161-
function deleteOperation(
162-
type: string,
164+
function deleteOperation<TEntityType>(
165+
type: TEntityType,
163166
keys: string[] | string,
164-
): DeleteOperation {
167+
): DeleteOperation<TEntityType> {
165168
return {
166169
action: "delete",
167170
entity_type: type,

source/project_schema.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as operation from "./operation.js";
44
import { Session } from "./session.js";
5-
import type { Data, QueryResponse } from "./types.js";
5+
import type { QueryResponse } from "./types.js";
66
/**
77
* Project schema namespace
88
* @namespace project_schema
@@ -18,8 +18,8 @@ import type { Data, QueryResponse } from "./types.js";
1818
*
1919
* @memberof project_schema
2020
*/
21-
export function getStatuses(
22-
session: Session,
21+
export function getStatuses<TEntityTypeMap extends Record<string, any>>(
22+
session: Session<TEntityTypeMap>,
2323
projectSchemaId: string,
2424
entityType: string,
2525
typeId: string | null = null,
@@ -71,7 +71,8 @@ export function getStatuses(
7171
),
7272
);
7373

74-
response = session.call<QueryResponse<Data>>(operations);
74+
response =
75+
session.call<QueryResponse<TEntityTypeMap["ProjectSchema"]>>(operations);
7576
response = response.then((results) => {
7677
// Since the operations where performed in one batched call,
7778
// the result will be merged into a single entity.

0 commit comments

Comments
 (0)