@omnibase/core-js 0.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/dist/auth/types.cjs +18 -0
- package/dist/auth/types.d.cts +27 -0
- package/dist/auth/types.d.ts +27 -0
- package/dist/auth/types.js +0 -0
- package/dist/database/index.cjs +40 -0
- package/dist/database/index.d.cts +56 -0
- package/dist/database/index.d.ts +56 -0
- package/dist/database/index.js +13 -0
- package/dist/index.cjs +218 -0
- package/dist/index.d.cts +204 -0
- package/dist/index.d.ts +204 -0
- package/dist/index.js +186 -0
- package/dist/tenants/index.cjs +205 -0
- package/dist/tenants/index.d.cts +124 -0
- package/dist/tenants/index.d.ts +124 -0
- package/dist/tenants/index.js +174 -0
- package/package.json +40 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return type of the `acceptTenantInvite` function
|
|
3
|
+
*/
|
|
4
|
+
type AcceptTenantInviteResponse = ApiResponse<{
|
|
5
|
+
tenant_id: string;
|
|
6
|
+
message: string;
|
|
7
|
+
}>;
|
|
8
|
+
/**
|
|
9
|
+
* Accept a tenant invite using token
|
|
10
|
+
* Calls PUT /api/v1/tenants/invites/accept
|
|
11
|
+
*/
|
|
12
|
+
declare function acceptTenantInvite(token: string): Promise<ApiResponse<{
|
|
13
|
+
tenant_id: string;
|
|
14
|
+
message: string;
|
|
15
|
+
}>>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Return type of the `createTenantUserInvite` function
|
|
19
|
+
*/
|
|
20
|
+
type CreateTenantUserInviteResponse = ApiResponse<{
|
|
21
|
+
invite: TenantInvite;
|
|
22
|
+
message: string;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* The TenantInvite Schema that maps directly to `auth.tenant_invites`
|
|
26
|
+
*/
|
|
27
|
+
interface TenantInvite {
|
|
28
|
+
id: string;
|
|
29
|
+
tenant_id: string;
|
|
30
|
+
email: string;
|
|
31
|
+
role: string;
|
|
32
|
+
token: string;
|
|
33
|
+
inviter_id: string;
|
|
34
|
+
expires_at: string;
|
|
35
|
+
created_at: string;
|
|
36
|
+
used_at?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Invite Data describing who the invite is going to `email` and their
|
|
40
|
+
* role `role`
|
|
41
|
+
*/
|
|
42
|
+
type CreateTenantUserInviteRequest = {
|
|
43
|
+
email: string;
|
|
44
|
+
role: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Create a tenant user invite
|
|
48
|
+
* Calls POST /api/v1/tenants/{id}/invites
|
|
49
|
+
*/
|
|
50
|
+
declare function createTenantUserInvite(tenantId: string, inviteData: CreateTenantUserInviteRequest): Promise<CreateTenantUserInviteResponse>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Return type of the `createTenant` function
|
|
54
|
+
*
|
|
55
|
+
* The `token` is the authenticated users `postgrest_jwt` cookie for RLS policies specific to the active tenant
|
|
56
|
+
*/
|
|
57
|
+
type CreateTenantResponse = ApiResponse<{
|
|
58
|
+
tenant: Tenant;
|
|
59
|
+
message: string;
|
|
60
|
+
token: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* The Tenant Schema that maps directly to `auth.tenants`
|
|
64
|
+
*/
|
|
65
|
+
type Tenant = {
|
|
66
|
+
id: string;
|
|
67
|
+
name: string;
|
|
68
|
+
stripe_customer_id: string;
|
|
69
|
+
type: string;
|
|
70
|
+
created_at: string;
|
|
71
|
+
updated_at: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The data required to create a tenant.
|
|
75
|
+
*/
|
|
76
|
+
type CreateTenantRequest = {
|
|
77
|
+
name: string;
|
|
78
|
+
billing_email: string;
|
|
79
|
+
user_id: string;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Create a new tenant
|
|
83
|
+
*
|
|
84
|
+
* Calls POST /api/v1/tenants
|
|
85
|
+
*/
|
|
86
|
+
declare function createTenant(tenantData: CreateTenantRequest): Promise<CreateTenantResponse>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Return type of the `deleteTenant` function
|
|
90
|
+
*/
|
|
91
|
+
type DeleteTenantResponse = ApiResponse<{
|
|
92
|
+
message: string;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Delete a tenant (only owners can delete)
|
|
96
|
+
*
|
|
97
|
+
* Calls DELETE /api/v1/tenants/{id}
|
|
98
|
+
*/
|
|
99
|
+
declare function deleteTenant(tenantId: string): Promise<DeleteTenantResponse>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Base API Response structure for API
|
|
103
|
+
*/
|
|
104
|
+
type ApiResponse<T> = {
|
|
105
|
+
data?: T;
|
|
106
|
+
status: number;
|
|
107
|
+
error?: string;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Return type of the `switchActiveTenant` function
|
|
112
|
+
*/
|
|
113
|
+
type SwitchActiveTenantResponse = ApiResponse<{
|
|
114
|
+
token: string;
|
|
115
|
+
message: string;
|
|
116
|
+
}>;
|
|
117
|
+
/**
|
|
118
|
+
* Switch the user's active tenant
|
|
119
|
+
*
|
|
120
|
+
* Calls PUT /api/v1/tenants/switch-active
|
|
121
|
+
*/
|
|
122
|
+
declare function switchActiveTenant(tenantId: string): Promise<SwitchActiveTenantResponse>;
|
|
123
|
+
|
|
124
|
+
export { type AcceptTenantInviteResponse, type CreateTenantRequest, type CreateTenantResponse, type CreateTenantUserInviteRequest, type CreateTenantUserInviteResponse, type DeleteTenantResponse, type SwitchActiveTenantResponse, type Tenant, type TenantInvite, acceptTenantInvite, createTenant, createTenantUserInvite, deleteTenant, switchActiveTenant };
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Return type of the `acceptTenantInvite` function
|
|
3
|
+
*/
|
|
4
|
+
type AcceptTenantInviteResponse = ApiResponse<{
|
|
5
|
+
tenant_id: string;
|
|
6
|
+
message: string;
|
|
7
|
+
}>;
|
|
8
|
+
/**
|
|
9
|
+
* Accept a tenant invite using token
|
|
10
|
+
* Calls PUT /api/v1/tenants/invites/accept
|
|
11
|
+
*/
|
|
12
|
+
declare function acceptTenantInvite(token: string): Promise<ApiResponse<{
|
|
13
|
+
tenant_id: string;
|
|
14
|
+
message: string;
|
|
15
|
+
}>>;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Return type of the `createTenantUserInvite` function
|
|
19
|
+
*/
|
|
20
|
+
type CreateTenantUserInviteResponse = ApiResponse<{
|
|
21
|
+
invite: TenantInvite;
|
|
22
|
+
message: string;
|
|
23
|
+
}>;
|
|
24
|
+
/**
|
|
25
|
+
* The TenantInvite Schema that maps directly to `auth.tenant_invites`
|
|
26
|
+
*/
|
|
27
|
+
interface TenantInvite {
|
|
28
|
+
id: string;
|
|
29
|
+
tenant_id: string;
|
|
30
|
+
email: string;
|
|
31
|
+
role: string;
|
|
32
|
+
token: string;
|
|
33
|
+
inviter_id: string;
|
|
34
|
+
expires_at: string;
|
|
35
|
+
created_at: string;
|
|
36
|
+
used_at?: string;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Invite Data describing who the invite is going to `email` and their
|
|
40
|
+
* role `role`
|
|
41
|
+
*/
|
|
42
|
+
type CreateTenantUserInviteRequest = {
|
|
43
|
+
email: string;
|
|
44
|
+
role: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Create a tenant user invite
|
|
48
|
+
* Calls POST /api/v1/tenants/{id}/invites
|
|
49
|
+
*/
|
|
50
|
+
declare function createTenantUserInvite(tenantId: string, inviteData: CreateTenantUserInviteRequest): Promise<CreateTenantUserInviteResponse>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Return type of the `createTenant` function
|
|
54
|
+
*
|
|
55
|
+
* The `token` is the authenticated users `postgrest_jwt` cookie for RLS policies specific to the active tenant
|
|
56
|
+
*/
|
|
57
|
+
type CreateTenantResponse = ApiResponse<{
|
|
58
|
+
tenant: Tenant;
|
|
59
|
+
message: string;
|
|
60
|
+
token: string;
|
|
61
|
+
}>;
|
|
62
|
+
/**
|
|
63
|
+
* The Tenant Schema that maps directly to `auth.tenants`
|
|
64
|
+
*/
|
|
65
|
+
type Tenant = {
|
|
66
|
+
id: string;
|
|
67
|
+
name: string;
|
|
68
|
+
stripe_customer_id: string;
|
|
69
|
+
type: string;
|
|
70
|
+
created_at: string;
|
|
71
|
+
updated_at: string;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* The data required to create a tenant.
|
|
75
|
+
*/
|
|
76
|
+
type CreateTenantRequest = {
|
|
77
|
+
name: string;
|
|
78
|
+
billing_email: string;
|
|
79
|
+
user_id: string;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Create a new tenant
|
|
83
|
+
*
|
|
84
|
+
* Calls POST /api/v1/tenants
|
|
85
|
+
*/
|
|
86
|
+
declare function createTenant(tenantData: CreateTenantRequest): Promise<CreateTenantResponse>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Return type of the `deleteTenant` function
|
|
90
|
+
*/
|
|
91
|
+
type DeleteTenantResponse = ApiResponse<{
|
|
92
|
+
message: string;
|
|
93
|
+
}>;
|
|
94
|
+
/**
|
|
95
|
+
* Delete a tenant (only owners can delete)
|
|
96
|
+
*
|
|
97
|
+
* Calls DELETE /api/v1/tenants/{id}
|
|
98
|
+
*/
|
|
99
|
+
declare function deleteTenant(tenantId: string): Promise<DeleteTenantResponse>;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Base API Response structure for API
|
|
103
|
+
*/
|
|
104
|
+
type ApiResponse<T> = {
|
|
105
|
+
data?: T;
|
|
106
|
+
status: number;
|
|
107
|
+
error?: string;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Return type of the `switchActiveTenant` function
|
|
112
|
+
*/
|
|
113
|
+
type SwitchActiveTenantResponse = ApiResponse<{
|
|
114
|
+
token: string;
|
|
115
|
+
message: string;
|
|
116
|
+
}>;
|
|
117
|
+
/**
|
|
118
|
+
* Switch the user's active tenant
|
|
119
|
+
*
|
|
120
|
+
* Calls PUT /api/v1/tenants/switch-active
|
|
121
|
+
*/
|
|
122
|
+
declare function switchActiveTenant(tenantId: string): Promise<SwitchActiveTenantResponse>;
|
|
123
|
+
|
|
124
|
+
export { type AcceptTenantInviteResponse, type CreateTenantRequest, type CreateTenantResponse, type CreateTenantUserInviteRequest, type CreateTenantUserInviteResponse, type DeleteTenantResponse, type SwitchActiveTenantResponse, type Tenant, type TenantInvite, acceptTenantInvite, createTenant, createTenantUserInvite, deleteTenant, switchActiveTenant };
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// src/tenants/switch-tenant.ts
|
|
2
|
+
async function switchActiveTenant(tenantId) {
|
|
3
|
+
const baseUrl = process.env.OMNIBASE_API_URL;
|
|
4
|
+
if (!baseUrl) {
|
|
5
|
+
throw new Error("OMNIBASE_API_URL is not configured");
|
|
6
|
+
}
|
|
7
|
+
const requestBody = {
|
|
8
|
+
tenant_id: tenantId
|
|
9
|
+
};
|
|
10
|
+
try {
|
|
11
|
+
const response = await fetch(`${baseUrl}/api/v1/tenants/switch-active`, {
|
|
12
|
+
method: "PUT",
|
|
13
|
+
headers: {
|
|
14
|
+
"Content-Type": "application/json"
|
|
15
|
+
},
|
|
16
|
+
body: JSON.stringify(requestBody),
|
|
17
|
+
credentials: "include"
|
|
18
|
+
});
|
|
19
|
+
if (!response.ok) {
|
|
20
|
+
const errorData = await response.text();
|
|
21
|
+
throw new Error(
|
|
22
|
+
`Failed to switch tenant: ${response.status} - ${errorData}`
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
const data = await response.json();
|
|
26
|
+
return data;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
console.error("Error switching active tenant:", error);
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/tenants/create-invite.ts
|
|
34
|
+
async function createTenantUserInvite(tenantId, inviteData) {
|
|
35
|
+
const baseUrl = process.env.OMNIBASE_API_URL;
|
|
36
|
+
if (!baseUrl) {
|
|
37
|
+
throw new Error("OMNIBASE_API_URL is not configured");
|
|
38
|
+
}
|
|
39
|
+
if (!tenantId) {
|
|
40
|
+
throw new Error("Tenant ID is required");
|
|
41
|
+
}
|
|
42
|
+
if (!inviteData.email || !inviteData.role) {
|
|
43
|
+
throw new Error("Email and role are required");
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
const response = await fetch(
|
|
47
|
+
`${baseUrl}/api/v1/tenants/${tenantId}/invites`,
|
|
48
|
+
{
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: {
|
|
51
|
+
"Content-Type": "application/json"
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify(inviteData),
|
|
54
|
+
credentials: "include"
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
const errorData = await response.text();
|
|
59
|
+
throw new Error(
|
|
60
|
+
`Failed to create invite: ${response.status} - ${errorData}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
const data = await response.json();
|
|
64
|
+
return data;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
console.error("Error creating tenant user invite:", error);
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/tenants/create-tenant.ts
|
|
72
|
+
async function createTenant(tenantData) {
|
|
73
|
+
const baseUrl = process.env.OMNIBASE_API_URL;
|
|
74
|
+
if (!baseUrl) {
|
|
75
|
+
throw new Error("OMNIBASE_API_URL is not configured");
|
|
76
|
+
}
|
|
77
|
+
if (!tenantData.name || !tenantData.user_id) {
|
|
78
|
+
throw new Error("Name and user_id are required");
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const response = await fetch(`${baseUrl}/api/v1/tenants`, {
|
|
82
|
+
method: "POST",
|
|
83
|
+
headers: {
|
|
84
|
+
"Content-Type": "application/json"
|
|
85
|
+
},
|
|
86
|
+
body: JSON.stringify(tenantData),
|
|
87
|
+
credentials: "include"
|
|
88
|
+
});
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const errorData = await response.text();
|
|
91
|
+
throw new Error(
|
|
92
|
+
`Failed to create tenant: ${response.status} - ${errorData}`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const data = await response.json();
|
|
96
|
+
return data;
|
|
97
|
+
} catch (error) {
|
|
98
|
+
console.error("Error creating tenant:", error);
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/tenants/accept-invite.ts
|
|
104
|
+
async function acceptTenantInvite(token) {
|
|
105
|
+
const baseUrl = process.env.OMNIBASE_API_URL;
|
|
106
|
+
if (!baseUrl) {
|
|
107
|
+
throw new Error("OMNIBASE_API_URL is not configured");
|
|
108
|
+
}
|
|
109
|
+
if (!token) {
|
|
110
|
+
throw new Error("Invite token is required");
|
|
111
|
+
}
|
|
112
|
+
const requestBody = {
|
|
113
|
+
token
|
|
114
|
+
};
|
|
115
|
+
try {
|
|
116
|
+
const response = await fetch(`${baseUrl}/api/v1/tenants/invites/accept`, {
|
|
117
|
+
method: "PUT",
|
|
118
|
+
headers: {
|
|
119
|
+
"Content-Type": "application/json"
|
|
120
|
+
},
|
|
121
|
+
body: JSON.stringify(requestBody),
|
|
122
|
+
credentials: "include"
|
|
123
|
+
});
|
|
124
|
+
if (!response.ok) {
|
|
125
|
+
const errorData = await response.text();
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Failed to accept invite: ${response.status} - ${errorData}`
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const data = await response.json();
|
|
131
|
+
return data;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
console.error("Error accepting tenant invite:", error);
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/tenants/delete-tenant.ts
|
|
139
|
+
async function deleteTenant(tenantId) {
|
|
140
|
+
const baseUrl = process.env.OMNIBASE_API_URL;
|
|
141
|
+
if (!baseUrl) {
|
|
142
|
+
throw new Error("OMNIBASE_API_URL is not configured");
|
|
143
|
+
}
|
|
144
|
+
if (!tenantId) {
|
|
145
|
+
throw new Error("Tenant ID is required");
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const response = await fetch(`${baseUrl}/api/v1/tenants/${tenantId}`, {
|
|
149
|
+
method: "DELETE",
|
|
150
|
+
headers: {
|
|
151
|
+
"Content-Type": "application/json"
|
|
152
|
+
},
|
|
153
|
+
credentials: "include"
|
|
154
|
+
});
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
const errorData = await response.text();
|
|
157
|
+
throw new Error(
|
|
158
|
+
`Failed to delete tenant: ${response.status} - ${errorData}`
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
const data = await response.json();
|
|
162
|
+
return data;
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.error("Error deleting tenant:", error);
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export {
|
|
169
|
+
acceptTenantInvite,
|
|
170
|
+
createTenant,
|
|
171
|
+
createTenantUserInvite,
|
|
172
|
+
deleteTenant,
|
|
173
|
+
switchActiveTenant
|
|
174
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@omnibase/core-js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OmniBase core Javascript SDK - framework agnostic",
|
|
5
|
+
"files": ["dist/**/*"],
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsup src/database/index.ts src/tenants/index.ts src/auth/types.ts --format cjs,esm --dts",
|
|
9
|
+
"prepublishOnly": "npm run build"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
"./auth": {
|
|
13
|
+
"types": "./dist/auth/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./database": {
|
|
16
|
+
"import": "./dist/database/index.js",
|
|
17
|
+
"require": "./dist/database/index.cjs",
|
|
18
|
+
"types": "./dist/database/index.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./tenants": {
|
|
21
|
+
"import": "./dist/tenants/index.js",
|
|
22
|
+
"require": "./dist/tenants/index.cjs",
|
|
23
|
+
"types": "./dist/tenants/index.d.ts"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"keywords": ["omnibase", "sdk", "javascript", "typescript"],
|
|
27
|
+
"author": "Phoenix Baker Van Urk",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/bun": "latest",
|
|
31
|
+
"tsup": "^8.5.0"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"typescript": "^5.9.2"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@ory/client-fetch": "^1.22.2",
|
|
38
|
+
"@supabase/postgrest-js": "^1.21.4"
|
|
39
|
+
}
|
|
40
|
+
}
|