@taruvi/sdk 1.0.8 → 1.1.0

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.0.8",
3
+ "version": "1.1.0",
4
4
  "description": "Taruvi SDK",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
package/src/index.ts CHANGED
@@ -9,11 +9,13 @@ export { Database } from "./lib/Database/DatabaseClient.js"
9
9
  export { Settings } from "./lib/Settings/SettingsClient.js"
10
10
  export { Functions } from "./lib/Function/FunctionsClient.js"
11
11
  export { Secrets } from "./lib/Secrets/SecretsClient.js"
12
+ export { Policy } from "./lib/Policy/PolicyClient.js"
12
13
 
13
14
  // Export types
14
15
  export type { TaruviConfig, StorageFilters, DatabaseFilters } from "./types.js"
15
16
  export type { AuthTokens } from "./lib-internal/token/TokenClient.js"
16
17
  export type { UserCreateRequest, UserCreateResponse as UserResponse, UserDataResponse } from "./lib/user/types.js"
18
+ export type { Principal, Resource, Resources } from "./lib/Policy/types.js"
17
19
  export type { FunctionRequest, FunctionResponse, FunctionInvocation } from "./lib/Function/types.js"
18
20
  export type { DatabaseRequest, DatabaseResponse } from "./lib/Database/types.js"
19
21
  export type { StorageRequest, StorageUpdateRequest, StorageResponse } from "./lib/Storage/types.js"
@@ -1,24 +1,50 @@
1
- import type { Client } from "../../client.js"
1
+ import type { Client } from "../../client.js";
2
+ import { AppRoutes, type AppRouteKey } from "../../lib-internal/routes/AppRoutes.js";
3
+ import { HttpMethod } from "../../lib-internal/http/types.js";
4
+ import type { TaruviConfig } from "../../types.js";
5
+ import type { UrlParams } from "./types.js";
2
6
 
3
7
  export class App {
4
-
5
8
  private client: Client
6
- private urlParams: {}
9
+ private urlParams: UrlParams
10
+ private config: TaruviConfig
11
+ private operation: HttpMethod | undefined
7
12
 
8
- constructor(client: Client, urlParams: {}, filters?: {}) {
13
+ constructor(client: Client, urlParams: UrlParams = {}, operation?: HttpMethod | undefined) {
9
14
  this.client = client
10
15
  this.urlParams = urlParams
16
+ this.operation = operation
17
+ this.config = this.client.getConfig()
11
18
  }
12
19
 
13
20
  roles() {
14
- return new App(this.client, {...this.urlParams})
21
+ return new App(this.client, { ...this.urlParams, roles: "roles" }, HttpMethod.GET)
15
22
  }
16
23
 
17
- filter(filters: {}) {
18
- return new App(this.client, { ...this.urlParams }, filters)
24
+ private buildRoute(): string {
25
+ return (
26
+ AppRoutes.baseUrl(this.config.appSlug) +
27
+ (Object.keys(this.urlParams) as AppRouteKey[]).reduce((acc, key) => {
28
+ const value = this.urlParams[key]
29
+ const routeBuilder = AppRoutes[key]
30
+
31
+ if (value && routeBuilder) {
32
+ acc += routeBuilder()
33
+ }
34
+
35
+ return acc
36
+ }, "")
37
+ )
19
38
  }
20
39
 
21
- execute() {
22
-
40
+ async execute() {
41
+ const url = this.buildRoute()
42
+ const operation = this.operation || HttpMethod.GET
43
+
44
+ switch (operation) {
45
+ case HttpMethod.GET:
46
+ default:
47
+ return await this.client.httpClient.get(url)
48
+ }
23
49
  }
24
50
  }
@@ -0,0 +1,25 @@
1
+ import type { Client } from "../../client.js"
2
+ import { HttpMethod } from "../../lib-internal/http/types.js"
3
+
4
+ export type AppOperation = HttpMethod
5
+
6
+ // Internal types
7
+ export interface UrlParams {
8
+ appSlug?: string
9
+ roles?: string
10
+ }
11
+
12
+ export interface AppClientInterface {
13
+ client: Client
14
+ urlParams?: UrlParams
15
+ }
16
+
17
+ // Response types
18
+ export interface RoleResponse {
19
+ id?: string | number
20
+ name?: string
21
+ permissions?: string[]
22
+ created_at?: string
23
+ updated_at?: string
24
+ [key: string]: unknown
25
+ }
@@ -0,0 +1,29 @@
1
+ import type { Client } from "../../client.js"
2
+ import { PolicyRoutes } from "../../lib-internal/routes/PolicyRoutes.js"
3
+ import type { TaruviConfig } from "../../types.js"
4
+ import type { Principal, Resource, Resources } from "./types.js"
5
+
6
+ export class Policy {
7
+ private client: Client
8
+ private config: TaruviConfig
9
+ constructor(client: Client) {
10
+ this.client = client
11
+ this.config = this.client.getConfig()
12
+ }
13
+
14
+ async checkResource(resources: Resources, principal?: Principal) {
15
+ const url = PolicyRoutes.baseUrl(this.config.appSlug) + PolicyRoutes.checkResource
16
+ const body = JSON.stringify({
17
+ principal,
18
+ resources: resources.map(r => ({
19
+ resource: {
20
+ kind: `${this.config.appSlug}:${r.tableName}`,
21
+ id: r.recordId,
22
+ attr: r.attributes || {}
23
+ }
24
+ }))
25
+ })
26
+
27
+ return await this.client.httpClient.post(url, body)
28
+ }
29
+ }
@@ -0,0 +1,23 @@
1
+ export interface Principal {
2
+ id: string
3
+ roles: string[]
4
+ attr: Record<string, unknown>
5
+ }
6
+
7
+ export type Resource = {
8
+ kind: string
9
+ id: string
10
+ attr: Record<string, unknown>
11
+ };
12
+
13
+ export type Resources = {
14
+ tableName: string
15
+ recordId: string
16
+ attributes: Record<string, unknown>
17
+ actions: string[]
18
+ }[]
19
+
20
+ export type ResourceCheckResponse = {
21
+ allowed: boolean
22
+ reason: string
23
+ }
@@ -1,5 +1,5 @@
1
1
  import type { Client } from "../../client.js";
2
- import type { UserCreateRequest, UserCreateResponse, UserDataResponse, UserList, UserUpdateRequest } from "./types.js";
2
+ import type { UserCreateRequest, UserCreateResponse, UserDataResponse, UserList, UserUpdateRequest, UserAppsResponse } from "./types.js";
3
3
  import { UserRoutes } from "../../lib-internal/routes/UserRoutes.js";
4
4
  import { buildQueryString } from "../../utils/utils.js";
5
5
 
@@ -26,6 +26,10 @@ export class User {
26
26
  return await this.client.httpClient.get(UserRoutes.listUser(queryString))
27
27
  }
28
28
 
29
+ async getUserApps(username: string): Promise<UserAppsResponse> {
30
+ return await this.client.httpClient.get<UserAppsResponse>(UserRoutes.getUserApps(username))
31
+ }
32
+
29
33
  // - createUser
30
34
  async createUser(userData: UserCreateRequest): Promise<UserCreateResponse> {
31
35
  return await this.client.httpClient.post<UserCreateResponse, UserCreateRequest>(
@@ -66,4 +66,14 @@ export interface UserList {
66
66
  ordering: string
67
67
  page: Number
68
68
  page_size: Number
69
- }
69
+ }
70
+
71
+ export interface UserApp {
72
+ name: string
73
+ slug: string
74
+ icon: string
75
+ url: string
76
+ display_name: string
77
+ }
78
+
79
+ export type UserAppsResponse = UserApp[]
@@ -0,0 +1,8 @@
1
+ export const AppRoutes = {
2
+ baseUrl: (appSlug: string) => `api/app/${appSlug}`,
3
+ roles: (): string => `/roles`
4
+ }
5
+
6
+ type AllRouteKeys = keyof typeof AppRoutes
7
+ export type AppRouteKey = Exclude<AllRouteKeys, 'baseUrl'>
8
+ export type AppUrlParams = Partial<Record<AppRouteKey, string>>
@@ -0,0 +1,4 @@
1
+ export const PolicyRoutes = {
2
+ baseUrl: (appSlug: string) => `api/apps/${appSlug}`,
3
+ checkResource: "/check/resources"
4
+ }
@@ -5,5 +5,6 @@ export const UserRoutes = {
5
5
  getCurrentUser: () => `${UserRoutes.baseUrl}me/`,
6
6
  updateUser: (username: string) => `${UserRoutes.baseUrl}${username}/`,
7
7
  deleteUser: (username: string) => `${UserRoutes.baseUrl}${username}/`,
8
- listUser: (filter: string) => `${UserRoutes.baseUrl}${filter}`
8
+ listUser: (filter: string) => `${UserRoutes.baseUrl}${filter}`,
9
+ getUserApps: (username: string) => `${UserRoutes.baseUrl}${username}/apps/`
9
10
  } as const