@taruvi/sdk 1.0.7 → 1.0.9

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.7",
3
+ "version": "1.0.9",
4
4
  "description": "Taruvi SDK",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -0,0 +1,50 @@
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";
6
+
7
+ export class App {
8
+ private client: Client
9
+ private urlParams: UrlParams
10
+ private config: TaruviConfig
11
+ private operation: HttpMethod | undefined
12
+
13
+ constructor(client: Client, urlParams: UrlParams = {}, operation?: HttpMethod | undefined) {
14
+ this.client = client
15
+ this.urlParams = urlParams
16
+ this.operation = operation
17
+ this.config = this.client.getConfig()
18
+ }
19
+
20
+ roles() {
21
+ return new App(this.client, { ...this.urlParams, roles: "roles" }, HttpMethod.GET)
22
+ }
23
+
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
+ )
38
+ }
39
+
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
+ }
49
+ }
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
+ }
@@ -40,7 +40,7 @@ export class Database {
40
40
  }
41
41
 
42
42
  update(body: any): Database {
43
- return new Database(this.client, this.urlParams = { ...this.urlParams }, HttpMethod.PUT, body)
43
+ return new Database(this.client, this.urlParams = { ...this.urlParams }, HttpMethod.PATCH, body)
44
44
  }
45
45
 
46
46
  delete(recordId?: any): Database {
@@ -67,7 +67,6 @@ export class Database {
67
67
  async execute() {
68
68
  // Build the API URL
69
69
  const url = this.buildRoute()
70
- // const fullUrl = `sites/eox_site/${url}` //remove for productions because baseurl is in the appscope, no need for site
71
70
 
72
71
  const operation = this.operation || HttpMethod.GET
73
72
 
@@ -75,11 +74,11 @@ export class Database {
75
74
  case HttpMethod.POST:
76
75
  return await this.client.httpClient.post(url, this.body)
77
76
 
78
- case HttpMethod.PUT:
77
+ case HttpMethod.PATCH:
79
78
  if (!this.urlParams.recordId) {
80
- throw new Error('PUT operation requires a record ID. Use .get(recordId) before .update()')
79
+ throw new Error('PATCH operation requires a record ID.')
81
80
  }
82
- return await this.client.httpClient.put(url, this.body)
81
+ return await this.client.httpClient.patch(url, this.body)
83
82
 
84
83
  case HttpMethod.DELETE:
85
84
  return await this.client.httpClient.delete(url)
@@ -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>>
@@ -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