corebase-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/README.md +69 -0
- package/dist/auth.d.ts +38 -0
- package/dist/auth.js +128 -0
- package/dist/client.d.ts +8 -0
- package/dist/client.js +21 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/types.d.ts +25 -0
- package/dist/types.js +2 -0
- package/package.json +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# CoreBase JS Client
|
|
2
|
+
|
|
3
|
+
A JavaScript/TypeScript client for CoreBase.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install corebase-js
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Initialization
|
|
12
|
+
|
|
13
|
+
Initialize the client with your project URL and API Key.
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { createClient } from 'corebase-js';
|
|
17
|
+
|
|
18
|
+
const corebase = createClient('https://your-project-url.com', 'your-public-api-key');
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Authentication
|
|
22
|
+
|
|
23
|
+
### Sign Up
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
const { data, error } = await corebase.auth.signUp({
|
|
27
|
+
email: 'user@example.com',
|
|
28
|
+
password: 'password123',
|
|
29
|
+
name: 'John Doe'
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (error) {
|
|
33
|
+
console.error('Signup failed:', error.message);
|
|
34
|
+
} else {
|
|
35
|
+
console.log('User created:', data.user);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Sign In
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
const { data, error } = await corebase.auth.signIn({
|
|
43
|
+
email: 'user@example.com',
|
|
44
|
+
password: 'password123'
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (error) {
|
|
48
|
+
console.error('Login failed:', error.message);
|
|
49
|
+
} else {
|
|
50
|
+
console.log('Logged in user:', data.user);
|
|
51
|
+
console.log('Access token:', data.access_token);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Get Current User
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
const { data, error } = await corebase.auth.getUser();
|
|
59
|
+
|
|
60
|
+
if (data) {
|
|
61
|
+
console.log('Current user:', data.user);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Sign Out
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
await corebase.auth.signOut();
|
|
69
|
+
```
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { AuthSession, ClientConfig, User } from './types';
|
|
2
|
+
export interface AuthResponse<T> {
|
|
3
|
+
data: T | null;
|
|
4
|
+
error: {
|
|
5
|
+
message: string;
|
|
6
|
+
code?: string;
|
|
7
|
+
details?: string;
|
|
8
|
+
} | null;
|
|
9
|
+
}
|
|
10
|
+
export declare class AuthClient {
|
|
11
|
+
private config;
|
|
12
|
+
private session;
|
|
13
|
+
private storageKey;
|
|
14
|
+
constructor(config: ClientConfig);
|
|
15
|
+
private get baseUrl();
|
|
16
|
+
private get headers();
|
|
17
|
+
private request;
|
|
18
|
+
signUp(credentials: {
|
|
19
|
+
email: string;
|
|
20
|
+
password: string;
|
|
21
|
+
name?: string;
|
|
22
|
+
}): Promise<AuthResponse<{
|
|
23
|
+
user: User;
|
|
24
|
+
}>>;
|
|
25
|
+
signIn(credentials: {
|
|
26
|
+
email: string;
|
|
27
|
+
password: string;
|
|
28
|
+
}): Promise<AuthResponse<AuthSession>>;
|
|
29
|
+
getUser(): Promise<AuthResponse<{
|
|
30
|
+
user: User;
|
|
31
|
+
}>>;
|
|
32
|
+
signOut(): Promise<{
|
|
33
|
+
error: null;
|
|
34
|
+
}>;
|
|
35
|
+
getSession(): AuthSession | null;
|
|
36
|
+
private setSession;
|
|
37
|
+
private loadSession;
|
|
38
|
+
}
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.AuthClient = void 0;
|
|
13
|
+
class AuthClient {
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.session = null;
|
|
16
|
+
this.storageKey = 'corebase-auth-session';
|
|
17
|
+
this.config = config;
|
|
18
|
+
this.loadSession();
|
|
19
|
+
}
|
|
20
|
+
get baseUrl() {
|
|
21
|
+
return this.config.baseUrl || 'http://localhost:3000';
|
|
22
|
+
}
|
|
23
|
+
get headers() {
|
|
24
|
+
var _a;
|
|
25
|
+
const headers = {
|
|
26
|
+
'Content-Type': 'application/json',
|
|
27
|
+
'x-api-key': this.config.apiKey,
|
|
28
|
+
};
|
|
29
|
+
if ((_a = this.session) === null || _a === void 0 ? void 0 : _a.access_token) {
|
|
30
|
+
headers['Authorization'] = `Bearer ${this.session.access_token}`;
|
|
31
|
+
}
|
|
32
|
+
return headers;
|
|
33
|
+
}
|
|
34
|
+
request(path_1) {
|
|
35
|
+
return __awaiter(this, arguments, void 0, function* (path, options = {}) {
|
|
36
|
+
try {
|
|
37
|
+
const url = `${this.baseUrl}/v1${path}`;
|
|
38
|
+
const response = yield fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, this.headers), options.headers) }));
|
|
39
|
+
const result = yield response.json();
|
|
40
|
+
if (!response.ok || result.status === 'error') {
|
|
41
|
+
return {
|
|
42
|
+
data: null,
|
|
43
|
+
error: result.error || { message: `Request failed with status ${response.status}` },
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return { data: result.data, error: null };
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
return { data: null, error: { message: err.message || 'Network request failed' } };
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
// --- Auth Methods ---
|
|
54
|
+
signUp(credentials) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const { data, error } = yield this.request('/auth/project/signup', {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
body: JSON.stringify(credentials),
|
|
59
|
+
});
|
|
60
|
+
if (error)
|
|
61
|
+
return { data: null, error };
|
|
62
|
+
if (!data)
|
|
63
|
+
return { data: null, error: { message: 'No data returned' } };
|
|
64
|
+
return {
|
|
65
|
+
data: {
|
|
66
|
+
user: {
|
|
67
|
+
id: data.id,
|
|
68
|
+
email: data.email,
|
|
69
|
+
name: data.name,
|
|
70
|
+
role: data.role,
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
error: null,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
signIn(credentials) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
const { data, error } = yield this.request('/auth/project/login', {
|
|
80
|
+
method: 'POST',
|
|
81
|
+
body: JSON.stringify(credentials),
|
|
82
|
+
});
|
|
83
|
+
if (data) {
|
|
84
|
+
this.setSession(data);
|
|
85
|
+
}
|
|
86
|
+
return { data, error };
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
getUser() {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
+
return this.request('/auth/project/me', {
|
|
92
|
+
method: 'GET',
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
signOut() {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
this.session = null;
|
|
99
|
+
if (typeof localStorage !== 'undefined') {
|
|
100
|
+
localStorage.removeItem(this.storageKey);
|
|
101
|
+
}
|
|
102
|
+
return { error: null };
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
getSession() {
|
|
106
|
+
return this.session;
|
|
107
|
+
}
|
|
108
|
+
setSession(session) {
|
|
109
|
+
this.session = session;
|
|
110
|
+
if (typeof localStorage !== 'undefined') {
|
|
111
|
+
localStorage.setItem(this.storageKey, JSON.stringify(session));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
loadSession() {
|
|
115
|
+
if (typeof localStorage !== 'undefined') {
|
|
116
|
+
const stored = localStorage.getItem(this.storageKey);
|
|
117
|
+
if (stored) {
|
|
118
|
+
try {
|
|
119
|
+
this.session = JSON.parse(stored);
|
|
120
|
+
}
|
|
121
|
+
catch (e) {
|
|
122
|
+
console.error('Failed to parse session', e);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.AuthClient = AuthClient;
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AuthClient } from './auth';
|
|
2
|
+
import { ClientConfig } from './types';
|
|
3
|
+
export declare class CoreBaseClient {
|
|
4
|
+
auth: AuthClient;
|
|
5
|
+
private config;
|
|
6
|
+
constructor(config: ClientConfig);
|
|
7
|
+
}
|
|
8
|
+
export declare function createClient(baseUrl: string, apiKey: string): CoreBaseClient;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CoreBaseClient = void 0;
|
|
4
|
+
exports.createClient = createClient;
|
|
5
|
+
const auth_1 = require("./auth");
|
|
6
|
+
class CoreBaseClient {
|
|
7
|
+
constructor(config) {
|
|
8
|
+
if (!config.apiKey) {
|
|
9
|
+
throw new Error('CoreBase: apiKey is required.');
|
|
10
|
+
}
|
|
11
|
+
if (!config.baseUrl) {
|
|
12
|
+
console.warn('CoreBase: baseUrl not provided, defaulting to http://localhost:3000');
|
|
13
|
+
}
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.auth = new auth_1.AuthClient(config);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.CoreBaseClient = CoreBaseClient;
|
|
19
|
+
function createClient(baseUrl, apiKey) {
|
|
20
|
+
return new CoreBaseClient({ baseUrl, apiKey });
|
|
21
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./client"), exports);
|
|
19
|
+
__exportStar(require("./auth"), exports);
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface User {
|
|
2
|
+
id: string;
|
|
3
|
+
email: string;
|
|
4
|
+
name?: string;
|
|
5
|
+
role?: string;
|
|
6
|
+
created_at?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface AuthSession {
|
|
9
|
+
access_token: string;
|
|
10
|
+
expires_in: number;
|
|
11
|
+
user: User;
|
|
12
|
+
}
|
|
13
|
+
export interface ApiResponse<T> {
|
|
14
|
+
status: 'success' | 'error';
|
|
15
|
+
data?: T;
|
|
16
|
+
error?: {
|
|
17
|
+
message: string;
|
|
18
|
+
code: string;
|
|
19
|
+
details?: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export interface ClientConfig {
|
|
23
|
+
apiKey: string;
|
|
24
|
+
baseUrl?: string;
|
|
25
|
+
}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "corebase-js",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "JavaScript client for CoreBase",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "npm run build"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"baas",
|
|
16
|
+
"corebase",
|
|
17
|
+
"sdk"
|
|
18
|
+
],
|
|
19
|
+
"author": "",
|
|
20
|
+
"license": "ISC",
|
|
21
|
+
"type": "commonjs",
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.0.0"
|
|
24
|
+
}
|
|
25
|
+
}
|