@taruvi/sdk 1.3.0 → 1.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taruvi/sdk",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Taruvi SDK",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -12,11 +12,24 @@ export { Secrets } from "./lib/Secrets/SecretsClient.js"
12
12
  export { Policy } from "./lib/Policy/PolicyClient.js"
13
13
  export { App } from "./lib/App/AppClient.js"
14
14
  export { Analytics } from "./lib/Analytics/AnalyticsClient.js"
15
+ export { Graph } from "./lib/Graphs/GraphClient.js"
15
16
 
16
17
  // Export types
17
18
  export type { TaruviConfig, StorageFilters, DatabaseFilters } from "./types.js"
18
19
  export type { AuthTokens } from "./lib-internal/token/TokenClient.js"
19
- export type { UserCreateRequest, UserCreateResponse as UserResponse, UserDataResponse, UserUpdateRequest, UserUpdateResponse, UserGroup, UserPermission, UserRole, UserList, UserApp, UserAppsResponse } from "./lib/user/types.js"
20
+ export type {
21
+ UserCreateRequest,
22
+ UserCreateResponse as UserResponse,
23
+ UserDataResponse as TaruviUser,
24
+ UserUpdateRequest,
25
+ UserUpdateResponse,
26
+ UserGroup,
27
+ UserPermission,
28
+ UserRole,
29
+ UserList,
30
+ UserApp,
31
+ UserAppsResponse
32
+ } from "./lib/user/types.js"
20
33
  export type { Principal, Resource, Resources } from "./lib/Policy/types.js"
21
34
  export type { RoleResponse, SettingsResponse as AppSettingsResponse } from "./lib/App/types.js"
22
35
  export type { FunctionRequest, FunctionResponse, FunctionInvocation } from "./lib/Function/types.js"
@@ -24,4 +37,5 @@ export type { DatabaseRequest, DatabaseResponse, FilterOperator, SortOrder } fro
24
37
  export type { StorageRequest, StorageUpdateRequest, StorageResponse } from "./lib/Storage/types.js"
25
38
  export type { SettingsResponse } from "./lib/Settings/types.js"
26
39
  export type { SecretRequest, SecretResponse } from "./lib/Secrets/types.js"
27
- export type { AnalyticsRequest, AnalyticsResponse } from "./lib/Analytics/types.js"
40
+ export type { AnalyticsRequest, AnalyticsResponse } from "./lib/Analytics/types.js"
41
+ export type { GraphInclude, GraphFormat, GraphQueryParams, EdgeRequest, EdgeResponse } from "./lib/Graphs/types.js"
@@ -0,0 +1,105 @@
1
+ import type { Client } from "../../client.js"
2
+ import type { TaruviConfig } from "../../types.js"
3
+ import type { GraphInclude, GraphFormat, GraphQueryParams, GraphUrlParams, EdgeRequest, EdgeResponse } from "./types.js"
4
+ import { GraphRoutes, GraphEdgeRoutes, type GraphRouteKey } from "../../lib-internal/routes/GraphRoutes.js"
5
+ import { buildQueryString } from "../../utils/utils.js"
6
+ import { HttpMethod } from "../../lib-internal/http/types.js"
7
+
8
+ export class Graph<T = Record<string, unknown>> {
9
+ private client: Client
10
+ private config: TaruviConfig
11
+ private urlParams: GraphUrlParams
12
+ private queryParams: GraphQueryParams
13
+ private operation: HttpMethod | undefined
14
+ private body: object | undefined
15
+ private edgeRoute: string | undefined
16
+
17
+ constructor(client: Client, urlParams: GraphUrlParams = {}, queryParams: GraphQueryParams = {}, operation?: HttpMethod, body?: object, edgeRoute?: string) {
18
+ this.client = client
19
+ this.config = this.client.getConfig()
20
+ this.urlParams = urlParams
21
+ this.queryParams = queryParams
22
+ this.operation = operation
23
+ this.body = body
24
+ this.edgeRoute = edgeRoute
25
+ }
26
+
27
+ from<U = Record<string, unknown>>(dataTables: string): Graph<U> {
28
+ return new Graph<U>(this.client, { ...this.urlParams, dataTables }, { ...this.queryParams })
29
+ }
30
+
31
+ get(recordId: string): Graph<T> {
32
+ return new Graph<T>(this.client, { ...this.urlParams, recordId }, { ...this.queryParams })
33
+ }
34
+
35
+ include(direction: GraphInclude): Graph<T> {
36
+ return new Graph<T>(this.client, { ...this.urlParams }, { ...this.queryParams, include: direction })
37
+ }
38
+
39
+ depth(n: number): Graph<T> {
40
+ return new Graph<T>(this.client, { ...this.urlParams }, { ...this.queryParams, depth: n })
41
+ }
42
+
43
+ format(fmt: GraphFormat): Graph<T> {
44
+ return new Graph<T>(this.client, { ...this.urlParams }, { ...this.queryParams, format: fmt })
45
+ }
46
+
47
+ types(types: string[]): Graph<T> {
48
+ return new Graph<T>(this.client, { ...this.urlParams }, { ...this.queryParams, graph_types: types.join(',') })
49
+ }
50
+
51
+ listEdges(): Graph<T> {
52
+ const route = GraphEdgeRoutes.baseUrl(this.config.appSlug) + GraphEdgeRoutes.edges(this.urlParams.dataTables!) + "/"
53
+ return new Graph<T>(this.client, { ...this.urlParams }, {}, HttpMethod.GET, undefined, route)
54
+ }
55
+
56
+ createEdge(edge: EdgeRequest): Graph<T> {
57
+ const route = GraphEdgeRoutes.baseUrl(this.config.appSlug) + GraphEdgeRoutes.edges(this.urlParams.dataTables!) + "/"
58
+ return new Graph<T>(this.client, { ...this.urlParams }, {}, HttpMethod.POST, edge, route)
59
+ }
60
+
61
+ updateEdge(edgeId: string, edge: EdgeRequest): Graph<T> {
62
+ const route = GraphEdgeRoutes.baseUrl(this.config.appSlug) + GraphEdgeRoutes.edges(this.urlParams.dataTables!) + GraphEdgeRoutes.edgeId(edgeId) + "/"
63
+ return new Graph<T>(this.client, { ...this.urlParams }, {}, HttpMethod.PATCH, edge, route)
64
+ }
65
+
66
+ deleteEdge(edgeIds: string[]): Graph<T> {
67
+ const route = GraphEdgeRoutes.baseUrl(this.config.appSlug) + GraphEdgeRoutes.edges(this.urlParams.dataTables!) + "/"
68
+ return new Graph<T>(this.client, { ...this.urlParams }, {}, HttpMethod.DELETE, edgeIds, route)
69
+ }
70
+
71
+ private buildRoute(): string {
72
+ if (this.edgeRoute) return this.edgeRoute
73
+
74
+ return (
75
+ GraphRoutes.baseUrl(this.config.appSlug) +
76
+ (Object.keys(this.urlParams) as GraphRouteKey[]).reduce((acc, key) => {
77
+ const value = this.urlParams[key]
78
+ const routeBuilder = GraphRoutes[key]
79
+ if (value && routeBuilder) {
80
+ acc += routeBuilder(value)
81
+ }
82
+ return acc
83
+ }, "") +
84
+ "/" +
85
+ buildQueryString(this.queryParams as Record<string, unknown>)
86
+ )
87
+ }
88
+
89
+ async execute(): Promise<T | T[]> {
90
+ const url = this.buildRoute()
91
+ const operation = this.operation || HttpMethod.GET
92
+
93
+ switch (operation) {
94
+ case HttpMethod.POST:
95
+ return await this.client.httpClient.post(url, this.body)
96
+ case HttpMethod.PATCH:
97
+ return await this.client.httpClient.patch(url, this.body)
98
+ case HttpMethod.DELETE:
99
+ return await this.client.httpClient.delete(url, this.body)
100
+ case HttpMethod.GET:
101
+ default:
102
+ return await this.client.httpClient.get(url)
103
+ }
104
+ }
105
+ }
@@ -0,0 +1,26 @@
1
+ export type GraphInclude = 'descendants' | 'ancestors' | 'both'
2
+ export type GraphFormat = 'tree' | 'graph'
3
+
4
+ export interface GraphQueryParams {
5
+ include?: GraphInclude
6
+ depth?: number
7
+ format?: GraphFormat
8
+ graph_types?: string
9
+ }
10
+
11
+ export interface GraphUrlParams {
12
+ dataTables?: string
13
+ recordId?: string
14
+ }
15
+
16
+ export interface EdgeRequest {
17
+ from_id: number
18
+ to_id: number
19
+ type: string
20
+ metadata?: Record<string, unknown>
21
+ }
22
+
23
+ export interface EdgeResponse extends EdgeRequest {
24
+ id: number
25
+ created_at?: string
26
+ }
@@ -0,0 +1,14 @@
1
+ export const GraphRoutes = {
2
+ baseUrl: (appSlug: string) => `api/apps/${appSlug}`,
3
+ dataTables: (tableName: string): string => `/datatables/${tableName}/data`,
4
+ recordId: (recordId: string): string => `/${recordId}`
5
+ }
6
+
7
+ export const GraphEdgeRoutes = {
8
+ baseUrl: (appSlug: string) => `api/apps/${appSlug}`,
9
+ edges: (tableName: string): string => `/datatables/${tableName}/edges`,
10
+ edgeId: (edgeId: string): string => `/${edgeId}`
11
+ }
12
+
13
+ type AllRouteKeys = keyof typeof GraphRoutes
14
+ export type GraphRouteKey = Exclude<AllRouteKeys, 'baseUrl'>
@@ -14,3 +14,11 @@ export const Visibility = {
14
14
  } as const
15
15
 
16
16
  export type Visibility = typeof Visibility[keyof typeof Visibility]
17
+
18
+
19
+ export const QueryParams = {
20
+ include: "include",
21
+ depth: "depth",
22
+ format: "format",
23
+ graph_type: "graph_type",
24
+ }